diff --git a/ecmascript/parser/Cargo.toml b/ecmascript/parser/Cargo.toml index e6b1717f2ff17..1ce167fd4073c 100644 --- a/ecmascript/parser/Cargo.toml +++ b/ecmascript/parser/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "swc_ecma_parser" -version = "0.17.1" +version = "0.17.2" authors = ["강동윤 "] license = "Apache-2.0/MIT" repository = "https://github.com/swc-project/swc.git" diff --git a/ecmascript/parser/src/parser/jsx.rs b/ecmascript/parser/src/parser/jsx.rs index 1d0e9c9d7ac66..cee951a1ee6b1 100644 --- a/ecmascript/parser/src/parser/jsx.rs +++ b/ecmascript/parser/src/parser/jsx.rs @@ -85,11 +85,16 @@ impl<'a, I: Tokens> Parser<'a, I> { JSXExpr::Expr(..) => Ok(node.into()), } } - Token::Str { .. } | Token::JSXTagStart => { - Ok(JSXAttrValue::JSXExprContainer(JSXExprContainer { - span: span!(start), - expr: JSXExpr::Expr(self.parse_lhs_expr()?), - })) + Token::Str { .. } => { + let lit = self.parse_lit()?; + Ok(JSXAttrValue::Lit(lit)) + } + Token::JSXTagStart => { + let expr = self.parse_jsx_element()?; + match expr { + Either::Left(n) => Ok(JSXAttrValue::JSXFragment(n)), + Either::Right(n) => Ok(JSXAttrValue::JSXElement(Box::new(n))), + } } _ => syntax_error!(span!(start), SyntaxError::InvalidJSXValue), diff --git a/ecmascript/parser/src/parser/jsx/tests.rs b/ecmascript/parser/src/parser/jsx/tests.rs index 8615a67de1656..efc81c8661546 100644 --- a/ecmascript/parser/src/parser/jsx/tests.rs +++ b/ecmascript/parser/src/parser/jsx/tests.rs @@ -74,14 +74,11 @@ fn escape_in_attr() { attrs: vec![JSXAttrOrSpread::JSXAttr(JSXAttr { span, name: JSXAttrName::Ident(Ident::new("id".into(), span)), - value: Some(JSXAttrValue::JSXExprContainer(JSXExprContainer { + value: Some(JSXAttrValue::Lit(Lit::Str(Str { span, - expr: JSXExpr::Expr(box Expr::Lit(Lit::Str(Str { - span, - value: "w < w".into(), - has_escape: false, - }))) - })), + value: "w < w".into(), + has_escape: false, + }))), })], name: JSXElementName::Ident(Ident::new("div".into(), span)), self_closing: true, diff --git a/ecmascript/parser/tests/jsx/basic/13/input.js.json b/ecmascript/parser/tests/jsx/basic/13/input.js.json index eb498663af082..04113c1b4bb4e 100644 --- a/ecmascript/parser/tests/jsx/basic/13/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/13/input.js.json @@ -58,44 +58,36 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "JSXElement", "span": { "start": 16, - "end": 16, + "end": 21, "ctxt": 0 }, - "expression": { - "type": "JSXElement", + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 17, + "end": 18, + "ctxt": 0 + }, + "value": "a", + "typeAnnotation": null, + "optional": false + }, "span": { "start": 16, "end": 21, "ctxt": 0 }, - "opening": { - "type": "JSXOpeningElement", - "name": { - "type": "Identifier", - "span": { - "start": 17, - "end": 18, - "ctxt": 0 - }, - "value": "a", - "typeAnnotation": null, - "optional": false - }, - "span": { - "start": 16, - "end": 21, - "ctxt": 0 - }, - "attributes": [], - "selfClosing": true, - "typeArguments": null - }, - "children": [], - "closing": null - } + "attributes": [], + "selfClosing": true, + "typeArguments": null + }, + "children": [], + "closing": null } }, { @@ -117,71 +109,63 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "JSXElement", "span": { "start": 28, - "end": 28, + "end": 54, "ctxt": 0 }, - "expression": { - "type": "JSXElement", + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 29, + "end": 30, + "ctxt": 0 + }, + "value": "b", + "typeAnnotation": null, + "optional": false + }, "span": { "start": 28, - "end": 54, + "end": 31, "ctxt": 0 }, - "opening": { - "type": "JSXOpeningElement", - "name": { - "type": "Identifier", - "span": { - "start": 29, - "end": 30, - "ctxt": 0 - }, - "value": "b", - "typeAnnotation": null, - "optional": false - }, + "attributes": [], + "selfClosing": false, + "typeArguments": null + }, + "children": [ + { + "type": "JSXText", "span": { - "start": 28, - "end": 31, + "start": 31, + "end": 50, "ctxt": 0 }, - "attributes": [], - "selfClosing": false, - "typeArguments": null + "value": "monkeys /> gorillas", + "raw": "monkeys /> gorillas" + } + ], + "closing": { + "type": "JSXClosingElement", + "span": { + "start": 50, + "end": 54, + "ctxt": 0 }, - "children": [ - { - "type": "JSXText", - "span": { - "start": 31, - "end": 50, - "ctxt": 0 - }, - "value": "monkeys /> gorillas", - "raw": "monkeys /> gorillas" - } - ], - "closing": { - "type": "JSXClosingElement", + "name": { + "type": "Identifier", "span": { - "start": 50, - "end": 54, + "start": 52, + "end": 53, "ctxt": 0 }, - "name": { - "type": "Identifier", - "span": { - "start": 52, - "end": 53, - "ctxt": 0 - }, - "value": "b", - "typeAnnotation": null, - "optional": false - } + "value": "b", + "typeAnnotation": null, + "optional": false } } } diff --git a/ecmascript/parser/tests/jsx/basic/18/input.js.json b/ecmascript/parser/tests/jsx/basic/18/input.js.json index 4ead10b9b13f5..e4605ef2f4fc3 100644 --- a/ecmascript/parser/tests/jsx/basic/18/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/18/input.js.json @@ -77,22 +77,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 21, - "end": 21, + "end": 32, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 21, - "end": 32, - "ctxt": 0 - }, - "value": "attribute", - "hasEscape": false - } + "value": "attribute", + "hasEscape": false } } ], diff --git a/ecmascript/parser/tests/jsx/basic/19/input.js.json b/ecmascript/parser/tests/jsx/basic/19/input.js.json index 771edafc656a9..ba17fc823507b 100644 --- a/ecmascript/parser/tests/jsx/basic/19/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/19/input.js.json @@ -58,22 +58,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 9, - "end": 9, + "end": 18, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 9, - "end": 18, - "ctxt": 0 - }, - "value": "leading", - "hasEscape": false - } + "value": "leading", + "hasEscape": false } }, { @@ -95,22 +87,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 24, - "end": 24, + "end": 35, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 24, - "end": 35, - "ctxt": 0 - }, - "value": "attribute", - "hasEscape": false - } + "value": "attribute", + "hasEscape": false } }, { diff --git a/ecmascript/parser/tests/jsx/basic/3/input.js.json b/ecmascript/parser/tests/jsx/basic/3/input.js.json index 0f3c91d65fc22..5e028068da504 100644 --- a/ecmascript/parser/tests/jsx/basic/3/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/3/input.js.json @@ -72,22 +72,14 @@ } }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 9, - "end": 9, + "end": 14, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 9, - "end": 14, - "ctxt": 0 - }, - "value": "bar", - "hasEscape": false - } + "value": "bar", + "hasEscape": false } } ], diff --git a/ecmascript/parser/tests/jsx/basic/4/input.js.json b/ecmascript/parser/tests/jsx/basic/4/input.js.json index 8806395caaa97..25c43ba8536a1 100644 --- a/ecmascript/parser/tests/jsx/basic/4/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/4/input.js.json @@ -95,22 +95,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 13, - "end": 13, + "end": 16, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 13, - "end": 16, - "ctxt": 0 - }, - "value": " ", - "hasEscape": false - } + "value": " ", + "hasEscape": false } }, { @@ -132,22 +124,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 19, - "end": 19, + "end": 26, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 19, - "end": 26, - "ctxt": 0 - }, - "value": "&", - "hasEscape": false - } + "value": "&", + "hasEscape": false } }, { @@ -169,22 +153,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 29, - "end": 29, + "end": 37, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 29, - "end": 37, - "ctxt": 0 - }, - "value": "&r;", - "hasEscape": false - } + "value": "&r;", + "hasEscape": false } } ], diff --git a/ecmascript/parser/tests/jsx/basic/7/input.js.json b/ecmascript/parser/tests/jsx/basic/7/input.js.json index d5b82c69150e2..b33caa384604f 100644 --- a/ecmascript/parser/tests/jsx/basic/7/input.js.json +++ b/ecmascript/parser/tests/jsx/basic/7/input.js.json @@ -58,22 +58,14 @@ "optional": false }, "value": { - "type": "JSXExpressionContainer", + "type": "StringLiteral", "span": { "start": 16, - "end": 16, + "end": 31, "ctxt": 0 }, - "expression": { - "type": "StringLiteral", - "span": { - "start": 16, - "end": 31, - "ctxt": 0 - }, - "value": "&&", - "hasEscape": false - } + "value": "&&", + "hasEscape": false } } ], diff --git a/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js b/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js new file mode 100644 index 0000000000000..54880f342e6a5 --- /dev/null +++ b/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js @@ -0,0 +1,2 @@ +const t = + diff --git a/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js.json b/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js.json new file mode 100644 index 0000000000000..beeb50446f278 --- /dev/null +++ b/ecmascript/parser/tests/jsx/basic/custom/issue-614/input.js.json @@ -0,0 +1,141 @@ +{ + "type": "Module", + "span": { + "start": 0, + "end": 42, + "ctxt": 0 + }, + "body": [ + { + "type": "VariableDeclaration", + "span": { + "start": 0, + "end": 42, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 6, + "end": 42, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 6, + "end": 7, + "ctxt": 0 + }, + "value": "t", + "typeAnnotation": null, + "optional": false + }, + "init": { + "type": "JSXElement", + "span": { + "start": 10, + "end": 42, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 11, + "end": 15, + "ctxt": 0 + }, + "value": "test", + "typeAnnotation": null, + "optional": false + }, + "span": { + "start": 10, + "end": 42, + "ctxt": 0 + }, + "attributes": [ + { + "type": "JSXAttribute", + "span": { + "start": 16, + "end": 27, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 16, + "end": 20, + "ctxt": 0 + }, + "value": "test", + "typeAnnotation": null, + "optional": false + }, + "value": { + "type": "StringLiteral", + "span": { + "start": 24, + "end": 27, + "ctxt": 0 + }, + "value": "5", + "hasEscape": false + } + }, + { + "type": "JSXAttribute", + "span": { + "start": 28, + "end": 40, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 28, + "end": 33, + "ctxt": 0 + }, + "value": "other", + "typeAnnotation": null, + "optional": false + }, + "value": { + "type": "JSXExpressionContainer", + "span": { + "start": 37, + "end": 40, + "ctxt": 0 + }, + "expression": { + "type": "NumericLiteral", + "span": { + "start": 38, + "end": 39, + "ctxt": 0 + }, + "value": 4.0 + } + } + } + ], + "selfClosing": true, + "typeArguments": null + }, + "children": [], + "closing": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js b/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js new file mode 100644 index 0000000000000..6d74e0b80f8dd --- /dev/null +++ b/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js @@ -0,0 +1 @@ +let c = ; diff --git a/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js.json b/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js.json new file mode 100644 index 0000000000000..53df7458b21b4 --- /dev/null +++ b/ecmascript/parser/tests/jsx/basic/custom/issue-615/input.js.json @@ -0,0 +1,105 @@ +{ + "type": "Module", + "span": { + "start": 0, + "end": 28, + "ctxt": 0 + }, + "body": [ + { + "type": "VariableDeclaration", + "span": { + "start": 0, + "end": 28, + "ctxt": 0 + }, + "kind": "let", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 4, + "end": 27, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 4, + "end": 5, + "ctxt": 0 + }, + "value": "c", + "typeAnnotation": null, + "optional": false + }, + "init": { + "type": "JSXElement", + "span": { + "start": 8, + "end": 27, + "ctxt": 0 + }, + "opening": { + "type": "JSXOpeningElement", + "name": { + "type": "Identifier", + "span": { + "start": 9, + "end": 13, + "ctxt": 0 + }, + "value": "Test", + "typeAnnotation": null, + "optional": false + }, + "span": { + "start": 8, + "end": 27, + "ctxt": 0 + }, + "attributes": [ + { + "type": "JSXAttribute", + "span": { + "start": 14, + "end": 24, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 14, + "end": 20, + "ctxt": 0 + }, + "value": "attrib", + "typeAnnotation": null, + "optional": false + }, + "value": { + "type": "StringLiteral", + "span": { + "start": 21, + "end": 24, + "ctxt": 0 + }, + "value": "5", + "hasEscape": false + } + } + ], + "selfClosing": true, + "typeArguments": null + }, + "children": [], + "closing": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +} diff --git a/ecmascript/transforms/tests/es2015_function_name.rs b/ecmascript/transforms/tests/es2015_function_name.rs index 8b36fdebd3249..42caa3b9d2bd4 100644 --- a/ecmascript/transforms/tests/es2015_function_name.rs +++ b/ecmascript/transforms/tests/es2015_function_name.rs @@ -145,10 +145,10 @@ test!( //identical!( // issue_288_02, // "function components_Link_extends() { -// components_Link_extends = Object.assign || function (target) { for (var i = 1; i < \ -// arguments.length; i++) { var source = arguments[i]; for (var key in source) { if \ -// (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } \ -// return target; }; +// components_Link_extends = Object.assign || function (target) { for (var +// i = 1; i < \ arguments.length; i++) { var source = arguments[i]; for (var +// key in source) { if \ (Object.prototype.hasOwnProperty.call(source, key)) +// { target[key] = source[key]; } } } \ return target; }; // return components_Link_extends.apply(this, arguments); }" //);