From f62cc369c2fc3cd1a41a72c05615966dd3b8b101 Mon Sep 17 00:00:00 2001 From: null Date: Mon, 23 Sep 2019 12:07:02 +0800 Subject: [PATCH] fix: local scope bug (#355) --- src/Plugin.js | 12 +++++-- test/fixtures/variable-scope/actual.js | 21 ++++++++++++ test/fixtures/variable-scope/expected.js | 42 +++++++++++++++++++++--- 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Plugin.js b/src/Plugin.js index 4fda416c..bdd1190d 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -184,8 +184,16 @@ export default class Plugin { if (pluginState.libraryObjs[node.object.name]) { // antd.Button -> _Button path.replaceWith(this.importMethod(node.property.name, file, pluginState)); - } else if (pluginState.specified[node.object.name]) { - node.object = this.importMethod(pluginState.specified[node.object.name], file, pluginState); + } else if (pluginState.specified[node.object.name] && path.scope.hasBinding(node.object.name)) { + const scope = path.scope.getBinding(node.object.name).scope; + // global variable in file scope + if (scope.path.parent.type === 'File') { + node.object = this.importMethod( + pluginState.specified[node.object.name], + file, + pluginState + ); + } } } diff --git a/test/fixtures/variable-scope/actual.js b/test/fixtures/variable-scope/actual.js index 48442c6e..2e4a2b56 100644 --- a/test/fixtures/variable-scope/actual.js +++ b/test/fixtures/variable-scope/actual.js @@ -2,6 +2,8 @@ import { message } from 'antd'; message('xxx'); +message.error('error'); + const testIf = (message) => { if (message) return message } @@ -15,6 +17,25 @@ const testExpression2 = () => message + 'test' const testNestFunction = message => a => message const testNestFunction2 = () => a => message +const testFunction = message => { + message.error('error'); + return message.test; +} +const testFunction1 = () => message.error('error'); +const testFunction2 = message => message.error('error'); +const testFunction3 = message => { + if(message) { + message = message.test.message; + for (let i = 0; i < 10; i++) { + const message = i; + if (message > 4) { + return message; + } + } + } + message = null; + return message; +} function App() { const message = 'xxx'; diff --git a/test/fixtures/variable-scope/expected.js b/test/fixtures/variable-scope/expected.js index 8e3cf47e..f42f8f17 100644 --- a/test/fixtures/variable-scope/expected.js +++ b/test/fixtures/variable-scope/expected.js @@ -2,18 +2,20 @@ var _react = _interopRequireDefault(require("react")); -var _message2 = _interopRequireDefault(require("antd/lib/message")); +var _message3 = _interopRequireDefault(require("antd/lib/message")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -(0, _message2.default)('xxx'); +(0, _message3.default)('xxx'); + +_message3.default.error('error'); var testIf = function testIf(message) { if (message) return message; }; var testIf2 = function testIf2() { - if (_message2.default) return _message2.default; + if (_message3.default) return _message3.default; }; var testExpression = function testExpression(message) { @@ -21,7 +23,7 @@ var testExpression = function testExpression(message) { }; var testExpression2 = function testExpression2() { - return _message2.default + 'test'; + return _message3.default + 'test'; }; var testNestFunction = function testNestFunction(message) { @@ -32,10 +34,40 @@ var testNestFunction = function testNestFunction(message) { var testNestFunction2 = function testNestFunction2() { return function (a) { - return _message2.default; + return _message3.default; }; }; +var testFunction = function testFunction(message) { + message.error('error'); + return message.test; +}; + +var testFunction1 = function testFunction1() { + return _message3.default.error('error'); +}; + +var testFunction2 = function testFunction2(message) { + return message.error('error'); +}; + +var testFunction3 = function testFunction3(message) { + if (message) { + message = message.test.message; + + for (var i = 0; i < 10; i++) { + var _message2 = i; + + if (_message2 > 4) { + return _message2; + } + } + } + + message = null; + return message; +}; + function App() { var message = 'xxx'; return _react.default.createElement("div", null, message);