Skip to content

Commit

Permalink
fix: 优化var regexp = getRegExp()转换判断逻辑,增加其他变量判断
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyang818 committed Oct 30, 2023
1 parent dd79f6f commit aaa513b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 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,10 +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 {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
} else {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
}
}
},
Expand Down
16 changes: 12 additions & 4 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,18 +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 {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
throw new Error('getRegExp 函数暂不支持传入非字符串类型的参数')
}
} else {
const newExpr = t.newExpression(t.identifier('RegExp'), [])
path.replaceWith(newExpr)
}
}
},
Expand Down

0 comments on commit aaa513b

Please sign in to comment.