babel-plugin-transform-taroapi
Advanced tools
Comparing version 1.3.12 to 1.3.13
@@ -6,5 +6,6 @@ "use strict"; | ||
// 这些变量需要在每个programe里重置 | ||
let taroName = ''; | ||
let needDefault = false; | ||
const invokedApis = new Map(); | ||
let taroName; | ||
let needDefault; | ||
let referrencedTaros; | ||
return { | ||
@@ -14,11 +15,10 @@ name: 'babel-plugin-transform-taro-api', | ||
ImportDeclaration(ast, state) { | ||
const packageName = state.opts.packageName; | ||
const apis = state.opts.apis; | ||
const packageName = state.opts.packageName; | ||
if (ast.node.source.value !== packageName) | ||
return; | ||
taroName = 'Taro'; | ||
ast.node.specifiers.forEach(node => { | ||
if (t.isImportDefaultSpecifier(node)) { | ||
needDefault = true; | ||
taroName = node.local.name; | ||
needDefault = true; | ||
} | ||
@@ -32,5 +32,9 @@ else if (t.isImportSpecifier(node)) { | ||
else { // 如果是未实现的api 改成Taro.xxx | ||
const binding = ast.scope.getBinding(propertyName); | ||
binding.referencePaths.forEach(reference => { | ||
reference.replaceWith(t.memberExpression(t.identifier(taroName), t.identifier(propertyName))); | ||
needDefault = true; | ||
const localName = node.local.name; | ||
const binding = ast.scope.getBinding(localName); | ||
const iden = t.identifier(taroName); | ||
referrencedTaros.push(iden); | ||
binding && binding.referencePaths.forEach(reference => { | ||
reference.replaceWith(t.memberExpression(iden, t.identifier(propertyName))); | ||
}); | ||
@@ -42,2 +46,3 @@ } | ||
MemberExpression(ast, state) { | ||
/* 处理Taro.xxx */ | ||
const apis = state.opts.apis; | ||
@@ -80,6 +85,9 @@ const isTaro = t.isIdentifier(ast.node.object, { name: taroName }); | ||
Program: { | ||
enter(ast, state) { | ||
taroName = ''; | ||
enter(ast) { | ||
needDefault = false; | ||
referrencedTaros = []; | ||
invokedApis.clear(); | ||
taroName = ast.scope.getBinding('Taro') | ||
? ast.scope.generateUid('Taro') | ||
: 'Taro'; | ||
}, | ||
@@ -89,2 +97,5 @@ exit(ast, state) { | ||
let isTaroApiImported = false; | ||
referrencedTaros.forEach(node => { | ||
node.name = taroName; | ||
}); | ||
ast.traverse({ | ||
@@ -91,0 +102,0 @@ ImportDeclaration(ast) { |
{ | ||
"name": "babel-plugin-transform-taroapi", | ||
"version": "1.3.12", | ||
"version": "1.3.13", | ||
"main": "dist/index.js", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@tarojs/taro-h5": "1.3.12", | ||
"@tarojs/taro-h5": "1.3.13", | ||
"@types/babel-core": "^6.25.5", | ||
@@ -16,0 +16,0 @@ "@types/babel-traverse": "^6.25.4", |
@@ -137,3 +137,2 @@ import * as apis from '@tarojs/taro-h5/dist/taroApis' | ||
it('should preserve assignments in lefthands', function () { | ||
@@ -156,1 +155,11 @@ const code = ` | ||
}) | ||
it('should support rename of imported names', function () { | ||
const code = ` | ||
// import { inject as mobxInject, observer as mobxObserver } from '@tarojs/mobx' | ||
import { Component as TaroComponent } from "@tarojs/taro-h5"; | ||
export class Connected extends TaroComponent {} | ||
` | ||
const result = babel.transform(code, { plugins: [pluginOptions] }) | ||
expect(result.code).toMatchSnapshot(); | ||
}) |
@@ -7,8 +7,10 @@ import { types as Types, PluginObj } from 'babel-core'; | ||
const t = babel.types | ||
// 这些变量需要在每个programe里重置 | ||
let taroName: string = '' | ||
let needDefault = false | ||
const invokedApis: Map<string, string> = new Map() | ||
let taroName: string | ||
let needDefault: boolean | ||
let referrencedTaros: Types.Identifier[] | ||
return { | ||
@@ -18,11 +20,10 @@ name: 'babel-plugin-transform-taro-api', | ||
ImportDeclaration (ast, state) { | ||
const packageName = state.opts.packageName | ||
const apis = state.opts.apis | ||
const packageName = state.opts.packageName | ||
if (ast.node.source.value !== packageName) return | ||
taroName = 'Taro' | ||
ast.node.specifiers.forEach(node => { | ||
if (t.isImportDefaultSpecifier(node)) { | ||
needDefault = true | ||
taroName = node.local.name | ||
needDefault = true | ||
} else if (t.isImportSpecifier(node)) { | ||
@@ -34,7 +35,11 @@ const propertyName = node.imported.name | ||
} else { // 如果是未实现的api 改成Taro.xxx | ||
const binding = ast.scope.getBinding(propertyName)! | ||
binding.referencePaths.forEach(reference => { | ||
needDefault = true | ||
const localName = node.local.name | ||
const binding = ast.scope.getBinding(localName) | ||
const iden = t.identifier(taroName) | ||
referrencedTaros.push(iden) | ||
binding && binding.referencePaths.forEach(reference => { | ||
reference.replaceWith( | ||
t.memberExpression( | ||
t.identifier(taroName), | ||
iden, | ||
t.identifier(propertyName) | ||
@@ -49,2 +54,3 @@ ) | ||
MemberExpression (ast, state) { | ||
/* 处理Taro.xxx */ | ||
const apis = state.opts.apis | ||
@@ -88,6 +94,10 @@ const isTaro = t.isIdentifier(ast.node.object, { name: taroName }) | ||
Program: { | ||
enter (ast, state) { | ||
taroName = '' | ||
enter (ast) { | ||
needDefault = false | ||
referrencedTaros = [] | ||
invokedApis.clear() | ||
taroName = ast.scope.getBinding('Taro') | ||
? ast.scope.generateUid('Taro') | ||
: 'Taro' | ||
}, | ||
@@ -97,2 +107,5 @@ exit (ast, state) { | ||
let isTaroApiImported = false | ||
referrencedTaros.forEach(node => { | ||
node.name = taroName | ||
}) | ||
@@ -99,0 +112,0 @@ ast.traverse({ |
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
19621
450