Skip to content

Commit d2cf83b

Browse files
committed
fix(transformer): if 语句中循环体内的变量不需要变量提升
1 parent 587a02c commit d2cf83b

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

packages/taro-transformer-wx/__tests__/state.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('State', () => {
9494
const instance = evalClass(ast)
9595

9696
expect(instance.state.anonymousState__temp).toBe(undefined)
97-
expect(template).toMatch(`<view style="{{\" width: \" + rate + \"px; \"}}">`)
97+
expect(template).toMatch(`<view style=\"{{'width: ' + rate + 'px;'}}\">`)
9898
})
9999

100100
test('可以使用array of object', () => {

packages/taro-transformer-wx/src/jsx.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import generate from 'babel-generator'
2-
import * as prettier from 'prettier'
32
import { NodePath } from 'babel-traverse'
43
import * as t from 'babel-types'
54
import { kebabCase } from 'lodash'
@@ -164,13 +163,8 @@ export function parseJSXElement (element: t.JSXElement): string {
164163
quotes: 'single',
165164
concise: true
166165
})
167-
code = prettier.format(code, {
168-
singleQuote: true,
169-
parser: 'babylon',
170-
semi: false,
171-
trailingComma: 'none'
172-
}).slice(0, -1)
173166
code = code
167+
.replace(/"/g, "'")
174168
.replace(/(this\.props\.)|(this\.state\.)/g, '')
175169
.replace(/this\./g, '')
176170
value = isBindEvent ? code : `{{${code}}}`

packages/taro-transformer-wx/src/render.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
hasComplexExpression,
1919
findMethodName,
2020
isVarName,
21-
setParentCondition
21+
setParentCondition,
22+
isContainJSXElement
2223
} from './utils'
2324
import { difference } from 'lodash'
2425
import {
@@ -391,11 +392,15 @@ export class RenderParser {
391392
//
392393
} else {
393394
const ifStatement = parentPath.findParent(p => p.isIfStatement())
394-
const blockStatement = parentPath.findParent(p => p.isBlockStatement() && p.parentPath === ifStatement) as NodePath<t.BlockStatement>
395+
const blockStatement = parentPath.findParent(p => p.isBlockStatement() && (p.parentPath === ifStatement)) as NodePath<t.BlockStatement>
395396
if (blockStatement && blockStatement.isBlockStatement()) {
396397
blockStatement.traverse({
397398
VariableDeclarator: (p) => {
398399
const { id, init } = p.node
400+
const ifStem = p.parentPath.parentPath.parentPath
401+
if (!ifStem.isIfStatement() || isContainJSXElement(p)) {
402+
return
403+
}
399404
if (t.isIdentifier(id)) {
400405
if (id.name.startsWith('loopArray')) {
401406
this.renderPath.node.body.body.unshift(
@@ -406,7 +411,7 @@ export class RenderParser {
406411
)
407412
} else {
408413
const newId = this.renderScope.generateDeclaredUidIdentifier('$' + id.name)
409-
this.renderScope.rename(id.name, newId.name)
414+
blockStatement.scope.rename(id.name, newId.name)
410415
p.parentPath.replaceWith(
411416
template('ID = INIT;')({ ID: newId, INIT: init })
412417
)
@@ -901,21 +906,26 @@ export class RenderParser {
901906
replaceOriginal(path, parent, name)
902907
}
903908
})
909+
const replacements = new Set()
904910
component.traverse({
905911
Identifier (path) {
906912
const name = path.node.name
907913
const parent = path.parent
914+
if (replacements.has(parent)) {
915+
return
916+
}
908917
if (stateToBeAssign.has(name) && path.isReferencedIdentifier()) {
909-
path.replaceWith(
910-
t.memberExpression(
911-
t.identifier(item.name),
912-
path.node
913-
)
918+
const replacement = t.memberExpression(
919+
t.identifier(item.name),
920+
path.node
914921
)
922+
path.replaceWith(replacement)
923+
replacements.add(replacement)
915924
hasOriginalRef = true
925+
} else {
926+
replaceOriginal(path, parent, name)
916927
}
917928

918-
replaceOriginal(path, parent, name)
919929
},
920930
MemberExpression (path) {
921931
const id = findFirstIdentifierFromMemberExpression(path.node)

0 commit comments

Comments
 (0)