Skip to content

Commit 85d50f9

Browse files
fgnasstmcw
authored andcommitted
feat: support flow comment types
1 parent 976e484 commit 85d50f9

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/parsers/parse_to_ast.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ const opts = {
2121
]
2222
};
2323

24+
/**
25+
* Convert flow comment types into flow annotations so that
26+
* they end up in the final AST. If the source does not contain
27+
* a flow pragma, the code is returned verbatim.
28+
* @param {*} source code with flow type comments
29+
* @returns {string} code with flow annotations
30+
*/
31+
function commentToFlow(source) {
32+
if (!/@flow/.test(source)) return source;
33+
return source
34+
.replace(/\/\*::([^]+?)\*\//g, '$1')
35+
.replace(/\/\*:\s*([^]+?)\s*\*\//g, ':$1');
36+
}
37+
2438
export function parseToAst(source: string) {
25-
return babylon.parse(source, opts);
39+
return babylon.parse(commentToFlow(source), opts);
2640
}

0 commit comments

Comments
 (0)