From 85d50f956839caca90c5f4c7bb4ec70a53119141 Mon Sep 17 00:00:00 2001 From: Felix Gnass Date: Fri, 25 May 2018 10:27:35 +0200 Subject: [PATCH] feat: support flow comment types --- src/parsers/parse_to_ast.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/parsers/parse_to_ast.js b/src/parsers/parse_to_ast.js index 1cd3ca22f..6be0c1710 100644 --- a/src/parsers/parse_to_ast.js +++ b/src/parsers/parse_to_ast.js @@ -21,6 +21,20 @@ const opts = { ] }; +/** + * Convert flow comment types into flow annotations so that + * they end up in the final AST. If the source does not contain + * a flow pragma, the code is returned verbatim. + * @param {*} source code with flow type comments + * @returns {string} code with flow annotations + */ +function commentToFlow(source) { + if (!/@flow/.test(source)) return source; + return source + .replace(/\/\*::([^]+?)\*\//g, '$1') + .replace(/\/\*:\s*([^]+?)\s*\*\//g, ':$1'); +} + export function parseToAst(source: string) { - return babylon.parse(source, opts); + return babylon.parse(commentToFlow(source), opts); }