Skip to content

Commit

Permalink
Fix location/range on TypeScript ExportNamedDeclarations (#9406)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Jan 25, 2019
1 parent 854313a commit 7dc157f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 27 deletions.
10 changes: 7 additions & 3 deletions packages/babel-parser/src/parser/node.js
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions packages/babel-parser/src/plugins/typescript.js
Expand Up @@ -1853,10 +1853,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

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();
}
Expand All @@ -1865,6 +1870,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

if (declaration && isDeclare) {
// Reset location to include `declare` in range
this.resetStartLocation(declaration, startPos, startLoc);

declaration.declare = true;
}

Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -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,
Expand Down
Expand Up @@ -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 {}
@@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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": []
Expand Down

0 comments on commit 7dc157f

Please sign in to comment.