1
+import assert from "assert";
2
import type { runReactForgetBabelPlugin as RunReactForgetBabelPlugin } from "babel-plugin-react-forget/src/Babel/RunReactForgetBabelPlugin";
3
+import { CompilationMode } from "babel-plugin-react-forget/src/Entrypoint";
4
import type { Effect, ValueKind } from "babel-plugin-react-forget/src/HIR";
5
4
-const FlowPragmas = [/\/\/\s@flow$/gm, /\*\s@flow$/gm];
5
-
6
export function parseLanguage(source: string): "flow" | "typescript" {
7
- let useFlow = false;
8
- for (const flowPragma of FlowPragmas) {
9
- useFlow ||= !!source.match(flowPragma);
10
- }
11
- return useFlow ? "flow" : "typescript";
7
+ return source.indexOf("@flow") !== -1 ? "flow" : "typescript";
8
}
9
10
export function transformFixtureInput(
17
const firstLine = input.substring(0, input.indexOf("\n"));
18
19
let language = parseLanguage(firstLine);
24
- let enableOnlyOnUseForgetDirective = false;
20
let gating = null;
21
let instrumentForget = null;
22
let panicOnBailout = true;
27
let validateRefAccessDuringRender = true;
28
let validateNoSetStateInRender = true;
29
let enableEmitFreeze = null;
35
- let enableOnlyOnReactScript = false;
36
- let enableInferReactFunctions = false;
30
+ let compilationMode: CompilationMode = "all";
31
38
- if (firstLine.indexOf("@forgetDirective") !== -1) {
39
- enableOnlyOnUseForgetDirective = true;
32
+ if (firstLine.indexOf("@compilationMode(annotation)") !== -1) {
33
+ assert(
34
+ compilationMode === "all",
35
+ "Cannot set @compilationMode(..) more than once"
36
+ );
37
+ compilationMode = "annotation";
38
+ }
39
+ if (firstLine.indexOf("@compilationMode(infer)") !== -1) {
40
+ assert(
41
+ compilationMode === "all",
42
+ "Cannot set @compilationMode(..) more than once"
43
+ );
44
+ compilationMode = "infer";
45
}
46
+
47
if (firstLine.includes("@gating")) {
48
gating = {
49
source: "ReactForgetFeatureFlag",
83
importSpecifierName: "makeReadOnly",
84
};
85
}
80
- if (firstLine.indexOf("@reactScriptDirective") !== -1) {
81
- enableOnlyOnReactScript = true;
82
- language = "flow";
83
- }
84
- if (firstLine.indexOf("@enableInferReactFunctions") !== -1) {
85
- enableInferReactFunctions = true;
86
- }
87
- if (
88
- [
89
- enableInferReactFunctions,
90
- enableOnlyOnReactScript,
91
- enableOnlyOnUseForgetDirective,
92
- ].filter((x) => x === true).length > 1
93
- ) {
94
- throw new Error(
95
- "Cannot enable more than one of @enableInferReactFunctions, @enableOnlyOnReactScript, and @enableOnlyOnUseForgetDirective at once"
96
- );
97
- }
86
87
return pluginFn(
88
input,
112
enableEmitFreeze,
113
assertValidMutableRanges: true,
114
},
127
- enableOnlyOnUseForgetDirective,
128
- enableOnlyOnReactScript,
129
- enableInferReactFunctions,
115
+ compilationMode,
116
logger: null,
117
gating,
118
instrumentForget,