Skip to content

Commit a980e13

Browse files
authored
Adjusted peekScope return and added appropriate errors (#527)
* made peekScope return Scope | undefined and added appropriate errors * removed unneccessary checks * removed unneccessary checks
1 parent 043b87d commit a980e13

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/LuaTransformer.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ export class LuaTransformer {
329329
const result: tstl.Statement[] = [];
330330

331331
const scope = this.peekScope();
332+
if (scope === undefined) {
333+
throw TSTLErrors.UndefinedScope();
334+
}
332335
if (!this.options.noHoisting && !scope.importStatements) {
333336
scope.importStatements = [];
334337
}
@@ -1756,6 +1759,9 @@ export class LuaTransformer {
17561759
// Remember symbols referenced in this function for hoisting later
17571760
if (!this.options.noHoisting && name.symbolId !== undefined) {
17581761
const scope = this.peekScope();
1762+
if (scope === undefined) {
1763+
throw TSTLErrors.UndefinedScope();
1764+
}
17591765
if (!scope.functionDefinitions) { scope.functionDefinitions = new Map(); }
17601766
const functionInfo = {referencedSymbols: functionScope.referencedSymbols || new Set()};
17611767
scope.functionDefinitions.set(name.symbolId, functionInfo);
@@ -2309,7 +2315,11 @@ export class LuaTransformer {
23092315
this.pushScope(ScopeType.Switch, statement);
23102316

23112317
// Give the switch a unique name to prevent nested switches from acting up.
2312-
const switchName = `____TS_switch${this.peekScope().id}`;
2318+
const scope = this.peekScope();
2319+
if (scope === undefined) {
2320+
throw TSTLErrors.UndefinedScope();
2321+
}
2322+
const switchName = `____TS_switch${scope.id}`;
23132323

23142324
const expression = this.transformExpression(statement.expression);
23152325
const switchVariable = tstl.createIdentifier(switchName);
@@ -4658,7 +4668,10 @@ export class LuaTransformer {
46584668
// Remember function definitions for hoisting later
46594669
const functionSymbolId = (lhs as tstl.Identifier).symbolId;
46604670
const scope = this.peekScope();
4661-
if (functionSymbolId && scope && scope.functionDefinitions) {
4671+
if (scope === undefined) {
4672+
throw TSTLErrors.UndefinedScope();
4673+
}
4674+
if (functionSymbolId && scope.functionDefinitions) {
46624675
const definitions = scope.functionDefinitions.get(functionSymbolId);
46634676
if (definitions) {
46644677
definitions.definition = declaration || assignment;
@@ -4834,7 +4847,7 @@ export class LuaTransformer {
48344847
return this.scopeStack.slice().reverse().find(s => (scopeTypes & s.type) !== 0);
48354848
}
48364849

4837-
protected peekScope(): Scope {
4850+
protected peekScope(): Scope | undefined {
48384851
return this.scopeStack[this.scopeStack.length - 1];
48394852
}
48404853

@@ -4977,6 +4990,9 @@ export class LuaTransformer {
49774990
}
49784991

49794992
const scope = this.peekScope();
4993+
if (scope === undefined) {
4994+
throw TSTLErrors.UndefinedScope();
4995+
}
49804996

49814997
let result = this.hoistFunctionDefinitions(scope, statements);
49824998

@@ -5008,6 +5024,9 @@ export class LuaTransformer {
50085024
const declaration = tstl.createVariableDeclarationStatement(variable, initializer, tsOriginal, parent);
50095025
if (!this.options.noHoisting && variable.symbolId) {
50105026
const scope = this.peekScope();
5027+
if (scope === undefined) {
5028+
throw TSTLErrors.UndefinedScope();
5029+
}
50115030
if (!scope.variableDeclarations) { scope.variableDeclarations = []; }
50125031
scope.variableDeclarations.push(declaration);
50135032
}

0 commit comments

Comments
 (0)