Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: flow pragma #9885

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 53 additions & 17 deletions packages/babel-parser/src/plugins/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.getPluginOption("flow", "all") || this.flowPragma === "flow";
}

addComment(comment: N.Comment): void {
if (this.flowPragma === undefined) {
// Try to parse a flow pragma.
const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
if (!matches) {
this.flowPragma = null;
} else if (matches[1] === "flow") {
this.flowPragma = "flow";
} else if (matches[1] === "noflow") {
this.flowPragma = "noflow";
} else {
throw new Error("Unexpected flow pragma");
}
}
return super.addComment(comment);
}

flowParseTypeInitialiser(tok?: TokenType): N.FlowType {
const oldInType = this.state.inType;
this.state.inType = true;
Expand Down Expand Up @@ -2744,7 +2727,60 @@ export default (superClass: Class<Parser>): Class<Parser> =>
super.readToken_pipe_amp(code);
}

lookAheadForFlowPragma() {
// TODO: lookahead

const old = this.state;
this.state = old.clone(true);

this.isLookahead = true;
for (let i = 0; i < 10; i++) {
this.next();
console.log(this.state.type);
}
this.isLookahead = false;

const curr = this.state;
this.state = old;
return curr;

// TODO: lookahead, that includes comment token
const lookaheadTokens = this.todo();
forLoop: for (const token of lookaheadTokens) {
switch (token.type) {
case tt.string:
case tt.semi:
continue;
// TODO: no such token atm
case tt.comment: {
const flowPragma = this.getFlowPragma(token);
if (flowPragma !== null) {
this.flowPragma = flowPragma;
}
continue;
}
default:
break forLoop;
}
}
}

getFlowPragma(comment: N.Comment): void {
// Try to parse a flow pragma.
const matches = FLOW_PRAGMA_REGEX.exec(comment.value);
if (!matches) {
return null;
} else if (matches[1] === "flow") {
return "flow";
} else if (matches[1] === "noflow") {
return "noflow";
} else {
throw new Error("Unexpected flow pragma");
}
}

parseTopLevel(file: N.File, program: N.Program): N.File {
this.lookAheadForFlowPragma();
const fileNode = super.parseTopLevel(file, program);
if (this.state.hasFlowComment) {
this.unexpected(null, "Unterminated flow-comment");
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo<x>(y);
// @flow
foo<x>(y);
3 changes: 3 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/2/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
// @flow
foo<x>(y);
12 changes: 12 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/3/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'1';
'2';
'3';
'4';
'5';
'6';
'7';
'8';
'9';
'10';
// @flow
foo<x>(y);
3 changes: 3 additions & 0 deletions packages/babel-parser/test/fixtures/flow/pragma/4/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// arbitrary comment
// @flow
foo<x>(y);