Skip to content

Commit afb40a5

Browse files
committed
Remove UndefinedScope
1 parent 1cfec8e commit afb40a5

4 files changed

Lines changed: 12 additions & 27 deletions

File tree

src/transformation/utils/errors.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export class TranspileError extends Error {
1010
export const InvalidDecoratorContext = (node: ts.Node) =>
1111
new TranspileError(`Decorator function cannot have 'this: void'.`, node);
1212

13-
export const UndefinedScope = () => new Error("Expected to pop a scope, but found undefined.");
14-
1513
export const UnresolvableRequirePath = (node: ts.Node, reason: string, path?: string) =>
1614
new TranspileError(`${reason}. TypeScript path: ${path}.`, node);
1715

src/transformation/utils/lua-ast.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
44
import { TransformationContext } from "../context";
55
import { getCurrentNamespace } from "../visitors/namespace";
6-
import { UndefinedScope } from "./errors";
76
import { createExportedIdentifier, getIdentifierExportScope } from "./export";
87
import { findScope, peekScope, ScopeType } from "./scope";
98
import { isFirstDeclaration, isFunctionType } from "./typescript";
@@ -193,15 +192,13 @@ export function createLocalOrExportedOrGlobalDeclaration(
193192
? peekScope(context)
194193
: findScope(context, ScopeType.Function | ScopeType.File);
195194

196-
if (scope === undefined) {
197-
throw UndefinedScope();
198-
}
195+
if (scope) {
196+
if (!scope.variableDeclarations) {
197+
scope.variableDeclarations = [];
198+
}
199199

200-
if (!scope.variableDeclarations) {
201-
scope.variableDeclarations = [];
200+
scope.variableDeclarations.push(declaration);
202201
}
203-
204-
scope.variableDeclarations.push(declaration);
205202
}
206203
} else if (rhs) {
207204
// global

src/transformation/utils/scope.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as ts from "typescript";
22
import * as lua from "../../LuaAST";
33
import { assert, getOrUpdate, isNonNull } from "../../utils";
44
import { TransformationContext } from "../context";
5-
import { UndefinedScope } from "./errors";
65
import { replaceStatementInParent } from "./lua-ast";
76
import { getSymbolInfo } from "./symbols";
87
import { getFirstDeclarationInFile } from "./typescript";
@@ -65,9 +64,7 @@ export function markSymbolAsReferencedInCurrentScopes(
6564
export function peekScope(context: TransformationContext): Scope {
6665
const scopeStack = getScopeStack(context);
6766
const scope = scopeStack[scopeStack.length - 1];
68-
if (!scope) {
69-
throw UndefinedScope();
70-
}
67+
assert(scope);
7168

7269
return scope;
7370
}
@@ -88,9 +85,7 @@ export function pushScope(context: TransformationContext, scopeType: ScopeType):
8885
export function popScope(context: TransformationContext): Scope {
8986
const scopeStack = getScopeStack(context);
9087
const scope = scopeStack.pop();
91-
if (!scope) {
92-
throw UndefinedScope();
93-
}
88+
assert(scope);
9489

9590
return scope;
9691
}

src/transformation/visitors/break-continue.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ import { LuaTarget } from "../../CompilerOptions";
33
import * as lua from "../../LuaAST";
44
import { FunctionVisitor } from "../context";
55
import { unsupportedForTarget } from "../utils/diagnostics";
6-
import { UndefinedScope } from "../utils/errors";
76
import { findScope, ScopeType } from "../utils/scope";
87

98
export const transformBreakStatement: FunctionVisitor<ts.BreakStatement> = (breakStatement, context) => {
109
const breakableScope = findScope(context, ScopeType.Loop | ScopeType.Switch);
11-
if (breakableScope === undefined) {
12-
throw UndefinedScope();
13-
}
14-
15-
if (breakableScope.type === ScopeType.Switch) {
10+
if (breakableScope?.type === ScopeType.Switch) {
1611
return lua.createGotoStatement(`____switch${breakableScope.id}_end`);
1712
} else {
1813
return lua.createBreakStatement(breakStatement);
@@ -25,10 +20,10 @@ export const transformContinueStatement: FunctionVisitor<ts.ContinueStatement> =
2520
}
2621

2722
const scope = findScope(context, ScopeType.Loop);
28-
if (scope === undefined) {
29-
throw UndefinedScope();
23+
24+
if (scope) {
25+
scope.loopContinued = true;
3026
}
3127

32-
scope.loopContinued = true;
33-
return lua.createGotoStatement(`__continue${scope.id}`, statement);
28+
return lua.createGotoStatement(`__continue${scope?.id ?? ""}`, statement);
3429
};

0 commit comments

Comments
 (0)