@samitouri / QOS-React-2 / commits / 9aed0fc6b1

[babel] Add flow support

Check if there's a pragma to toggle flow parsing

Sathya Gunasekaran committed Jan 24, 2023 at 10:21 UTC 9aed0fc6b123d5e18f7b1d13af7ef9a6b751e48b
2 files changed +13 -3
compiler/forget/src/Babel/RunReactForgetBabelPlugin.ts
+3 -2
@@ -13,11 +13,12 @@ type ReactForgetBabelPluginResult = {
13
14 export default function runReactForgetBabelPlugin(
15 text: string,
16 - file: string
16 + file: string,
17 + language: "flow" | "typescript"
18 ): ReactForgetBabelPluginResult {
19 const ast = parser.parse(text, {
20 sourceFilename: file,
20 - plugins: ["typescript", "jsx"],
21 + plugins: ["jsx", language],
22 sourceType: "module",
23 });
24 const result = transformFromAstSync(ast, text, {
compiler/forget/src/__tests__/hir-test.ts
+10 -1
@@ -28,6 +28,7 @@ wasmFolder(
28 );
29
30 const Pragma_RE = /\/\/\s*@enable\((\w+)\)$/gm;
31 +const FlowPragmas = [/\/\/\s@flow$/gm, /\*\s@flow$/gm];
32
33 describe("React Forget (HIR version)", () => {
34 generateTestsFromFixtures(
@@ -46,13 +47,21 @@ describe("React Forget (HIR version)", () => {
47 }
48 }
49
50 + let useFlow: boolean = false;
51 + for (const flowPragma of FlowPragmas) {
52 + useFlow ||= !!input.match(flowPragma);
53 + }
54 +
55 + let language: "flow" | "typescript" = useFlow ? "flow" : "typescript";
56 let items: Array<TestOutput> = [];
57 let error: Error | null = null;
58 if (options.debug) {
59 toggleLogging(options.debug);
60 }
61 try {
55 - items.push({ js: runReactForgetBabelPlugin(input, file).code });
62 + items.push({
63 + js: runReactForgetBabelPlugin(input, file, language).code,
64 + });
65 } catch (e) {
66 error = e;
67 }