From 560fdaae8e2c069f21ee81f7341ce0c4403068de Mon Sep 17 00:00:00 2001 From: Nathan Totten Date: Tue, 19 Nov 2019 08:24:48 -0500 Subject: [PATCH] Fixed issue where vsc config and local were merged. Closes #1074 --- CHANGELOG.md | 4 ++ package.json | 2 +- src/ConfigResolver.ts | 48 ++++++++++------- src/test/suite/config.test.ts | 54 ++++++++----------- src/test/suite/format.test.ts | 8 +++ test-fixtures/.prettierrc | 1 - .../config/editorconfig/test.result.js | 3 ++ .../config/jsconfigfile/test.result.js | 3 ++ test-fixtures/config/jsfile/test.js | 8 ++- test-fixtures/config/jsfile/test.result.js | 16 ++++++ test-fixtures/config/rcfile/test.result.js | 3 ++ .../config/vscodeconfig/test.result.js | 16 ++++++ 12 files changed, 111 insertions(+), 55 deletions(-) delete mode 100644 test-fixtures/.prettierrc create mode 100644 test-fixtures/config/editorconfig/test.result.js create mode 100644 test-fixtures/config/jsconfigfile/test.result.js create mode 100644 test-fixtures/config/jsfile/test.result.js create mode 100644 test-fixtures/config/rcfile/test.result.js create mode 100644 test-fixtures/config/vscodeconfig/test.result.js diff --git a/CHANGELOG.md b/CHANGELOG.md index aa1557121..7dda9b9f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi +## [3.8.0] + +- Fixed issue where VS Code and local config where merged. If local config is present, only it will be used. #1074 + ## [3.7.0] - Removed deprecation message from `requireConfig` (Was added by mistake). #1056 diff --git a/package.json b/package.json index 03347c229..d8cc6812c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "prettier-vscode", "displayName": "Prettier - Code formatter", "description": "Code formatter using prettier", - "version": "3.7.0", + "version": "3.8.0", "publisher": "esbenp", "author": "Prettier <@prettiercode>", "galleryBanner": { diff --git a/src/ConfigResolver.ts b/src/ConfigResolver.ts index dcf7bd0ee..cf0d50421 100644 --- a/src/ConfigResolver.ts +++ b/src/ConfigResolver.ts @@ -34,29 +34,37 @@ export class ConfigResolver { ); } - const vsOpts = { - arrowParens: vsCodeConfig.arrowParens, - bracketSpacing: vsCodeConfig.bracketSpacing, - endOfLine: vsCodeConfig.endOfLine, - htmlWhitespaceSensitivity: vsCodeConfig.htmlWhitespaceSensitivity, - insertPragma: vsCodeConfig.insertPragma, - jsxBracketSameLine: vsCodeConfig.jsxBracketSameLine, - jsxSingleQuote: vsCodeConfig.jsxSingleQuote, - printWidth: vsCodeConfig.printWidth, - proseWrap: vsCodeConfig.proseWrap, - quoteProps: vsCodeConfig.quoteProps, - requirePragma: vsCodeConfig.requirePragma, - semi: vsCodeConfig.semi, - singleQuote: vsCodeConfig.singleQuote, - tabWidth: vsCodeConfig.tabWidth, - trailingComma: vsCodeConfig.trailingComma, - useTabs: vsCodeConfig.useTabs, + // TODO: Set type once type definition is updated https://github.com/DefinitelyTyped/DefinitelyTyped/pull/40469 + const vsOpts: any = {}; + + if (configOptions === null) { + vsOpts.arrowParens = vsCodeConfig.arrowParens; + vsOpts.bracketSpacing = vsCodeConfig.bracketSpacing; + vsOpts.endOfLine = vsCodeConfig.endOfLine; + vsOpts.htmlWhitespaceSensitivity = vsCodeConfig.htmlWhitespaceSensitivity; + vsOpts.insertPragma = vsCodeConfig.insertPragma; + vsOpts.jsxBracketSameLine = vsCodeConfig.jsxBracketSameLine; + vsOpts.jsxSingleQuote = vsCodeConfig.jsxSingleQuote; + vsOpts.printWidth = vsCodeConfig.printWidth; + vsOpts.proseWrap = vsCodeConfig.proseWrap; + vsOpts.quoteProps = vsCodeConfig.quoteProps; + vsOpts.requirePragma = vsCodeConfig.requirePragma; + vsOpts.semi = vsCodeConfig.semi; + vsOpts.singleQuote = vsCodeConfig.singleQuote; + vsOpts.tabWidth = vsCodeConfig.tabWidth; + vsOpts.trailingComma = vsCodeConfig.trailingComma; + vsOpts.useTabs = vsCodeConfig.useTabs; // TODO: Remove once type definition is updated https://github.com/DefinitelyTyped/DefinitelyTyped/pull/40469 - vueIndentScriptAndStyle: (vsCodeConfig as any).vueIndentScriptAndStyle - }; + vsOpts.vueIndentScriptAndStyle = (vsCodeConfig as any).vueIndentScriptAndStyle; + + this.loggingService.logMessage( + "No local configuration detected, using VS Code configuration.", + "INFO" + ); + } const prettierOptions: prettier.Options = { - ...vsOpts, + ...(configOptions === null ? vsOpts : {}), ...{ filepath: fileName, parser: parser as prettier.BuiltInParserName diff --git a/src/test/suite/config.test.ts b/src/test/suite/config.test.ts index d3973a9eb..b52d8d4a9 100644 --- a/src/test/suite/config.test.ts +++ b/src/test/suite/config.test.ts @@ -1,53 +1,45 @@ import * as assert from "assert"; -import { format } from "./format.test"; +import * as fs from "fs"; +import * as path from "path"; +import { format, getText } from "./format.test"; -const standardResult = `function foo() { - console.log("test"); -} -`; - -const vscodeResult = `function foo() { - const foo = [ - "aaaaaaaaa", - "bbbbbb", - "c", - "a", - "b", - "c", - "a", - "b", - "c", - "a", - "b", - "c", - ]; -} -`; - -const testConfig = (filePath: string, expected: string = standardResult) => { +const testConfig = (testPath: string, resultPath: string) => { return async () => { - const { result } = await format("config", filePath); + const { result } = await format("config", testPath); + const expected = await getText("config", resultPath); assert.equal(result, expected); }; }; +const prettierConfigOrig = path.resolve(__dirname, "../../../.prettierrc"); +const prettierConfigTemp = path.resolve(__dirname, "../../../old.prettierrc"); + suite("Test configurations", function() { this.timeout(10000); - test("it uses config from .prettierrc file ", testConfig("rcfile/test.js")); + this.beforeAll(cb => { + fs.rename(prettierConfigOrig, prettierConfigTemp, cb); + }); + this.afterAll(cb => { + fs.rename(prettierConfigTemp, prettierConfigOrig, cb); + }); + test( + "it uses config from .prettierrc file and does not inherit VS Code settings ", + testConfig("rcfile/test.js", "rcfile/test.result.js") + ); test( "it uses config from prettier.config.js file ", - testConfig("jsconfigfile/test.js") + testConfig("jsconfigfile/test.js", "jsconfigfile/test.result.js") ); test( "it uses config from .prettierrc.js file ", - testConfig("jsfile/test.js") + testConfig("jsfile/test.js", "jsfile/test.result.js") ); test( "it uses config from .editorconfig file ", - testConfig("editorconfig/test.js") + testConfig("editorconfig/test.js", "editorconfig/test.result.js") ); test( "it uses config from vscode settings ", - testConfig("vscodeconfig/test.js", vscodeResult) + testConfig("vscodeconfig/test.js", "vscodeconfig/test.result.js") ); }); diff --git a/src/test/suite/format.test.ts b/src/test/suite/format.test.ts index f2beee2ee..7d05dee11 100644 --- a/src/test/suite/format.test.ts +++ b/src/test/suite/format.test.ts @@ -15,6 +15,14 @@ const getWorkspaceFolderUri = (workspaceFolderName: string) => { return workspaceFolder!.uri; }; +export async function getText(workspaceFolderName: string, file: string) { + const base = getWorkspaceFolderUri(workspaceFolderName); + const absPath = path.join(base.fsPath, file); + const doc = await vscode.workspace.openTextDocument(absPath); + const text = doc.getText(); + return text; +} + /** * loads and format a file. * @param file path relative to base URI (a workspaceFolder's URI) diff --git a/test-fixtures/.prettierrc b/test-fixtures/.prettierrc deleted file mode 100644 index 9e26dfeeb..000000000 --- a/test-fixtures/.prettierrc +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/test-fixtures/config/editorconfig/test.result.js b/test-fixtures/config/editorconfig/test.result.js new file mode 100644 index 000000000..2b2804a0f --- /dev/null +++ b/test-fixtures/config/editorconfig/test.result.js @@ -0,0 +1,3 @@ +function foo() { + console.log("test"); +} diff --git a/test-fixtures/config/jsconfigfile/test.result.js b/test-fixtures/config/jsconfigfile/test.result.js new file mode 100644 index 000000000..2b2804a0f --- /dev/null +++ b/test-fixtures/config/jsconfigfile/test.result.js @@ -0,0 +1,3 @@ +function foo() { + console.log("test"); +} diff --git a/test-fixtures/config/jsfile/test.js b/test-fixtures/config/jsfile/test.js index 017498dad..73dcd6a35 100644 --- a/test-fixtures/config/jsfile/test.js +++ b/test-fixtures/config/jsfile/test.js @@ -1,3 +1,7 @@ function foo() { - console.log("test"); -} + const foo = ["aaaaaaaaa", "bbbbbb", + "c","a", "b", + "c","a", "b", + "c","a", "b", + "c"] + } \ No newline at end of file diff --git a/test-fixtures/config/jsfile/test.result.js b/test-fixtures/config/jsfile/test.result.js new file mode 100644 index 000000000..c035be617 --- /dev/null +++ b/test-fixtures/config/jsfile/test.result.js @@ -0,0 +1,16 @@ +function foo() { + const foo = [ + "aaaaaaaaa", + "bbbbbb", + "c", + "a", + "b", + "c", + "a", + "b", + "c", + "a", + "b", + "c" + ]; +} diff --git a/test-fixtures/config/rcfile/test.result.js b/test-fixtures/config/rcfile/test.result.js new file mode 100644 index 000000000..2b2804a0f --- /dev/null +++ b/test-fixtures/config/rcfile/test.result.js @@ -0,0 +1,3 @@ +function foo() { + console.log("test"); +} diff --git a/test-fixtures/config/vscodeconfig/test.result.js b/test-fixtures/config/vscodeconfig/test.result.js new file mode 100644 index 000000000..591fbaec1 --- /dev/null +++ b/test-fixtures/config/vscodeconfig/test.result.js @@ -0,0 +1,16 @@ +function foo() { + const foo = [ + "aaaaaaaaa", + "bbbbbb", + "c", + "a", + "b", + "c", + "a", + "b", + "c", + "a", + "b", + "c", + ]; +}