Skip to content

Commit

Permalink
Handle flow comments with leading spaces (#9168)
Browse files Browse the repository at this point in the history
* check for spaces and tabs before a flow comment

* fix issue with using string index and shift interchangably

* update tests

* Use update charcodes version

* Disallow flow-comments in flow-comments and check for unterminated comments
  • Loading branch information
vikr01 authored and danez committed Dec 14, 2018
1 parent b9340bc commit 72471af
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 133 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -24,11 +24,11 @@
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.4",
"babel-plugin-transform-charcodes": "^0.1.0",
"babel-plugin-transform-charcodes": "^0.1.1",
"browserify": "^16.2.2",
"bundle-collapser": "^1.2.1",
"chalk": "^2.3.2",
"charcodes": "^0.1.0",
"charcodes": "^0.1.1",
"derequire": "^2.0.2",
"enhanced-resolve": "^3.0.0",
"eslint": "^5.9.0",
Expand Down
44 changes: 36 additions & 8 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2697,21 +2697,34 @@ export default (superClass: Class<Parser>): Class<Parser> =>
super.readToken_mult_modulo(code);
}

parseTopLevel(file: N.File, program: N.Program): N.File {
const fileNode = super.parseTopLevel(file, program);
if (this.state.hasFlowComment) {
this.unexpected(null, "Unterminated flow-comment");
}
return fileNode;
}

skipBlockComment(): void {
if (
this.hasPlugin("flow") &&
this.hasPlugin("flowComments") &&
this.skipFlowComment()
) {
if (this.state.hasFlowComment) {
this.unexpected(
null,
"Cannot have a flow comment inside another flow comment",
);
}
this.hasFlowCommentCompletion();
this.state.pos += this.skipFlowComment();
this.state.hasFlowComment = true;
return;
}
let end;
if (this.hasPlugin("flow") && this.state.hasFlowComment) {
end = this.input.indexOf("*-/", (this.state.pos += 2));
const end = this.input.indexOf("*-/", (this.state.pos += 2));
if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
this.state.pos = end + 3;
return;
Expand All @@ -2721,17 +2734,32 @@ 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);
const { pos } = this.state;
let shiftToFirstNonWhiteSpace = 2;
while (
[charCodes.space, charCodes.tab].includes(
this.input.charCodeAt(pos + shiftToFirstNonWhiteSpace),
)
) {
shiftToFirstNonWhiteSpace++;
}
const ch2 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos);
const ch3 = this.input.charCodeAt(shiftToFirstNonWhiteSpace + pos + 1);
if (ch2 === charCodes.colon && ch3 === charCodes.colon) {
return 4; // check for /*::
return shiftToFirstNonWhiteSpace + 2; // check for /*::
}
if (this.input.slice(this.state.pos + 2, 14) === "flow-include") {
return 14; // check for /*flow-include
if (
this.input.slice(
shiftToFirstNonWhiteSpace + pos,
shiftToFirstNonWhiteSpace + pos + 12,
) === "flow-include"
) {
return shiftToFirstNonWhiteSpace + 12; // check for /*flow-include
}
if (ch2 === charCodes.colon && ch3 !== charCodes.colon) {
return 2; // check for /*:, advance only 2 steps
return shiftToFirstNonWhiteSpace; // check for /*:, advance up to :
}
return false;
}
Expand Down
@@ -1,3 +1,4 @@
class MyClass {
/*:: prop: string; */
/* :: foo: number; */
}
@@ -1,28 +1,28 @@
{
"type": "File",
"start": 0,
"end": 41,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 41,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -32,14 +32,14 @@
{
"type": "ClassDeclaration",
"start": 0,
"end": 41,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -64,14 +64,14 @@
"body": {
"type": "ClassBody",
"start": 14,
"end": 41,
"end": 68,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -92,6 +92,22 @@
"column": 23
}
}
},
{
"type": "CommentBlock",
"value": " :: foo: number; ",
"start": 42,
"end": 66,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 26
}
}
}
]
}
Expand All @@ -115,6 +131,22 @@
"column": 23
}
}
},
{
"type": "CommentBlock",
"value": " :: foo: number; ",
"start": 42,
"end": 66,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 26
}
}
}
]
}
@@ -1,4 +1,4 @@
/*flow-include
/* flow-include
type Foo = {
foo: number,
bar: boolean,
Expand Down
@@ -1,7 +1,7 @@
{
"type": "File",
"start": 0,
"end": 78,
"end": 79,
"loc": {
"start": {
"line": 1,
Expand All @@ -15,7 +15,7 @@
"program": {
"type": "Program",
"start": 0,
"end": 78,
"end": 79,
"loc": {
"start": {
"line": 1,
Expand All @@ -33,9 +33,9 @@
"innerComments": [
{
"type": "CommentBlock",
"value": "flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n",
"value": " flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n",
"start": 0,
"end": 78,
"end": 79,
"loc": {
"start": {
"line": 1,
Expand All @@ -52,9 +52,9 @@
"comments": [
{
"type": "CommentBlock",
"value": "flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n",
"value": " flow-include\ntype Foo = {\n foo: number,\n bar: boolean,\n baz: string\n};\n",
"start": 0,
"end": 78,
"end": 79,
"loc": {
"start": {
"line": 1,
Expand Down
@@ -1,3 +1,4 @@
class MyClass {
/*flow-include prop: string; */
/* flow-include foo: number; */
}
@@ -1,28 +1,28 @@
{
"type": "File",
"start": 0,
"end": 51,
"end": 90,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 51,
"end": 90,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -32,14 +32,14 @@
{
"type": "ClassDeclaration",
"start": 0,
"end": 51,
"end": 90,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -64,14 +64,14 @@
"body": {
"type": "ClassBody",
"start": 14,
"end": 51,
"end": 90,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 3,
"line": 4,
"column": 1
}
},
Expand All @@ -92,6 +92,22 @@
"column": 33
}
}
},
{
"type": "CommentBlock",
"value": " flow-include foo: number; ",
"start": 52,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 38
}
}
}
]
}
Expand All @@ -115,6 +131,22 @@
"column": 33
}
}
},
{
"type": "CommentBlock",
"value": " flow-include foo: number; ",
"start": 52,
"end": 88,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 38
}
}
}
]
}
@@ -1,3 +1,4 @@
class MyClass {
/*:: prop: string; */
/* :: prop: string; */
/* :: prop2: number; */
}

0 comments on commit 72471af

Please sign in to comment.