Skip to content

Commit abb427c

Browse files
committed
fix: incorrect JS parsing when / chars encountered within strings, fixes #1250
1 parent 806d9e8 commit abb427c

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.943
1+
0.3.944

parser/v2/scriptparser.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,17 @@ loop:
130130
for {
131131
before := pi.Index()
132132

133+
// If we're outside of a string literal, check for a regexp literal.
133134
// Check for a regular expression literal.
134-
r, ok, err := regexpLiteral.Parse(pi)
135-
if err != nil {
136-
return nil, false, err
137-
}
138-
if ok {
139-
sb.WriteString(r)
140-
continue charLoop
135+
if stringLiteralDelimiter == jsQuoteNone {
136+
r, ok, err := regexpLiteral.Parse(pi)
137+
if err != nil {
138+
return nil, false, err
139+
}
140+
if ok {
141+
sb.WriteString(r)
142+
continue charLoop
143+
}
141144
}
142145

143146
// Check for EOF.

parser/v2/scriptparser_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,22 @@ to see if it works";</script>`,
345345
},
346346
},
347347
},
348+
{
349+
name: "single line comments after expressions are allowed",
350+
input: `<script>
351+
const category = path.split('/')[2]; // example comment
352+
</script>`,
353+
expected: &ScriptElement{
354+
Contents: []ScriptContents{
355+
NewScriptContentsScriptCode("\nconst category = path.split('/')[2]; "),
356+
NewScriptContentsScriptCode("// example comment\n"),
357+
},
358+
Range: Range{
359+
From: Position{Index: 0, Line: 0, Col: 0},
360+
To: Position{Index: 74, Line: 2, Col: 9},
361+
},
362+
},
363+
},
348364
{
349365
name: "multiline commented out go expressions are ignored",
350366
input: `<script>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- in --
2+
<script>
3+
const category = path.split('/')[2]; // example comment
4+
</script>
5+
-- out --
6+
7+
const category = path.split('/')[2]; // example comment
8+

0 commit comments

Comments
 (0)