Type-check State (#492)

This commit is contained in:
Andy 2017-05-10 12:55:09 -07:00 committed by Daniel Tschinder
parent 8862c96237
commit 1773ca7457
3 changed files with 31 additions and 14 deletions

View File

@ -29,7 +29,7 @@
import BaseParser from "./base";
import type { Comment, Node } from "../types";
function last(stack) {
function last<T>(stack: $ReadOnlyArray<T>): T {
return stack[stack.length - 1];
}

View File

@ -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) {

View File

@ -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<Object>;
labels: Array<{ kind: ?("loop" | "switch"), statementStart?: number }>;
// Leading decorators.
decorators: Array<Object>;
decorators: Array<N.Decorator>;
// Token store.
tokens: Array<Object>;
tokens: Array<Token | N.Comment>;
// Comment store.
comments: Array<Object>;
comments: Array<N.Comment>;
// Comment attachment store
trailingComments: Array<Object>;
leadingComments: Array<Object>;
commentStack: Array<Object>;
trailingComments: Array<N.Comment>;
leadingComments: Array<N.Comment>;
commentStack: Array<{
start: number;
leadingComments: ?Array<N.Comment>;
trailingComments: ?Array<N.Comment>;
}>;
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<string>;
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;