[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.
This commit is contained in:
Sathya Gunasekaran
2023-11-08 16:09:13 +00:00
parent 33ffde4836
commit 42497b60f5
8 changed files with 18 additions and 16 deletions
@@ -116,7 +116,9 @@ export class CompilerError extends Error {
}
}
static todo(options: Omit<CompilerErrorDetailOptions, "severity">): never {
static throwTodo(
options: Omit<CompilerErrorDetailOptions, "severity">
): never {
const errors = new CompilerError();
errors.pushErrorDetail(
new CompilerErrorDetail({ ...options, severity: ErrorSeverity.Todo })
@@ -124,7 +126,7 @@ export class CompilerError extends Error {
throw errors;
}
static invalidJS(
static throwInvalidJS(
options: Omit<CompilerErrorDetailOptions, "severity">
): never {
const errors = new CompilerError();
@@ -137,7 +139,7 @@ export class CompilerError extends Error {
throw errors;
}
static invalidReact(
static throwInvalidReact(
options: Omit<CompilerErrorDetailOptions, "severity">
): never {
const errors = new CompilerError();
@@ -150,7 +152,7 @@ export class CompilerError extends Error {
throw errors;
}
static invalidConfig(
static throwInvalidConfig(
options: Omit<CompilerErrorDetailOptions, "severity">
): never {
const errors = new CompilerError();
@@ -24,7 +24,7 @@ export function addImportsToProgram(
* validation here
*/
if (identifiers.has(importSpecifierName)) {
CompilerError.invalidConfig({
CompilerError.throwInvalidConfig({
reason: `Encountered conflicting import specifier for ${importSpecifierName} in Forget config.`,
description: null,
loc: GeneratedSource,
@@ -32,7 +32,7 @@ export function addImportsToProgram(
});
}
if (path.scope.hasBinding(importSpecifierName)) {
CompilerError.invalidConfig({
CompilerError.throwInvalidConfig({
reason: `Encountered conflicting import specifiers for ${importSpecifierName} in generated program.`,
description: null,
loc: GeneratedSource,
@@ -288,7 +288,7 @@ export function parseConfigPragma(pragma: string): EnvironmentConfig {
if (config.success) {
return config.data;
}
CompilerError.invalidConfig({
CompilerError.throwInvalidConfig({
reason: `${fromZodError(config.error)}`,
description: "Update Forget config to fix the error",
loc: null,
@@ -456,7 +456,7 @@ export function validateEnvironmentConfig(
return config.data;
}
CompilerError.invalidConfig({
CompilerError.throwInvalidConfig({
reason: `${fromZodError(config.error)}`,
description: "Update Forget config to fix the error",
loc: null,
@@ -474,7 +474,7 @@ export function tryParseExternalFunction(
return externalFunction.data;
}
CompilerError.invalidConfig({
CompilerError.throwInvalidConfig({
reason: `${fromZodError(externalFunction.error)}`,
description: "Update Forget config to fix the error",
loc: null,
@@ -189,7 +189,7 @@ function handleAssignment(
break;
}
default: {
CompilerError.todo({
CompilerError.throwTodo({
reason: `[FindContextIdentifiers] Cannot handle Object destructuring assignment target ${lvalNode.type}`,
description: null,
loc: lvalNode.loc ?? GeneratedSource,
@@ -353,7 +353,7 @@ class InferenceState {
) {
effect = Effect.Mutate;
} else {
CompilerError.invalidReact({
CompilerError.throwInvalidReact({
reason: `This mutates a variable after it was passed to React, which means that React cannot observe changes to it`,
description:
place.identifier.name !== null
@@ -370,7 +370,7 @@ class InferenceState {
valueKind !== ValueKind.Mutable &&
valueKind !== ValueKind.Context
) {
CompilerError.invalidReact({
CompilerError.throwInvalidReact({
reason: `This mutates a variable after it was passed to React, which means that React cannot observe changes to it`,
description:
place.identifier.name !== null
@@ -464,7 +464,7 @@ function codegenTerminal(
suggestions: null,
});
if (terminal.init.instructions.length !== 2) {
CompilerError.todo({
CompilerError.throwTodo({
reason: "Support non-trivial ForOf inits",
description: null,
loc: terminal.init.loc,
@@ -98,7 +98,7 @@ class SSABuilder {
definePlace(oldPlace: Place): Place {
const oldId = oldPlace.identifier;
if (this.#unknown.has(oldId)) {
CompilerError.todo({
CompilerError.throwTodo({
reason: `EnterSSA: Expected identifier to be defined before being used`,
description: `Identifier ${printIdentifier(oldId)} is undefined`,
loc: oldPlace.loc,
@@ -61,7 +61,7 @@ export function validateUseMemo(fn: HIRFunction): void {
}
if (body.loweredFunc.func.params.length > 0) {
CompilerError.invalidReact({
CompilerError.throwInvalidReact({
reason: "useMemo callbacks may not accept any arguments",
description: null,
loc: body.loc,
@@ -70,7 +70,7 @@ export function validateUseMemo(fn: HIRFunction): void {
}
if (body.loweredFunc.func.async || body.loweredFunc.func.generator) {
CompilerError.invalidReact({
CompilerError.throwInvalidReact({
reason:
"useMemo callbacks may not be async or generator functions",
description: null,