Skip to content

Commit

Permalink
fix: local scope bug (umijs#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
zquancai authored and sorrycc committed Sep 23, 2019
1 parent 6900745 commit b4151ae
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/variable-scope/actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { message } from 'antd';

message('xxx');

message.error('error');

const testIf = (message) => {
if (message) return message
}
Expand All @@ -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';
Expand Down
42 changes: 37 additions & 5 deletions test/fixtures/variable-scope/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

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) {
return message + 'test';
};

var testExpression2 = function testExpression2() {
return _message2.default + 'test';
return _message3.default + 'test';
};

var testNestFunction = function testNestFunction(message) {
Expand All @@ -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);
Expand Down

0 comments on commit b4151ae

Please sign in to comment.