Skip to content

Commit

Permalink
fix(taroize): 通过 this.onLoad 的形式调用生命周期也需要转换为对应的生命周期函数,close #2183
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Feb 18, 2019
1 parent b1e610d commit 4b75433
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/taro-cli/src/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const babylonConfig = require('./config/babylon')
const prettierJSConfig = {
semi: false,
singleQuote: true,
parser: 'babylon'
parser: 'babel'
}

const OUTPUT_STYLE_EXTNAME = '.scss'
Expand Down
27 changes: 18 additions & 9 deletions packages/taroize/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as t from 'babel-types'
import traverse, { NodePath, Visitor } from 'babel-traverse'
import * as template from 'babel-template'
import { buildImportStatement, codeFrameError, buildRender, buildBlockElement, parseCode } from './utils'
import { buildImportStatement, codeFrameError, buildRender, buildBlockElement, parseCode, isAliasThis } from './utils'
import { WXS } from './wxml'
import { PageLifecycle, Lifecycle } from './lifecycle'
import { usedComponents } from './global'
Expand Down Expand Up @@ -127,18 +127,27 @@ function parsePage (
}
if (callee.isMemberExpression()) {
const object = callee.get('object')
const property = callee.get('property')
if (object.isIdentifier()) {
const methodName = object.node.name
const hooks = ['onLoad', 'onShow', 'onReady', 'onHide', 'onUnload', 'onError', 'onLaunch']
hooks.forEach(hook => {
if (methodName === hook) {
object.replaceWith(t.identifier(PageLifecycle.get(methodName)!))
}
})
if (methodName === 'wx') {
const objectName = object.node.name
if (objectName === 'wx') {
object.replaceWith(t.identifier('Taro'))
}
}

let isThis = property.isThisExpression()

if (property.isIdentifier() && object.isIdentifier()) {
const propertyName = property.node.name
const objectName = object.node.name
if (PageLifecycle.has(propertyName) && isAliasThis(property, objectName)) {
isThis = true
}

if (isThis && PageLifecycle.has(propertyName)) {
property.replaceWith(t.identifier(PageLifecycle.get(propertyName)))
}
}
}
},
ObjectProperty (path) {
Expand Down
9 changes: 9 additions & 0 deletions packages/taroize/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import * as t from 'babel-types'
import { transform } from 'babel-core'
import { codeFrameColumns } from '@babel/code-frame'
import { camelCase, capitalize } from 'lodash'
import { NodePath } from 'babel-traverse'

export function isAliasThis (p: NodePath<t.Node>, name: string) {
const binding = p.scope.getBinding(name)
if (binding) {
return binding.path.isVariableDeclarator() && binding.path.get('init').isThisExpression()
}
return false
}

export function parseCode (code: string) {
return (transform(code, {
Expand Down

0 comments on commit 4b75433

Please sign in to comment.