From e1084ab0bee42625105ff332365b05ab30654d47 Mon Sep 17 00:00:00 2001 From: Alban Auzeill Date: Mon, 23 Dec 2019 14:38:46 +0100 Subject: Send file content to NodeJS process if encoding not UFT-8 (#224) --- sonar-css-plugin/css-bundle/src/server.ts | 7 ++++--- sonar-css-plugin/css-bundle/tests/server.test.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'sonar-css-plugin/css-bundle') diff --git a/sonar-css-plugin/css-bundle/src/server.ts b/sonar-css-plugin/css-bundle/src/server.ts index 05405c8..93bb6cb 100644 --- a/sonar-css-plugin/css-bundle/src/server.ts +++ b/sonar-css-plugin/css-bundle/src/server.ts @@ -53,9 +53,9 @@ function analyzeWithStylelint( response: express.Response ) { const parsedRequest = request.body as AnalysisInput; - const { filePath, configFile } = parsedRequest; - const code = getFileContent(filePath); - + const { filePath, fileContent, configFile } = parsedRequest; + const code = + typeof fileContent == "string" ? fileContent : getFileContent(filePath); const options = { code, codeFilename: filePath, @@ -106,6 +106,7 @@ function getFileContent(filePath: string) { export interface AnalysisInput { filePath: string; + fileContent: string | undefined; configFile: string; } diff --git a/sonar-css-plugin/css-bundle/tests/server.test.ts b/sonar-css-plugin/css-bundle/tests/server.test.ts index 8465810..84fc617 100644 --- a/sonar-css-plugin/css-bundle/tests/server.test.ts +++ b/sonar-css-plugin/css-bundle/tests/server.test.ts @@ -147,6 +147,22 @@ describe("server", () => { ); }); + it("should use fileContent from the request and not from the filesystem", async () => { + const request = JSON.stringify({ + filePath: path.join(__dirname, "fixtures", "file.css"), + fileContent: "\n\n a { }", // move the issue on line 3 + configFile + }); + const response = await post(request, "/analyze"); + expect(JSON.parse(response)).toEqual([ + { + line: 3, + rule: "block-no-empty", + text: "Unexpected empty block (block-no-empty)" + } + ]); + }); + function post(data: string, endpoint: string): Promise { return postToServer(data, endpoint, server); } -- cgit v1.2.3