Skip to content

Commit

Permalink
Fix parsing of jsx attribute values (#616)
Browse files Browse the repository at this point in the history
Closes #614.
Closes #615.
  • Loading branch information
kdy1 committed Jan 29, 2020
1 parent e92923b commit 095a8b4
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 188 deletions.
2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swc_ecma_parser"
version = "0.17.1"
version = "0.17.2"
authors = ["강동윤 <[email protected]>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"
Expand Down
15 changes: 10 additions & 5 deletions ecmascript/parser/src/parser/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 4 additions & 7 deletions ecmascript/parser/src/parser/jsx/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
142 changes: 63 additions & 79 deletions ecmascript/parser/tests/jsx/basic/13/input.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
{
Expand All @@ -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
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions ecmascript/parser/tests/jsx/basic/18/input.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
],
Expand Down
32 changes: 8 additions & 24 deletions ecmascript/parser/tests/jsx/basic/19/input.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
},
{
Expand All @@ -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
}
},
{
Expand Down
16 changes: 4 additions & 12 deletions ecmascript/parser/tests/jsx/basic/3/input.js.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
],
Expand Down
Loading

0 comments on commit 095a8b4

Please sign in to comment.