Skip to content

Commit

Permalink
fix(transformer): 函数式组件去掉 props 前缀,close #3117
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed May 21, 2019
1 parent bcb4db8 commit 15b33d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/taro-transformer-wx/src/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function initialIsCapital (word: string) {
return word[0] !== word[0].toLowerCase()
}

export const Status = {
isSFC: false
}

export const functionalComponent: () => {
visitor: Visitor
} = () => {
Expand Down Expand Up @@ -91,6 +95,7 @@ const ${id.name} = ${generate(t.arrowFunctionExpression(params, body)).code}
throw codeFrameError(arg, '函数式组件只支持传入一个简单标识符或使用对象结构')
}
}
Status.isSFC = true
const classDecl = t.classDeclaration(id, t.memberExpression(t.identifier('Taro'), t.identifier('Component')), t.classBody([
t.classMethod('method', t.identifier('render'), [], cloneBody)
]), [])
Expand Down
12 changes: 8 additions & 4 deletions packages/taro-transformer-wx/src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { createHTMLElement } from './create-html-element'
import { codeFrameError, decodeUnicode } from './utils'
import { Adapter, Adapters, isNewPropsSystem } from './adapter'
import { Status } from './functional'

export function isStartWithWX (str: string) {
return str[0] === 'w' && str[1] === 'x'
Expand Down Expand Up @@ -96,17 +97,20 @@ export function setJSXAttr (
}

export function generateJSXAttr (ast: t.Node) {
return decodeUnicode(
const code = decodeUnicode(
generate(ast, {
quotes: 'single',
jsonCompatibleStrings: true
})
.code
)
.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/(props\.)|(state\.)/g, '')
.replace(/this\./g, '')
.replace(/</g, lessThanSignPlacehold)
if (Status.isSFC) {
return code
}
return code.replace(/(this\.props\.)|(this\.state\.)/g, '')
.replace(/(props\.)|(state\.)/g, '')
.replace(/this\./g, '')
}

export function isAllLiteral (...args) {
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Adapters } from './adapter'
import { eslintValidation } from './eslint'
import { TransformOptions } from 'babel-core'
import { functionalComponent } from './functional'
import { functionalComponent, Status } from './functional'
import { isTestEnv } from './env'

export interface Options {
Expand Down Expand Up @@ -30,6 +30,7 @@ export const setTransformOptions = (options: Options) => {
}

export const buildBabelTransformOptions: () => TransformOptions = () => {
Status.isSFC = false
return {
parserOpts: {
sourceType: 'module',
Expand Down

0 comments on commit 15b33d1

Please sign in to comment.