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