From b37796c318c6c41baed9547fcf806fab58c8a624 Mon Sep 17 00:00:00 2001 From: Ika Date: Mon, 23 Jul 2018 13:31:17 +0800 Subject: [PATCH 1/2] test: add tests --- tests/fixtures/jsx/spread-child.src.js | 1 + tests/lib/__snapshots__/jsx.js.snap | 422 +++++++++++++++++++++++++ 2 files changed, 423 insertions(+) create mode 100644 tests/fixtures/jsx/spread-child.src.js diff --git a/tests/fixtures/jsx/spread-child.src.js b/tests/fixtures/jsx/spread-child.src.js new file mode 100644 index 0000000..48ce23c --- /dev/null +++ b/tests/fixtures/jsx/spread-child.src.js @@ -0,0 +1 @@ +
{...[0]}
diff --git a/tests/lib/__snapshots__/jsx.js.snap b/tests/lib/__snapshots__/jsx.js.snap index 663ef27..a366ea6 100644 --- a/tests/lib/__snapshots__/jsx.js.snap +++ b/tests/lib/__snapshots__/jsx.js.snap @@ -6824,6 +6824,428 @@ Object { exports[`JSX useJSXTextNode: false fixtures/self-closing-tag-with-newline.src 1`] = `"Invalid character."`; +exports[`JSX useJSXTextNode: false fixtures/spread-child.src 1`] = ` +Object { + "body": Array [ + Object { + "expression": Object { + "children": Array [ + Object { + "expression": Object { + "elements": Array [ + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 10, + "line": 1, + }, + }, + "range": Array [ + 10, + 11, + ], + "raw": "0", + "type": "Literal", + "value": 0, + }, + ], + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 9, + "line": 1, + }, + }, + "range": Array [ + 9, + 12, + ], + "type": "ArrayExpression", + }, + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 5, + "line": 1, + }, + }, + "range": Array [ + 5, + 13, + ], + "type": "JSXExpressionContainer", + }, + ], + "closingElement": Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 18, + "line": 1, + }, + "start": Object { + "column": 15, + "line": 1, + }, + }, + "name": "div", + "range": Array [ + 15, + 18, + ], + "type": "JSXIdentifier", + }, + "range": Array [ + 13, + 19, + ], + "type": "JSXClosingElement", + }, + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "openingElement": Object { + "attributes": Array [], + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "div", + "range": Array [ + 1, + 4, + ], + "type": "JSXIdentifier", + }, + "range": Array [ + 0, + 5, + ], + "selfClosing": false, + "type": "JSXOpeningElement", + }, + "range": Array [ + 0, + 19, + ], + "type": "JSXElement", + }, + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 19, + ], + "type": "ExpressionStatement", + }, + ], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 20, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 1, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 4, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "range": Array [ + 1, + 4, + ], + "type": "JSXIdentifier", + "value": "div", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "range": Array [ + 4, + 5, + ], + "type": "Punctuator", + "value": ">", + }, + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 5, + "line": 1, + }, + }, + "range": Array [ + 5, + 6, + ], + "type": "Punctuator", + "value": "{", + }, + Object { + "loc": Object { + "end": Object { + "column": 9, + "line": 1, + }, + "start": Object { + "column": 6, + "line": 1, + }, + }, + "range": Array [ + 6, + 9, + ], + "type": "Punctuator", + "value": "...", + }, + Object { + "loc": Object { + "end": Object { + "column": 10, + "line": 1, + }, + "start": Object { + "column": 9, + "line": 1, + }, + }, + "range": Array [ + 9, + 10, + ], + "type": "Punctuator", + "value": "[", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 10, + "line": 1, + }, + }, + "range": Array [ + 10, + 11, + ], + "type": "Numeric", + "value": "0", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 11, + "line": 1, + }, + }, + "range": Array [ + 11, + 12, + ], + "type": "Punctuator", + "value": "]", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "range": Array [ + 12, + 13, + ], + "type": "Punctuator", + "value": "}", + }, + Object { + "loc": Object { + "end": Object { + "column": 14, + "line": 1, + }, + "start": Object { + "column": 13, + "line": 1, + }, + }, + "range": Array [ + 13, + 14, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 14, + "line": 1, + }, + }, + "range": Array [ + 14, + 15, + ], + "type": "Punctuator", + "value": "/", + }, + Object { + "loc": Object { + "end": Object { + "column": 18, + "line": 1, + }, + "start": Object { + "column": 15, + "line": 1, + }, + }, + "range": Array [ + 15, + 18, + ], + "type": "JSXIdentifier", + "value": "div", + }, + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 18, + "line": 1, + }, + }, + "range": Array [ + 18, + 19, + ], + "type": "Punctuator", + "value": ">", + }, + ], + "type": "Program", +} +`; + exports[`JSX useJSXTextNode: false fixtures/spread-operator-attribute-and-regular-attribute.src 1`] = ` Object { "body": Array [ From c34f1aa4f34b0ec3cd9d282787aab1d6f6587eeb Mon Sep 17 00:00:00 2001 From: Ika Date: Mon, 23 Jul 2018 13:31:28 +0800 Subject: [PATCH 2/2] fix: support JSX spread child --- lib/ast-node-types.js | 1 + lib/convert.js | 4 +++- tests/lib/__snapshots__/jsx.js.snap | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/ast-node-types.js b/lib/ast-node-types.js index 5839455..3826a53 100644 --- a/lib/ast-node-types.js +++ b/lib/ast-node-types.js @@ -70,6 +70,7 @@ module.exports = { JSXNamespacedName: "JSXNamespacedName", JSXOpeningElement: "JSXOpeningElement", JSXSpreadAttribute: "JSXSpreadAttribute", + JSXSpreadChild: "JSXSpreadChild", JSXText: "JSXText", LabeledStatement: "LabeledStatement", Literal: "Literal", diff --git a/lib/convert.js b/lib/convert.js index f178aa7..8365a2b 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -1897,7 +1897,9 @@ module.exports = function convert(config) { }; Object.assign(result, { - type: AST_NODE_TYPES.JSXExpressionContainer, + type: node.dotDotDotToken + ? AST_NODE_TYPES.JSXSpreadChild + : AST_NODE_TYPES.JSXExpressionContainer, expression }); diff --git a/tests/lib/__snapshots__/jsx.js.snap b/tests/lib/__snapshots__/jsx.js.snap index a366ea6..0be9fe0 100644 --- a/tests/lib/__snapshots__/jsx.js.snap +++ b/tests/lib/__snapshots__/jsx.js.snap @@ -6883,7 +6883,7 @@ Object { 5, 13, ], - "type": "JSXExpressionContainer", + "type": "JSXSpreadChild", }, ], "closingElement": Object {