Skip to content

Commit

Permalink
fix(es/parser): Fix parsing of JSX with ASI (#6577)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #6522.
  • Loading branch information
alexander-akait committed Dec 6, 2022
1 parent c3dd8ff commit ad95ead
Show file tree
Hide file tree
Showing 3 changed files with 981 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/swc_ecma_parser/src/lexer/state.rs
Expand Up @@ -25,6 +25,8 @@ pub(super) struct State {
pub next_regexp: Option<BytePos>,
/// if line break exists between previous token and new token?
pub had_line_break: bool,
/// if line break exists before last?
pub had_line_break_before_last: bool,
/// TODO: Remove this field.
is_first: bool,
pub start: BytePos,
Expand Down Expand Up @@ -352,8 +354,10 @@ impl<'a, I: Input> Iterator for Lexer<'a, I> {
});
}
}

self.state.update(start, token);
self.state.prev_hi = self.last_pos();
self.state.had_line_break_before_last = self.had_line_break_before_last();
}

token.map(|token| {
Expand All @@ -376,6 +380,7 @@ impl State {
next_regexp: None,
is_first: true,
had_line_break: false,
had_line_break_before_last: false,
prev_hi: start_pos,
context,
token_type: None,
Expand Down Expand Up @@ -438,6 +443,7 @@ impl State {
start,
next,
self.had_line_break,
self.had_line_break_before_last,
self.is_expr_allowed,
);
}
Expand All @@ -451,6 +457,7 @@ impl State {
start: BytePos,
next: &Token,
had_line_break: bool,
had_line_break_before_last: bool,
is_expr_allowed: bool,
) -> bool {
let is_next_keyword = matches!(*next, Word(Word::Keyword(..)));
Expand Down Expand Up @@ -544,7 +551,7 @@ impl State {
TokenType::Keyword(Let)
| TokenType::Keyword(Const)
| TokenType::Keyword(Var)
if had_line_break =>
if had_line_break_before_last =>
{
true
}
Expand Down
32 changes: 32 additions & 0 deletions crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js
@@ -0,0 +1,32 @@
let x
<Comp></Comp>

let x

<Comp></Comp>

let x;
<Comp></Comp>

var x
<Comp></Comp>

var x;
<Comp></Comp>

function x() {
let x
<div />
}

{ foo: 'test' }
<Comp></Comp>

function test1() {}
<Comp></Comp>

class Foo {}
<>
<Comp></Comp>
<Comp></Comp>
</>

1 comment on commit ad95ead

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ad95ead Previous: 586bfd5 Ratio
es/full/bugs-1 349126 ns/iter (± 51224) 350100 ns/iter (± 45789) 1.00
es/full/minify/libraries/antd 2005709384 ns/iter (± 23971869) 2079368547 ns/iter (± 56382394) 0.96
es/full/minify/libraries/d3 431507299 ns/iter (± 17555624) 452273945 ns/iter (± 13399812) 0.95
es/full/minify/libraries/echarts 1737759431 ns/iter (± 55567864) 1925032414 ns/iter (± 136213967) 0.90
es/full/minify/libraries/jquery 115306802 ns/iter (± 8146660) 138793302 ns/iter (± 16550415) 0.83
es/full/minify/libraries/lodash 131533739 ns/iter (± 22798873) 161781225 ns/iter (± 9311455) 0.81
es/full/minify/libraries/moment 67778714 ns/iter (± 6074974) 80058081 ns/iter (± 7019018) 0.85
es/full/minify/libraries/react 22135480 ns/iter (± 2337502) 24731757 ns/iter (± 1327717) 0.90
es/full/minify/libraries/terser 335168257 ns/iter (± 16355075) 380187969 ns/iter (± 23873811) 0.88
es/full/minify/libraries/three 626864277 ns/iter (± 12529049) 668744489 ns/iter (± 29634046) 0.94
es/full/minify/libraries/typescript 3806480072 ns/iter (± 41925400) 4079520728 ns/iter (± 135939358) 0.93
es/full/minify/libraries/victory 980598818 ns/iter (± 62152431) 971799374 ns/iter (± 85780814) 1.01
es/full/minify/libraries/vue 186171761 ns/iter (± 39465352) 204497167 ns/iter (± 13635709) 0.91
es/full/codegen/es3 35530 ns/iter (± 3535) 35216 ns/iter (± 4610) 1.01
es/full/codegen/es5 34897 ns/iter (± 4061) 34559 ns/iter (± 3108) 1.01
es/full/codegen/es2015 35544 ns/iter (± 5642) 34962 ns/iter (± 9023) 1.02
es/full/codegen/es2016 35340 ns/iter (± 3283) 35475 ns/iter (± 1608) 1.00
es/full/codegen/es2017 36077 ns/iter (± 4358) 35138 ns/iter (± 1558) 1.03
es/full/codegen/es2018 35021 ns/iter (± 5510) 34240 ns/iter (± 788) 1.02
es/full/codegen/es2019 34958 ns/iter (± 5465) 34225 ns/iter (± 1433) 1.02
es/full/codegen/es2020 35144 ns/iter (± 4445) 34298 ns/iter (± 1822) 1.02
es/full/all/es3 198734925 ns/iter (± 15894404) 191446317 ns/iter (± 25438241) 1.04
es/full/all/es5 187761665 ns/iter (± 15165323) 183373108 ns/iter (± 9189378) 1.02
es/full/all/es2015 151063053 ns/iter (± 10703975) 165586574 ns/iter (± 25538818) 0.91
es/full/all/es2016 150740393 ns/iter (± 15137766) 162010049 ns/iter (± 20415451) 0.93
es/full/all/es2017 151336708 ns/iter (± 31271585) 147239744 ns/iter (± 11160821) 1.03
es/full/all/es2018 147259927 ns/iter (± 13510691) 156883716 ns/iter (± 14331707) 0.94
es/full/all/es2019 157021835 ns/iter (± 20201457) 143742018 ns/iter (± 9816889) 1.09
es/full/all/es2020 141912546 ns/iter (± 11883928) 143493057 ns/iter (± 14207730) 0.99
es/full/parser 761806 ns/iter (± 79287) 719063 ns/iter (± 78550) 1.06
es/full/base/fixer 27263 ns/iter (± 3450) 26510 ns/iter (± 1024) 1.03
es/full/base/resolver_and_hygiene 95987 ns/iter (± 8357) 93078 ns/iter (± 6110) 1.03
serialization of ast node 213 ns/iter (± 17) 211 ns/iter (± 5) 1.01
serialization of serde 231 ns/iter (± 26) 234 ns/iter (± 7) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.