From 34acecca2e72dbbeb3b0964114cf0b76a5e038dc Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 25 Apr 2017 13:07:01 -0700 Subject: [PATCH] Type-check CommentsParser and LocationParser (#484) --- .flowconfig | 1 + src/parser/base.js | 7 +++++++ src/parser/comments.js | 13 +++++++------ src/parser/location.js | 7 +++++-- src/tokenizer/index.js | 2 -- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.flowconfig b/.flowconfig index ec70beff84..3dccffe4e4 100644 --- a/.flowconfig +++ b/.flowconfig @@ -11,3 +11,4 @@ strip_root=true suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe suppress_comment= \\(.\\|\n\\)*\\$FlowIssue +suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore diff --git a/src/parser/base.js b/src/parser/base.js index fa1fc13539..3795b71668 100644 --- a/src/parser/base.js +++ b/src/parser/base.js @@ -3,6 +3,8 @@ import type { Options } from "../options"; import { reservedWords } from "../util/identifier"; +import type State from "../tokenizer/state"; + export default class BaseParser { // Properties set by constructor in index.js options: Options; @@ -10,6 +12,11 @@ export default class BaseParser { plugins: { [key: string]: boolean }; filename: ?string; + // Initialized by Tokenizer + state: State; + input: string; + + isReservedWord(word: string): boolean { if (word === "await") { return this.inModule; diff --git a/src/parser/comments.js b/src/parser/comments.js index a634491848..c39f91e44a 100644 --- a/src/parser/comments.js +++ b/src/parser/comments.js @@ -1,5 +1,7 @@ /* eslint max-len: 0 */ +// @flow + /** * Based on the comment attachment algorithm used in espree and estraverse. * @@ -25,19 +27,20 @@ */ import BaseParser from "./base"; +import type { Comment, Node } from "../types"; function last(stack) { return stack[stack.length - 1]; } export default class CommentsParser extends BaseParser { - addComment(comment) { + addComment(comment: Comment): void { if (this.filename) comment.loc.filename = this.filename; this.state.trailingComments.push(comment); this.state.leadingComments.push(comment); } - processComment(node) { + processComment(node: Node): void { if (node.type === "Program" && node.body.length > 0) return; const stack = this.state.commentStack; @@ -127,10 +130,8 @@ export default class CommentsParser extends BaseParser { // that comes after the node. Keep in mind that this could // result in an empty array, and if so, the array must be // deleted. - node.leadingComments = this.state.leadingComments.slice(0, i); - if ((node.leadingComments: Array).length === 0) { - node.leadingComments = null; - } + const leadingComments = this.state.leadingComments.slice(0, i); + node.leadingComments = leadingComments.length === 0 ? null : leadingComments; // Similarly, trailing comments are attached later. The variable // must be reset to null if there are no trailing comments. diff --git a/src/parser/location.js b/src/parser/location.js index 1562f52fb6..aed2760d62 100644 --- a/src/parser/location.js +++ b/src/parser/location.js @@ -1,3 +1,5 @@ +// @flow + import { getLineInfo } from "../util/location"; import CommentsParser from "./comments"; @@ -8,10 +10,11 @@ import CommentsParser from "./comments"; // message. export default class LocationParser extends CommentsParser { - raise(pos, message) { + raise(pos: number, message: string): empty { const loc = getLineInfo(this.input, pos); message += ` (${loc.line}:${loc.column})`; - const err = new SyntaxError(message); + // $FlowIgnore + const err: SyntaxError & { pos: number, loc: Position } = new SyntaxError(message); err.pos = pos; err.loc = loc; throw err; diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 2303175c18..fde3448c0d 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -49,9 +49,7 @@ export default class Tokenizer extends LocationParser { // parser/util.js +unexpected: (pos?: ?number, messageOrType?: string | TokenType) => empty; - state: State; isLookahead: boolean; - input: string; constructor(options: Options, input: string) { super();