@samitouri / QOS-React-2 / commits / 42497b60f5

[error] Prefix side-effecting function names with throw

I did a double take when I thought we didn't handle returning the error when reading the code and when I edited the code, typescript told me that there's no need to return as creating the error will throw. This PR makes it clear from the name of the function that we will throw.

Sathya Gunasekaran committed Nov 8, 2023 at 16:09 UTC 42497b60f5935c14fdbcd4b6250d507aa3149e63
8 files changed +18 -16
compiler/packages/babel-plugin-react-forget/src/CompilerError.ts
+6 -4
@@ -116,7 +116,9 @@ export class CompilerError extends Error {
116 }
117 }
118
119 - static todo(options: Omit<CompilerErrorDetailOptions, "severity">): never {
119 + static throwTodo(
120 + options: Omit<CompilerErrorDetailOptions, "severity">
121 + ): never {
122 const errors = new CompilerError();
123 errors.pushErrorDetail(
124 new CompilerErrorDetail({ ...options, severity: ErrorSeverity.Todo })
@@ -124,7 +126,7 @@ export class CompilerError extends Error {
126 throw errors;
127 }
128
127 - static invalidJS(
129 + static throwInvalidJS(
130 options: Omit<CompilerErrorDetailOptions, "severity">
131 ): never {
132 const errors = new CompilerError();
@@ -137,7 +139,7 @@ export class CompilerError extends Error {
139 throw errors;
140 }
141
140 - static invalidReact(
142 + static throwInvalidReact(
143 options: Omit<CompilerErrorDetailOptions, "severity">
144 ): never {
145 const errors = new CompilerError();
@@ -150,7 +152,7 @@ export class CompilerError extends Error {
152 throw errors;
153 }
154
153 - static invalidConfig(
155 + static throwInvalidConfig(
156 options: Omit<CompilerErrorDetailOptions, "severity">
157 ): never {
158 const errors = new CompilerError();
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts
+2 -2
@@ -24,7 +24,7 @@ export function addImportsToProgram(
24 * validation here
25 */
26 if (identifiers.has(importSpecifierName)) {
27 - CompilerError.invalidConfig({
27 + CompilerError.throwInvalidConfig({
28 reason: `Encountered conflicting import specifier for ${importSpecifierName} in Forget config.`,
29 description: null,
30 loc: GeneratedSource,
@@ -32,7 +32,7 @@ export function addImportsToProgram(
32 });
33 }
34 if (path.scope.hasBinding(importSpecifierName)) {
35 - CompilerError.invalidConfig({
35 + CompilerError.throwInvalidConfig({
36 reason: `Encountered conflicting import specifiers for ${importSpecifierName} in generated program.`,
37 description: null,
38 loc: GeneratedSource,
compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts
+3 -3
@@ -288,7 +288,7 @@ export function parseConfigPragma(pragma: string): EnvironmentConfig {
288 if (config.success) {
289 return config.data;
290 }
291 - CompilerError.invalidConfig({
291 + CompilerError.throwInvalidConfig({
292 reason: `${fromZodError(config.error)}`,
293 description: "Update Forget config to fix the error",
294 loc: null,
@@ -456,7 +456,7 @@ export function validateEnvironmentConfig(
456 return config.data;
457 }
458
459 - CompilerError.invalidConfig({
459 + CompilerError.throwInvalidConfig({
460 reason: `${fromZodError(config.error)}`,
461 description: "Update Forget config to fix the error",
462 loc: null,
@@ -474,7 +474,7 @@ export function tryParseExternalFunction(
474 return externalFunction.data;
475 }
476
477 - CompilerError.invalidConfig({
477 + CompilerError.throwInvalidConfig({
478 reason: `${fromZodError(externalFunction.error)}`,
479 description: "Update Forget config to fix the error",
480 loc: null,
compiler/packages/babel-plugin-react-forget/src/HIR/FindContextIdentifiers.ts
+1 -1
@@ -189,7 +189,7 @@ function handleAssignment(
189 break;
190 }
191 default: {
192 - CompilerError.todo({
192 + CompilerError.throwTodo({
193 reason: `[FindContextIdentifiers] Cannot handle Object destructuring assignment target ${lvalNode.type}`,
194 description: null,
195 loc: lvalNode.loc ?? GeneratedSource,
compiler/packages/babel-plugin-react-forget/src/Inference/InferReferenceEffects.ts
+2 -2
@@ -353,7 +353,7 @@ class InferenceState {
353 ) {
354 effect = Effect.Mutate;
355 } else {
356 - CompilerError.invalidReact({
356 + CompilerError.throwInvalidReact({
357 reason: `This mutates a variable after it was passed to React, which means that React cannot observe changes to it`,
358 description:
359 place.identifier.name !== null
@@ -370,7 +370,7 @@ class InferenceState {
370 valueKind !== ValueKind.Mutable &&
371 valueKind !== ValueKind.Context
372 ) {
373 - CompilerError.invalidReact({
373 + CompilerError.throwInvalidReact({
374 reason: `This mutates a variable after it was passed to React, which means that React cannot observe changes to it`,
375 description:
376 place.identifier.name !== null
compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+1 -1
@@ -464,7 +464,7 @@ function codegenTerminal(
464 suggestions: null,
465 });
466 if (terminal.init.instructions.length !== 2) {
467 - CompilerError.todo({
467 + CompilerError.throwTodo({
468 reason: "Support non-trivial ForOf inits",
469 description: null,
470 loc: terminal.init.loc,
compiler/packages/babel-plugin-react-forget/src/SSA/EnterSSA.ts
+1 -1
@@ -98,7 +98,7 @@ class SSABuilder {
98 definePlace(oldPlace: Place): Place {
99 const oldId = oldPlace.identifier;
100 if (this.#unknown.has(oldId)) {
101 - CompilerError.todo({
101 + CompilerError.throwTodo({
102 reason: `EnterSSA: Expected identifier to be defined before being used`,
103 description: `Identifier ${printIdentifier(oldId)} is undefined`,
104 loc: oldPlace.loc,
compiler/packages/babel-plugin-react-forget/src/Validation/ValidateUseMemo.ts
+2 -2
@@ -61,7 +61,7 @@ export function validateUseMemo(fn: HIRFunction): void {
61 }
62
63 if (body.loweredFunc.func.params.length > 0) {
64 - CompilerError.invalidReact({
64 + CompilerError.throwInvalidReact({
65 reason: "useMemo callbacks may not accept any arguments",
66 description: null,
67 loc: body.loc,
@@ -70,7 +70,7 @@ export function validateUseMemo(fn: HIRFunction): void {
70 }
71
72 if (body.loweredFunc.func.async || body.loweredFunc.func.generator) {
73 - CompilerError.invalidReact({
73 + CompilerError.throwInvalidReact({
74 reason:
75 "useMemo callbacks may not be async or generator functions",
76 description: null,