babel-plugin-transform-taroapi
Advanced tools
Comparing version 1.3.0-beta.5 to 1.3.0-beta.6
@@ -21,2 +21,3 @@ "use strict"; | ||
taroName = node.local.name; | ||
needDefault = true; | ||
} | ||
@@ -55,13 +56,17 @@ else if (t.isImportSpecifier(node)) { | ||
if (apis.has(propertyName)) { | ||
let identifier; | ||
if (invokedApis.has(propertyName)) { | ||
identifier = t.identifier(invokedApis.get(propertyName)); | ||
const parentNode = ast.parent; | ||
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === ast.node; | ||
if (!isAssignment) { | ||
let 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); | ||
} | ||
else { | ||
const newPropertyName = ast.scope.generateUid(propertyName); | ||
invokedApis.set(propertyName, newPropertyName); | ||
/* 未绑定作用域 */ | ||
identifier = t.identifier(newPropertyName); | ||
} | ||
ast.replaceWith(identifier); | ||
} | ||
@@ -68,0 +73,0 @@ else { |
{ | ||
"name": "babel-plugin-transform-taroapi", | ||
"version": "1.3.0-beta.5", | ||
"version": "1.3.0-beta.6", | ||
"main": "dist/index.js", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@tarojs/taro-h5": "1.3.0-beta.5", | ||
"@tarojs/taro-h5": "1.3.0-beta.6", | ||
"@types/babel-core": "^6.25.5", | ||
@@ -16,0 +16,0 @@ "@types/babel-traverse": "^6.25.4", |
@@ -127,1 +127,29 @@ import * as apis from '@tarojs/taro-h5/dist/taroApis' | ||
}) | ||
it('should preserve default imports', function () { | ||
const code = ` | ||
import Taro from '@tarojs/taro-h5' | ||
console.log(Taro) | ||
` | ||
const result = babel.transform(code, { plugins: [pluginOptions] }) | ||
expect(result.code).toMatchSnapshot(); | ||
}) | ||
it('should preserve assignments in lefthands', function () { | ||
const code = ` | ||
import Taro from '@tarojs/taro-h5' | ||
let animation | ||
animation = Taro.createAnimation({ | ||
transformOrigin: "50% 50%", | ||
duration: 1000, | ||
timingFunction: "ease", | ||
delay: 0 | ||
}); | ||
Taro.request() | ||
Taro.request = '' | ||
Taro['request'] = '' | ||
` | ||
const result = babel.transform(code, { plugins: [pluginOptions] }) | ||
expect(result.code).toMatchSnapshot(); | ||
}) |
@@ -25,2 +25,3 @@ import { types as Types, PluginObj } from 'babel-core'; | ||
taroName = node.local.name | ||
needDefault = true | ||
} else if (t.isImportSpecifier(node)) { | ||
@@ -64,12 +65,17 @@ const propertyName = node.imported.name | ||
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 parentNode = ast.parent | ||
const isAssignment = t.isAssignmentExpression(parentNode) && parentNode.left === ast.node | ||
if (!isAssignment) { | ||
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 { | ||
@@ -76,0 +82,0 @@ needDefault = true |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18059
420