Skip to content

Commit

Permalink
Merge pull request #357 from caoyang818/release-3.6.8-app
Browse files Browse the repository at this point in the history
fix: 修复var regexp = getRegExp()没有转换成var regexp = new RegExp()的问题以及补充单元测试
  • Loading branch information
qican777 authored Oct 30, 2023
2 parents fcf9e28 + aaa513b commit 6468e36
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
14 changes: 14 additions & 0 deletions packages/taro-transformer-wx/__tests__/wxs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import transform from '../src'
import { baseOptions } from './utils'

describe('wxs.ts测试', () => {
test('wxs文件中的var regexp = getRegExp()转换为var regexp = new RegExp()', () => {
const wxsCode = `var regexp = getRegExp();`
baseOptions.sourcePath = '.wxs'
const { code } = transform({
...baseOptions,
code: wxsCode,
})
expect(code).toEqual('var regexp = new RegExp();')
})
})
15 changes: 13 additions & 2 deletions packages/taro-transformer-wx/src/wxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function traverseWxsFile(ast: t.File, defaultResult: TransformResult) {
if (path.node.arguments.length > 1) {
const regex = path.node.arguments[0]
const modifier = path.node.arguments[1]
if (t.isStringLiteral(regex)) {
if (t.isStringLiteral(regex) && t.isStringLiteral(modifier)) {
const regexStr = regex.extra?.raw as string
const regexModifier = modifier.extra?.rawValue as string
const regexWithoutQuotes = regexStr.replace(/^['"](.*)['"]$/, '$1')
Expand All @@ -30,8 +30,12 @@ export function traverseWxsFile(ast: t.File, defaultResult: TransformResult) {
t.stringLiteral(regexModifier),
])
path.replaceWith(newExpr)
} else if (t.isIdentifier(regex) || t.isIdentifier(modifier)) {
throw new Error('getRegExp 函数暂不支持传入变量类型的参数')
} else {
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
}else {
} else if (path.node.arguments.length === 1) {
const regex = path.node.arguments[0]
if (t.isStringLiteral(regex)) {
const regexStr = regex.extra?.raw as string
Expand All @@ -40,7 +44,14 @@ export function traverseWxsFile(ast: t.File, defaultResult: TransformResult) {
t.stringLiteral(regexWithoutQuotes)
])
path.replaceWith(newExpr)
} else if (t.isIdentifier(regex)) {
throw new Error('getRegExp 函数暂不支持传入变量类型的参数')
} else {
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
} else {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
}
}
},
Expand Down
10 changes: 10 additions & 0 deletions packages/taroize/__tests__/wxml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe('wxml.ts测试', () => {
const { wxml }: any = parseWXML(option.path, option.wxml)
expect(wxml).toMatchSnapshot()
})

test('wxs模块中的var regexp = getRegExp()转换为var regexp = new RegExp()', () => {
option.wxml = `<wxs module="wxs_regexp">
var regexp = getRegExp()
</wxs>`
option.path = 'wxml_wxs_regexp'
const { wxses, imports }: any = parseWXML(option.path, option.wxml)
expect(wxses).toMatchSnapshot()
expect(imports).toMatchSnapshot()
})
})

describe('parseContent', () => {
Expand Down
15 changes: 13 additions & 2 deletions packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports:
if (path.node.arguments.length > 1) {
const regex = path.node.arguments[0]
const modifier = path.node.arguments[1]
if (t.isStringLiteral(regex)) {
if (t.isStringLiteral(regex) && t.isStringLiteral(modifier)) {
const regexStr = regex.extra?.raw as string
const regexModifier = modifier.extra?.rawValue as string
const regexWithoutQuotes = regexStr.replace(/^['"](.*)['"]$/, '$1')
Expand All @@ -727,15 +727,26 @@ function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports:
t.stringLiteral(regexModifier),
])
path.replaceWith(newExpr)
} else if (t.isIdentifier(regex) || t.isIdentifier(modifier)) {
throw new Error('getRegExp 函数暂不支持传入变量类型的参数')
} else {
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
} else {
} else if (path.node.arguments.length === 1) {
const regex = path.node.arguments[0]
if (t.isStringLiteral(regex)) {
const regexStr = regex.extra?.raw as string
const regexWithoutQuotes = regexStr.replace(/^['"](.*)['"]$/, '$1')
const newExpr = t.newExpression(t.identifier('RegExp'), [t.stringLiteral(regexWithoutQuotes)])
path.replaceWith(newExpr)
} else if (t.isIdentifier(regex)) {
throw new Error('getRegExp 函数暂不支持传入变量类型的参数')
} else {
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
} else {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
}
}
},
Expand Down

0 comments on commit 6468e36

Please sign in to comment.