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; 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 * Reset the start location of node to the start location of locationNode
*/ */
resetStartLocationFromNode(node: NodeBase, locationNode: NodeBase): void { resetStartLocationFromNode(node: NodeBase, locationNode: NodeBase): void {
node.start = locationNode.start; this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
node.loc.start = locationNode.loc.start;
if (this.options.ranges) node.range[0] = locationNode.range[0];
} }
} }

View File

@ -1853,10 +1853,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration { 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". // "export declare" is equivalent to just "export".
const isDeclare = this.eatContextual("declare"); const isDeclare = this.eatContextual("declare");
let declaration: ?N.Declaration; let declaration: ?N.Declaration;
if (this.match(tt.name)) { if (this.match(tt.name)) {
declaration = this.tsTryParseExportDeclaration(); declaration = this.tsTryParseExportDeclaration();
} }
@ -1865,6 +1870,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} }
if (declaration && isDeclare) { if (declaration && isDeclare) {
// Reset location to include `declare` in range
this.resetStartLocation(declaration, startPos, startLoc);
declaration.declare = true; declaration.declare = true;
} }

View File

@ -114,12 +114,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSInterfaceDeclaration", "type": "TSInterfaceDeclaration",
"start": 42, "start": 34,
"end": 56, "end": 56,
"loc": { "loc": {
"start": { "start": {
"line": 2, "line": 2,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 2, "line": 2,

View File

@ -47,12 +47,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSEnumDeclaration", "type": "TSEnumDeclaration",
"start": 15, "start": 7,
"end": 30, "end": 30,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 1, "line": 1,

View File

@ -5,3 +5,4 @@ export declare interface I {}
export declare type T = number; export declare type T = number;
export declare module M {} export declare module M {}
export declare namespace N {} export declare namespace N {}
export declare enum foo {}

View File

@ -1,29 +1,29 @@
{ {
"type": "File", "type": "File",
"start": 0, "start": 0,
"end": 211, "end": 238,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 7, "line": 8,
"column": 29 "column": 26
} }
}, },
"program": { "program": {
"type": "Program", "type": "Program",
"start": 0, "start": 0,
"end": 211, "end": 238,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 0 "column": 0
}, },
"end": { "end": {
"line": 7, "line": 8,
"column": 29 "column": 26
} }
}, },
"sourceType": "module", "sourceType": "module",
@ -47,12 +47,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "VariableDeclaration", "type": "VariableDeclaration",
"start": 15, "start": 7,
"end": 31, "end": 31,
"loc": { "loc": {
"start": { "start": {
"line": 1, "line": 1,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 1, "line": 1,
@ -146,12 +146,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSDeclareFunction", "type": "TSDeclareFunction",
"start": 47, "start": 39,
"end": 66, "end": 66,
"loc": { "loc": {
"start": { "start": {
"line": 2, "line": 2,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 2, "line": 2,
@ -229,12 +229,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "ClassDeclaration", "type": "ClassDeclaration",
"start": 82, "start": 74,
"end": 92, "end": 92,
"loc": { "loc": {
"start": { "start": {
"line": 3, "line": 3,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 3, "line": 3,
@ -296,12 +296,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSInterfaceDeclaration", "type": "TSInterfaceDeclaration",
"start": 108, "start": 100,
"end": 122, "end": 122,
"loc": { "loc": {
"start": { "start": {
"line": 4, "line": 4,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 4, "line": 4,
@ -362,12 +362,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSTypeAliasDeclaration", "type": "TSTypeAliasDeclaration",
"start": 138, "start": 130,
"end": 154, "end": 154,
"loc": { "loc": {
"start": { "start": {
"line": 5, "line": 5,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 5, "line": 5,
@ -427,12 +427,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSModuleDeclaration", "type": "TSModuleDeclaration",
"start": 170, "start": 162,
"end": 181, "end": 181,
"loc": { "loc": {
"start": { "start": {
"line": 6, "line": 6,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 6, "line": 6,
@ -493,12 +493,12 @@
"source": null, "source": null,
"declaration": { "declaration": {
"type": "TSModuleDeclaration", "type": "TSModuleDeclaration",
"start": 197, "start": 189,
"end": 211, "end": 211,
"loc": { "loc": {
"start": { "start": {
"line": 7, "line": 7,
"column": 15 "column": 7
}, },
"end": { "end": {
"line": 7, "line": 7,
@ -540,6 +540,57 @@
}, },
"declare": true "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": [] "directives": []