Skip to content

Commit

Permalink
Insert semicolons between lines that JS would combine
Browse files Browse the repository at this point in the history
Fixes DanielXMoore#272.
Adds some extra semicolons, as it is based on input instead of generated code.
  • Loading branch information
edemaine committed Jan 23, 2023
1 parent 3582e8e commit f856103
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 8 deletions.
8 changes: 8 additions & 0 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -3575,6 +3575,10 @@ ExpressionDelimiter

StatementDelimiter
SemicolonDelimiter
# NOTE: Avoid automatic continuation onto lines that start with
# certain characters by adding an explicit semicolon. See
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion
&( Samedent [\(\[`\+\-\*\/] ) InsertSemicolon
&EOS

SemicolonDelimiter
Expand Down Expand Up @@ -4788,6 +4792,10 @@ Debugger

# Insertions

InsertSemicolon
"" ->
return { $loc, token: ";" }

InsertOpenParen
"" ->
return { $loc, token: "(" }
Expand Down
4 changes: 2 additions & 2 deletions test/autolet.civet
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe "auto let with multiple declaration", ->
[a, b] = [1, 2]
[c, d] = [3, 4]
---
let c = "str"
let [a, b] = [1, 2]
let c = "str";
let [a, b] = [1, 2];
let d
[c, d] = [3, 4]
"""
Expand Down
21 changes: 21 additions & 0 deletions test/call-expression.civet
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,24 @@ describe "call-expression", ->
data!
.map((x) => x + 1)
"""

describe "insert semicolons to prevent accidental calls", ->
testCase """
parenthetical
---
x
(z)
---
x;
(z)
"""

testCase """
arrow function
---
x
(z) => z
---
x;
(z) => z
"""
2 changes: 1 addition & 1 deletion test/chained-comparisons.civet
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe "chained comparisons", ->
(a < b) < c
(a + b) < (c + d) < (e + f)
---
(a < b) < c
(a < b) < c;
(a + b) < (c + d) && (c + d) < (e + f)
"""

Expand Down
4 changes: 2 additions & 2 deletions test/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe "function", ->
() => x
(x) => x
---
() => x
() => x;
(x) => x
"""

Expand Down Expand Up @@ -617,7 +617,7 @@ describe "function", ->
(x) => x+a
---
function test(a) {
a = simplify(a)
a = simplify(a);
return (x) => x+a
}
"""
Expand Down
2 changes: 1 addition & 1 deletion test/jsx/indent.civet
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ describe "Unbraced function children in JSX", ->
`[${priority}] ${item}`
---
<For each={items}>{(item) => {
const {priority, text} = item
const {priority, text} = item;
return `[${priority}] ${item}`
}}
</For>
Expand Down
2 changes: 1 addition & 1 deletion test/parentheses.civet
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ describe "parentheses", ->
a
/** @type Y */ (x)
---
a
a;
/** @type Y */ (x)
"""
2 changes: 1 addition & 1 deletion test/types/function.civet
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe "[TS] function", ->
() : number => x
(x) : number => x
---
() : number => x
() : number => x;
(x) : number => x
"""

Expand Down

0 comments on commit f856103

Please sign in to comment.