Fix location/range on TypeScript ExportNamedDeclarations (#9406)

This commit is contained in:
Brian Ng 2019-01-24 19:44:22 -06:00 committed by GitHub
parent 854313a759
commit 7dc157f9be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 27 deletions

View File

@ -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);
}
}

View File

@ -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();
}
@ -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;
}

View File

@ -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,

View File

@ -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,

View File

@ -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 {}

View File

@ -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": []