Skip to content

Commit

Permalink
fix(transformer): objectExpression property key'type (#3932)
Browse files Browse the repository at this point in the history
  • Loading branch information
cangku authored and yuche committed Jul 25, 2019
1 parent 2d3bd05 commit c6f587c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/taro-transformer-wx/__tests__/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,45 @@ describe('字符不转义', () => {
expect(inst.state.anonymousState__temp).toEqual('')
})
})

describe("ObjectExpression property key", () => {
test("StringLiteral", () => {
const { template, ast, code } = transform({
...baseOptions,
isRoot: true,
code: buildComponent(`
return {
'weapp': (
<View>weapp</View>
),
'h5': (
<View>h5</View>
)
}[process.env.TARO_ENV]
`)
})

const inst = evalClass(ast, "", true);
expect(template).toMatch(`'weapp' === 'weapp'`);
})
test("Identifier", () => {
const { template, ast, code } = transform({
...baseOptions,
isRoot: true,
code: buildComponent(`
return {
weapp: (
<View>weapp</View>
),
h5: (
<View>h5</View>
)
}[process.env.TARO_ENV]
`)
})

const inst = evalClass(ast, "", true);
expect(template).toMatch(`'weapp' === 'weapp'`);
})
})
})
3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ export class RenderParser {
}
const children = properties.map(p => p.node).map((p, index) => {
const block = buildBlockElement()
const tester = t.binaryExpression('===', p.key, rval.node as any)
const leftExpression = t.isStringLiteral(p.key) ? p.key : t.stringLiteral(p.key.name)
const tester = t.binaryExpression('===', leftExpression, rval.node as any)
block.children = [t.jSXExpressionContainer(p.value)]
if (index === 0) {
newJSXIfAttr(block, tester)
Expand Down

0 comments on commit c6f587c

Please sign in to comment.