From 35d900619b78010e257b46e568b2d9b90c98e3ad Mon Sep 17 00:00:00 2001 From: shaodahong Date: Wed, 3 Jun 2020 22:12:32 +0800 Subject: [PATCH 1/4] fix: import order --- .gitignore | 1 + package.json | 1 + src/Plugin.js | 46 +++-- test/fixtures/import-order/actual.js | 4 + test/fixtures/import-order/expected.js | 9 + test/index.test.js | 18 +- yarn.lock | 260 +++++++++++++++++++++++++ 7 files changed, 322 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/import-order/actual.js create mode 100644 test/fixtures/import-order/expected.js diff --git a/.gitignore b/.gitignore index fc3495b2..885756bd 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ coverage lib *.log *.log.* +.vscode diff --git a/package.json b/package.json index bf21fdc2..196eb8a2 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@babel/core": "^7.0.0", "@babel/preset-react": "^7.0.0", "babel-core": "^7.0.0-0", + "babel-plugin-istanbul": "^6.0.0", "babel-preset-umi": "^1.0.0", "coveralls": "^3.0.6", "eslint": "^7.1.0", diff --git a/src/Plugin.js b/src/Plugin.js index 95259b13..78be7741 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -52,6 +52,15 @@ export default class Plugin { this.pluginStateKey = `importPluginState${index}`; } + transformFilename(methodName) { + // eslint-disable-next-line no-nested-ternary + return this.camel2UnderlineComponentName + ? transCamel(methodName, '_') + : this.camel2DashComponentName + ? transCamel(methodName, '-') + : methodName; + } + getPluginState(state) { if (!state[this.pluginStateKey]) { state[this.pluginStateKey] = {}; // eslint-disable-line @@ -61,12 +70,9 @@ export default class Plugin { importMethod(methodName, file, pluginState) { if (!pluginState.selectedMethods[methodName]) { - const { style, libraryDirectory } = this; - const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line - ? transCamel(methodName, '_') - : this.camel2DashComponentName - ? transCamel(methodName, '-') - : methodName; + const { libraryDirectory } = this; + const { style } = this; + const transformedMethodName = this.transformFilename(methodName); const path = winPath( this.customName ? this.customName(transformedMethodName, file) @@ -146,17 +152,29 @@ export default class Plugin { const { value } = node.source; const { libraryName } = this; + const { libraryDirectory } = this; const { types } = this; - const pluginState = this.getPluginState(state); if (value === libraryName) { - node.specifiers.forEach(spec => { - if (types.isImportSpecifier(spec)) { - pluginState.specified[spec.local.name] = spec.imported.name; - } else { - pluginState.libraryObjs[spec.local.name] = true; - } + const newImports = node.specifiers.map(item => { + const transformedMethodName = this.transformFilename( + types.isImportSpecifier(item) ? item.imported.name : item.local.name, + ); + + const file = (path && path.hub && path.hub.file) || (state && state.file); + // eslint-disable-next-line new-cap + return types.ImportDeclaration( + [types.importDefaultSpecifier(item.local)], + // eslint-disable-next-line new-cap + types.StringLiteral( + winPath( + this.customName + ? this.customName(transformedMethodName, file) + : join(this.libraryName, libraryDirectory, transformedMethodName, this.fileName), // eslint-disable-line + ), + ), + ); }); - pluginState.pathsToRemove.push(path); + path.replaceWithMultiple(newImports); } } diff --git a/test/fixtures/import-order/actual.js b/test/fixtures/import-order/actual.js new file mode 100644 index 00000000..7a1c979b --- /dev/null +++ b/test/fixtures/import-order/actual.js @@ -0,0 +1,4 @@ +import 'polyfill'; +import { Button } from 'antd'; + +console.log(Button); diff --git a/test/fixtures/import-order/expected.js b/test/fixtures/import-order/expected.js new file mode 100644 index 00000000..c8cea95b --- /dev/null +++ b/test/fixtures/import-order/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _button = _interopRequireDefault(require("antd/lib/button")); + +require("polyfill"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +console.log(_button.default); diff --git a/test/index.test.js b/test/index.test.js index 7f8e54dc..13090e02 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -10,13 +10,15 @@ describe('index', () => { const fixturesDir = join(__dirname, 'fixtures'); let fixtures = readdirSync(fixturesDir); - const onlyFixtures = fixtures.filter(fixture => fixture.indexOf('-only') > -1); + const onlyFixtures = fixtures.filter( + (fixture) => fixture.indexOf('-only') > -1, + ); if (onlyFixtures.length) { fixtures = onlyFixtures; } - fixtures.map(caseName => { + fixtures.map((caseName) => { const fixtureDir = join(fixturesDir, caseName); const actualFile = join(fixtureDir, 'actual.js'); const expectedFile = join(fixtureDir, 'expected.js'); @@ -61,7 +63,12 @@ describe('index', () => { plugin, { libraryName: 'plat/antd', - customName: join(__dirname, 'fixtures', 'custom-name-source-file', 'customName.js'), + customName: join( + __dirname, + 'fixtures', + 'custom-name-source-file', + 'customName.js', + ), }, ]; } else if (caseName === 'custom-style-path') { @@ -160,6 +167,11 @@ describe('index', () => { } const expected = readFileSync(expectedFile, 'utf-8'); + if(caseName === 'import-order') { + + console.log(actual.trim()); + } + expect(actual.trim()).toEqual(expected.trim()); }); }); diff --git a/yarn.lock b/yarn.lock index bdc8c2dc..bf7202f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9,6 +9,13 @@ dependencies: "@babel/highlight" "^7.8.3" +"@babel/code-frame@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.10.1.tgz#d5481c5095daa1c57e16e54c6f9198443afb49ff" + integrity sha512-IGhtTmpjGbYzcEDOw7DcQtbQSXcG9ftmAXtWTu9V936vDye4xjjekktFAtgZsWpzTj/X01jocB46mTywm/4SZw== + dependencies: + "@babel/highlight" "^7.10.1" + "@babel/core@7.4.5": version "7.4.5" resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.4.5.tgz#081f97e8ffca65a9b4b0fdc7e274e703f000c06a" @@ -50,6 +57,38 @@ semver "^5.4.1" source-map "^0.5.0" +"@babel/core@^7.7.5": + version "7.10.2" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.10.2.tgz#bd6786046668a925ac2bd2fd95b579b92a23b36a" + integrity sha512-KQmV9yguEjQsXqyOUGKjS4+3K8/DlOCE2pZcq4augdQmtTy5iv5EHtmMSJ7V4c1BIPjuwtZYqYLCq9Ga+hGBRQ== + dependencies: + "@babel/code-frame" "^7.10.1" + "@babel/generator" "^7.10.2" + "@babel/helper-module-transforms" "^7.10.1" + "@babel/helpers" "^7.10.1" + "@babel/parser" "^7.10.2" + "@babel/template" "^7.10.1" + "@babel/traverse" "^7.10.1" + "@babel/types" "^7.10.2" + convert-source-map "^1.7.0" + debug "^4.1.0" + gensync "^1.0.0-beta.1" + json5 "^2.1.2" + lodash "^4.17.13" + resolve "^1.3.2" + semver "^5.4.1" + source-map "^0.5.0" + +"@babel/generator@^7.10.1", "@babel/generator@^7.10.2": + version "7.10.2" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.10.2.tgz#0fa5b5b2389db8bfdfcc3492b551ee20f5dd69a9" + integrity sha512-AxfBNHNu99DTMvlUPlt1h2+Hn7knPpH5ayJ8OqDWSeLld+Fi2AYBTC/IejWDM9Edcii4UzZRCsbUt0WlSDsDsA== + dependencies: + "@babel/types" "^7.10.2" + jsesc "^2.5.1" + lodash "^4.17.13" + source-map "^0.5.0" + "@babel/generator@^7.4.4", "@babel/generator@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.8.4.tgz#35bbc74486956fe4251829f9f6c48330e8d0985e" @@ -142,6 +181,15 @@ "@babel/template" "^7.1.0" "@babel/types" "^7.0.0" +"@babel/helper-function-name@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.10.1.tgz#92bd63829bfc9215aca9d9defa85f56b539454f4" + integrity sha512-fcpumwhs3YyZ/ttd5Rz0xn0TpIwVkN7X0V38B9TWNfVF42KEkhkAAuPCQ3oXmtTRtiPJrmZ0TrfS0GKF0eMaRQ== + dependencies: + "@babel/helper-get-function-arity" "^7.10.1" + "@babel/template" "^7.10.1" + "@babel/types" "^7.10.1" + "@babel/helper-function-name@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.7.4.tgz#ab6e041e7135d436d8f0a3eca15de5b67a341a2e" @@ -167,6 +215,13 @@ dependencies: "@babel/types" "^7.0.0" +"@babel/helper-get-function-arity@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.10.1.tgz#7303390a81ba7cb59613895a192b93850e373f7d" + integrity sha512-F5qdXkYGOQUb0hpRaPoetF9AnsXknKjWMZ+wmsIRsp5ge5sFh4c3h1eH2pRTTuy9KKAA2+TTYomGXAtEL2fQEw== + dependencies: + "@babel/types" "^7.10.1" + "@babel/helper-get-function-arity@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.7.4.tgz#cb46348d2f8808e632f0ab048172130e636005f0" @@ -188,6 +243,13 @@ dependencies: "@babel/types" "^7.4.4" +"@babel/helper-member-expression-to-functions@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.10.1.tgz#432967fd7e12a4afef66c4687d4ca22bc0456f15" + integrity sha512-u7XLXeM2n50gb6PWJ9hoO5oO7JFPaZtrh35t8RqKLT1jFKj9IWeD1zrcrYp1q1qiZTdEarfDWfTIP8nGsu0h5g== + dependencies: + "@babel/types" "^7.10.1" + "@babel/helper-member-expression-to-functions@^7.5.5": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.5.5.tgz#1fb5b8ec4453a93c439ee9fe3aeea4a84b76b590" @@ -209,6 +271,13 @@ dependencies: "@babel/types" "^7.8.3" +"@babel/helper-module-imports@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.10.1.tgz#dd331bd45bccc566ce77004e9d05fe17add13876" + integrity sha512-SFxgwYmZ3HZPyZwJRiVNLRHWuW2OgE5k2nrVs6D9Iv4PPnXVffuEHy83Sfx/l4SqF+5kyJXjAyUmrG7tNm+qVg== + dependencies: + "@babel/types" "^7.10.1" + "@babel/helper-module-transforms@^7.1.0": version "7.1.0" resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.1.0.tgz#470d4f9676d9fad50b324cdcce5fbabbc3da5787" @@ -221,6 +290,19 @@ "@babel/types" "^7.0.0" lodash "^4.17.10" +"@babel/helper-module-transforms@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.10.1.tgz#24e2f08ee6832c60b157bb0936c86bef7210c622" + integrity sha512-RLHRCAzyJe7Q7sF4oy2cB+kRnU4wDZY/H2xJFGof+M+SJEGhZsb+GFj5j1AD8NiSaVBJ+Pf0/WObiXu/zxWpFg== + dependencies: + "@babel/helper-module-imports" "^7.10.1" + "@babel/helper-replace-supers" "^7.10.1" + "@babel/helper-simple-access" "^7.10.1" + "@babel/helper-split-export-declaration" "^7.10.1" + "@babel/template" "^7.10.1" + "@babel/types" "^7.10.1" + lodash "^4.17.13" + "@babel/helper-module-transforms@^7.4.4": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.5.5.tgz#f84ff8a09038dcbca1fd4355661a500937165b4a" @@ -240,6 +322,13 @@ dependencies: "@babel/types" "^7.0.0" +"@babel/helper-optimise-call-expression@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.10.1.tgz#b4a1f2561870ce1247ceddb02a3860fa96d72543" + integrity sha512-a0DjNS1prnBsoKx83dP2falChcs7p3i8VMzdrSbfLhuQra/2ENC4sbri34dz/rWmDADsmF1q5GbfaXydh0Jbjg== + dependencies: + "@babel/types" "^7.10.1" + "@babel/helper-optimise-call-expression@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.7.4.tgz#034af31370d2995242aa4df402c3b7794b2dcdf2" @@ -277,6 +366,16 @@ "@babel/traverse" "^7.1.0" "@babel/types" "^7.0.0" +"@babel/helper-replace-supers@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.10.1.tgz#ec6859d20c5d8087f6a2dc4e014db7228975f13d" + integrity sha512-SOwJzEfpuQwInzzQJGjGaiG578UYmyi2Xw668klPWV5n07B73S0a9btjLk/52Mlcxa+5AdIYqws1KyXRfMoB7A== + dependencies: + "@babel/helper-member-expression-to-functions" "^7.10.1" + "@babel/helper-optimise-call-expression" "^7.10.1" + "@babel/traverse" "^7.10.1" + "@babel/types" "^7.10.1" + "@babel/helper-replace-supers@^7.5.5": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.5.5.tgz#f84ce43df031222d2bad068d2626cb5799c34bc2" @@ -305,6 +404,14 @@ "@babel/template" "^7.1.0" "@babel/types" "^7.0.0" +"@babel/helper-simple-access@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.10.1.tgz#08fb7e22ace9eb8326f7e3920a1c2052f13d851e" + integrity sha512-VSWpWzRzn9VtgMJBIWTZ+GP107kZdQ4YplJlCmIrjoLVSi/0upixezHCDG8kpPVTBJpKfxTH01wDhh+jS2zKbw== + dependencies: + "@babel/template" "^7.10.1" + "@babel/types" "^7.10.1" + "@babel/helper-split-export-declaration@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0.tgz#3aae285c0311c2ab095d997b8c9a94cad547d813" @@ -312,6 +419,13 @@ dependencies: "@babel/types" "^7.0.0" +"@babel/helper-split-export-declaration@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.10.1.tgz#c6f4be1cbc15e3a868e4c64a17d5d31d754da35f" + integrity sha512-UQ1LVBPrYdbchNhLwj6fetj46BcFwfS4NllJo/1aJsT+1dLTEnXJL0qHqtY7gPzF8S2fXBJamf1biAXV3X077g== + dependencies: + "@babel/types" "^7.10.1" + "@babel/helper-split-export-declaration@^7.4.4": version "7.4.4" resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.4.4.tgz#ff94894a340be78f53f06af038b205c49d993677" @@ -333,6 +447,11 @@ dependencies: "@babel/types" "^7.8.3" +"@babel/helper-validator-identifier@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.1.tgz#5770b0c1a826c4f53f5ede5e153163e0318e94b5" + integrity sha512-5vW/JXLALhczRCWP0PnFDMCJAchlBvM7f4uk/jXritBnIa6E1KmqmtrS3yn1LAnxFBypQ3eneLuXjsnfQsgILw== + "@babel/helper-wrap-function@^7.1.0": version "7.1.0" resolved "https://registry.yarnpkg.com/@babel/helper-wrap-function/-/helper-wrap-function-7.1.0.tgz#8cf54e9190706067f016af8f75cb3df829cc8c66" @@ -343,6 +462,15 @@ "@babel/traverse" "^7.1.0" "@babel/types" "^7.0.0" +"@babel/helpers@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.10.1.tgz#a6827b7cb975c9d9cef5fd61d919f60d8844a973" + integrity sha512-muQNHF+IdU6wGgkaJyhhEmI54MOZBKsFfsXFhboz1ybwJ1Kl7IHlbm2a++4jwrmY5UYsgitt5lfqo1wMFcHmyw== + dependencies: + "@babel/template" "^7.10.1" + "@babel/traverse" "^7.10.1" + "@babel/types" "^7.10.1" + "@babel/helpers@^7.4.4", "@babel/helpers@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.8.4.tgz#754eb3ee727c165e0a240d6c207de7c455f36f73" @@ -352,6 +480,15 @@ "@babel/traverse" "^7.8.4" "@babel/types" "^7.8.3" +"@babel/highlight@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.10.1.tgz#841d098ba613ba1a427a2b383d79e35552c38ae0" + integrity sha512-8rMof+gVP8mxYZApLF/JgNDAkdKa+aJt3ZYxF8z6+j/hpeXL7iMsKCPHa2jNMHu/qqBwzQF4OHNoYi8dMA/rYg== + dependencies: + "@babel/helper-validator-identifier" "^7.10.1" + chalk "^2.0.0" + js-tokens "^4.0.0" + "@babel/highlight@^7.8.3": version "7.8.3" resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.8.3.tgz#28f173d04223eaaa59bc1d439a3836e6d1265797" @@ -361,6 +498,11 @@ esutils "^2.0.2" js-tokens "^4.0.0" +"@babel/parser@^7.10.1", "@babel/parser@^7.10.2": + version "7.10.2" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.10.2.tgz#871807f10442b92ff97e4783b9b54f6a0ca812d0" + integrity sha512-PApSXlNMJyB4JiGVhCOlzKIif+TKFTvu0aQAhnTvfP/z3vVSN6ZypH5bfUNwFXXjRQtUEBNFd2PtmCmG2Py3qQ== + "@babel/parser@^7.4.5", "@babel/parser@^7.8.3", "@babel/parser@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.8.4.tgz#d1dbe64691d60358a974295fa53da074dd2ce8e8" @@ -1020,6 +1162,15 @@ "@babel/parser" "^7.8.3" "@babel/types" "^7.8.3" +"@babel/template@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.10.1.tgz#e167154a94cb5f14b28dc58f5356d2162f539811" + integrity sha512-OQDg6SqvFSsc9A0ej6SKINWrpJiNonRIniYondK2ViKhB06i3c0s+76XUft71iqBEe9S1OKsHwPAjfHnuvnCig== + dependencies: + "@babel/code-frame" "^7.10.1" + "@babel/parser" "^7.10.1" + "@babel/types" "^7.10.1" + "@babel/traverse@^7.1.0", "@babel/traverse@^7.4.4", "@babel/traverse@^7.4.5", "@babel/traverse@^7.5.5", "@babel/traverse@^7.7.4", "@babel/traverse@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.8.4.tgz#f0845822365f9d5b0e312ed3959d3f827f869e3c" @@ -1035,6 +1186,21 @@ globals "^11.1.0" lodash "^4.17.13" +"@babel/traverse@^7.10.1": + version "7.10.1" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.10.1.tgz#bbcef3031e4152a6c0b50147f4958df54ca0dd27" + integrity sha512-C/cTuXeKt85K+p08jN6vMDz8vSV0vZcI0wmQ36o6mjbuo++kPMdpOYw23W2XH04dbRt9/nMEfA4W3eR21CD+TQ== + dependencies: + "@babel/code-frame" "^7.10.1" + "@babel/generator" "^7.10.1" + "@babel/helper-function-name" "^7.10.1" + "@babel/helper-split-export-declaration" "^7.10.1" + "@babel/parser" "^7.10.1" + "@babel/types" "^7.10.1" + debug "^4.1.0" + globals "^11.1.0" + lodash "^4.17.13" + "@babel/types@^7.0.0", "@babel/types@^7.4.4", "@babel/types@^7.5.5", "@babel/types@^7.7.4", "@babel/types@^7.8.3": version "7.8.3" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.8.3.tgz#5a383dffa5416db1b73dedffd311ffd0788fb31c" @@ -1044,6 +1210,31 @@ lodash "^4.17.13" to-fast-properties "^2.0.0" +"@babel/types@^7.10.1", "@babel/types@^7.10.2": + version "7.10.2" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.10.2.tgz#30283be31cad0dbf6fb00bd40641ca0ea675172d" + integrity sha512-AD3AwWBSz0AWF0AkCN9VPiWrvldXq+/e3cHa4J89vo4ymjz1XwrBFFVZmkJTsQIPNk+ZVomPSXUJqq8yyjZsng== + dependencies: + "@babel/helper-validator-identifier" "^7.10.1" + lodash "^4.17.13" + to-fast-properties "^2.0.0" + +"@istanbuljs/load-nyc-config@^1.0.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" + integrity sha512-VjeHSlIzpv/NyD3N0YuHfXOPDIixcA1q2ZV98wsMqcYlPmv2n3Yb2lYP9XMElnaFVXg5A7YLTeLu6V84uQDjmQ== + dependencies: + camelcase "^5.3.1" + find-up "^4.1.0" + get-package-type "^0.1.0" + js-yaml "^3.13.1" + resolve-from "^5.0.0" + +"@istanbuljs/schema@^0.1.2": + version "0.1.2" + resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd" + integrity sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw== + "@samverschueren/stream-to-observable@^0.3.0": version "0.3.0" resolved "https://registry.yarnpkg.com/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f" @@ -1563,6 +1754,17 @@ babel-plugin-istanbul@^4.1.6: istanbul-lib-instrument "^1.10.1" test-exclude "^4.2.1" +babel-plugin-istanbul@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-6.0.0.tgz#e159ccdc9af95e0b570c75b4573b7c34d671d765" + integrity sha512-AF55rZXpe7trmEylbaE1Gv54wn6rwU03aptvRoVIGP8YykoSxqdVLV1TfwflBCE/QtHmqtP8SWlTENqbK8GCSQ== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@istanbuljs/load-nyc-config" "^1.0.0" + "@istanbuljs/schema" "^0.1.2" + istanbul-lib-instrument "^4.0.0" + test-exclude "^6.0.0" + babel-plugin-jest-hoist@^23.2.0: version "23.2.0" resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-23.2.0.tgz#e61fae05a1ca8801aadee57a6d66b8cefaf44167" @@ -3718,6 +3920,11 @@ get-own-enumerable-property-symbols@^3.0.0: resolved "https://registry.yarnpkg.com/get-own-enumerable-property-symbols/-/get-own-enumerable-property-symbols-3.0.2.tgz#b5fde77f22cbe35f390b4e089922c50bce6ef664" integrity sha512-I0UBV/XOz1XkIJHEUDMZAbzCThU/H8DxmSfmdGcKPnVhu2VfFqr34jr9777IyaTYvxjedWhqVIilEDsCdP5G6g== +get-package-type@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/get-package-type/-/get-package-type-0.1.0.tgz#8de2d803cff44df3bc6c456e6668b36c3926e11a" + integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q== + get-stdin@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-6.0.0.tgz#9e09bf712b360ab9225e812048f71fde9c89657b" @@ -3817,6 +4024,18 @@ glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.1.4: + version "7.1.6" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6" + integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + global-dirs@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-2.0.1.tgz#acdf3bb6685bcd55cb35e8a052266569e9469201" @@ -4814,6 +5033,11 @@ istanbul-lib-coverage@^1.2.0, istanbul-lib-coverage@^1.2.1: resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.2.1.tgz#ccf7edcd0a0bb9b8f729feeb0930470f9af664f0" integrity sha512-PzITeunAgyGbtY1ibVIUiV679EFChHjoMNRibEIobvmrCRaIgwLxNucOSimtNWUhEib/oO7QY2imD75JVgCJWQ== +istanbul-lib-coverage@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-3.0.0.tgz#f5944a37c70b550b02a78a5c3b2055b280cec8ec" + integrity sha512-UiUIqxMgRDET6eR+o5HbfRYP1l0hqkWOs7vNxC/mggutCMUIhWMm8gAHb8tHlyfD3/l6rlgNA5cKdDzEAf6hEg== + istanbul-lib-hook@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/istanbul-lib-hook/-/istanbul-lib-hook-1.2.2.tgz#bc6bf07f12a641fbf1c85391d0daa8f0aea6bf86" @@ -4834,6 +5058,16 @@ istanbul-lib-instrument@^1.10.1, istanbul-lib-instrument@^1.10.2: istanbul-lib-coverage "^1.2.1" semver "^5.3.0" +istanbul-lib-instrument@^4.0.0: + version "4.0.3" + resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-4.0.3.tgz#873c6fff897450118222774696a3f28902d77c1d" + integrity sha512-BXgQl9kf4WTCPCCpmFGoJkz/+uhvm7h7PFKUYxh7qarQd3ER33vHG//qaE8eN25l07YqZPpHXU9I09l/RD5aGQ== + dependencies: + "@babel/core" "^7.7.5" + "@istanbuljs/schema" "^0.1.2" + istanbul-lib-coverage "^3.0.0" + semver "^6.3.0" + istanbul-lib-report@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-1.1.5.tgz#f2a657fc6282f96170aaf281eb30a458f7f4170c" @@ -5320,6 +5554,13 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" +json5@^2.1.2: + version "2.1.3" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43" + integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA== + dependencies: + minimist "^1.2.5" + jsonify@~0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" @@ -5955,6 +6196,11 @@ minimist@^1.1.1, minimist@^1.2.0: resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ= +minimist@^1.2.5: + version "1.2.5" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" + integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== + minimist@~0.0.1: version "0.0.10" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" @@ -7820,6 +8066,11 @@ resolve-from@^4.0.0: resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6" integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g== +resolve-from@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69" + integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw== + resolve-options@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/resolve-options/-/resolve-options-1.1.0.tgz#32bb9e39c06d67338dc9378c0d6d6074566ad131" @@ -8681,6 +8932,15 @@ test-exclude@^4.2.1: read-pkg-up "^1.0.1" require-main-filename "^1.0.1" +test-exclude@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e" + integrity sha512-cAGWPIyOHU6zlmg88jwm7VRyXnMN7iV68OGAbYDk/Mh/xC/pzVPlQtY6ngoIH/5/tciuhGfvESU8GrHrcxD56w== + dependencies: + "@istanbuljs/schema" "^0.1.2" + glob "^7.1.4" + minimatch "^3.0.4" + text-table@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" From 4632fc5ae819489c376a80e952f041bbcc5cd94b Mon Sep 17 00:00:00 2001 From: shaodahong Date: Thu, 4 Jun 2020 10:55:54 +0800 Subject: [PATCH 2/4] remove useless code --- package.json | 1 - test/index.test.js | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 196eb8a2..bf21fdc2 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "@babel/core": "^7.0.0", "@babel/preset-react": "^7.0.0", "babel-core": "^7.0.0-0", - "babel-plugin-istanbul": "^6.0.0", "babel-preset-umi": "^1.0.0", "coveralls": "^3.0.6", "eslint": "^7.1.0", diff --git a/test/index.test.js b/test/index.test.js index 13090e02..1a9fd6e8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -10,15 +10,13 @@ describe('index', () => { const fixturesDir = join(__dirname, 'fixtures'); let fixtures = readdirSync(fixturesDir); - const onlyFixtures = fixtures.filter( - (fixture) => fixture.indexOf('-only') > -1, - ); + const onlyFixtures = fixtures.filter(fixture => fixture.indexOf('-only') > -1); if (onlyFixtures.length) { fixtures = onlyFixtures; } - fixtures.map((caseName) => { + fixtures.map(caseName => { const fixtureDir = join(fixturesDir, caseName); const actualFile = join(fixtureDir, 'actual.js'); const expectedFile = join(fixtureDir, 'expected.js'); @@ -63,12 +61,7 @@ describe('index', () => { plugin, { libraryName: 'plat/antd', - customName: join( - __dirname, - 'fixtures', - 'custom-name-source-file', - 'customName.js', - ), + customName: join(__dirname, 'fixtures', 'custom-name-source-file', 'customName.js'), }, ]; } else if (caseName === 'custom-style-path') { @@ -167,10 +160,6 @@ describe('index', () => { } const expected = readFileSync(expectedFile, 'utf-8'); - if(caseName === 'import-order') { - - console.log(actual.trim()); - } expect(actual.trim()).toEqual(expected.trim()); }); From 23e1d4c4e712732c947a0829205f72aa6428033f Mon Sep 17 00:00:00 2001 From: shaodahong Date: Fri, 5 Jun 2020 10:19:16 +0800 Subject: [PATCH 3/4] fix expected --- src/Plugin.js | 198 +++++++----------- .../custom-style-path-ignore/expected.js | 8 +- test/fixtures/custom-style-path/expected.js | 10 +- test/fixtures/execute-direct/expected.js | 4 +- test/fixtures/execute-member/expected.js | 4 +- test/fixtures/import-css/expected.js | 12 +- test/fixtures/import-order/expected.js | 4 +- test/fixtures/keep-named-import/expected.js | 12 +- test/fixtures/material-ui/expected.js | 4 +- test/fixtures/modules-false/expected.js | 8 +- .../multiple-libraries-hilojs/expected.js | 6 +- test/fixtures/multiple-libraries/expected.js | 8 +- test/fixtures/object-shorthand/expected.js | 6 +- test/fixtures/return/expected.js | 6 +- test/fixtures/super-class/expected.js | 4 +- test/fixtures/use-multiple-times/expected.js | 6 +- test/fixtures/variable-scope/expected.js | 22 +- 17 files changed, 138 insertions(+), 184 deletions(-) diff --git a/src/Plugin.js b/src/Plugin.js index 78be7741..c4c27d27 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -61,18 +61,34 @@ export default class Plugin { : methodName; } - getPluginState(state) { - if (!state[this.pluginStateKey]) { - state[this.pluginStateKey] = {}; // eslint-disable-line + getInjectStylePath(path, file, transformedMethodName) { + let stylePath; + const { style } = this; + + if (this.customStyleName) { + stylePath = winPath(this.customStyleName(transformedMethodName)); + } else if (this.styleLibraryDirectory) { + stylePath = winPath( + join(this.libraryName, this.styleLibraryDirectory, transformedMethodName, this.fileName), + ); + } else if (style === true) { + stylePath = `${path}/style`; + } else if (style === 'css') { + stylePath = `${path}/style/css`; + } else if (typeof style === 'function') { + stylePath = style(path, file); } - return state[this.pluginStateKey]; + return stylePath; } importMethod(methodName, file, pluginState) { if (!pluginState.selectedMethods[methodName]) { - const { libraryDirectory } = this; - const { style } = this; - const transformedMethodName = this.transformFilename(methodName); + const { style, libraryDirectory } = this; + const transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line + ? transCamel(methodName, '_') + : this.camel2DashComponentName + ? transCamel(methodName, '-') + : methodName; const path = winPath( this.customName ? this.customName(transformedMethodName, file) @@ -103,6 +119,13 @@ export default class Plugin { return { ...pluginState.selectedMethods[methodName] }; } + getPluginState(state) { + if (!state[this.pluginStateKey]) { + state[this.pluginStateKey] = {}; // eslint-disable-line + } + return state[this.pluginStateKey]; + } + buildExpressionHandler(node, props, path, state) { const file = (path && path.hub && path.hub.file) || (state && state.file); const { types } = this; @@ -151,59 +174,57 @@ export default class Plugin { if (!node) return; const { value } = node.source; - const { libraryName } = this; - const { libraryDirectory } = this; - const { types } = this; + const { libraryName, libraryDirectory, types } = this; + const pluginState = this.getPluginState(state); if (value === libraryName) { - const newImports = node.specifiers.map(item => { - const transformedMethodName = this.transformFilename( - types.isImportSpecifier(item) ? item.imported.name : item.local.name, - ); - - const file = (path && path.hub && path.hub.file) || (state && state.file); - // eslint-disable-next-line new-cap - return types.ImportDeclaration( - [types.importDefaultSpecifier(item.local)], + const newImports = node.specifiers + .map(item => { + /** + * LibraryObjs for @MemberExpression shaking, like + * import antd from 'antd' + * + * + * to + * + * var Button = require("antd/lib/button") + *