Skip to content

Commit

Permalink
fix(babel-plugin-transform-taroapi): 保留@tarojs/taro-h5的default import
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed May 28, 2019
1 parent c2ff527 commit 769bb08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Taro.noop();"

exports[`should not go wrong when using an api twice 1`] = `
"
import { createAnimation as _createAnimation } from '@tarojs/taro-h5';
import Taro, { createAnimation as _createAnimation } from '@tarojs/taro-h5';
const animation = _createAnimation({
duration: dura * 1000,
timingFunction: 'linear'
Expand All @@ -35,6 +35,15 @@ _createAnimation();
Taro.initPxTransform();"
`;

exports[`should preserve default imports 1`] = `
"
import Taro, { request as _request } from '@tarojs/taro-h5';
console.log(Taro);
_request();
Taro.request = '';
Taro['request'] = '';"
`;

exports[`should work! 1`] = `
"
import Taro, { setStorage as _setStorage, getStorage as _getStorage } from '@tarojs/taro-h5';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,15 @@ it('should not go wrong when using an api twice', function () {
expect(result.code).toMatchSnapshot();
}).not.toThrowError()
})

it('should preserve default imports', function () {
const code = `
import Taro from '@tarojs/taro-h5'
console.log(Taro)
Taro.request()
Taro.request = ''
Taro['request'] = ''
`
const result = babel.transform(code, { plugins: [pluginOptions] })
expect(result.code).toMatchSnapshot();
})
22 changes: 13 additions & 9 deletions packages/babel-plugin-transform-taroapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const plugin = function (babel: {
ast.node.specifiers.forEach(node => {
if (t.isImportDefaultSpecifier(node)) {
taroName = node.local.name
needDefault = true
} else if (t.isImportSpecifier(node)) {
const propertyName = node.imported.name
if (apis.has(propertyName)) { // 记录api名字
Expand Down Expand Up @@ -60,16 +61,19 @@ const plugin = function (babel: {

// 同一api使用多次, 读取变量名
if (apis.has(propertyName)) {
let identifier: Types.Identifier
if (invokedApis.has(propertyName)) {
identifier = t.identifier(invokedApis.get(propertyName)!)
} else {
const newPropertyName = ast.scope.generateUid(propertyName)
invokedApis.set(propertyName, newPropertyName)
/* 未绑定作用域 */
identifier = t.identifier(newPropertyName)
const isAssigning = ast.findParent(parentPath => parentPath.isAssignmentExpression())
if (!isAssigning) {
let identifier: Types.Identifier
if (invokedApis.has(propertyName)) {
identifier = t.identifier(invokedApis.get(propertyName)!)
} else {
const newPropertyName = ast.scope.generateUid(propertyName)
invokedApis.set(propertyName, newPropertyName)
/* 未绑定作用域 */
identifier = t.identifier(newPropertyName)
}
ast.replaceWith(identifier)
}
ast.replaceWith(identifier)
} else {
needDefault = true
}
Expand Down

0 comments on commit 769bb08

Please sign in to comment.