Skip to content

Commit

Permalink
Fix regular expressions with /[^]/ (#168)
Browse files Browse the repository at this point in the history
Fixes #146
  • Loading branch information
sebastienros authored Jun 15, 2021
1 parent c053278 commit d9dabfa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Esprima/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,13 @@ public Token ScanTemplate()
tmp = Regex.Replace(tmp, "[\uD800-\uDBFF][\uDC00-\uDFFF]", astralSubstitute);
}

// .NET doesn't support [^] which is equivalent to `[^.]`
// c.f. https://github.com/sebastienros/esprima-dotnet/issues/146
if (tmp.Contains("[^]"))
{
tmp = tmp.Replace("[^]", "[^.]");
}

// First, detect invalid regular expressions.
var options = ParseRegexOptions(flags);

Expand Down
12 changes: 12 additions & 0 deletions test/Esprima.Tests/RegExpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,17 @@ private static Regex CreateRegex(string code)
var token = new Scanner(code, options).ScanRegExp();
return (Regex) token.Value;
}

[Theory]
[InlineData(@"/[^]*? (:[rp] [el] a[\w -]+)[^]*/")]
[InlineData(@"/[^]/")]
[InlineData(@"/[^ ]/")]
public void ShouldParseRegularExpression(string regexp)
{
var parser = new JavaScriptParser(@"var O = " + regexp);
var program = parser.ParseScript();

Assert.NotNull(program);
}
}
}

0 comments on commit d9dabfa

Please sign in to comment.