Skip to content

Commit

Permalink
feat: support flow comment types
Browse files Browse the repository at this point in the history
  • Loading branch information
fgnass authored and tmcw committed May 26, 2018
1 parent 976e484 commit 85d50f9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/parsers/parse_to_ast.js
Expand Up @@ -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);
}

0 comments on commit 85d50f9

Please sign in to comment.