Skip to content

Commit

Permalink
Fix incorrect regexp matching when followed by comment
Browse files Browse the repository at this point in the history
Fixes #717
  • Loading branch information
sheetalkamat committed Feb 6, 2019
1 parent e632043 commit 06d49b5
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 7 deletions.
4 changes: 2 additions & 2 deletions TypeScript.YAML-tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ repository:
regex:
patterns:
- name: string.regexp.ts
begin: (?<!\+\+|--|})(?<=[=(:,\[?+!]|{{lookBehindReturn}}|{{lookBehindCase}}|=>|&&|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))
begin: (?<!\+\+|--|})(?<=[=(:,\[?+!]|{{lookBehindReturn}}|{{lookBehindCase}}|=>|&&|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[\()]|\\.|\[([^\]\\]|\\.)+\]|\(([^\)\\]|\\.)+\))+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))
beginCaptures:
'1': { name: punctuation.definition.string.begin.ts }
end: (/)([gimsuy]*)
Expand All @@ -2530,7 +2530,7 @@ repository:
- include: '#regexp'
# Check if complete regexp syntax
- name: string.regexp.ts
begin: (?<![_$[:alnum:])\]]|\+\+|--|})\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))
begin: (?<![_$[:alnum:])\]]|\+\+|--|}|\*\/)\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))
beginCaptures:
'0': { name: punctuation.definition.string.begin.ts }
end: (/)([gimsuy]*)
Expand Down
4 changes: 2 additions & 2 deletions TypeScript.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -7600,7 +7600,7 @@
<key>name</key>
<string>string.regexp.ts</string>
<key>begin</key>
<string>(?&lt;!\+\+|--|})(?&lt;=[=(:,\[?+!]|^return|[^\._$[:alnum:]]return|^case|[^\._$[:alnum:]]case|=&gt;|&amp;&amp;|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<string>(?&lt;!\+\+|--|})(?&lt;=[=(:,\[?+!]|^return|[^\._$[:alnum:]]return|^case|[^\._$[:alnum:]]case|=&gt;|&amp;&amp;|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[\()]|\\.|\[([^\]\\]|\\.)+\]|\(([^\)\\]|\\.)+\))+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -7636,7 +7636,7 @@
<key>name</key>
<string>string.regexp.ts</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:])\]]|\+\+|--|})\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<string>(?&lt;![_$[:alnum:])\]]|\+\+|--|}|\*\/)\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
Expand Down
4 changes: 2 additions & 2 deletions TypeScriptReact.tmLanguage
Original file line number Diff line number Diff line change
Expand Up @@ -7522,7 +7522,7 @@
<key>name</key>
<string>string.regexp.tsx</string>
<key>begin</key>
<string>(?&lt;!\+\+|--|})(?&lt;=[=(:,\[?+!]|^return|[^\._$[:alnum:]]return|^case|[^\._$[:alnum:]]case|=&gt;|&amp;&amp;|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<string>(?&lt;!\+\+|--|})(?&lt;=[=(:,\[?+!]|^return|[^\._$[:alnum:]]return|^case|[^\._$[:alnum:]]case|=&gt;|&amp;&amp;|\|\||\*\/)\s*(\/)(?![\/*])(?=(?:[^\/\\\[\()]|\\.|\[([^\]\\]|\\.)+\]|\(([^\)\\]|\\.)+\))+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
Expand Down Expand Up @@ -7558,7 +7558,7 @@
<key>name</key>
<string>string.regexp.tsx</string>
<key>begin</key>
<string>(?&lt;![_$[:alnum:])\]]|\+\+|--|})\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<string>(?&lt;![_$[:alnum:])\]]|\+\+|--|}|\*\/)\/(?![\/*])(?=(?:[^\/\\\[]|\\.|\[([^\]\\]|\\.)+\])+\/[gimsuy]*(?!\s*[a-zA-Z0-9_$]))</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
Expand Down
268 changes: 268 additions & 0 deletions tests/baselines/Issue717.baseline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
original file
-----------------------------------
/* regex? */
function test() {
return /"/.test('xxx"xxx');
}

console.log(test());
a = 3 /*test*/ / parseInt(/.+/.exec('3f23fg')[0]);
console.log(a);

b = 1
a = () => b
2 + (b=2), 3
b = 6
console.log(a(),b);
(b)

if (a === 1) {
b = 2
} else if (a === 2) b = 3
else b = 4
c = 1
-----------------------------------

Grammar: TypeScript.tmLanguage
-----------------------------------
>/* regex? */
^^
source.ts comment.block.ts punctuation.definition.comment.ts
^^^^^^^^
source.ts comment.block.ts
^^
source.ts comment.block.ts punctuation.definition.comment.ts
>function test() {
^^^^^^^^
source.ts meta.function.ts storage.type.function.ts
^
source.ts meta.function.ts
^^^^
source.ts meta.function.ts meta.definition.function.ts entity.name.function.ts
^
source.ts meta.function.ts meta.parameters.ts punctuation.definition.parameters.begin.ts
^
source.ts meta.function.ts meta.parameters.ts punctuation.definition.parameters.end.ts
^
source.ts meta.function.ts
^
source.ts meta.function.ts meta.block.ts punctuation.definition.block.ts
> return /"/.test('xxx"xxx');
^^^
source.ts meta.function.ts meta.block.ts
^^^^^^
source.ts meta.function.ts meta.block.ts keyword.control.flow.ts
^
source.ts meta.function.ts meta.block.ts string.regexp.ts
^
source.ts meta.function.ts meta.block.ts string.regexp.ts punctuation.definition.string.begin.ts
^
source.ts meta.function.ts meta.block.ts string.regexp.ts
^
source.ts meta.function.ts meta.block.ts string.regexp.ts punctuation.definition.string.end.ts
^
source.ts meta.function.ts meta.block.ts meta.function-call.ts punctuation.accessor.ts
^^^^
source.ts meta.function.ts meta.block.ts meta.function-call.ts support.function.ts
^
source.ts meta.function.ts meta.block.ts meta.brace.round.ts
^
source.ts meta.function.ts meta.block.ts string.quoted.single.ts punctuation.definition.string.begin.ts
^^^^^^^
source.ts meta.function.ts meta.block.ts string.quoted.single.ts
^
source.ts meta.function.ts meta.block.ts string.quoted.single.ts punctuation.definition.string.end.ts
^
source.ts meta.function.ts meta.block.ts meta.brace.round.ts
^
source.ts meta.function.ts meta.block.ts punctuation.terminator.statement.ts
>}
^
source.ts meta.function.ts meta.block.ts punctuation.definition.block.ts
>
^
source.ts
>console.log(test());
^^^^^^^
source.ts meta.function-call.ts support.class.console.ts
^
source.ts meta.function-call.ts punctuation.accessor.ts
^^^
source.ts meta.function-call.ts support.function.console.ts
^
source.ts meta.brace.round.ts
^^^^
source.ts meta.function-call.ts entity.name.function.ts
^
source.ts meta.brace.round.ts
^
source.ts meta.brace.round.ts
^
source.ts meta.brace.round.ts
^
source.ts punctuation.terminator.statement.ts
>a = 3 /*test*/ / parseInt(/.+/.exec('3f23fg')[0]);
^
source.ts variable.other.readwrite.ts
^
source.ts
^
source.ts keyword.operator.assignment.ts
^
source.ts
^
source.ts constant.numeric.decimal.ts
^
source.ts
^^
source.ts comment.block.ts punctuation.definition.comment.ts
^^^^
source.ts comment.block.ts
^^
source.ts comment.block.ts punctuation.definition.comment.ts
^
source.ts
^
source.ts string.regexp.ts punctuation.definition.string.begin.ts
^^^^^^^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp constant.other.character-class.regexp
^
source.ts string.regexp.ts meta.group.regexp keyword.operator.quantifier.regexp
^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp constant.other.character-class.regexp
^^^^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp meta.group.regexp punctuation.definition.group.regexp
^^^^^^^^
source.ts string.regexp.ts meta.group.regexp meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp constant.other.character-class.set.regexp punctuation.definition.character-class.regexp
^
source.ts string.regexp.ts meta.group.regexp constant.other.character-class.set.regexp
^
source.ts string.regexp.ts meta.group.regexp constant.other.character-class.set.regexp punctuation.definition.character-class.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^
source.ts string.regexp.ts
>console.log(a);
^^^^^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts constant.other.character-class.regexp
^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^
source.ts string.regexp.ts
>
^
source.ts string.regexp.ts
>b = 1
^^^^^^
source.ts string.regexp.ts
>a = () => b
^^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^^^^
source.ts string.regexp.ts
>2 + (b=2), 3
^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts keyword.operator.quantifier.regexp
^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^^
source.ts string.regexp.ts
>b = 6
^^^^^^
source.ts string.regexp.ts
>console.log(a(),b);
^^^^^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts constant.other.character-class.regexp
^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp meta.group.regexp punctuation.definition.group.regexp
^^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^
source.ts string.regexp.ts
>(b)
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
>
^
source.ts string.regexp.ts
>if (a === 1) {
^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^^^^^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^
source.ts string.regexp.ts
> b = 2
^^^^^^^^^
source.ts string.regexp.ts
>} else if (a === 2) b = 3
^^^^^^^^^^
source.ts string.regexp.ts
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^^^^^
source.ts string.regexp.ts meta.group.regexp
^
source.ts string.regexp.ts meta.group.regexp punctuation.definition.group.regexp
^^^^^^^
source.ts string.regexp.ts
>else b = 4
^^^^^^^^^^^
source.ts string.regexp.ts
>c = 1
^^^^^^
source.ts string.regexp.ts
2 changes: 1 addition & 1 deletion tests/baselines/regexp.baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Grammar: TypeScript.tmLanguage
^
source.ts meta.var.expr.ts keyword.operator.assignment.ts
^
source.ts meta.var.expr.ts string.regexp.ts
source.ts meta.var.expr.ts
^
source.ts meta.var.expr.ts string.regexp.ts punctuation.definition.string.begin.ts
^
Expand Down
21 changes: 21 additions & 0 deletions tests/cases/Issue717.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* regex? */
function test() {
return /"/.test('xxx"xxx');
}

console.log(test());
a = 3 /*test*/ / parseInt(/.+/.exec('3f23fg')[0]);
console.log(a);

b = 1
a = () => b
2 + (b=2), 3
b = 6
console.log(a(),b);
(b)

if (a === 1) {
b = 2
} else if (a === 2) b = 3
else b = 4
c = 1

0 comments on commit 06d49b5

Please sign in to comment.