Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface OutputFile {
let lualibContent: string;
export function emitTranspiledFiles(
options: CompilerOptions,
transpiledFiles: Map<string, TranspiledFile>
transpiledFiles: TranspiledFile[]
): OutputFile[] {
let { rootDir, outDir, outFile, luaLibImport } = options;

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

const files: OutputFile[] = [];
for (const [fileName, { lua, sourceMap, declaration, declarationMap }] of transpiledFiles) {
for (const { fileName, lua, sourceMap, declaration, declarationMap } of transpiledFiles) {
let outPath = fileName;
if (outDir !== rootDir) {
outPath = path.resolve(outDir, path.relative(rootDir, fileName));
Expand Down
19 changes: 12 additions & 7 deletions src/Transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function getCustomTransformers(
}

export interface TranspiledFile {
fileName: string;
luaAst?: Block;
lua?: string;
sourceMap?: string;
Expand All @@ -46,7 +47,7 @@ export interface TranspiledFile {

export interface TranspileResult {
diagnostics: ts.Diagnostic[];
transpiledFiles: Map<string, TranspiledFile>;
transpiledFiles: TranspiledFile[];
}

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

const diagnostics: ts.Diagnostic[] = [];
const transpiledFiles = new Map<string, TranspiledFile>();
const updateTranspiledFile = (filePath: string, file: TranspiledFile) => {
if (transpiledFiles.has(filePath)) {
Object.assign(transpiledFiles.get(filePath), file);
let transpiledFiles: TranspiledFile[] = [];

// TODO: Included in TS3.5
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
const updateTranspiledFile = (fileName: string, update: Omit<TranspiledFile, "fileName">) => {
const file = transpiledFiles.find(f => f.fileName === fileName);
if (file) {
Object.assign(file, update);
} else {
transpiledFiles.set(filePath, file);
transpiledFiles.push({ fileName, ...update });
}
};

Expand Down Expand Up @@ -172,7 +177,7 @@ export function transpile({
options.noEmit = oldNoEmit;

if (options.noEmit || (options.noEmitOnError && diagnostics.length > 0)) {
transpiledFiles.clear();
transpiledFiles = [];
}

return { diagnostics, transpiledFiles };
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ export function transpileString(
options: CompilerOptions = {}
): TranspileStringResult {
const { diagnostics, transpiledFiles } = transpileVirtualProject({ "main.ts": main }, options);
return { diagnostics, file: transpiledFiles.get("main.ts") };
return { diagnostics, file: transpiledFiles.find(({ fileName }) => fileName === "main.ts") };
}
2 changes: 1 addition & 1 deletion src/tstl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function performCompilation(
const exitCode =
diagnostics.length === 0
? ts.ExitStatus.Success
: transpiledFiles.size === 0
: transpiledFiles.length === 0
? ts.ExitStatus.DiagnosticsPresent_OutputsSkipped
: ts.ExitStatus.DiagnosticsPresent_OutputsGenerated;

Expand Down
6 changes: 3 additions & 3 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export function transpileStringResult(
optionsWithDefaults,
);

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

return { diagnostics, file: transpiledFiles.get(mainFileName)! };
return { diagnostics, file };
}

const lualibContent = fs.readFileSync(
Expand Down