From a94e43f67a00c1a45b359eff503b2b8bc4a783e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 9 Dec 2021 13:36:25 -0500 Subject: [PATCH] maintain estree string literal shape when cloned --- packages/babel-parser/src/parser/node.js | 9 +++- .../module-string-names/mixed/options.json | 3 -- .../estree/module-string-names/options.json | 4 ++ .../module-string-names/shorthand/input.js | 1 + .../module-string-names/shorthand/output.json | 42 +++++++++++++++++++ 5 files changed, 54 insertions(+), 5 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/estree/module-string-names/mixed/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/module-string-names/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/output.json diff --git a/packages/babel-parser/src/parser/node.js b/packages/babel-parser/src/parser/node.js index 1d8a08b90a0d..de4a7da943ba 100644 --- a/packages/babel-parser/src/parser/node.js +++ b/packages/babel-parser/src/parser/node.js @@ -79,12 +79,17 @@ export function cloneStringLiteral(node: any): any { return clonePlaceholder(node); } const cloned = Object.create(NodePrototype); - cloned.type = "StringLiteral"; + cloned.type = type; cloned.start = start; cloned.end = end; cloned.loc = loc; cloned.range = range; - cloned.extra = extra; + if (node.raw !== undefined) { + // estree set node.raw instead of node.extra + cloned.raw = node.raw; + } else { + cloned.extra = extra; + } cloned.value = node.value; return cloned; } diff --git a/packages/babel-parser/test/fixtures/estree/module-string-names/mixed/options.json b/packages/babel-parser/test/fixtures/estree/module-string-names/mixed/options.json deleted file mode 100644 index 2104ca43283f..000000000000 --- a/packages/babel-parser/test/fixtures/estree/module-string-names/mixed/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "sourceType": "module" -} diff --git a/packages/babel-parser/test/fixtures/estree/module-string-names/options.json b/packages/babel-parser/test/fixtures/estree/module-string-names/options.json new file mode 100644 index 000000000000..5d922f20985e --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/module-string-names/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["flow", "jsx", "estree"] +} diff --git a/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/input.js b/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/input.js new file mode 100644 index 000000000000..16354b710dc8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/input.js @@ -0,0 +1 @@ +export { "foo" } from "module-a"; diff --git a/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/output.json b/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/output.json new file mode 100644 index 000000000000..faf5d03cf814 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/module-string-names/shorthand/output.json @@ -0,0 +1,42 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":33}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}}, + "local": { + "type": "Literal", + "start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}}, + "value": "foo", + "raw": "\"foo\"" + }, + "exported": { + "type": "Literal", + "start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}}, + "raw": "\"foo\"", + "value": "foo" + } + } + ], + "source": { + "type": "Literal", + "start":22,"end":32,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":32}}, + "value": "module-a", + "raw": "\"module-a\"" + }, + "declaration": null, + "exportKind": "value" + } + ] + } +} \ No newline at end of file