Skip to content

Commit 6500548

Browse files
committed
Refactor to new transformSourceFileWithState method
1 parent 8972539 commit 6500548

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

src/LuaTransformer.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ export class LuaTransformer {
9292
this.genVarCounter = 0;
9393
this.luaLibFeatureSet = new Set<LuaLibFeature>();
9494

95-
this.visitedExportEquals = false;
96-
9795
this.scopeStack = [];
9896
this.classStack = [];
9997

@@ -111,16 +109,7 @@ export class LuaTransformer {
111109
public transform(node: ts.Bundle | ts.SourceFile): [tstl.Block, Set<LuaLibFeature>] {
112110
this.setupState();
113111
if (ts.isSourceFile(node)) {
114-
this.currentSourceFile = node;
115-
this.isModule = tsHelper.isFileModule(node);
116-
117-
// Use `getParseTreeNode` to get original SourceFile node, before it was substituted by custom transformers.
118-
// It's required because otherwise `getEmitResolver` won't use cached diagnostics, produced in `emitWorker`
119-
// and would try to re-analyze the file, which would fail because of replaced nodes.
120-
const originalSourceFile = ts.getParseTreeNode(node, ts.isSourceFile) || node;
121-
this.resolver = this.checker.getEmitResolver(originalSourceFile);
122-
123-
return [this.transformSourceFile(node), this.luaLibFeatureSet];
112+
return [this.transformSourceFileWithState(node), this.luaLibFeatureSet];
124113
} else {
125114
return [this.transformBundle(node), this.luaLibFeatureSet];
126115
}
@@ -130,12 +119,7 @@ export class LuaTransformer {
130119
this.isWithinBundle = true;
131120
const combinedStatements = bundle.sourceFiles.reduce(
132121
(statements: tstl.Statement[], sourceFile: ts.SourceFile) => {
133-
this.currentSourceFile = sourceFile;
134-
this.visitedExportEquals = false;
135-
this.isModule = tsHelper.isFileModule(sourceFile);
136-
const originalSourceFile = ts.getParseTreeNode(sourceFile, ts.isSourceFile) || sourceFile;
137-
this.resolver = this.checker.getEmitResolver(originalSourceFile);
138-
const transformResult = this.transformSourceFile(sourceFile);
122+
const transformResult = this.transformSourceFileWithState(sourceFile);
139123
return [...statements, ...transformResult.statements];
140124
},
141125
[]
@@ -157,6 +141,7 @@ export class LuaTransformer {
157141
}
158142

159143
public transformSourceFile(sourceFile: ts.SourceFile): tstl.Block {
144+
this.visitedExportEquals = false;
160145
let statements: tstl.Statement[] = [];
161146
if (sourceFile.flags & ts.NodeFlags.JsonFile) {
162147
const statement = sourceFile.statements[0];
@@ -203,6 +188,19 @@ export class LuaTransformer {
203188
return tstl.createBlock(statements, sourceFile);
204189
}
205190

191+
private transformSourceFileWithState(node: ts.SourceFile): tstl.Block {
192+
this.currentSourceFile = node;
193+
this.isModule = tsHelper.isFileModule(node);
194+
195+
// Use `getParseTreeNode` to get original SourceFile node, before it was substituted by custom transformers.
196+
// It's required because otherwise `getEmitResolver` won't use cached diagnostics, produced in `emitWorker`
197+
// and would try to re-analyze the file, which would fail because of replaced nodes.
198+
const originalSourceFile = ts.getParseTreeNode(node, ts.isSourceFile) || node;
199+
this.resolver = this.checker.getEmitResolver(originalSourceFile);
200+
201+
return this.transformSourceFile(node);
202+
}
203+
206204
public transformStatement(node: ts.Statement): StatementVisitResult {
207205
// Ignore declarations
208206
if (node.modifiers && node.modifiers.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)) {

0 commit comments

Comments
 (0)