Skip to content

Commit f90befb

Browse files
committed
EmitHost -> TranspilerHost
1 parent ae52da3 commit f90befb

10 files changed

Lines changed: 43 additions & 45 deletions

File tree

src/LuaLib.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as path from "path";
2-
import { EmitHost } from "./transpilation";
2+
import { TranspilerHost } from "./transpilation";
33

44
export enum LuaLibFeature {
55
ArrayConcat = "ArrayConcat",
@@ -93,7 +93,7 @@ const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
9393
SymbolRegistry: [LuaLibFeature.Symbol],
9494
};
9595

96-
export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
96+
export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, host: TranspilerHost): string {
9797
let result = "";
9898

9999
const loadedFeatures = new Set<LuaLibFeature>();
@@ -108,7 +108,7 @@ export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost:
108108
}
109109

110110
const featurePath = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
111-
const luaLibFeature = emitHost.readFile(featurePath);
111+
const luaLibFeature = host.readFile(featurePath);
112112
if (luaLibFeature !== undefined) {
113113
result += luaLibFeature + "\n";
114114
} else {
@@ -124,10 +124,10 @@ export function loadLuaLibFeatures(features: Iterable<LuaLibFeature>, emitHost:
124124
}
125125

126126
let luaLibBundleContent: string;
127-
export function getLuaLibBundle(emitHost: EmitHost): string {
127+
export function getLuaLibBundle(host: TranspilerHost): string {
128128
if (luaLibBundleContent === undefined) {
129129
const lualibPath = path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua");
130-
const result = emitHost.readFile(lualibPath);
130+
const result = host.readFile(lualibPath);
131131
if (result !== undefined) {
132132
luaLibBundleContent = result;
133133
} else {

src/LuaPrinter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
55
import * as lua from "./LuaAST";
66
import { loadLuaLibFeatures, LuaLibFeature } from "./LuaLib";
77
import { isValidLuaIdentifier } from "./transformation/utils/safe-names";
8-
import { EmitHost } from "./transpilation";
8+
import { TranspilerHost } from "./transpilation";
99
import { assert, intersperse, invertObject, normalizeSlashes, trimExtension } from "./utils";
1010

1111
// https://www.lua.org/pil/2.4.html
@@ -87,7 +87,7 @@ type SourceChunk = string | SourceNode;
8787

8888
export type Printer = (
8989
program: ts.Program,
90-
emitHost: EmitHost,
90+
host: TranspilerHost,
9191
fileName: string,
9292
block: lua.Block,
9393
luaLibFeatures: Set<LuaLibFeature>
@@ -101,7 +101,7 @@ export interface PrintResult {
101101

102102
export function createPrinter(printers: Printer[]): Printer {
103103
if (printers.length === 0) {
104-
return (program, emitHost, fileName, ...args) => new LuaPrinter(emitHost, program, fileName).print(...args);
104+
return (program, host, fileName, ...args) => new LuaPrinter(host, program, fileName).print(...args);
105105
} else if (printers.length === 1) {
106106
return printers[0];
107107
} else {
@@ -142,7 +142,7 @@ export class LuaPrinter {
142142
private sourceFile: string;
143143
private options: CompilerOptions;
144144

145-
constructor(private emitHost: EmitHost, program: ts.Program, fileName: string) {
145+
constructor(private host: TranspilerHost, program: ts.Program, fileName: string) {
146146
this.options = program.getCompilerOptions();
147147

148148
if (this.options.outDir) {
@@ -234,7 +234,7 @@ export class LuaPrinter {
234234
} else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
235235
// Inline lualib features
236236
header += "-- Lua Library inline imports\n";
237-
header += loadLuaLibFeatures(luaLibFeatures, this.emitHost);
237+
header += loadLuaLibFeatures(luaLibFeatures, this.host);
238238
}
239239

240240
if (this.options.sourceMapTraceback) {

src/transpilation/chunk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949
export function modulesToBundleChunks(transpilation: Transpilation, modules: Module[]): Chunk[] {
5050
const options = transpilation.program.getCompilerOptions() as CompilerOptions;
5151
assert(isBundleEnabled(options));
52-
const projectDirectory = getConfigDirectory(options, transpilation.emitHost);
52+
const projectDirectory = getConfigDirectory(options, transpilation.host);
5353
const outputPath = normalizeSlashes(path.resolve(projectDirectory, options.luaBundle));
5454

5555
// Resolve project settings relative to project file.

src/transpilation/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from "./managed";
22
export * from "./transpile";
33
export * from "./transpiler";
4-
export { EmitHost } from "./utils";

src/transpilation/transpilation.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import { assert, cast, isNonNull, normalizeSlashes, trimExtension } from "../uti
88
import { Chunk, modulesToBundleChunks, modulesToChunks } from "./chunk";
99
import { createResolutionErrorDiagnostic } from "./diagnostics";
1010
import { buildModule, Module } from "./module";
11-
import { Transpiler } from "./transpiler";
12-
import { EmitHost } from "./utils";
11+
import { Transpiler, TranspilerHost } from "./transpiler";
1312

1413
export class Transpilation {
1514
public readonly diagnostics: ts.Diagnostic[] = [];
@@ -18,27 +17,27 @@ export class Transpilation {
1817
public options = this.program.getCompilerOptions() as CompilerOptions;
1918
public rootDir: string;
2019
public outDir: string;
21-
public emitHost: EmitHost;
20+
public host: TranspilerHost;
2221

2322
private readonly implicitScriptExtensions = [".ts", ".tsx", ".js", ".jsx"] as const;
2423
protected resolver: Resolver;
2524

2625
constructor(public transpiler: Transpiler, public program: ts.Program) {
27-
this.emitHost = transpiler.emitHost;
26+
this.host = transpiler.host;
2827

2928
const { rootDir } = program.getCompilerOptions();
3029
this.rootDir =
3130
// getCommonSourceDirectory ignores provided rootDir when TS6059 is emitted
3231
rootDir == null
3332
? program.getCommonSourceDirectory()
34-
: ts.getNormalizedAbsolutePath(rootDir, this.emitHost.getCurrentDirectory());
33+
: ts.getNormalizedAbsolutePath(rootDir, this.host.getCurrentDirectory());
3534

3635
this.outDir = this.options.outDir ?? this.rootDir;
3736

3837
this.resolver = ResolverFactory.createResolver({
3938
extensions: [".lua", ...this.implicitScriptExtensions],
4039
conditionNames: ["lua", `lua:${this.options.luaTarget ?? LuaTarget.Universal}`],
41-
fileSystem: this.emitHost.resolutionFileSystem ?? fs,
40+
fileSystem: this.host.resolutionFileSystem ?? fs,
4241
useSyncFileSystemCalls: true,
4342
});
4443
}
@@ -50,7 +49,7 @@ export class Transpilation {
5049
const lualibRequired = this.modules.some(m => m.code.toString().includes('require("lualib_bundle")'));
5150
if (lualibRequired) {
5251
const fileName = normalizeSlashes(path.resolve(this.rootDir, "lualib_bundle.lua"));
53-
this.modules.unshift({ request: fileName, code: getLuaLibBundle(this.emitHost), isBuilt: true });
52+
this.modules.unshift({ request: fileName, code: getLuaLibBundle(this.host), isBuilt: true });
5453
}
5554

5655
return this.mapModulesToChunks(this.modules);
@@ -86,7 +85,7 @@ export class Transpilation {
8685
// TODO: Load source map files
8786
module = {
8887
request: resolvedPath,
89-
code: cast(this.emitHost.readFile(resolvedPath), isNonNull),
88+
code: cast(this.host.readFile(resolvedPath), isNonNull),
9089
isBuilt: false,
9190
};
9291

src/transpilation/transpile/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createPrinter } from "../../LuaPrinter";
55
import { createVisitorMap, transformSourceFile } from "../../transformation";
66
import { assert, isNonNull } from "../../utils";
77
import { Module } from "../module";
8-
import { EmitHost } from "../utils";
8+
import { TranspilerHost } from "../transpiler";
99
import { getPlugins, Plugin } from "./plugins";
1010
import { getTransformers } from "./transformers";
1111

@@ -19,7 +19,7 @@ export interface TranspileOptions {
1919
}
2020

2121
export function emitProgramModules(
22-
emitHost: EmitHost,
22+
host: TranspilerHost,
2323
writeFileResult: ts.WriteFileCallback,
2424
{ program, sourceFiles: targetSourceFiles, customTransformers = {}, plugins: customPlugins = [] }: TranspileOptions
2525
) {
@@ -66,13 +66,13 @@ export function emitProgramModules(
6666

6767
diagnostics.push(...transformDiagnostics);
6868
if (!options.noEmit && !options.emitDeclarationOnly) {
69-
const printResult = printer(program, emitHost, sourceFile.fileName, luaAst, luaLibFeatures);
69+
const printResult = printer(program, host, sourceFile.fileName, luaAst, luaLibFeatures);
7070

7171
let fileName: string;
7272
if (path.isAbsolute(sourceFile.fileName)) {
7373
fileName = sourceFile.fileName;
7474
} else {
75-
const currentDirectory = emitHost.getCurrentDirectory();
75+
const currentDirectory = host.getCurrentDirectory();
7676
// Having no absolute path in path.resolve would make it fallback to real cwd
7777
assert(path.isAbsolute(currentDirectory), `Invalid path: ${currentDirectory}`);
7878
fileName = path.resolve(currentDirectory, sourceFile.fileName);

src/transpilation/transpiler.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { FileSystem } from "enhanced-resolve";
12
import * as ts from "typescript";
23
import { Transpilation } from "./transpilation";
34
import { emitProgramModules, TranspileOptions } from "./transpile";
4-
import { EmitHost } from "./utils";
5+
6+
export interface TranspilerHost extends Pick<ts.System, "getCurrentDirectory" | "readFile" | "writeFile"> {
7+
resolutionFileSystem?: FileSystem;
8+
}
59

610
export interface TranspilerOptions {
7-
emitHost?: EmitHost;
11+
host?: TranspilerHost;
812
}
913

1014
export interface EmitOptions extends TranspileOptions {
@@ -17,15 +21,15 @@ export interface EmitResult {
1721
}
1822

1923
export class Transpiler {
20-
public emitHost: EmitHost;
21-
constructor({ emitHost = ts.sys }: TranspilerOptions = {}) {
22-
this.emitHost = emitHost;
24+
public host: TranspilerHost;
25+
constructor({ host = ts.sys }: TranspilerOptions = {}) {
26+
this.host = host;
2327
}
2428

2529
public emit(emitOptions: EmitOptions): EmitResult {
26-
const { program, writeFile = this.emitHost.writeFile } = emitOptions;
30+
const { program, writeFile = this.host.writeFile } = emitOptions;
2731
const transpilation = new Transpilation(this, program);
28-
const { diagnostics, modules } = emitProgramModules(this.emitHost, writeFile, emitOptions);
32+
const { diagnostics, modules } = emitProgramModules(this.host, writeFile, emitOptions);
2933
const emitPlan = transpilation.emit(modules);
3034
diagnostics.push(...transpilation.diagnostics);
3135

src/transpilation/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FileSystem } from "enhanced-resolve";
21
import * as fs from "fs";
32
import * as path from "path";
43
import * as resolve from "resolve";
@@ -7,14 +6,11 @@ import * as ts from "typescript";
76
import * as cliDiagnostics from "../cli/diagnostics";
87
import { CompilerOptions } from "../CompilerOptions";
98
import * as diagnosticFactories from "./diagnostics";
10-
11-
export interface EmitHost extends Pick<ts.System, "getCurrentDirectory" | "readFile" | "writeFile"> {
12-
resolutionFileSystem?: FileSystem;
13-
}
9+
import { TranspilerHost } from "./transpiler";
1410

1511
// TODO: Require emit host
16-
export const getConfigDirectory = (options: ts.CompilerOptions, emitHost?: EmitHost) =>
17-
options.configFilePath ? path.dirname(options.configFilePath) : emitHost?.getCurrentDirectory() ?? process.cwd();
12+
export const getConfigDirectory = (options: ts.CompilerOptions, host?: TranspilerHost) =>
13+
options.configFilePath ? path.dirname(options.configFilePath) : host?.getCurrentDirectory() ?? process.cwd();
1814

1915
export function resolvePlugin(
2016
kind: string,

test/transpile/plugins/printer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as tstl from "../../../src";
22

33
const plugin: tstl.Plugin = {
4-
printer(program, emitHost, fileName, ...args) {
5-
const result = new tstl.LuaPrinter(emitHost, program, fileName).print(...args);
4+
printer(program, host, fileName, ...args) {
5+
const result = new tstl.LuaPrinter(host, program, fileName).print(...args);
66
result.code = `-- Plugin\n${result.code}`;
77
return result;
88
},

test/util.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,18 @@ export abstract class TestBuilder {
252252
const collector = createEmitOutputCollector();
253253
const program = this.getProgram();
254254

255-
const emitHost: tstl.EmitHost = { ...ts.sys };
255+
const host: tstl.TranspilerHost = { ...ts.sys };
256256
if (!this.nativeFileSystem) {
257257
const virtualFS = Volume.fromJSON({ ...this.extraRawFiles, ...this.getSourceFiles() }, "/");
258-
emitHost.resolutionFileSystem = virtualFS;
259-
emitHost.getCurrentDirectory = () => "/";
260-
emitHost.readFile = (fileName, encoding = "utf8") =>
258+
host.resolutionFileSystem = virtualFS;
259+
host.getCurrentDirectory = () => "/";
260+
host.readFile = (fileName, encoding = "utf8") =>
261261
fileName.endsWith("lualib_bundle.lua")
262262
? ts.sys.readFile(fileName, encoding)
263263
: (virtualFS.readFileSync(fileName, encoding) as string);
264264
}
265265

266-
const { diagnostics: transpileDiagnostics } = new tstl.Transpiler({ emitHost }).emit({
266+
const { diagnostics: transpileDiagnostics } = new tstl.Transpiler({ host }).emit({
267267
program,
268268
customTransformers: this.customTransformers,
269269
writeFile: collector.writeFile,

0 commit comments

Comments
 (0)