Use CompilerError.invariant in BabelPlugin
This means we get more information when we hit these invariants (ie the source location)
Joe Savona committed
Apr 4, 2023 at 17:56 UTC
a3dfc7e3a2740dcf6d5a3f9c31282fc1d7bd8423
2 files changed
+39
-14
compiler/forget/src/Babel/BabelPlugin.ts
+38
-13
@@ -10,8 +10,9 @@
10
import type * as BabelCore from "@babel/core";
11
import jsx from "@babel/plugin-syntax-jsx";
12
import * as t from "@babel/types";
13
-import invariant from "invariant";
13
+import { CompilerError } from "../CompilerError";
14
import { compile } from "../CompilerPipeline";
15
+import { GeneratedSource } from "../HIR";
16
import {
17
GatingOptions,
18
parsePluginOptions,
@@ -50,12 +51,22 @@ export default function ReactForgetBabelPlugin(
51
52
if (pass.opts.gating != null) {
53
// Rename existing function
53
- invariant(fn.node.id, "FunctionDeclaration must have a name");
54
+ if (fn.node.id == null) {
55
+ CompilerError.invariant(
56
+ "FunctionDeclaration must have a name",
57
+ fn.node.loc ?? GeneratedSource
58
+ );
59
+ }
60
const original = fn.node.id;
61
fn.node.id = addSuffix(fn.node.id, "_uncompiled");
62
63
// Rename and append compiled function
58
- invariant(compiled.id, "FunctionDeclaration must produce a name");
64
+ if (compiled.id == null) {
65
+ CompilerError.invariant(
66
+ "FunctionDeclaration must produce a name",
67
+ fn.node.loc ?? GeneratedSource
68
+ );
69
+ }
70
compiled.id = addSuffix(compiled.id, "_forget");
71
const compiledFn = fn.insertAfter(compiled)[0];
72
compiledFn.skip();
@@ -158,20 +169,29 @@ function shouldCompile(
169
function buildFunctionDeclaration(
170
fn: BabelCore.NodePath<t.ArrowFunctionExpression>
171
): BabelCore.NodePath<t.FunctionDeclaration> {
161
- invariant(
162
- fn.parentPath.isVariableDeclarator(),
163
- "ArrowFunctionExpression must be declared in variable declaration"
164
- );
172
+ if (!fn.parentPath.isVariableDeclarator()) {
173
+ CompilerError.invariant(
174
+ "ArrowFunctionExpression must be declared in variable declaration",
175
+ fn.node.loc ?? GeneratedSource
176
+ );
177
+ }
178
const variableDeclarator = fn.parentPath;
179
167
- invariant(
168
- variableDeclarator.parentPath.isVariableDeclaration(),
169
- "ArrowFunctionExpression must be a single declaration"
170
- );
180
+ if (!variableDeclarator.parentPath.isVariableDeclaration()) {
181
+ CompilerError.invariant(
182
+ "ArrowFunctionExpression must be a single declaration",
183
+ fn.node.loc ?? GeneratedSource
184
+ );
185
+ }
186
const variableDeclaration = variableDeclarator.parentPath;
187
188
const id = variableDeclarator.get("id");
174
- invariant(id.isIdentifier(), "ArrowFunctionExpression must have an id");
189
+ if (!id.isIdentifier()) {
190
+ CompilerError.invariant(
191
+ "ArrowFunctionExpression must have an id",
192
+ fn.node.loc ?? GeneratedSource
193
+ );
194
+ }
195
196
const rewrittenFn = variableDeclaration.replaceWith(
197
t.functionDeclaration(
@@ -199,7 +219,12 @@ function buildBlockStatement(
219
return wrappedBody.node;
220
}
221
202
- invariant(body.isBlockStatement(), "Body must be a BlockStatement");
222
+ if (!body.isBlockStatement()) {
223
+ CompilerError.invariant(
224
+ "Body must be a BlockStatement",
225
+ body.node.loc ?? GeneratedSource
226
+ );
227
+ }
228
return body.node;
229
}
230
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
-ArrowFunctionExpression must be declared in variable declaration
21
+[ReactForget] Invariant: ArrowFunctionExpression must be declared in variable declaration (4:9)
22
```
23
24
\ No newline at end of file