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

[parser] Handle flow comments with leading spaces #9168

Merged
merged 5 commits into from Dec 14, 2018
Merged
Changes from 1 commit
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
26 changes: 20 additions & 6 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -10,6 +10,8 @@ import { types as tc } from "../tokenizer/context";
import * as charCodes from "charcodes";
import { isIteratorStart } from "../util/identifier";

const charCodeTab = 9;

danez marked this conversation as resolved.
Show resolved Hide resolved
const reservedTypes = [
"any",
"bool",
Expand Down Expand Up @@ -2721,17 +2723,29 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

skipFlowComment(): number | boolean {
const ch2 = this.input.charCodeAt(this.state.pos + 2);
const ch3 = this.input.charCodeAt(this.state.pos + 3);
let firstNonWhiteSpace = this.state.pos + 2;
while (
[charCodes.space, charCodeTab].includes(
this.input.charCodeAt(firstNonWhiteSpace),
)
) {
firstNonWhiteSpace++;
}

const ch2 = this.input.charCodeAt(firstNonWhiteSpace);
const ch3 = this.input.charCodeAt(firstNonWhiteSpace + 1);

if (ch2 === charCodes.colon && ch3 === charCodes.colon) {
return 4; // check for /*::
return firstNonWhiteSpace + 2; // check for /*::
danez marked this conversation as resolved.
Show resolved Hide resolved
}
if (this.input.slice(this.state.pos + 2, 14) === "flow-include") {
danez marked this conversation as resolved.
Show resolved Hide resolved
return 14; // check for /*flow-include
if (
this.input.slice(firstNonWhiteSpace, firstNonWhiteSpace + 12) ===
"flow-include"
) {
return firstNonWhiteSpace + 12; // check for /*flow-include
}
if (ch2 === charCodes.colon && ch3 !== charCodes.colon) {
return 2; // check for /*:, advance only 2 steps
return firstNonWhiteSpace - this.state.pos; // check for /*:, advance up to :
}
return false;
}
Expand Down