Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Type-check State (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy authored and danez committed May 10, 2017
1 parent 8862c96 commit 1773ca7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/parser/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
1 change: 1 addition & 0 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
42 changes: 29 additions & 13 deletions src/tokenizer/state.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 1773ca7

Please sign in to comment.