Skip to content

Commit 2763d0d

Browse files
committed
Replace unactionable errors with assertions
1 parent a3a9732 commit 2763d0d

6 files changed

Lines changed: 15 additions & 31 deletions

File tree

src/transformation/utils/errors.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ export const InvalidExtensionMetaExtension = (node: ts.Node) =>
3434
export const InvalidNewExpressionOnExtension = (node: ts.Node) =>
3535
new TranspileError(`Cannot construct classes with '@extension' or '@metaExtension' annotation.`, node);
3636

37-
export const InvalidExportDeclaration = (declaration: ts.ExportDeclaration) =>
38-
new TranspileError("Encountered invalid export declaration without exports and without module.", declaration);
39-
4037
export const InvalidExtendsExtension = (node: ts.Node) =>
4138
new TranspileError(`Cannot extend classes with '@extension' or '@metaExtension' annotation.`, node);
4239

@@ -48,24 +45,17 @@ export const InvalidInstanceOfExtension = (node: ts.Node) =>
4845

4946
export const InvalidJsonFileContent = (node: ts.Node) => new TranspileError("Invalid JSON file content", node);
5047

51-
export const MissingClassName = (node: ts.Node) => new TranspileError(`Class declarations must have a name.`, node);
52-
5348
export const MissingForOfVariables = (node: ts.Node) =>
5449
new TranspileError("Transpiled ForOf variable declaration list contains no declarations.", node);
5550

5651
export const MissingMetaExtension = (node: ts.Node) =>
5752
new TranspileError(`'@metaExtension' annotation requires the extension of the metatable class.`, node);
5853

59-
export const UndefinedFunctionDefinition = (functionSymbolId: number) =>
60-
new Error(`Function definition for function symbol ${functionSymbolId} is undefined.`);
61-
6254
export const UnsupportedForInVariable = (node: ts.Node) =>
6355
new TranspileError(`Unsupported for-in variable kind.`, node);
6456

6557
export const UndefinedScope = () => new Error("Expected to pop a scope, but found undefined.");
6658

67-
export const UndefinedTypeNode = (node: ts.Node) => new TranspileError("Failed to resolve required type node.", node);
68-
6959
export const UnsupportedKind = (description: string, kind: ts.SyntaxKind, node: ts.Node) =>
7060
new TranspileError(`Unsupported ${description} kind: ${ts.SyntaxKind[kind]}`, node);
7161

src/transformation/utils/scope.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
3-
import { getOrUpdate, isNonNull } from "../../utils";
3+
import { assert, getOrUpdate, isNonNull } from "../../utils";
44
import { TransformationContext } from "../context";
5-
import { UndefinedFunctionDefinition, UndefinedScope } from "./errors";
5+
import { UndefinedScope } from "./errors";
66
import { replaceStatementInParent } from "./lua-ast";
77
import { getSymbolInfo } from "./symbols";
88
import { getFirstDeclarationInFile } from "./typescript";
@@ -125,9 +125,7 @@ function shouldHoistSymbol(context: TransformationContext, symbolId: lua.SymbolI
125125

126126
if (scope.functionDefinitions) {
127127
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
128-
if (functionDefinition.definition === undefined) {
129-
throw UndefinedFunctionDefinition(functionSymbolId);
130-
}
128+
assert(functionDefinition.definition);
131129

132130
const { line, column } = lua.getOriginalPos(functionDefinition.definition);
133131
if (line !== undefined && column !== undefined) {
@@ -202,9 +200,7 @@ function hoistFunctionDefinitions(
202200
const result = [...statements];
203201
const hoistedFunctions: Array<lua.VariableDeclarationStatement | lua.AssignmentStatement> = [];
204202
for (const [functionSymbolId, functionDefinition] of scope.functionDefinitions) {
205-
if (functionDefinition.definition === undefined) {
206-
throw UndefinedFunctionDefinition(functionSymbolId);
207-
}
203+
assert(functionDefinition.definition);
208204

209205
if (shouldHoistSymbol(context, functionSymbolId, scope)) {
210206
const index = result.indexOf(functionDefinition.definition);

src/transformation/visitors/class/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as ts from "typescript";
22
import * as lua from "../../../LuaAST";
3-
import { getOrUpdate, isNonNull } from "../../../utils";
3+
import { assert, getOrUpdate, isNonNull } from "../../../utils";
44
import { FunctionVisitor, TransformationContext } from "../../context";
55
import { AnnotationKind, getTypeAnnotations } from "../../utils/annotations";
66
import {
@@ -9,7 +9,6 @@ import {
99
InvalidExtendsExtension,
1010
InvalidExtendsLuaTable,
1111
InvalidExtensionMetaExtension,
12-
MissingClassName,
1312
MissingMetaExtension,
1413
} from "../../utils/errors";
1514
import {
@@ -331,9 +330,7 @@ export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (ex
331330
}
332331

333332
if (!baseClassName) {
334-
if (classDeclaration.name === undefined) {
335-
throw MissingClassName(expression);
336-
}
333+
assert(classDeclaration.name);
337334

338335
// Use "className.____super" if the base is not a simple identifier
339336
baseClassName = lua.createTableIndexExpression(

src/transformation/visitors/class/setup.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../../LuaAST";
3+
import { assert } from "../../../utils";
34
import { TransformationContext } from "../../context";
4-
import { UndefinedTypeNode } from "../../utils/errors";
55
import {
66
createDefaultExportStringLiteral,
77
createExportedIdentifier,
@@ -159,9 +159,7 @@ export function createClassSetup(
159159

160160
if (extendsType) {
161161
const extendedTypeNode = getExtendedTypeNode(context, statement);
162-
if (extendedTypeNode === undefined) {
163-
throw UndefinedTypeNode(statement);
164-
}
162+
assert(extendedTypeNode);
165163

166164
// localClassName.____super = extendsExpression
167165
const createClassBase = () =>

src/transformation/visitors/modules/export.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from "typescript";
22
import * as lua from "../../../LuaAST";
3+
import { assert } from "../../../utils";
34
import { FunctionVisitor, TransformationContext } from "../../context";
4-
import { InvalidExportDeclaration } from "../../utils/errors";
55
import {
66
createDefaultExportIdentifier,
77
createDefaultExportStringLiteral,
@@ -37,9 +37,7 @@ export const transformExportAssignment: FunctionVisitor<ts.ExportAssignment> = (
3737
};
3838

3939
function transformExportAllFrom(context: TransformationContext, node: ts.ExportDeclaration): lua.Statement | undefined {
40-
if (node.moduleSpecifier === undefined) {
41-
throw InvalidExportDeclaration(node);
42-
}
40+
assert(node.moduleSpecifier);
4341

4442
if (!context.resolver.moduleExportsSomeValue(node.moduleSpecifier)) {
4543
return undefined;

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as nativeAssert from "assert";
12
import * as path from "path";
23

34
export const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
@@ -67,6 +68,10 @@ export function castEach<TOriginal, TCast extends TOriginal>(
6768
}
6869
}
6970

71+
export function assert(value: any, message?: string | Error): asserts value {
72+
nativeAssert(value, message);
73+
}
74+
7075
export function assertNever(_value: never): never {
7176
throw new Error("Value is expected to be never");
7277
}

0 commit comments

Comments
 (0)