diff --git a/code/lib/csf-tools/src/babelParse.ts b/code/lib/csf-tools/src/babelParse.ts index 3c6e55622756..636bb3af07bd 100644 --- a/code/lib/csf-tools/src/babelParse.ts +++ b/code/lib/csf-tools/src/babelParse.ts @@ -1,11 +1,28 @@ -import * as parser from '@babel/parser'; +import * as babelParser from '@babel/parser'; import * as recast from 'recast'; import type { ParserOptions } from '@babel/parser'; +function parseWithFlowOrTypescript(source: string, parserOptions: babelParser.ParserOptions) { + const flowCommentPattern = /^\s*\/\/\s*@flow/; + const useFlowPlugin = flowCommentPattern.test(source); + + const parserPlugins: babelParser.ParserOptions['plugins'] = useFlowPlugin + ? ['flow'] + : ['typescript']; + + // Merge the provided parserOptions with the custom parser plugins + const mergedParserOptions = { + ...parserOptions, + plugins: [...parserOptions.plugins, ...parserPlugins], + }; + + return babelParser.parse(source, mergedParserOptions); +} + export const parserOptions: ParserOptions = { sourceType: 'module', // FIXME: we should get this from the project config somehow? - plugins: ['jsx', 'typescript', 'decorators-legacy', 'classProperties'], + plugins: ['jsx', 'decorators-legacy', 'classProperties'], tokens: true, }; @@ -13,12 +30,12 @@ export const babelParse = (code: string) => { return recast.parse(code, { parser: { parse(source: string) { - return parser.parse(source, parserOptions); + return parseWithFlowOrTypescript(source, parserOptions); }, }, }); }; export const babelParseExpression = (code: string) => { - return parser.parseExpression(code, parserOptions); + return babelParser.parseExpression(code, parserOptions); };