diff --git a/docs/configuration.md b/docs/configuration.md index 6d1b706e..34783b02 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -83,11 +83,3 @@ For more information on how to use the CLI to locate a file, see the [CLI](cli.m ## Configuration Schema If you'd like a JSON schema to validate your configuration, one is available here: http://json.schemastore.org/prettierrc. - -## EditorConfig - -If an [`.editorconfig` file](http://editorconfig.org/) is in your project, Prettier will parse it and convert its properties to the corresponding prettier configuration. This configuration will be overridden by `.prettierrc`, etc. Currently, the following EditorConfig properties are supported: - -* `indent_style` -* `indent_size`/`tab_width` -* `max_line_length` diff --git a/package.json b/package.json index 88528814..4e7376f9 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,6 @@ "cosmiconfig": "3.1.0", "dashify": "0.2.2", "diff": "3.2.0", - "editorconfig": "0.14.2", - "editorconfig-to-prettier": "0.0.1", "emoji-regex": "6.5.1", "escape-string-regexp": "1.0.5", "esutils": "2.0.2", @@ -39,7 +37,6 @@ "minimatch": "3.0.4", "minimist": "1.2.0", "parse5": "3.0.3", - "path-root": "0.1.1", "postcss-less": "1.1.1", "postcss-media-query-parser": "0.2.3", "postcss-scss": "1.0.2", diff --git a/src/resolve-config-editorconfig.js b/src/resolve-config-editorconfig.js deleted file mode 100644 index 5aa0b390..00000000 --- a/src/resolve-config-editorconfig.js +++ /dev/null @@ -1,43 +0,0 @@ -"use strict"; - -const editorconfig = require("editorconfig"); -const mem = require("mem"); -const pathRoot = require("path-root"); -const editorConfigToPrettier = require("editorconfig-to-prettier"); - -const maybeParse = (filePath, config, parse) => { - const root = filePath && pathRoot(filePath); - return filePath && !config && parse(filePath, { root }); -}; - -const editorconfigAsyncNoCache = (filePath, config) => { - return Promise.resolve(maybeParse(filePath, config, editorconfig.parse)).then( - editorConfigToPrettier - ); -}; -const editorconfigAsyncWithCache = mem(editorconfigAsyncNoCache); - -const editorconfigSyncNoCache = (filePath, config) => { - return editorConfigToPrettier( - maybeParse(filePath, config, editorconfig.parseSync) - ); -}; -const editorconfigSyncWithCache = mem(editorconfigSyncNoCache); - -function getLoadFunction(opts) { - if (opts.sync) { - return opts.cache ? editorconfigSyncWithCache : editorconfigSyncNoCache; - } - - return opts.cache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache; -} - -function clearCache() { - mem.clear(editorconfigSyncWithCache); - mem.clear(editorconfigAsyncWithCache); -} - -module.exports = { - getLoadFunction, - clearCache -}; diff --git a/src/resolve-config.js b/src/resolve-config.js index d4695656..b97433f8 100644 --- a/src/resolve-config.js +++ b/src/resolve-config.js @@ -5,8 +5,6 @@ const minimatch = require("minimatch"); const path = require("path"); const mem = require("mem"); -const resolveEditorConfig = require("./resolve-config-editorconfig"); - const getExplorerMemoized = mem(opts => cosmiconfig("prettier", { sync: opts.sync, @@ -28,43 +26,23 @@ function getLoadFunction(opts) { return getExplorerMemoized(opts).load; } -function _resolveConfig(filePath, opts, sync) { +function resolveConfig(filePath, opts) { opts = Object.assign({ useCache: true }, opts); - const loadOpts = { cache: !!opts.useCache, sync: !!sync }; - const load = getLoadFunction(loadOpts); - const loadEditorConfig = resolveEditorConfig.getLoadFunction(loadOpts); - const arr = [load, loadEditorConfig].map(l => l(filePath, opts.config)); - - const unwrapAndMerge = arr => { - const result = arr[0]; - const editorConfigured = arr[1]; - const merged = Object.assign( - {}, - editorConfigured, - mergeOverrides(Object.assign({}, result), filePath) - ); - - if (Object.keys(merged).length === 0) { - return null; - } - - return merged; - }; - - if (loadOpts.sync) { - return unwrapAndMerge(arr); - } - - return Promise.all(arr).then(unwrapAndMerge); + const load = getLoadFunction({ cache: !!opts.useCache, sync: false }); + return load(filePath, opts.config).then(result => { + return !result ? null : mergeOverrides(result, filePath); + }); } -const resolveConfig = (filePath, opts) => _resolveConfig(filePath, opts, false); - -resolveConfig.sync = (filePath, opts) => _resolveConfig(filePath, opts, true); +resolveConfig.sync = (filePath, opts) => { + opts = Object.assign({ useCache: true }, opts); + const load = getLoadFunction({ cache: !!opts.useCache, sync: true }); + const result = load(filePath, opts.config); + return !result ? null : mergeOverrides(result, filePath); +}; function clearCache() { mem.clear(getExplorerMemoized); - resolveEditorConfig.clearCache(); } function resolveConfigFile(filePath) { diff --git a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap index 7f05743b..e4ff66ca 100644 --- a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap @@ -3,22 +3,7 @@ exports[`CLI overrides take precedence (stderr) 1`] = `""`; exports[`CLI overrides take precedence (stdout) 1`] = ` -"function f() { - console.log( - \\"should have tab width 8\\" - ) -} -function f() { - console.log( - \\"should have space width 2\\" - ) -} -function f() { - console.log( - \\"should have space width 8\\" - ) -} -console.log( +"console.log( \\"jest/__best-tests__/file.js should have semi\\" ); console.log( @@ -99,16 +84,7 @@ exports[`resolves configuration file with --find-config-path file (write) 1`] = exports[`resolves configuration from external files (stderr) 1`] = `""`; exports[`resolves configuration from external files (stdout) 1`] = ` -"function f() { - console.log(\\"should have tab width 8\\") -} -function f() { - console.log(\\"should have space width 2\\") -} -function f() { - console.log(\\"should have space width 8\\") -} -console.log(\\"jest/__best-tests__/file.js should have semi\\"); +"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() { diff --git a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap index 907521e3..03eb6f6c 100644 --- a/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap +++ b/tests_integration/__tests__/__snapshots__/with-config-precedence.js.snap @@ -80,22 +80,7 @@ exports[`CLI overrides take lower precedence with --config-precedence file-overr exports[`CLI overrides take precedence with --config-precedence cli-override (stderr) 1`] = `""`; exports[`CLI overrides take precedence with --config-precedence cli-override (stdout) 1`] = ` -"function f() { - console.log( - \\"should have tab width 8\\" - ) -} -function f() { - console.log( - \\"should have space width 2\\" - ) -} -function f() { - console.log( - \\"should have space width 8\\" - ) -} -console.log( +"console.log( \\"jest/__best-tests__/file.js should have semi\\" ); console.log( @@ -152,22 +137,7 @@ exports[`CLI overrides take precedence with --config-precedence cli-override (wr exports[`CLI overrides take precedence without --config-precedence (stderr) 1`] = `""`; exports[`CLI overrides take precedence without --config-precedence (stdout) 1`] = ` -"function f() { - console.log( - \\"should have tab width 8\\" - ) -} -function f() { - console.log( - \\"should have space width 2\\" - ) -} -function f() { - console.log( - \\"should have space width 8\\" - ) -} -console.log( +"console.log( \\"jest/__best-tests__/file.js should have semi\\" ); console.log( diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index 0332cf06..390e022c 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -102,98 +102,6 @@ test("API resolveConfig.sync with file arg and extension override", () => { }); }); -test("API resolveConfig with file arg and .editorconfig", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/file.js") - ); - return prettier.resolveConfig(file).then(result => { - expect(result).toMatchObject({ - useTabs: true, - tabWidth: 8, - printWidth: 100 - }); - }); -}); - -test("API resolveConfig.sync with file arg and .editorconfig", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/file.js") - ); - expect(prettier.resolveConfig.sync(file)).toMatchObject({ - useTabs: true, - tabWidth: 8, - printWidth: 100 - }); -}); - -test("API resolveConfig with nested file arg and .editorconfig", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/lib/file.js") - ); - return prettier.resolveConfig(file).then(result => { - expect(result).toMatchObject({ - useTabs: false, - tabWidth: 2, - printWidth: 100 - }); - }); -}); - -test("API resolveConfig.sync with nested file arg and .editorconfig", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/lib/file.js") - ); - expect(prettier.resolveConfig.sync(file)).toMatchObject({ - useTabs: false, - tabWidth: 2, - printWidth: 100 - }); -}); - -test("API resolveConfig with nested file arg and .editorconfig and indent_size = tab", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js") - ); - return prettier.resolveConfig(file).then(result => { - expect(result).toMatchObject({ - useTabs: false, - tabWidth: 8, - printWidth: 100 - }); - }); -}); - -test("API resolveConfig.sync with nested file arg and .editorconfig and indent_size = tab", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js") - ); - expect(prettier.resolveConfig.sync(file)).toMatchObject({ - useTabs: false, - tabWidth: 8, - printWidth: 100 - }); -}); - -test("API resolveConfig with missing file arg", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/file.shouldnotexist") - ); - return prettier.resolveConfig(file).then(result => { - expect(result).toBeNull(); - }); -}); - -test("API resolveConfig.sync with missing file arg", () => { - const file = path.resolve( - path.join(__dirname, "../cli/config/editorconfig/file.shouldnotexist") - ); - expect(prettier.resolveConfig.sync(file)).toBeNull(); -}); - -test("API clearConfigCache", () => { - expect(() => prettier.clearConfigCache()).not.toThrowError(); -}); - test("API resolveConfig.sync overrides work with absolute paths", () => { // Absolute path const file = path.join(__dirname, "../cli/config/filepath/subfolder/file.js"); diff --git a/tests_integration/cli/config/.prettierrc b/tests_integration/cli/config/.prettierrc index 8f522b78..177d6bda 100644 --- a/tests_integration/cli/config/.prettierrc +++ b/tests_integration/cli/config/.prettierrc @@ -1,7 +1,6 @@ +semi: false + overrides: -- files: "*.js" - options: - semi: false - files: "*.ts" options: semi: true diff --git a/tests_integration/cli/config/editorconfig/.editorconfig b/tests_integration/cli/config/editorconfig/.editorconfig deleted file mode 100644 index 42984e9d..00000000 --- a/tests_integration/cli/config/editorconfig/.editorconfig +++ /dev/null @@ -1,15 +0,0 @@ -root = true - -[*.js] -indent_style = tab -tab_width = 8 -indent_size = 2 # overridden by tab_width since indent_style = tab -max_line_length = 100 - -# Indentation override for all JS under lib directory -[lib/**.js] -indent_style = space -indent_size = 2 - -[lib/indent_size=tab.js] -indent_size = tab diff --git a/tests_integration/cli/config/editorconfig/file.js b/tests_integration/cli/config/editorconfig/file.js deleted file mode 100644 index 2e2b4c35..00000000 --- a/tests_integration/cli/config/editorconfig/file.js +++ /dev/null @@ -1,3 +0,0 @@ -function f() { - console.log("should have tab width 8"); -} diff --git a/tests_integration/cli/config/editorconfig/lib/file.js b/tests_integration/cli/config/editorconfig/lib/file.js deleted file mode 100644 index b9050554..00000000 --- a/tests_integration/cli/config/editorconfig/lib/file.js +++ /dev/null @@ -1,3 +0,0 @@ -function f() { - console.log("should have space width 2"); -} diff --git a/tests_integration/cli/config/editorconfig/lib/indent_size=tab.js b/tests_integration/cli/config/editorconfig/lib/indent_size=tab.js deleted file mode 100644 index 1b4f9e78..00000000 --- a/tests_integration/cli/config/editorconfig/lib/indent_size=tab.js +++ /dev/null @@ -1,3 +0,0 @@ -function f() { - console.log("should have space width 8"); -} diff --git a/tests_integration/cli/config/js/.editorconfig b/tests_integration/cli/config/js/.editorconfig deleted file mode 100644 index 7a2bce1a..00000000 --- a/tests_integration/cli/config/js/.editorconfig +++ /dev/null @@ -1,4 +0,0 @@ -# This file should be overridden by prettier.config.js - -[*] -tab_width = 1 diff --git a/tests_integration/cli/config/package/.editorconfig b/tests_integration/cli/config/package/.editorconfig deleted file mode 100644 index e22303d2..00000000 --- a/tests_integration/cli/config/package/.editorconfig +++ /dev/null @@ -1,7 +0,0 @@ -# This file should be overridden by package.json - -[*] -tab_width = 1 - -[*.ts] -tab_width = 1 diff --git a/yarn.lock b/yarn.lock index a5f3325a..edf4cce7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -823,10 +823,6 @@ block-stream@*: dependencies: inherits "~2.0.0" -bluebird@^3.0.5: - version "3.5.0" - resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.0.tgz#791420d7f551eea2897453a8a77653f96606d67c" - bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0: version "4.11.6" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.6.tgz#53344adb14617a13f6e8dd2ce28905d1c0ba3215" @@ -1374,20 +1370,6 @@ ecc-jsbn@~0.1.1: dependencies: jsbn "~0.1.0" -editorconfig-to-prettier@0.0.1: - version "0.0.1" - resolved "https://registry.yarnpkg.com/editorconfig-to-prettier/-/editorconfig-to-prettier-0.0.1.tgz#2ce9555fbb4267c15fc0de70a0eb63220c1963cd" - -editorconfig@0.14.2: - version "0.14.2" - resolved "https://registry.yarnpkg.com/editorconfig/-/editorconfig-0.14.2.tgz#60fe3336878f38d6e19e0b16cf1f41903f57e6f4" - dependencies: - bluebird "^3.0.5" - commander "^2.9.0" - lru-cache "^3.2.0" - semver "^5.1.0" - sigmund "^1.0.1" - elliptic@^6.0.0: version "6.4.0" resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.4.0.tgz#cac9af8762c85836187003c8dfe193e5e2eae5df" @@ -2827,12 +2809,6 @@ loose-envify@^1.0.0: dependencies: js-tokens "^3.0.0" -lru-cache@^3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-3.2.0.tgz#71789b3b7f5399bec8565dda38aa30d2a097efee" - dependencies: - pseudomap "^1.0.1" - lru-cache@^4.0.1: version "4.1.1" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55" @@ -3258,16 +3234,6 @@ path-parse@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" -path-root-regex@^0.1.0: - version "0.1.2" - resolved "https://registry.yarnpkg.com/path-root-regex/-/path-root-regex-0.1.2.tgz#bfccdc8df5b12dc52c8b43ec38d18d72c04ba96d" - -path-root@0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/path-root/-/path-root-0.1.1.tgz#9a4a6814cac1c0cd73360a95f32083c8ea4745b7" - dependencies: - path-root-regex "^0.1.0" - path-to-regexp@^1.0.1: version "1.7.0" resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d" @@ -3429,7 +3395,7 @@ prr@~0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a" -pseudomap@^1.0.1, pseudomap@^1.0.2: +pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" @@ -3870,7 +3836,7 @@ sax@^1.2.1: version "1.2.2" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.2.tgz#fd8631a23bc7826bef5d871bdb87378c95647828" -"semver@2 || 3 || 4 || 5", semver@5.4.1, semver@^5.1.0, semver@^5.3.0: +"semver@2 || 3 || 4 || 5", semver@5.4.1, semver@^5.3.0: version "5.4.1" resolved "https://registry.yarnpkg.com/semver/-/semver-5.4.1.tgz#e059c09d8571f0540823733433505d3a2f00b18e" @@ -3918,10 +3884,6 @@ shellwords@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14" -sigmund@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590" - signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"