diff --git a/src/plugins/estree.js b/src/plugins/estree.js index 5a78438108..3502e311bf 100644 --- a/src/plugins/estree.js +++ b/src/plugins/estree.js @@ -18,11 +18,7 @@ pp.estreeParseRegExpLiteral = function ({ pattern, flags }) { }; pp.estreeParseLiteral = function (value) { - const node = this.parseLiteral(value, "Literal"); - node.raw = node.extra.raw; - delete node.extra; - - return node; + return this.parseLiteral(value, "Literal"); }; pp.directiveToStmt = function (directive) { @@ -177,6 +173,16 @@ export default function (instance) { }; }); + instance.extend("parseLiteral", function(inner) { + return function (...args) { + const node = inner.call(this, ...args); + node.raw = node.extra.raw; + delete node.extra; + + return node; + }; + }); + instance.extend("parseMethod", function(inner) { return function (node, ...args) { let funcNode = this.startNode(); diff --git a/test/fixtures/estree/flow/string-literal-annotation/actual.js b/test/fixtures/estree/flow/string-literal-annotation/actual.js new file mode 100644 index 0000000000..c0a3b807b1 --- /dev/null +++ b/test/fixtures/estree/flow/string-literal-annotation/actual.js @@ -0,0 +1 @@ +var x: "string"; diff --git a/test/fixtures/estree/flow/string-literal-annotation/expected.json b/test/fixtures/estree/flow/string-literal-annotation/expected.json new file mode 100644 index 0000000000..9d301570c3 --- /dev/null +++ b/test/fixtures/estree/flow/string-literal-annotation/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 5, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeAnnotation": { + "type": "StringLiteralTypeAnnotation", + "start": 7, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "value": "string", + "raw": "\"string\"" + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + } +} \ No newline at end of file