[playground] Refactor pragma parsing into separate function
Sathya Gunasekaran committed
Jun 23, 2023 at 14:56 UTC
64637ac84c0dcafb93bfdaf38a92c0452f1f0670
1 file changed
+73
-64
compiler/forget/apps/playground/components/Editor/index.tsx
+73
-64
@@ -108,6 +108,76 @@ const COMMON_HOOKS: Array<[string, Hook]> = [
108
],
109
];
110
111
+function parsePragma(pragma: string) {
112
+ let memoizeJsxElements = true;
113
+ let enableAssumeHooksFollowRulesOfReact = false;
114
+ let enableTreatHooksAsFunctions = true;
115
+ let disableAllMemoization = false;
116
+ let validateRefAccessDuringRender = true;
117
+ let enableEmitFreeze = null;
118
+ let enableOptimizeFunctionExpressions = false;
119
+ let inlineUseMemo = true;
120
+ let validateHooksUsage = true;
121
+ let enableFunctionCallSignatureOptimizations = true;
122
+ let validateFrozenLambdas = true;
123
+ let assertValidMutableRanges = true;
124
+
125
+ if (pragma.includes("@memoizeJsxElements false")) {
126
+ memoizeJsxElements = false;
127
+ }
128
+ if (pragma.includes("@enableAssumeHooksFollowRulesOfReact true")) {
129
+ enableAssumeHooksFollowRulesOfReact = true;
130
+ }
131
+ if (pragma.includes("@enableTreatHooksAsFunctions false")) {
132
+ enableTreatHooksAsFunctions = false;
133
+ }
134
+ if (pragma.includes("@disableAllMemoization true")) {
135
+ disableAllMemoization = true;
136
+ }
137
+ if (pragma.includes("@validateRefAccessDuringRender false")) {
138
+ validateRefAccessDuringRender = false;
139
+ }
140
+ if (pragma.includes("@enableOptimizeFunctionExpressions")) {
141
+ enableOptimizeFunctionExpressions = true;
142
+ }
143
+ if (pragma.includes("@enableEmitFreeze")) {
144
+ enableEmitFreeze = {
145
+ source: "react-forget-runtime",
146
+ importSpecifierName: "makeReadOnly",
147
+ };
148
+ }
149
+ if (pragma.includes("@inlineUseMemo false")) {
150
+ inlineUseMemo = false;
151
+ }
152
+ if (pragma.includes("@enableFunctionCallSignatureOptimizations false")) {
153
+ enableFunctionCallSignatureOptimizations = false;
154
+ }
155
+ if (pragma.includes("@validateHooksUsage false")) {
156
+ validateHooksUsage = false;
157
+ }
158
+ if (pragma.includes("@validateFrozenLambdas false")) {
159
+ validateHooksUsage = false;
160
+ }
161
+ if (pragma.includes("@assertValidMutableRanges false")) {
162
+ assertValidMutableRanges = false;
163
+ }
164
+
165
+ return {
166
+ enableAssumeHooksFollowRulesOfReact,
167
+ enableFunctionCallSignatureOptimizations,
168
+ disableAllMemoization,
169
+ enableTreatHooksAsFunctions,
170
+ inlineUseMemo,
171
+ memoizeJsxElements,
172
+ validateHooksUsage,
173
+ validateRefAccessDuringRender,
174
+ validateFrozenLambdas,
175
+ enableEmitFreeze,
176
+ enableOptimizeFunctionExpressions,
177
+ assertValidMutableRanges,
178
+ };
179
+}
180
+
181
function compile(source: string): CompilerOutput {
182
const results = new Map<string, PrintedCompilerPipelineValue[]>();
183
const upsert = (result: PrintedCompilerPipelineValue) => {
@@ -120,74 +190,13 @@ function compile(source: string): CompilerOutput {
190
};
191
try {
192
// Extract the first line to quickly check for custom test directives
123
- const firstLine = source.substring(0, source.indexOf("\n"));
193
+ const pragma = source.substring(0, source.indexOf("\n"));
194
+ const options = parsePragma(pragma);
195
125
- let memoizeJsxElements = true;
126
- let enableAssumeHooksFollowRulesOfReact = false;
127
- let enableTreatHooksAsFunctions = true;
128
- let disableAllMemoization = false;
129
- let validateRefAccessDuringRender = true;
130
- let enableEmitFreeze = null;
131
- let enableOptimizeFunctionExpressions = false;
132
- let inlineUseMemo = true;
133
- let validateHooksUsage = true;
134
- let enableFunctionCallSignatureOptimizations = true;
135
- let validateFrozenLambdas = true;
136
- let assertValidMutableRanges = true;
137
- if (firstLine.includes("@memoizeJsxElements false")) {
138
- memoizeJsxElements = false;
139
- }
140
- if (firstLine.includes("@enableAssumeHooksFollowRulesOfReact true")) {
141
- enableAssumeHooksFollowRulesOfReact = true;
142
- }
143
- if (firstLine.includes("@enableTreatHooksAsFunctions false")) {
144
- enableTreatHooksAsFunctions = false;
145
- }
146
- if (firstLine.includes("@disableAllMemoization true")) {
147
- disableAllMemoization = true;
148
- }
149
- if (firstLine.includes("@validateRefAccessDuringRender false")) {
150
- validateRefAccessDuringRender = false;
151
- }
152
- if (firstLine.includes("@enableOptimizeFunctionExpressions")) {
153
- enableOptimizeFunctionExpressions = true;
154
- }
155
- if (firstLine.includes("@enableEmitFreeze")) {
156
- enableEmitFreeze = {
157
- source: "react-forget-runtime",
158
- importSpecifierName: "makeReadOnly",
159
- };
160
- }
161
- if (firstLine.includes("@inlineUseMemo false")) {
162
- inlineUseMemo = false;
163
- }
164
- if (firstLine.includes("@enableFunctionCallSignatureOptimizations false")) {
165
- enableFunctionCallSignatureOptimizations = false;
166
- }
167
- if (firstLine.includes("@validateHooksUsage false")) {
168
- validateHooksUsage = false;
169
- }
170
- if (firstLine.includes("@validateFrozenLambdas false")) {
171
- validateHooksUsage = false;
172
- }
173
- if (firstLine.includes("@assertValidMutableRanges false")) {
174
- assertValidMutableRanges = false;
175
- }
196
for (const fn of parseFunctions(source)) {
197
for (const result of run(fn, {
198
customHooks: new Map([...COMMON_HOOKS]),
179
- enableAssumeHooksFollowRulesOfReact,
180
- enableFunctionCallSignatureOptimizations,
181
- disableAllMemoization,
182
- enableTreatHooksAsFunctions,
183
- inlineUseMemo,
184
- memoizeJsxElements,
185
- validateHooksUsage,
186
- validateRefAccessDuringRender,
187
- validateFrozenLambdas,
188
- enableEmitFreeze,
189
- enableOptimizeFunctionExpressions,
190
- assertValidMutableRanges,
199
+ ...options,
200
})) {
201
const fnName = fn.node.id?.name ?? null;
202
switch (result.kind) {