From 1773ca74578b44200ff754534299a7fd1962adbd Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 10 May 2017 12:55:09 -0700 Subject: [PATCH] Type-check State (#492) --- src/parser/comments.js | 2 +- src/tokenizer/index.js | 1 + src/tokenizer/state.js | 42 +++++++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/parser/comments.js b/src/parser/comments.js index c39f91e44a..96e339389b 100644 --- a/src/parser/comments.js +++ b/src/parser/comments.js @@ -29,7 +29,7 @@ import BaseParser from "./base"; import type { Comment, Node } from "../types"; -function last(stack) { +function last(stack: $ReadOnlyArray): T { return stack[stack.length - 1]; } diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 34f75fa490..42273d664c 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -626,6 +626,7 @@ export default class Tokenizer extends LocationParser { code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid); ++this.state.pos; if (code === null) { + // $FlowFixMe (is this always non-null?) --this.state.invalidTemplateEscapePosition; // to point to the '\'' instead of the 'u' } else if (code > 0x10FFFF) { if (throwOnInvalid) { diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 1bbfbda23d..3200c4e646 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -1,11 +1,17 @@ +// @flow + +import type { Options } from "../options"; +import * as N from "../types"; + import type { TokContext } from "./context"; +import type { Token } from "./index"; import type { TokenType } from "./types"; import { Position } from "../util/location"; import { types as ct } from "./context"; import { types as tt } from "./types"; export default class State { - init(options: Object, input: string) { + init(options: Options, input: string): void { this.strict = options.strictMode === false ? false : options.sourceType === "module"; this.input = input; @@ -70,28 +76,34 @@ export default class State { // Flags to track whether we are in a function, a generator. inFunction: boolean; inGenerator: boolean; - inMethod: boolean; + inMethod: boolean | N.MethodKind; inAsync: boolean; inType: boolean; + noAnonFunctionType: boolean; inPropertyName: boolean; inClassProperty: boolean; // Labels in scope. - labels: Array; + labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>; // Leading decorators. - decorators: Array; + decorators: Array; // Token store. - tokens: Array; + tokens: Array; // Comment store. - comments: Array; + comments: Array; // Comment attachment store - trailingComments: Array; - leadingComments: Array; - commentStack: Array; + trailingComments: Array; + leadingComments: Array; + commentStack: Array<{ + start: number; + leadingComments: ?Array; + trailingComments: ?Array; + }>; + commentPreviousNode: N.Node; // The current position of the tokenizer in the input. pos: number; @@ -115,8 +127,8 @@ export default class State { endLoc: Position; // Position information for the previous token - lastTokEndLoc: ?Position; - lastTokStartLoc: ?Position; + lastTokEndLoc: Position; + lastTokStartLoc: Position; lastTokStart: number; lastTokEnd: number; @@ -139,19 +151,23 @@ export default class State { // `export default foo;` and `export { foo as default };`. exportedIdentifiers: Array; - curPosition() { + invalidTemplateEscapePosition: ?number; + + curPosition(): Position { return new Position(this.curLine, this.pos - this.lineStart); } - clone(skipArrays?) { + clone(skipArrays?: boolean): State { const state = new State; for (const key in this) { + // $FlowIgnore let val = this[key]; if ((!skipArrays || key === "context") && Array.isArray(val)) { val = val.slice(); } + // $FlowIgnore state[key] = val; } return state;