From cd1c9657c2b60640a65f1814251bae0f8c865cfb Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Wed, 6 Jul 2016 20:28:54 +0800 Subject: [PATCH] fix variable scope --- src/core.js | 4 +++- test/fixtures/variable-scope/actual.js | 8 ++++++++ test/fixtures/variable-scope/expected.js | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/variable-scope/actual.js create mode 100644 test/fixtures/variable-scope/expected.js diff --git a/src/core.js b/src/core.js index 1d1aaed3..b0a9337b 100644 --- a/src/core.js +++ b/src/core.js @@ -83,7 +83,9 @@ export default function(defaultLibraryName) { // if (object && object.name === 'React' && property && property.name === 'createElement' && node.arguments) { node.arguments = node.arguments.map(arg => { const { name: argName } = arg; - if (specified[argName]) { + if (specified[argName] && + path.scope.hasBinding(argName) && + path.scope.getBinding(argName).path.type === 'ImportSpecifier') { return importMethod(specified[argName], file, opts); } return arg; diff --git a/test/fixtures/variable-scope/actual.js b/test/fixtures/variable-scope/actual.js new file mode 100644 index 00000000..9f135b22 --- /dev/null +++ b/test/fixtures/variable-scope/actual.js @@ -0,0 +1,8 @@ +import { message } from 'antd'; + +message('xxx'); + +function App() { + const message = 'xxx'; + return
{message}
; +} diff --git a/test/fixtures/variable-scope/expected.js b/test/fixtures/variable-scope/expected.js new file mode 100644 index 00000000..852763b4 --- /dev/null +++ b/test/fixtures/variable-scope/expected.js @@ -0,0 +1,18 @@ +'use strict'; + +var _message = require('antd/lib/message'); + +var _message2 = _interopRequireDefault(_message); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +(0, _message2.default)('xxx'); + +function App() { + var message = 'xxx'; + return React.createElement( + 'div', + null, + message + ); +}