mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[babel] Add flow support
Check if there's a pragma to toggle flow parsing
This commit is contained in:
@@ -13,11 +13,12 @@ type ReactForgetBabelPluginResult = {
|
||||
|
||||
export default function runReactForgetBabelPlugin(
|
||||
text: string,
|
||||
file: string
|
||||
file: string,
|
||||
language: "flow" | "typescript"
|
||||
): ReactForgetBabelPluginResult {
|
||||
const ast = parser.parse(text, {
|
||||
sourceFilename: file,
|
||||
plugins: ["typescript", "jsx"],
|
||||
plugins: ["jsx", language],
|
||||
sourceType: "module",
|
||||
});
|
||||
const result = transformFromAstSync(ast, text, {
|
||||
|
||||
@@ -28,6 +28,7 @@ wasmFolder(
|
||||
);
|
||||
|
||||
const Pragma_RE = /\/\/\s*@enable\((\w+)\)$/gm;
|
||||
const FlowPragmas = [/\/\/\s@flow$/gm, /\*\s@flow$/gm];
|
||||
|
||||
describe("React Forget (HIR version)", () => {
|
||||
generateTestsFromFixtures(
|
||||
@@ -46,13 +47,21 @@ describe("React Forget (HIR version)", () => {
|
||||
}
|
||||
}
|
||||
|
||||
let useFlow: boolean = false;
|
||||
for (const flowPragma of FlowPragmas) {
|
||||
useFlow ||= !!input.match(flowPragma);
|
||||
}
|
||||
|
||||
let language: "flow" | "typescript" = useFlow ? "flow" : "typescript";
|
||||
let items: Array<TestOutput> = [];
|
||||
let error: Error | null = null;
|
||||
if (options.debug) {
|
||||
toggleLogging(options.debug);
|
||||
}
|
||||
try {
|
||||
items.push({ js: runReactForgetBabelPlugin(input, file).code });
|
||||
items.push({
|
||||
js: runReactForgetBabelPlugin(input, file, language).code,
|
||||
});
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user