From c08159efdfb4476fea971281e11eb73edc940454 Mon Sep 17 00:00:00 2001 From: Wei-Wei Wu Date: Mon, 26 Nov 2018 23:53:42 -0800 Subject: [PATCH 1/5] adding test --- package.json | 2 +- .../__tests__/config-resolution.js | 29 +++++++++ yarn.lock | 64 ++++++++++++++++--- 3 files changed, 85 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 45631dadb34b..9f7a42f85551 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "camelcase": "4.1.0", "chalk": "2.1.0", "cjk-regex": "2.0.0", - "cosmiconfig": "5.0.6", + "cosmiconfig": "5.0.7", "dashify": "0.2.2", "dedent": "0.7.0", "diff": "3.2.0", diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index 96ff204c6506..8118db83e198 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -1,6 +1,8 @@ "use strict"; const path = require("path"); +const tempy = require("tempy"); +const fs = require("fs"); const runPrettier = require("../runPrettier"); const prettier = require("prettier/local"); @@ -252,3 +254,30 @@ test("API resolveConfig resolves relative path values based on config filepath", pluginSearchDirs: [path.join(parentDir, "path-to-plugin-search-dir")] }); }); + +test("API resolveConfig should pick up .js config changes", () => { + const tempDir = tempy.directory(); + const tempConfigFile = `${tempDir}/prettier.config.js`; + const oldConfig = ` + "use strict"; + + module.exports = { + tabWidth: 8 + }; + `; + + const newConfig = ` + "use strict"; + + module.exports = { + tabWidth: 16 + }; + `; + fs.writeFileSync(tempConfigFile, oldConfig); + prettier.resolveConfig.sync(tempConfigFile, { useCache: false }); + fs.writeFileSync(tempConfigFile, newConfig); + prettier.clearConfigCache(); + expect( + prettier.resolveConfig.sync(tempConfigFile, { useCache: false }) + ).toEqual({ tabWidth: 16 }); +}); diff --git a/yarn.lock b/yarn.lock index dde24fd53eb9..32590033db97 100644 --- a/yarn.lock +++ b/yarn.lock @@ -775,8 +775,9 @@ are-we-there-yet@~1.1.2: readable-stream "^2.0.6" argparse@^1.0.7: - version "1.0.9" - resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.9.tgz#73d83bc263f86e97f8cc4f6bae1b0e90a7d22c86" + version "1.0.10" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" + integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg== dependencies: sprintf-js "~1.0.2" @@ -1279,12 +1280,26 @@ cache-base@^1.0.1: union-value "^1.0.0" unset-value "^1.0.0" +caller-callsite@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/caller-callsite/-/caller-callsite-2.0.0.tgz#847e0fce0a223750a9a027c54b33731ad3154134" + integrity sha1-hH4PzgoiN1CpoCfFSzNzGtMVQTQ= + dependencies: + callsites "^2.0.0" + caller-path@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f" dependencies: callsites "^0.2.0" +caller-path@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-2.0.0.tgz#468f83044e369ab2010fac5f06ceee15bb2cb1f4" + integrity sha1-Ro+DBE42mrIBD6xfBs7uFbsssfQ= + dependencies: + caller-callsite "^2.0.0" + callsites@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/callsites/-/callsites-0.2.0.tgz#afab96262910a7f33c19a5775825c69f34e350ca" @@ -1576,10 +1591,12 @@ core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" -cosmiconfig@5.0.6: - version "5.0.6" - resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.6.tgz#dca6cf680a0bd03589aff684700858c81abeeb39" +cosmiconfig@5.0.7: + version "5.0.7" + resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.0.7.tgz#39826b292ee0d78eda137dfa3173bd1c21a43b04" + integrity sha512-PcLqxTKiDmNT6pSpy4N6KtuPwb53W+2tzNvwOZw0WH9N6O0vLIBq0x8aj8Oj75ere4YcGi48bDFCL+3fRJdlNA== dependencies: + import-fresh "^2.0.0" is-directory "^0.3.1" js-yaml "^3.9.0" parse-json "^4.0.0" @@ -1911,12 +1928,19 @@ errno@^0.1.3: dependencies: prr "~0.0.0" -error-ex@^1.2.0, error-ex@^1.3.1: +error-ex@^1.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" dependencies: is-arrayish "^0.2.1" +error-ex@^1.3.1: + version "1.3.2" + resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf" + integrity sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g== + dependencies: + is-arrayish "^0.2.1" + es-abstract@^1.5.1, es-abstract@^1.7.0: version "1.12.0" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.12.0.tgz#9dbbdd27c6856f0001421ca18782d786bf8a6165" @@ -2148,8 +2172,9 @@ esprima@^3.1.3: resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633" esprima@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804" + version "4.0.1" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71" + integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A== esquery@^1.0.0: version "1.0.0" @@ -2832,6 +2857,14 @@ ignore@3.3.7, ignore@^3.3.3, ignore@^3.3.5: version "3.3.7" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021" +import-fresh@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546" + integrity sha1-2BNVwVYS04bGH53dOSLUMEgipUY= + dependencies: + caller-path "^2.0.0" + resolve-from "^3.0.0" + import-local@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/import-local/-/import-local-1.0.0.tgz#5e4ffdc03f4fe6c009c6729beb29631c2f8227bc" @@ -2939,6 +2972,7 @@ is-alphanumerical@^1.0.0: is-arrayish@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" + integrity sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0= is-binary-path@^1.0.0: version "1.0.1" @@ -3005,6 +3039,7 @@ is-descriptor@^1.0.0, is-descriptor@^1.0.2: is-directory@^0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1" + integrity sha1-YTObbyR1/Hcv2cnYP1yFddwVSuE= is-dotfile@^1.0.0: version "1.0.3" @@ -3664,13 +3699,21 @@ js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" -js-yaml@^3.7.0, js-yaml@^3.9.0, js-yaml@^3.9.1: +js-yaml@^3.7.0, js-yaml@^3.9.1: version "3.10.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" dependencies: argparse "^1.0.7" esprima "^4.0.0" +js-yaml@^3.9.0: + version "3.12.0" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1" + integrity sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A== + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + jsbn@~0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513" @@ -3729,6 +3772,7 @@ json-loader@^0.5.4: json-parse-better-errors@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" + integrity sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw== json-schema-traverse@^0.3.0: version "0.3.1" @@ -4470,6 +4514,7 @@ parse-json@^2.2.0: parse-json@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0" + integrity sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA= dependencies: error-ex "^1.3.1" json-parse-better-errors "^1.0.1" @@ -5481,6 +5526,7 @@ split-string@^3.0.1, split-string@^3.0.2: sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" + integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= sshpk@^1.7.0: version "1.13.1" From cfebbf2e49b19321c1f95de4f1c729164d5a53d4 Mon Sep 17 00:00:00 2001 From: Wei-Wei Wu Date: Mon, 26 Nov 2018 23:57:30 -0800 Subject: [PATCH 2/5] using tempy file --- tests_integration/__tests__/config-resolution.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index 8118db83e198..f901c1cd4d3a 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -256,8 +256,7 @@ test("API resolveConfig resolves relative path values based on config filepath", }); test("API resolveConfig should pick up .js config changes", () => { - const tempDir = tempy.directory(); - const tempConfigFile = `${tempDir}/prettier.config.js`; + const tempConfigFile = tempy.file({ name: "prettier.config.js" }); const oldConfig = ` "use strict"; From d441bb2d845befbdde8f7cb1bff10b5b2bc3ae8c Mon Sep 17 00:00:00 2001 From: Wei-Wei Wu Date: Tue, 27 Nov 2018 10:40:25 -0800 Subject: [PATCH 3/5] removing test --- .../__tests__/config-resolution.js | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index f901c1cd4d3a..96ff204c6506 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -1,8 +1,6 @@ "use strict"; const path = require("path"); -const tempy = require("tempy"); -const fs = require("fs"); const runPrettier = require("../runPrettier"); const prettier = require("prettier/local"); @@ -254,29 +252,3 @@ test("API resolveConfig resolves relative path values based on config filepath", pluginSearchDirs: [path.join(parentDir, "path-to-plugin-search-dir")] }); }); - -test("API resolveConfig should pick up .js config changes", () => { - const tempConfigFile = tempy.file({ name: "prettier.config.js" }); - const oldConfig = ` - "use strict"; - - module.exports = { - tabWidth: 8 - }; - `; - - const newConfig = ` - "use strict"; - - module.exports = { - tabWidth: 16 - }; - `; - fs.writeFileSync(tempConfigFile, oldConfig); - prettier.resolveConfig.sync(tempConfigFile, { useCache: false }); - fs.writeFileSync(tempConfigFile, newConfig); - prettier.clearConfigCache(); - expect( - prettier.resolveConfig.sync(tempConfigFile, { useCache: false }) - ).toEqual({ tabWidth: 16 }); -}); From d2a6deaeb149cade1dbc2feea39f552a1934e312 Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 28 Nov 2018 12:38:00 +0800 Subject: [PATCH 4/5] chore: fix build --- .../babel-plugins/transform-custom-require.js | 24 +++++++++++++++---- scripts/build/config.js | 5 ++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/scripts/build/babel-plugins/transform-custom-require.js b/scripts/build/babel-plugins/transform-custom-require.js index 16127cee6f88..523a97fda2a4 100644 --- a/scripts/build/babel-plugins/transform-custom-require.js +++ b/scripts/build/babel-plugins/transform-custom-require.js @@ -4,10 +4,12 @@ // BEFORE: // eval("require")("./path/to/file") // eval("require")(identifier) +// eval("require").cache // // AFTER: // require("./file") // require(identifier) +// require.cache // module.exports = function(babel) { @@ -17,7 +19,7 @@ module.exports = function(babel) { visitor: { CallExpression(path) { const node = path.node; - if (isEvalRequire(node)) { + if (isEvalRequire(node.callee) && node.arguments.length === 1) { let arg = node.arguments[0]; if (t.isLiteral(arg) && arg.value.startsWith(".")) { const value = "." + arg.value.substring(arg.value.lastIndexOf("/")); @@ -25,17 +27,29 @@ module.exports = function(babel) { } path.replaceWith(t.callExpression(t.identifier("require"), [arg])); } + }, + MemberExpression(path) { + const node = path.node; + if (isEvalRequire(node.object)) { + path.replaceWith( + t.memberExpression( + t.identifier("require"), + node.property, + node.compute, + node.optional + ) + ); + } } } }; function isEvalRequire(node) { return ( - t.isCallExpression(node.callee) && + t.isCallExpression(node) && + t.isIdentifier(node.callee, { name: "eval" }) && node.arguments.length === 1 && - t.isIdentifier(node.callee.callee, { name: "eval" }) && - node.callee.arguments.length === 1 && - t.isLiteral(node.callee.arguments[0], { value: "require" }) + t.isLiteral(node.arguments[0], { value: "require" }) ); } }; diff --git a/scripts/build/config.js b/scripts/build/config.js index 40db88202388..b840055b6031 100644 --- a/scripts/build/config.js +++ b/scripts/build/config.js @@ -125,9 +125,10 @@ const coreBundles = [ type: "core", target: "node", replace: { - // cosmiconfig@5 uses `require` to resolve js config, which caused Error: + // cosmiconfig@5 -> import-fresh uses `require` to resolve js config, which caused Error: // Dynamic requires are not currently supported by rollup-plugin-commonjs. - "require(filepath)": "eval('require')(filepath)" + "require(filePath)": "eval('require')(filePath)", + "require.cache": "eval('require').cache" } } ]; From f8b7c225d3b4128dc9471e755a424257ccdf0b2b Mon Sep 17 00:00:00 2001 From: Ika Date: Wed, 28 Nov 2018 12:53:05 +0800 Subject: [PATCH 5/5] chore: update cache version --- scripts/build/build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/build.js b/scripts/build/build.js index b35ffcf45570..e9f2c35a2b1d 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -99,7 +99,7 @@ async function run(params) { await execa("rm", ["-rf", ".cache"]); } - const bundleCache = new Cache(".cache/", "v8"); + const bundleCache = new Cache(".cache/", "v9"); await bundleCache.load(); console.log(chalk.inverse(" Building packages "));