@samitouri / QOS-React / commits / e5c07ad3e1

[babel] Skip compilation if there's no function name

Sathya Gunasekaran committed May 3, 2023 at 14:32 UTC e5c07ad3e171a796478542db0c8884b013f18c7c
2 files changed +45 -11
compiler/forget/src/Babel/BabelPlugin.ts
+44 -10
@@ -19,8 +19,8 @@ import { compile } from "../CompilerPipeline";
19 import { GeneratedSource } from "../HIR";
20 import {
21 GatingOptions,
22 - parsePluginOptions,
22 PluginOptions,
23 + parsePluginOptions,
24 } from "./PluginOptions";
25
26 type BabelPluginPass = {
@@ -137,7 +137,24 @@ export default function ReactForgetBabelPlugin(
137 return;
138 }
139
140 - visitFn(buildFunctionDeclaration(fn), pass);
140 + const loweredFn = buildFunctionDeclaration(fn);
141 + if (loweredFn instanceof CompilerError) {
142 + const error = loweredFn;
143 +
144 + const options = parsePluginOptions(pass.opts);
145 + if (options.logger != null) {
146 + options.logger.logEvent("err", error);
147 + }
148 +
149 + if (options.panicOnBailout || error.isCritical()) {
150 + throw error;
151 + } else {
152 + console.error(error);
153 + }
154 + return;
155 + }
156 +
157 + visitFn(loweredFn, pass);
158 },
159 };
160
@@ -310,30 +327,47 @@ function shouldCompile(
327 return true;
328 }
329
330 +function makeError(
331 + reason: string,
332 + loc: t.SourceLocation | null
333 +): CompilerError {
334 + const error = new CompilerError();
335 + error.pushErrorDetail(
336 + new CompilerErrorDetail({
337 + reason,
338 + description: null,
339 + severity: ErrorSeverity.InvalidInput,
340 + codeframe: null,
341 + loc,
342 + })
343 + );
344 + return error;
345 +}
346 +
347 function buildFunctionDeclaration(
348 fn: BabelCore.NodePath<t.ArrowFunctionExpression>
315 -): BabelCore.NodePath<t.FunctionDeclaration> {
349 +): BabelCore.NodePath<t.FunctionDeclaration> | CompilerError {
350 if (!fn.parentPath.isVariableDeclarator()) {
317 - CompilerError.invariant(
318 - "ArrowFunctionExpression must be declared in variable declaration",
319 - fn.node.loc ?? GeneratedSource
351 + return makeError(
352 + "Skipping compilation: ArrowFunctionExpression must be declared in variable declaration",
353 + fn.node.loc ?? null
354 );
355 }
356 const variableDeclarator = fn.parentPath;
357
358 if (!variableDeclarator.parentPath.isVariableDeclaration()) {
325 - CompilerError.invariant(
359 + return makeError(
360 "ArrowFunctionExpression must be a single declaration",
327 - fn.node.loc ?? GeneratedSource
361 + fn.node.loc ?? null
362 );
363 }
364 const variableDeclaration = variableDeclarator.parentPath;
365
366 const id = variableDeclarator.get("id");
367 if (!id.isIdentifier()) {
334 - CompilerError.invariant(
368 + return makeError(
369 "ArrowFunctionExpression must have an id",
336 - fn.node.loc ?? GeneratedSource
370 + fn.node.loc ?? null
371 );
372 }
373
compiler/forget/src/__tests__/fixtures/compiler/error._todo.multi-arrow-expr-export-default-gating-test.expect.md
+1 -1
@@ -18,7 +18,7 @@ export default Renderer = (props) => (
18 ## Error
19
20 ```
21 -[ReactForget] Invariant: ArrowFunctionExpression must be declared in variable declaration (4:9)
21 +[ReactForget] InvalidInput: Skipping compilation: ArrowFunctionExpression must be declared in variable declaration (4:9)
22 ```
23
24
\ No newline at end of file