From 592f99fde69880c69fadb3887db911d87fa306a6 Mon Sep 17 00:00:00 2001 From: Nick Ribal Date: Sat, 16 Sep 2017 19:47:53 +0300 Subject: [PATCH] Enable cosmiconfig rcExtensions (#2749) * Enable cosmiconfig rcExtensions; add two tests * Remove default value; make integration input files self descripting; update all integration snapshots * Add engines field to package.json --- README.md | 2 +- package.json | 3 + src/resolve-config.js | 6 +- .../__snapshots__/config-resolution.js.snap | 87 +++++++++++++------ .../with-config-precedence.js.snap | 80 ++++++++++------- .../__tests__/config-resolution.js | 18 ++++ .../cli/config/jest/Component.js | 2 +- .../cli/config/jest/Component.test.js | 2 +- .../cli/config/jest/__best-tests__/file.js | 2 +- tests_integration/cli/config/js/file.js | 4 +- .../cli/config/no-config/file.js | 4 +- .../cli/config/no-config/file.ts | 4 +- tests_integration/cli/config/package/file.js | 4 +- tests_integration/cli/config/package/file.ts | 4 +- .../rc-json/{.prettierrc => .prettierrc.json} | 0 tests_integration/cli/config/rc-json/file.js | 6 +- .../cli/config/rc-yaml/.prettierrc.yaml | 2 + tests_integration/cli/config/rc-yaml/file.js | 7 ++ 18 files changed, 161 insertions(+), 76 deletions(-) rename tests_integration/cli/config/rc-json/{.prettierrc => .prettierrc.json} (100%) create mode 100644 tests_integration/cli/config/rc-yaml/.prettierrc.yaml create mode 100644 tests_integration/cli/config/rc-yaml/file.js diff --git a/README.md b/README.md index 3be99e973..faab0f741 100644 --- a/README.md +++ b/README.md @@ -699,7 +699,7 @@ Default | CLI Override | API Override Prettier uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for configuration file support. This means you can configure prettier via: -* A `.prettierrc` file, written in YAML or JSON. +* A `.prettierrc` file, written in YAML or JSON, with optional extensions: `.yaml/.yml/.json/.js`. * A `prettier.config.js` file that exports an object. * A `"prettier"` key in your `package.json` file. diff --git a/package.json b/package.json index 33b256593..b3f43ee5b 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,9 @@ "author": "James Long", "license": "MIT", "main": "./index.js", + "engines": { + "node": ">=4" + }, "dependencies": { "babel-code-frame": "7.0.0-alpha.12", "babylon": "7.0.0-beta.23", diff --git a/src/resolve-config.js b/src/resolve-config.js index 3140490fa..72b9ea853 100644 --- a/src/resolve-config.js +++ b/src/resolve-config.js @@ -5,7 +5,11 @@ const minimatch = require("minimatch"); const mem = require("mem"); const getExplorerMemoized = mem(opts => - cosmiconfig("prettier", { sync: opts.sync, cache: opts.cache }) + cosmiconfig("prettier", { + sync: opts.sync, + cache: opts.cache, + rcExtensions: true + }) ); /** @param {{ cache: boolean, sync: boolean }} opts */ diff --git a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap index da0b82de5..f607859a0 100644 --- a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap @@ -2,17 +2,17 @@ exports[`CLI overrides take precedence 1`] = ` "console.log( - \\"should have semi\\" + \\"jest/__best-tests__/file.js should have semi\\" ); console.log( - \\"should not have semi\\" + \\"jest/Component.js should not have semi\\" ) console.log( - \\"should have semi\\" + \\"jest/Component.test.js should have semi\\" ); -function f() { +function js() { console.log( - \\"should have tab width 8\\" + \\"js/file.js should have tab width 8\\" ); } \\"use strict\\"; @@ -20,21 +20,31 @@ function f() { module.exports = { tabWidth: 8 }; -function f() { +function noConfigJs() { console.log( - \\"should have no semicolons\\" + \\"no-config/file.js should have no semicolons\\" ) } -function f() { +function packageJs() { console.log( - \\"should have tab width 3\\" + \\"package/file.js should have tab width 3\\" ); } -function f() { +function rcJson() { console.log.apply( null, [ - 'this file', + 'rc-json/file.js', + 'should have trailing comma', + 'and single quotes', + ], + ); +} +function rcYaml() { + console.log.apply( + null, + [ + 'rc-yaml/file.js', 'should have trailing comma', 'and single quotes', ], @@ -44,8 +54,8 @@ function f() { `; exports[`accepts configuration from --config 1`] = ` -"function f() { - console.log(\\"should have tab width 8\\") +"function js() { + console.log(\\"js/file.js should have tab width 8\\") } " `; @@ -56,26 +66,33 @@ exports[`resolves configuration file with --find-config-path file 1`] = ` `; exports[`resolves configuration from external files 1`] = ` -"console.log(\\"should have semi\\"); -console.log(\\"should not have semi\\") -console.log(\\"should have semi\\"); -function f() { - console.log(\\"should have tab width 8\\"); +"console.log(\\"jest/__best-tests__/file.js should have semi\\"); +console.log(\\"jest/Component.js should not have semi\\") +console.log(\\"jest/Component.test.js should have semi\\"); +function js() { + console.log(\\"js/file.js should have tab width 8\\"); } \\"use strict\\"; module.exports = { tabWidth: 8 }; -function f() { - console.log(\\"should have no semicolons\\") +function noConfigJs() { + console.log(\\"no-config/file.js should have no semicolons\\") } -function f() { - console.log(\\"should have tab width 3\\"); +function packageJs() { + console.log(\\"package/file.js should have tab width 3\\"); } -function f() { +function rcJson() { console.log.apply(null, [ - 'this file', + 'rc-json/file.js', + 'should have trailing comma', + 'and single quotes', + ]); +} +function rcYaml() { + console.log.apply(null, [ + 'rc-yaml/file.js', 'should have trailing comma', 'and single quotes', ]); @@ -84,11 +101,25 @@ function f() { `; exports[`resolves configuration from external files and overrides by extname 1`] = ` -"function g() { - console.log(\\"should have semicolons because it has a .ts extension\\"); +"function noConfigTs() { + console.log( + \\"no-config/file.ts should have semicolons because it has a .ts extension\\" + ); } -function g() { - console.log(\\"should have tab width 5 because it has .ts extension\\"); +function packageTs() { + console.log( + \\"package/file.ts should have tab width 5 because it has .ts extension\\" + ); } " `; + +exports[`resolves json configuration file with --find-config-path file 1`] = ` +"rc-json/.prettierrc.json +" +`; + +exports[`resolves yaml configuration file with --find-config-path file 1`] = ` +"rc-yaml/.prettierrc.yaml +" +`; diff --git a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap index 7b469f9a3..d9e6081db 100644 --- a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap +++ b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap @@ -1,24 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`CLI overrides are still applied when no config is found with --config-precedence file-override 1`] = ` -"function f() { - console.log(\\"should have no semicolons\\") +"function noConfigJs() { + console.log(\\"no-config/file.js should have no semicolons\\") } " `; exports[`CLI overrides gets applied when no config exists with --config-precedence prefer-file 1`] = ` -"function f() { +"function noConfigJs() { console.log( - \\"should have no semicolons\\" + \\"no-config/file.js should have no semicolons\\" ); } " `; exports[`CLI overrides gets ignored when config exists with --config-precedence prefer-file 1`] = ` -"function f() { - console.log(\\"should have tab width 8\\"); +"function js() { + console.log(\\"js/file.js should have tab width 8\\"); } \\"use strict\\"; @@ -29,8 +29,8 @@ module.exports = { `; exports[`CLI overrides take lower precedence with --config-precedence file-override 1`] = ` -"function f() { - console.log(\\"should have tab width 8\\"); +"function js() { + console.log(\\"js/file.js should have tab width 8\\"); } \\"use strict\\"; @@ -42,17 +42,17 @@ module.exports = { exports[`CLI overrides take precedence with --config-precedence cli-override 1`] = ` "console.log( - \\"should have semi\\" + \\"jest/__best-tests__/file.js should have semi\\" ); console.log( - \\"should not have semi\\" + \\"jest/Component.js should not have semi\\" ) console.log( - \\"should have semi\\" + \\"jest/Component.test.js should have semi\\" ); -function f() { +function js() { console.log( - \\"should have tab width 8\\" + \\"js/file.js should have tab width 8\\" ); } \\"use strict\\"; @@ -60,21 +60,31 @@ function f() { module.exports = { tabWidth: 8 }; -function f() { +function noConfigJs() { console.log( - \\"should have no semicolons\\" + \\"no-config/file.js should have no semicolons\\" ) } -function f() { +function packageJs() { console.log( - \\"should have tab width 3\\" + \\"package/file.js should have tab width 3\\" ); } -function f() { +function rcJson() { console.log.apply( null, [ - 'this file', + 'rc-json/file.js', + 'should have trailing comma', + 'and single quotes', + ], + ); +} +function rcYaml() { + console.log.apply( + null, + [ + 'rc-yaml/file.js', 'should have trailing comma', 'and single quotes', ], @@ -85,17 +95,17 @@ function f() { exports[`CLI overrides take precedence without --config-precedence 1`] = ` "console.log( - \\"should have semi\\" + \\"jest/__best-tests__/file.js should have semi\\" ); console.log( - \\"should not have semi\\" + \\"jest/Component.js should not have semi\\" ) console.log( - \\"should have semi\\" + \\"jest/Component.test.js should have semi\\" ); -function f() { +function js() { console.log( - \\"should have tab width 8\\" + \\"js/file.js should have tab width 8\\" ); } \\"use strict\\"; @@ -103,21 +113,31 @@ function f() { module.exports = { tabWidth: 8 }; -function f() { +function noConfigJs() { console.log( - \\"should have no semicolons\\" + \\"no-config/file.js should have no semicolons\\" ) } -function f() { +function packageJs() { console.log( - \\"should have tab width 3\\" + \\"package/file.js should have tab width 3\\" ); } -function f() { +function rcJson() { console.log.apply( null, [ - 'this file', + 'rc-json/file.js', + 'should have trailing comma', + 'and single quotes', + ], + ); +} +function rcYaml() { + console.log.apply( + null, + [ + 'rc-yaml/file.js', 'should have trailing comma', 'and single quotes', ], diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index 87b976ce8..437930a8b 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -36,6 +36,24 @@ test("resolves configuration file with --find-config-path file", () => { expect(output.status).toEqual(0); }); +test("resolves json configuration file with --find-config-path file", () => { + const output = runPrettier("cli/config/", [ + "--find-config-path", + "rc-json/file.js" + ]); + expect(output.stdout).toMatchSnapshot(); + expect(output.status).toEqual(0); +}); + +test("resolves yaml configuration file with --find-config-path file", () => { + const output = runPrettier("cli/config/", [ + "--find-config-path", + "rc-yaml/file.js" + ]); + expect(output.stdout).toMatchSnapshot(); + expect(output.status).toEqual(0); +}); + test("prints nothing when no file found with --find-config-path", () => { const output = runPrettier("cli/config/", ["--find-config-path", ".."]); expect(output.stdout).toEqual(""); diff --git a/tests_integration/cli/config/jest/Component.js b/tests_integration/cli/config/jest/Component.js index 190c83a1c..2b939d666 100644 --- a/tests_integration/cli/config/jest/Component.js +++ b/tests_integration/cli/config/jest/Component.js @@ -1 +1 @@ -console.log("should not have semi") +console.log("jest/Component.js should not have semi") diff --git a/tests_integration/cli/config/jest/Component.test.js b/tests_integration/cli/config/jest/Component.test.js index c4a064694..ce6da3184 100644 --- a/tests_integration/cli/config/jest/Component.test.js +++ b/tests_integration/cli/config/jest/Component.test.js @@ -1 +1 @@ -console.log("should have semi"); +console.log("jest/Component.test.js should have semi"); diff --git a/tests_integration/cli/config/jest/__best-tests__/file.js b/tests_integration/cli/config/jest/__best-tests__/file.js index c4a064694..6378e7a20 100644 --- a/tests_integration/cli/config/jest/__best-tests__/file.js +++ b/tests_integration/cli/config/jest/__best-tests__/file.js @@ -1 +1 @@ -console.log("should have semi"); +console.log("jest/__best-tests__/file.js should have semi"); diff --git a/tests_integration/cli/config/js/file.js b/tests_integration/cli/config/js/file.js index 2e2b4c359..b6ce43500 100644 --- a/tests_integration/cli/config/js/file.js +++ b/tests_integration/cli/config/js/file.js @@ -1,3 +1,3 @@ -function f() { - console.log("should have tab width 8"); +function js() { + console.log("js/file.js should have tab width 8"); } diff --git a/tests_integration/cli/config/no-config/file.js b/tests_integration/cli/config/no-config/file.js index e5f18dccc..40f160bb7 100644 --- a/tests_integration/cli/config/no-config/file.js +++ b/tests_integration/cli/config/no-config/file.js @@ -1,3 +1,3 @@ -function f() { - console.log("should have no semicolons"); +function noConfigJs() { + console.log("no-config/file.js should have no semicolons"); } diff --git a/tests_integration/cli/config/no-config/file.ts b/tests_integration/cli/config/no-config/file.ts index 0aa076691..fc53f20db 100644 --- a/tests_integration/cli/config/no-config/file.ts +++ b/tests_integration/cli/config/no-config/file.ts @@ -1,3 +1,3 @@ -function g() { - console.log("should have semicolons because it has a .ts extension"); +function noConfigTs() { + console.log("no-config/file.ts should have semicolons because it has a .ts extension"); } diff --git a/tests_integration/cli/config/package/file.js b/tests_integration/cli/config/package/file.js index 668d86f50..e33d718e4 100644 --- a/tests_integration/cli/config/package/file.js +++ b/tests_integration/cli/config/package/file.js @@ -1,3 +1,3 @@ -function f() { - console.log("should have tab width 3"); +function packageJs() { + console.log("package/file.js should have tab width 3"); } diff --git a/tests_integration/cli/config/package/file.ts b/tests_integration/cli/config/package/file.ts index 77097eff9..37a36bd65 100644 --- a/tests_integration/cli/config/package/file.ts +++ b/tests_integration/cli/config/package/file.ts @@ -1,3 +1,3 @@ -function g() { - console.log("should have tab width 5 because it has .ts extension"); +function packageTs() { + console.log("package/file.ts should have tab width 5 because it has .ts extension"); } diff --git a/tests_integration/cli/config/rc-json/.prettierrc b/tests_integration/cli/config/rc-json/.prettierrc.json similarity index 100% rename from tests_integration/cli/config/rc-json/.prettierrc rename to tests_integration/cli/config/rc-json/.prettierrc.json diff --git a/tests_integration/cli/config/rc-json/file.js b/tests_integration/cli/config/rc-json/file.js index e3340e7e9..fd4434480 100644 --- a/tests_integration/cli/config/rc-json/file.js +++ b/tests_integration/cli/config/rc-json/file.js @@ -1,7 +1,7 @@ -function f() { +function rcJson() { console.log.apply(null, [ - "this file", + "rc-json/file.js", "should have trailing comma", - "and single quotes", + "and single quotes" ]); } diff --git a/tests_integration/cli/config/rc-yaml/.prettierrc.yaml b/tests_integration/cli/config/rc-yaml/.prettierrc.yaml new file mode 100644 index 000000000..6f1285d62 --- /dev/null +++ b/tests_integration/cli/config/rc-yaml/.prettierrc.yaml @@ -0,0 +1,2 @@ +trailingComma: all +singleQuote: true diff --git a/tests_integration/cli/config/rc-yaml/file.js b/tests_integration/cli/config/rc-yaml/file.js new file mode 100644 index 000000000..94bc24853 --- /dev/null +++ b/tests_integration/cli/config/rc-yaml/file.js @@ -0,0 +1,7 @@ +function rcYaml() { + console.log.apply(null, [ + "rc-yaml/file.js", + "should have trailing comma", + "and single quotes" + ]); +}