From 3199ceecdbc04b5c50e102155bd9f2baf251b16b Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 25 Apr 2017 14:54:47 -0700 Subject: [PATCH] Type-check node.js (#486) --- src/parser/node.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/parser/node.js b/src/parser/node.js index dafe10af9b..cc8f514366 100644 --- a/src/parser/node.js +++ b/src/parser/node.js @@ -1,13 +1,16 @@ +// @flow + import Parser from "./index"; import UtilParser from "./util"; import { SourceLocation, type Position } from "../util/location"; +import type { Comment, Node as NodeType, NodeBase } from "../types"; // Start an AST node, attaching a start offset. const commentKeys = ["leadingComments", "trailingComments", "innerComments"]; -class Node { - constructor(parser?: Parser, pos?: number, loc?: Position) { +class Node implements NodeBase { + constructor(parser: Parser, pos: number, loc: Position) { this.type = ""; this.start = pos; this.end = 0; @@ -17,15 +20,22 @@ class Node { } type: string; - start: ?number; + start: number; end: number; loc: SourceLocation; + range: [number, number]; + leadingComments: ?Array; + trailingComments: ?Array; + innerComments: ?Array; + extra: { [key: string]: any }; - __clone(): Node { - const node2 = new Node; + __clone(): this { + // $FlowIgnore + const node2: any = new Node; for (const key in this) { // Do not clone comments that are already attached to the node if (commentKeys.indexOf(key) < 0) { + // $FlowIgnore node2[key] = this[key]; } } @@ -35,23 +45,25 @@ class Node { } export class NodeUtils extends UtilParser { - startNode() { + startNode(): T { + // $FlowIgnore return new Node(this, this.state.start, this.state.startLoc); } - startNodeAt(pos, loc) { + startNodeAt(pos: number, loc: Position): T { + // $FlowIgnore return new Node(this, pos, loc); } // Finish an AST node, adding `type` and `end` properties. - finishNode(node, type) { + finishNode(node: T, type: string): T { return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc); } // Finish node at given position - finishNodeAt(node, type, pos, loc) { + finishNodeAt(node: T, type: string, pos: number, loc: Position): T { node.type = type; node.end = pos; node.loc.end = loc; @@ -63,11 +75,9 @@ export class NodeUtils extends UtilParser { /** * Reset the start location of node to the start location of locationNode */ - resetStartLocationFromNode(node, 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]; - - return node; } }