From 2ae2c9ca7c8d20490b3219b6500b02b5e7af7664 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 5 Dec 2022 22:31:30 +0300 Subject: [PATCH 1/4] fix(ecma/parser): parse JSX with ASI --- crates/swc_ecma_parser/src/lexer/state.rs | 8 +- .../tests/jsx/basic/issue-6522/input.js | 8 + .../tests/jsx/basic/issue-6522/input.js.json | 278 ++++++++++++++++++ 3 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js create mode 100644 crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json diff --git a/crates/swc_ecma_parser/src/lexer/state.rs b/crates/swc_ecma_parser/src/lexer/state.rs index 58cebcf42e49..127161d3f329 100644 --- a/crates/swc_ecma_parser/src/lexer/state.rs +++ b/crates/swc_ecma_parser/src/lexer/state.rs @@ -24,6 +24,7 @@ pub(super) struct State { pub is_expr_allowed: bool, pub next_regexp: Option, /// if line break exists between previous token and new token? + pub prev_had_line_break: bool, pub had_line_break: bool, /// TODO: Remove this field. is_first: bool, @@ -352,8 +353,10 @@ impl<'a, I: Input> Iterator for Lexer<'a, I> { }); } } + self.state.update(start, token); self.state.prev_hi = self.last_pos(); + self.state.prev_had_line_break = self.state.had_line_break; } token.map(|token| { @@ -376,6 +379,7 @@ impl State { next_regexp: None, is_first: true, had_line_break: false, + prev_had_line_break: false, prev_hi: start_pos, context, token_type: None, @@ -438,6 +442,7 @@ impl State { start, next, self.had_line_break, + self.prev_had_line_break, self.is_expr_allowed, ); } @@ -451,6 +456,7 @@ impl State { start: BytePos, next: &Token, had_line_break: bool, + prev_had_line_break: bool, is_expr_allowed: bool, ) -> bool { let is_next_keyword = matches!(*next, Word(Word::Keyword(..))); @@ -544,7 +550,7 @@ impl State { TokenType::Keyword(Let) | TokenType::Keyword(Const) | TokenType::Keyword(Var) - if had_line_break => + if prev_had_line_break => { true } diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js new file mode 100644 index 000000000000..51897026e2eb --- /dev/null +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js @@ -0,0 +1,8 @@ +let x + + +var x + + +const x + \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json new file mode 100644 index 000000000000..a04554c3c45f --- /dev/null +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json @@ -0,0 +1,278 @@ +{ + "type": "Module", + "span": { + "start": 1, + "end": 64, + "ctxt": 0 + }, + "body": [ + { + "type": "VariableDeclaration", + "span": { + "start": 1, + "end": 6, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 5, + "end": 6, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 5, + "end": 6, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 7, + "end": 20, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 7, + "end": 20, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 8, + "end": 12, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 7, + "end": 13, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 13, + "end": 20, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 15, + "end": 19, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 22, + "end": 27, + "ctxt": 0 + }, + "kind": "var", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 26, + "end": 27, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 28, + "end": 41, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 28, + "end": 41, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 29, + "end": 33, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 28, + "end": 34, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 34, + "end": 41, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 36, + "end": 40, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 43, + "end": 50, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 49, + "end": 50, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 49, + "end": 50, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 51, + "end": 64, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 51, + "end": 64, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 52, + "end": 56, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 51, + "end": 57, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 57, + "end": 64, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 59, + "end": 63, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + } + ], + "interpreter": null +} From 98ea3f8d1355a48688ca3bd319dd185635a56320 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 5 Dec 2022 22:33:12 +0300 Subject: [PATCH 2/4] test: more --- .../tests/jsx/basic/issue-6522/input.js | 11 +- .../tests/jsx/basic/issue-6522/input.js.json | 333 ++++++++++++++++-- 2 files changed, 310 insertions(+), 34 deletions(-) diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js index 51897026e2eb..da10ece1eb85 100644 --- a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js @@ -1,8 +1,17 @@ let x +let x; + + var x +var x; + + const x - \ No newline at end of file + + +const x; + diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json index a04554c3c45f..10ee1bc190b7 100644 --- a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json @@ -2,7 +2,7 @@ "type": "Module", "span": { "start": 1, - "end": 64, + "end": 132, "ctxt": 0 }, "body": [ @@ -99,10 +99,10 @@ "type": "VariableDeclaration", "span": { "start": 22, - "end": 27, + "end": 28, "ctxt": 0 }, - "kind": "var", + "kind": "let", "declare": false, "declarations": [ { @@ -131,15 +131,282 @@ { "type": "ExpressionStatement", "span": { - "start": 28, - "end": 41, + "start": 29, + "end": 42, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 29, + "end": 42, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 30, + "end": 34, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 29, + "end": 35, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 35, + "end": 42, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 37, + "end": 41, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 44, + "end": 49, + "ctxt": 0 + }, + "kind": "var", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 48, + "end": 49, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 48, + "end": 49, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 50, + "end": 63, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 50, + "end": 63, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 51, + "end": 55, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 50, + "end": 56, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 56, + "end": 63, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 58, + "end": 62, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 65, + "end": 71, + "ctxt": 0 + }, + "kind": "var", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 69, + "end": 70, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 69, + "end": 70, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 72, + "end": 85, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 72, + "end": 85, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 73, + "end": 77, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 72, + "end": 78, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 78, + "end": 85, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 80, + "end": 84, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "VariableDeclaration", + "span": { + "start": 87, + "end": 94, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 93, + "end": 94, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 93, + "end": 94, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 95, + "end": 108, "ctxt": 0 }, "expression": { "type": "JSXElement", "span": { - "start": 28, - "end": 41, + "start": 95, + "end": 108, "ctxt": 0 }, "opening": { @@ -147,16 +414,16 @@ "name": { "type": "Identifier", "span": { - "start": 29, - "end": 33, + "start": 96, + "end": 100, "ctxt": 0 }, "value": "Comp", "optional": false }, "span": { - "start": 28, - "end": 34, + "start": 95, + "end": 101, "ctxt": 0 }, "attributes": [], @@ -167,15 +434,15 @@ "closing": { "type": "JSXClosingElement", "span": { - "start": 34, - "end": 41, + "start": 101, + "end": 108, "ctxt": 0 }, "name": { "type": "Identifier", "span": { - "start": 36, - "end": 40, + "start": 103, + "end": 107, "ctxt": 0 }, "value": "Comp", @@ -187,8 +454,8 @@ { "type": "VariableDeclaration", "span": { - "start": 43, - "end": 50, + "start": 110, + "end": 118, "ctxt": 0 }, "kind": "const", @@ -197,15 +464,15 @@ { "type": "VariableDeclarator", "span": { - "start": 49, - "end": 50, + "start": 116, + "end": 117, "ctxt": 0 }, "id": { "type": "Identifier", "span": { - "start": 49, - "end": 50, + "start": 116, + "end": 117, "ctxt": 0 }, "value": "x", @@ -220,15 +487,15 @@ { "type": "ExpressionStatement", "span": { - "start": 51, - "end": 64, + "start": 119, + "end": 132, "ctxt": 0 }, "expression": { "type": "JSXElement", "span": { - "start": 51, - "end": 64, + "start": 119, + "end": 132, "ctxt": 0 }, "opening": { @@ -236,16 +503,16 @@ "name": { "type": "Identifier", "span": { - "start": 52, - "end": 56, + "start": 120, + "end": 124, "ctxt": 0 }, "value": "Comp", "optional": false }, "span": { - "start": 51, - "end": 57, + "start": 119, + "end": 125, "ctxt": 0 }, "attributes": [], @@ -256,15 +523,15 @@ "closing": { "type": "JSXClosingElement", "span": { - "start": 57, - "end": 64, + "start": 125, + "end": 132, "ctxt": 0 }, "name": { "type": "Identifier", "span": { - "start": 59, - "end": 63, + "start": 127, + "end": 131, "ctxt": 0 }, "value": "Comp", From 57badd1fcb2ef7d80c4f4d15b5e9814bd366c686 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 5 Dec 2022 22:39:11 +0300 Subject: [PATCH 3/4] test: more --- .../tests/jsx/basic/issue-6522/input.js | 19 +- .../tests/jsx/basic/issue-6522/input.js.json | 532 +++++++++++++++--- 2 files changed, 481 insertions(+), 70 deletions(-) diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js index da10ece1eb85..eb1a46e813fe 100644 --- a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js @@ -1,6 +1,10 @@ let x +let x + + + let x; @@ -10,8 +14,19 @@ var x var x; -const x +function x() { + let x +
+} + +{ foo: 'test' } + + +function test1() {} -const x; +class Foo {} +<> + + diff --git a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json index 10ee1bc190b7..7123568906c6 100644 --- a/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json +++ b/crates/swc_ecma_parser/tests/jsx/basic/issue-6522/input.js.json @@ -2,7 +2,7 @@ "type": "Module", "span": { "start": 1, - "end": 132, + "end": 262, "ctxt": 0 }, "body": [ @@ -99,7 +99,7 @@ "type": "VariableDeclaration", "span": { "start": 22, - "end": 28, + "end": 27, "ctxt": 0 }, "kind": "let", @@ -188,10 +188,10 @@ "type": "VariableDeclaration", "span": { "start": 44, - "end": 49, + "end": 50, "ctxt": 0 }, - "kind": "var", + "kind": "let", "declare": false, "declarations": [ { @@ -220,15 +220,15 @@ { "type": "ExpressionStatement", "span": { - "start": 50, - "end": 63, + "start": 51, + "end": 64, "ctxt": 0 }, "expression": { "type": "JSXElement", "span": { - "start": 50, - "end": 63, + "start": 51, + "end": 64, "ctxt": 0 }, "opening": { @@ -236,16 +236,16 @@ "name": { "type": "Identifier", "span": { - "start": 51, - "end": 55, + "start": 52, + "end": 56, "ctxt": 0 }, "value": "Comp", "optional": false }, "span": { - "start": 50, - "end": 56, + "start": 51, + "end": 57, "ctxt": 0 }, "attributes": [], @@ -256,15 +256,15 @@ "closing": { "type": "JSXClosingElement", "span": { - "start": 56, - "end": 63, + "start": 57, + "end": 64, "ctxt": 0 }, "name": { "type": "Identifier", "span": { - "start": 58, - "end": 62, + "start": 59, + "end": 63, "ctxt": 0 }, "value": "Comp", @@ -276,7 +276,7 @@ { "type": "VariableDeclaration", "span": { - "start": 65, + "start": 66, "end": 71, "ctxt": 0 }, @@ -286,15 +286,15 @@ { "type": "VariableDeclarator", "span": { - "start": 69, - "end": 70, + "start": 70, + "end": 71, "ctxt": 0 }, "id": { "type": "Identifier", "span": { - "start": 69, - "end": 70, + "start": 70, + "end": 71, "ctxt": 0 }, "value": "x", @@ -366,24 +366,24 @@ "type": "VariableDeclaration", "span": { "start": 87, - "end": 94, + "end": 93, "ctxt": 0 }, - "kind": "const", + "kind": "var", "declare": false, "declarations": [ { "type": "VariableDeclarator", "span": { - "start": 93, - "end": 94, + "start": 91, + "end": 92, "ctxt": 0 }, "id": { "type": "Identifier", "span": { - "start": 93, - "end": 94, + "start": 91, + "end": 92, "ctxt": 0 }, "value": "x", @@ -398,15 +398,15 @@ { "type": "ExpressionStatement", "span": { - "start": 95, - "end": 108, + "start": 94, + "end": 107, "ctxt": 0 }, "expression": { "type": "JSXElement", "span": { - "start": 95, - "end": 108, + "start": 94, + "end": 107, "ctxt": 0 }, "opening": { @@ -414,16 +414,16 @@ "name": { "type": "Identifier", "span": { - "start": 96, - "end": 100, + "start": 95, + "end": 99, "ctxt": 0 }, "value": "Comp", "optional": false }, "span": { - "start": 95, - "end": 101, + "start": 94, + "end": 100, "ctxt": 0 }, "attributes": [], @@ -434,15 +434,15 @@ "closing": { "type": "JSXClosingElement", "span": { - "start": 101, - "end": 108, + "start": 100, + "end": 107, "ctxt": 0 }, "name": { "type": "Identifier", "span": { - "start": 103, - "end": 107, + "start": 102, + "end": 106, "ctxt": 0 }, "value": "Comp", @@ -452,50 +452,170 @@ } }, { - "type": "VariableDeclaration", + "type": "FunctionDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 118, + "end": 119, + "ctxt": 0 + }, + "value": "x", + "optional": false + }, + "declare": false, + "params": [], + "decorators": [], "span": { - "start": 110, - "end": 118, + "start": 109, + "end": 147, "ctxt": 0 }, - "kind": "const", - "declare": false, - "declarations": [ + "body": { + "type": "BlockStatement", + "span": { + "start": 122, + "end": 147, + "ctxt": 0 + }, + "stmts": [ + { + "type": "VariableDeclaration", + "span": { + "start": 128, + "end": 133, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 132, + "end": 133, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 132, + "end": 133, + "ctxt": 0 + }, + "value": "x", + "optional": false, + "typeAnnotation": null + }, + "init": null, + "definite": false + } + ] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 138, + "end": 145, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 138, + "end": 145, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 139, + "end": 142, + "ctxt": 0 + }, + "value": "div", + "optional": false + }, + "span": { + "start": 138, + "end": 145, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": true, + "typeArguments": null + }, + "children": [], + "closing": null + } + } + ] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null + }, + { + "type": "BlockStatement", + "span": { + "start": 149, + "end": 164, + "ctxt": 0 + }, + "stmts": [ { - "type": "VariableDeclarator", + "type": "LabeledStatement", "span": { - "start": 116, - "end": 117, + "start": 151, + "end": 162, "ctxt": 0 }, - "id": { + "label": { "type": "Identifier", "span": { - "start": 116, - "end": 117, + "start": 151, + "end": 154, "ctxt": 0 }, - "value": "x", - "optional": false, - "typeAnnotation": null + "value": "foo", + "optional": false }, - "init": null, - "definite": false + "body": { + "type": "ExpressionStatement", + "span": { + "start": 156, + "end": 162, + "ctxt": 0 + }, + "expression": { + "type": "StringLiteral", + "span": { + "start": 156, + "end": 162, + "ctxt": 0 + }, + "value": "test", + "raw": "'test'" + } + } } ] }, { "type": "ExpressionStatement", "span": { - "start": 119, - "end": 132, + "start": 165, + "end": 178, "ctxt": 0 }, "expression": { "type": "JSXElement", "span": { - "start": 119, - "end": 132, + "start": 165, + "end": 178, "ctxt": 0 }, "opening": { @@ -503,16 +623,16 @@ "name": { "type": "Identifier", "span": { - "start": 120, - "end": 124, + "start": 166, + "end": 170, "ctxt": 0 }, "value": "Comp", "optional": false }, "span": { - "start": 119, - "end": 125, + "start": 165, + "end": 171, "ctxt": 0 }, "attributes": [], @@ -523,15 +643,15 @@ "closing": { "type": "JSXClosingElement", "span": { - "start": 125, - "end": 132, + "start": 171, + "end": 178, "ctxt": 0 }, "name": { "type": "Identifier", "span": { - "start": 127, - "end": 131, + "start": 173, + "end": 177, "ctxt": 0 }, "value": "Comp", @@ -539,6 +659,282 @@ } } } + }, + { + "type": "FunctionDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 189, + "end": 194, + "ctxt": 0 + }, + "value": "test1", + "optional": false + }, + "declare": false, + "params": [], + "decorators": [], + "span": { + "start": 180, + "end": 199, + "ctxt": 0 + }, + "body": { + "type": "BlockStatement", + "span": { + "start": 197, + "end": 199, + "ctxt": 0 + }, + "stmts": [] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null + }, + { + "type": "ExpressionStatement", + "span": { + "start": 200, + "end": 213, + "ctxt": 0 + }, + "expression": { + "type": "JSXElement", + "span": { + "start": 200, + "end": 213, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 201, + "end": 205, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 200, + "end": 206, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 206, + "end": 213, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 208, + "end": 212, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + } + }, + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 221, + "end": 224, + "ctxt": 0 + }, + "value": "Foo", + "optional": false + }, + "declare": false, + "span": { + "start": 215, + "end": 227, + "ctxt": 0 + }, + "decorators": [], + "body": [], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + }, + { + "type": "ExpressionStatement", + "span": { + "start": 228, + "end": 262, + "ctxt": 0 + }, + "expression": { + "type": "JSXFragment", + "span": { + "start": 228, + "end": 262, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningFragment", + "span": { + "start": 228, + "end": 230, + "ctxt": 0 + } + }, + "children": [ + { + "type": "JSXText", + "span": { + "start": 230, + "end": 231, + "ctxt": 0 + }, + "value": "\n\n", + "raw": "\n\n" + }, + { + "type": "JSXElement", + "span": { + "start": 231, + "end": 244, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 232, + "end": 236, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 231, + "end": 237, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 237, + "end": 244, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 239, + "end": 243, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + }, + { + "type": "JSXText", + "span": { + "start": 244, + "end": 245, + "ctxt": 0 + }, + "value": "\n\n", + "raw": "\n\n" + }, + { + "type": "JSXElement", + "span": { + "start": 245, + "end": 258, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 246, + "end": 250, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + }, + "span": { + "start": 245, + "end": 251, + "ctxt": 0 + }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 251, + "end": 258, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 253, + "end": 257, + "ctxt": 0 + }, + "value": "Comp", + "optional": false + } + } + }, + { + "type": "JSXText", + "span": { + "start": 258, + "end": 259, + "ctxt": 0 + }, + "value": "\n\n", + "raw": "\n\n" + } + ], + "closing": { + "type": "JSXClosingFragment", + "span": { + "start": 259, + "end": 262, + "ctxt": 0 + } + } + } } ], "interpreter": null From 5a16aa936b5e18b2671d7c141bd754783c8f5288 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 5 Dec 2022 23:12:31 +0300 Subject: [PATCH 4/4] refactor: better name --- crates/swc_ecma_parser/src/lexer/state.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/state.rs b/crates/swc_ecma_parser/src/lexer/state.rs index 127161d3f329..3f588eda7686 100644 --- a/crates/swc_ecma_parser/src/lexer/state.rs +++ b/crates/swc_ecma_parser/src/lexer/state.rs @@ -24,8 +24,9 @@ pub(super) struct State { pub is_expr_allowed: bool, pub next_regexp: Option, /// if line break exists between previous token and new token? - pub prev_had_line_break: bool, 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, @@ -356,7 +357,7 @@ impl<'a, I: Input> Iterator for Lexer<'a, I> { self.state.update(start, token); self.state.prev_hi = self.last_pos(); - self.state.prev_had_line_break = self.state.had_line_break; + self.state.had_line_break_before_last = self.had_line_break_before_last(); } token.map(|token| { @@ -379,7 +380,7 @@ impl State { next_regexp: None, is_first: true, had_line_break: false, - prev_had_line_break: false, + had_line_break_before_last: false, prev_hi: start_pos, context, token_type: None, @@ -442,7 +443,7 @@ impl State { start, next, self.had_line_break, - self.prev_had_line_break, + self.had_line_break_before_last, self.is_expr_allowed, ); } @@ -456,7 +457,7 @@ impl State { start: BytePos, next: &Token, had_line_break: bool, - prev_had_line_break: bool, + had_line_break_before_last: bool, is_expr_allowed: bool, ) -> bool { let is_next_keyword = matches!(*next, Word(Word::Keyword(..))); @@ -550,7 +551,7 @@ impl State { TokenType::Keyword(Let) | TokenType::Keyword(Const) | TokenType::Keyword(Var) - if prev_had_line_break => + if had_line_break_before_last => { true }