diff --git a/packages/babel-parser/src/parser/node.js b/packages/babel-parser/src/parser/node.js index ea04076bf317..df3302e8f880 100644 --- a/packages/babel-parser/src/parser/node.js +++ b/packages/babel-parser/src/parser/node.js @@ -91,12 +91,16 @@ export class NodeUtils extends UtilParser { return node; } + resetStartLocation(node: NodeBase, start: number, startLoc: Position): void { + node.start = start; + node.loc.start = startLoc; + if (this.options.ranges) node.range[0] = start; + } + /** * Reset the start location of node to the start location of locationNode */ resetStartLocationFromNode(node: NodeBase, locationNode: NodeBase): void { - node.start = locationNode.start; - node.loc.start = locationNode.loc.start; - if (this.options.ranges) node.range[0] = locationNode.range[0]; + this.resetStartLocation(node, locationNode.start, locationNode.loc.start); } } diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 3afd59203809..3914611406e9 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -1853,10 +1853,15 @@ export default (superClass: Class): Class => } parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration { + // Store original location/position + const startPos = this.state.start; + const startLoc = this.state.startLoc; + // "export declare" is equivalent to just "export". const isDeclare = this.eatContextual("declare"); let declaration: ?N.Declaration; + if (this.match(tt.name)) { declaration = this.tsTryParseExportDeclaration(); } @@ -1865,6 +1870,9 @@ export default (superClass: Class): Class => } if (declaration && isDeclare) { + // Reset location to include `declare` in range + this.resetStartLocation(declaration, startPos, startLoc); + declaration.declare = true; } diff --git a/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json b/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json index 1c9388e7c02b..912eb1b41745 100644 --- a/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json +++ b/packages/babel-parser/test/fixtures/experimental/export-extensions/export-with-ts/output.json @@ -114,12 +114,12 @@ "source": null, "declaration": { "type": "TSInterfaceDeclaration", - "start": 42, + "start": 34, "end": 56, "loc": { "start": { "line": 2, - "column": 15 + "column": 7 }, "end": { "line": 2, diff --git a/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json b/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json index 1e1638d1caf4..d0a529fa2711 100644 --- a/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json +++ b/packages/babel-parser/test/fixtures/typescript/enum/export-declare-const/output.json @@ -47,12 +47,12 @@ "source": null, "declaration": { "type": "TSEnumDeclaration", - "start": 15, + "start": 7, "end": 30, "loc": { "start": { "line": 1, - "column": 15 + "column": 7 }, "end": { "line": 1, diff --git a/packages/babel-parser/test/fixtures/typescript/export/declare/input.js b/packages/babel-parser/test/fixtures/typescript/export/declare/input.js index 58d3f7937e9e..e269282a91de 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/declare/input.js +++ b/packages/babel-parser/test/fixtures/typescript/export/declare/input.js @@ -5,3 +5,4 @@ export declare interface I {} export declare type T = number; export declare module M {} export declare namespace N {} +export declare enum foo {} diff --git a/packages/babel-parser/test/fixtures/typescript/export/declare/output.json b/packages/babel-parser/test/fixtures/typescript/export/declare/output.json index 5d93c9e8b1c5..bd75590c3bd1 100644 --- a/packages/babel-parser/test/fixtures/typescript/export/declare/output.json +++ b/packages/babel-parser/test/fixtures/typescript/export/declare/output.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 211, + "end": 238, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 7, - "column": 29 + "line": 8, + "column": 26 } }, "program": { "type": "Program", "start": 0, - "end": 211, + "end": 238, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 7, - "column": 29 + "line": 8, + "column": 26 } }, "sourceType": "module", @@ -47,12 +47,12 @@ "source": null, "declaration": { "type": "VariableDeclaration", - "start": 15, + "start": 7, "end": 31, "loc": { "start": { "line": 1, - "column": 15 + "column": 7 }, "end": { "line": 1, @@ -146,12 +146,12 @@ "source": null, "declaration": { "type": "TSDeclareFunction", - "start": 47, + "start": 39, "end": 66, "loc": { "start": { "line": 2, - "column": 15 + "column": 7 }, "end": { "line": 2, @@ -229,12 +229,12 @@ "source": null, "declaration": { "type": "ClassDeclaration", - "start": 82, + "start": 74, "end": 92, "loc": { "start": { "line": 3, - "column": 15 + "column": 7 }, "end": { "line": 3, @@ -296,12 +296,12 @@ "source": null, "declaration": { "type": "TSInterfaceDeclaration", - "start": 108, + "start": 100, "end": 122, "loc": { "start": { "line": 4, - "column": 15 + "column": 7 }, "end": { "line": 4, @@ -362,12 +362,12 @@ "source": null, "declaration": { "type": "TSTypeAliasDeclaration", - "start": 138, + "start": 130, "end": 154, "loc": { "start": { "line": 5, - "column": 15 + "column": 7 }, "end": { "line": 5, @@ -427,12 +427,12 @@ "source": null, "declaration": { "type": "TSModuleDeclaration", - "start": 170, + "start": 162, "end": 181, "loc": { "start": { "line": 6, - "column": 15 + "column": 7 }, "end": { "line": 6, @@ -493,12 +493,12 @@ "source": null, "declaration": { "type": "TSModuleDeclaration", - "start": 197, + "start": 189, "end": 211, "loc": { "start": { "line": 7, - "column": 15 + "column": 7 }, "end": { "line": 7, @@ -540,6 +540,57 @@ }, "declare": true } + }, + { + "type": "ExportNamedDeclaration", + "start": 212, + "end": 238, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 219, + "end": 238, + "loc": { + "start": { + "line": 8, + "column": 7 + }, + "end": { + "line": 8, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 232, + "end": 235, + "loc": { + "start": { + "line": 8, + "column": 20 + }, + "end": { + "line": 8, + "column": 23 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "members": [], + "declare": true + } } ], "directives": []