Skip to content

Commit

Permalink
Fixed issue where vsc config and local were merged. Closes #1074
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Nov 19, 2019
1 parent 74abd65 commit 560fdaa
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->

## [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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
48 changes: 28 additions & 20 deletions src/ConfigResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 23 additions & 31 deletions src/test/suite/config.test.ts
Original file line number Diff line number Diff line change
@@ -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")
);
});
8 changes: 8 additions & 0 deletions src/test/suite/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion test-fixtures/.prettierrc

This file was deleted.

3 changes: 3 additions & 0 deletions test-fixtures/config/editorconfig/test.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() {
console.log("test");
}
3 changes: 3 additions & 0 deletions test-fixtures/config/jsconfigfile/test.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() {
console.log("test");
}
8 changes: 6 additions & 2 deletions test-fixtures/config/jsfile/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function foo() {
console.log("test");
}
const foo = ["aaaaaaaaa", "bbbbbb",
"c","a", "b",
"c","a", "b",
"c","a", "b",
"c"]
}
16 changes: 16 additions & 0 deletions test-fixtures/config/jsfile/test.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function foo() {
const foo = [
"aaaaaaaaa",
"bbbbbb",
"c",
"a",
"b",
"c",
"a",
"b",
"c",
"a",
"b",
"c"
];
}
3 changes: 3 additions & 0 deletions test-fixtures/config/rcfile/test.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() {
console.log("test");
}
16 changes: 16 additions & 0 deletions test-fixtures/config/vscodeconfig/test.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function foo() {
const foo = [
"aaaaaaaaa",
"bbbbbb",
"c",
"a",
"b",
"c",
"a",
"b",
"c",
"a",
"b",
"c",
];
}

0 comments on commit 560fdaa

Please sign in to comment.