Type-check CommentsParser and LocationParser (#484)
This commit is contained in:
parent
68967bf515
commit
34acecca2e
@ -11,3 +11,4 @@
|
||||
strip_root=true
|
||||
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
|
||||
suppress_comment= \\(.\\|\n\\)*\\$FlowIssue
|
||||
suppress_comment= \\(.\\|\n\\)*\\$FlowIgnore
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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<any>).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.
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user