Skip to content

Commit bd2d81b

Browse files
ark120202Perryvw
authored andcommitted
Make transpiledFiles an Array (#553)
1 parent 9526729 commit bd2d81b

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/Emit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface OutputFile {
1414
let lualibContent: string;
1515
export function emitTranspiledFiles(
1616
options: CompilerOptions,
17-
transpiledFiles: Map<string, TranspiledFile>
17+
transpiledFiles: TranspiledFile[]
1818
): OutputFile[] {
1919
let { rootDir, outDir, outFile, luaLibImport } = options;
2020

@@ -26,7 +26,7 @@ export function emitTranspiledFiles(
2626
outDir = outDir ? path.resolve(baseDir, outDir) : rootDir;
2727

2828
const files: OutputFile[] = [];
29-
for (const [fileName, { lua, sourceMap, declaration, declarationMap }] of transpiledFiles) {
29+
for (const { fileName, lua, sourceMap, declaration, declarationMap } of transpiledFiles) {
3030
let outPath = fileName;
3131
if (outDir !== rootDir) {
3232
outPath = path.resolve(outDir, path.relative(rootDir, fileName));

src/Transpile.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function getCustomTransformers(
3737
}
3838

3939
export interface TranspiledFile {
40+
fileName: string;
4041
luaAst?: Block;
4142
lua?: string;
4243
sourceMap?: string;
@@ -46,7 +47,7 @@ export interface TranspiledFile {
4647

4748
export interface TranspileResult {
4849
diagnostics: ts.Diagnostic[];
49-
transpiledFiles: Map<string, TranspiledFile>;
50+
transpiledFiles: TranspiledFile[];
5051
}
5152

5253
export interface TranspileOptions {
@@ -67,12 +68,16 @@ export function transpile({
6768
const options = program.getCompilerOptions() as CompilerOptions;
6869

6970
const diagnostics: ts.Diagnostic[] = [];
70-
const transpiledFiles = new Map<string, TranspiledFile>();
71-
const updateTranspiledFile = (filePath: string, file: TranspiledFile) => {
72-
if (transpiledFiles.has(filePath)) {
73-
Object.assign(transpiledFiles.get(filePath), file);
71+
let transpiledFiles: TranspiledFile[] = [];
72+
73+
// TODO: Included in TS3.5
74+
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
75+
const updateTranspiledFile = (fileName: string, update: Omit<TranspiledFile, "fileName">) => {
76+
const file = transpiledFiles.find(f => f.fileName === fileName);
77+
if (file) {
78+
Object.assign(file, update);
7479
} else {
75-
transpiledFiles.set(filePath, file);
80+
transpiledFiles.push({ fileName, ...update });
7681
}
7782
};
7883

@@ -172,7 +177,7 @@ export function transpile({
172177
options.noEmit = oldNoEmit;
173178

174179
if (options.noEmit || (options.noEmitOnError && diagnostics.length > 0)) {
175-
transpiledFiles.clear();
180+
transpiledFiles = [];
176181
}
177182

178183
return { diagnostics, transpiledFiles };

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,5 @@ export function transpileString(
121121
options: CompilerOptions = {}
122122
): TranspileStringResult {
123123
const { diagnostics, transpiledFiles } = transpileVirtualProject({ "main.ts": main }, options);
124-
return { diagnostics, file: transpiledFiles.get("main.ts") };
124+
return { diagnostics, file: transpiledFiles.find(({ fileName }) => fileName === "main.ts") };
125125
}

src/tstl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function performCompilation(
170170
const exitCode =
171171
diagnostics.length === 0
172172
? ts.ExitStatus.Success
173-
: transpiledFiles.size === 0
173+
: transpiledFiles.length === 0
174174
? ts.ExitStatus.DiagnosticsPresent_OutputsSkipped
175175
: ts.ExitStatus.DiagnosticsPresent_OutputsGenerated;
176176

test/util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export function transpileStringResult(
4444
optionsWithDefaults,
4545
);
4646

47-
const mainFileName = [...transpiledFiles.keys()].find(x => /\bmain\.[a-z]+$/.test(x));
48-
if (mainFileName === undefined) {
47+
const file = transpiledFiles.find(({ fileName }) => /\bmain\.[a-z]+$/.test(fileName));
48+
if (file === undefined) {
4949
throw new Error('Program should have a file named "main"');
5050
}
5151

52-
return { diagnostics, file: transpiledFiles.get(mainFileName)! };
52+
return { diagnostics, file };
5353
}
5454

5555
const lualibContent = fs.readFileSync(

0 commit comments

Comments
 (0)