Skip to content

Commit

Permalink
fix: 正则 match 方法,g 判断错误
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Jul 30, 2020
1 parent 920d3f8 commit 5c11fbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/features/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function replace(

const expNode = node.expression as PropertyAccessExpression;

let nodeList = [...node.arguments, expNode.expression];
const nodeList = [...node.arguments, expNode.expression];
let method = '%helper::str_replace_once';

if (isFunctionLike(node.arguments[1], typeChecker)) {
Expand All @@ -54,7 +54,7 @@ function replace(
method = 'preg_replace';
const firstArg = node.arguments[0] as RegularExpressionLiteral;
if (!/gi?$/.test(getLiteralTextOfNode(firstArg, true))) {
nodeList.push(createNumericLiteral("1"));
nodeList.push(createNumericLiteral('1'));
}
}

Expand All @@ -68,7 +68,7 @@ function split(node: CallExpression, {emitExpressionList, writePunctuation}, sta
const pattern = node.arguments[0];
const isPreg = !isStringLike(pattern, state.typeChecker);
const method = isPreg ? 'preg_split' : '%helper::strSplit';
let nodeList = [node.arguments[0], expNode.expression];
const nodeList = [node.arguments[0], expNode.expression];

if (isPreg) {
nodeList.push(createNull(), createIdentifier('PREG_SPLIT_DELIM_CAPTURE'));
Expand All @@ -82,17 +82,17 @@ function split(node: CallExpression, {emitExpressionList, writePunctuation}, sta
function match(node: CallExpression, {emitExpressionList, writePunctuation}, state) {
const expNode = node.expression as PropertyAccessExpression;
const pattern = node.arguments[0];
let isRegularExpressionLiteral = pattern.kind === SyntaxKind.RegularExpressionLiteral;
let method = '%helper::match';
let nodeList = [node.arguments[0], expNode.expression];
const isRegularExpressionLiteral = pattern.kind === SyntaxKind.RegularExpressionLiteral;
const method = '%helper::match';
const nodeList = [node.arguments[0], expNode.expression];

if (!isRegularExpressionLiteral) {
// mark is string
nodeList.push(createTrue());
}
else {
// mark all match
let isAll = pattern.getText(state.sourceFile).split('/')[2].indexOf('g') != -1;
const isAll = pattern.getText(state.sourceFile).split('/').pop().indexOf('g') !== -1;
if (isAll) {
nodeList.push(createFalse());
nodeList.push(createTrue());
Expand Down
6 changes: 4 additions & 2 deletions test/features/stringMatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ let a = 'abc';
let b = a.match(/a/gi);
let c = a.match(/a(?<name>bc)/ig);
let d = a.match(/a(?<name>bc)/g);
let e = a.match('a');
let e = a.match(/a(?<name>\/bc)/g);
let f = a.match('a');
```

```php
$a = "abc";
$b = \Ts2Php_Helper::match("/a/i", $a, false, true);
$c = \Ts2Php_Helper::match("/a(?<name>bc)/i", $a, false, true);
$d = \Ts2Php_Helper::match("/a(?<name>bc)/", $a, false, true);
$e = \Ts2Php_Helper::match("a", $a, true);
$e = \Ts2Php_Helper::match("/a(?<name>\\/bc)/", $a, false, true);
$f = \Ts2Php_Helper::match("a", $a, true);
```

0 comments on commit 5c11fbd

Please sign in to comment.