From c310f986634086fc6f6f63e2c63ed5e710991f74 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 10 Jul 2021 13:52:10 +0200 Subject: [PATCH 1/3] Disallow bundling with buildmode library --- src/CompilerOptions.ts | 4 ++++ src/transpilation/diagnostics.ts | 5 +++++ test/transpile/module-resolution.spec.ts | 19 ------------------- test/unit/__snapshots__/bundle.spec.ts.snap | 2 ++ test/unit/bundle.spec.ts | 8 +++++++- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/CompilerOptions.ts b/src/CompilerOptions.ts index e06adc1f3..50e0f3fa7 100644 --- a/src/CompilerOptions.ts +++ b/src/CompilerOptions.ts @@ -73,5 +73,9 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] { diagnostics.push(diagnosticFactories.usingLuaBundleWithInlineMightGenerateDuplicateCode()); } + if (options.luaBundle && options.buildMode === BuildMode.Library) { + diagnostics.push(diagnosticFactories.cannotBundleLibrary()); + } + return diagnostics; } diff --git a/src/transpilation/diagnostics.ts b/src/transpilation/diagnostics.ts index bfae6b140..4b57c329d 100644 --- a/src/transpilation/diagnostics.ts +++ b/src/transpilation/diagnostics.ts @@ -48,3 +48,8 @@ export const usingLuaBundleWithInlineMightGenerateDuplicateCode = createSerialDi "Using 'luaBundle' with 'luaLibImport: \"inline\"' might generate duplicate code. " + "It is recommended to use 'luaLibImport: \"require\"'.", })); + +export const cannotBundleLibrary = createDiagnosticFactory( + () => + 'Cannot bundle probjects with"buildmode": "library". Projects including the library can still bundle (which will include external library files).' +); diff --git a/test/transpile/module-resolution.spec.ts b/test/transpile/module-resolution.spec.ts index 872bd47c7..b995e75a5 100644 --- a/test/transpile/module-resolution.spec.ts +++ b/test/transpile/module-resolution.spec.ts @@ -230,25 +230,6 @@ describe("module resolution in library mode", () => { expect(file.lua).not.toContain('require("lua_modules'); } }); - - test("bundle works in library mode because no external dependencies", () => { - const projectPath = path.resolve(__dirname, "module-resolution", "project-with-lua-sources"); - const mainFile = path.join(projectPath, "main.ts"); - - const { transpiledFiles } = util - .testProject(path.join(projectPath, "tsconfig.json")) - .setMainFileName(path.join(projectPath, "main.ts")) - .setOptions({ buildMode: tstl.BuildMode.Library, luaBundle: "bundle.lua", luaBundleEntry: mainFile }) - .expectToEqual({ - funcFromLuaFile: "lua file in subdir", - funcFromSubDirLuaFile: "lua file in subdir", - }) - .getLuaResult(); - - for (const file of transpiledFiles) { - expect(file.lua).not.toContain('require("lua_modules'); - } - }); }); describe("module resolution project with dependencies built by tstl library mode", () => { diff --git a/test/unit/__snapshots__/bundle.spec.ts.snap b/test/unit/__snapshots__/bundle.spec.ts.snap index 84c10f3d5..20d4ec657 100644 --- a/test/unit/__snapshots__/bundle.spec.ts.snap +++ b/test/unit/__snapshots__/bundle.spec.ts.snap @@ -2,6 +2,8 @@ exports[`LuaLibImportKind.Inline generates a warning: diagnostics 1`] = `"warning TSTL: Using 'luaBundle' with 'luaLibImport: \\"inline\\"' might generate duplicate code. It is recommended to use 'luaLibImport: \\"require\\"'."`; +exports[`bundling not allowed for buildmode library: diagnostics 1`] = `"error TSTL: Cannot bundle probjects with\\"buildmode\\": \\"library\\". Projects including the library can still bundle (which will include external library files)."`; + exports[`luaEntry doesn't exist: diagnostics 1`] = `"error TSTL: Could not find bundle entry point 'entry.ts'. It should be a file in the project."`; exports[`no entry point: diagnostics 1`] = `"error TSTL: 'luaBundleEntry' is required when 'luaBundle' is enabled."`; diff --git a/test/unit/bundle.spec.ts b/test/unit/bundle.spec.ts index f961dddb0..ddc678f34 100644 --- a/test/unit/bundle.spec.ts +++ b/test/unit/bundle.spec.ts @@ -1,4 +1,4 @@ -import { LuaLibImportKind } from "../../src"; +import { BuildMode, LuaLibImportKind } from "../../src"; import * as diagnosticFactories from "../../src/transpilation/diagnostics"; import * as util from "../util"; @@ -120,3 +120,9 @@ test("luaEntry doesn't exist", () => { .setEntryPoint("entry.ts") .expectDiagnosticsToMatchSnapshot([diagnosticFactories.couldNotFindBundleEntryPoint.code], true); }); + +test("bundling not allowed for buildmode library", () => { + util.testBundle`` + .setOptions({ buildMode: BuildMode.Library }) + .expectDiagnosticsToMatchSnapshot([diagnosticFactories.cannotBundleLibrary.code], true); +}); From 8605f6bd52760e1d66c5e977f2c6663279f1fac2 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 10 Jul 2021 13:59:46 +0200 Subject: [PATCH 2/3] Add tstl verbose CLI flag --- package-lock.json | 14 ++--- package.json | 2 +- src/CompilerOptions.ts | 11 ++-- src/transformation/context/context.ts | 3 +- src/transpilation/resolve.ts | 19 ++++++- src/transpilation/transpile.ts | 17 ++++++ src/transpilation/transpiler.ts | 25 ++++++++- src/typescript-internal.d.ts | 3 ++ .../__snapshots__/project.spec.ts.snap | 20 +++++++ test/transpile/project.spec.ts | 20 +++++++ test/transpile/transformers/fixtures.ts | 52 ++++++++++--------- test/unit/modules/resolution.spec.ts | 10 ++-- test/util.ts | 38 +++++++------- 13 files changed, 166 insertions(+), 68 deletions(-) diff --git a/package-lock.json b/package-lock.json index c3acb08d1..0d5b08870 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,7 +34,7 @@ "jest-circus": "^25.1.0", "lua-types": "^2.8.0", "lua-wasm-bindings": "^0.2.2", - "prettier": "^2.0.5", + "prettier": "^2.3.2", "ts-jest": "^26.3.0", "ts-node": "^8.6.2" }, @@ -9554,9 +9554,9 @@ } }, "node_modules/prettier": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", - "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", + "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", "dev": true, "bin": { "prettier": "bin-prettier.js" @@ -19259,9 +19259,9 @@ "dev": true }, "prettier": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", - "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", + "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", "dev": true }, "pretty-format": { diff --git a/package.json b/package.json index f96178a81..a45f56929 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "jest-circus": "^25.1.0", "lua-types": "^2.8.0", "lua-wasm-bindings": "^0.2.2", - "prettier": "^2.0.5", + "prettier": "^2.3.2", "ts-jest": "^26.3.0", "ts-node": "^8.6.2" } diff --git a/src/CompilerOptions.ts b/src/CompilerOptions.ts index 50e0f3fa7..0610d26b3 100644 --- a/src/CompilerOptions.ts +++ b/src/CompilerOptions.ts @@ -1,13 +1,9 @@ import * as ts from "typescript"; import * as diagnosticFactories from "./transpilation/diagnostics"; -type KnownKeys = { [K in keyof T]: string extends K ? never : number extends K ? never : K } extends { - [K in keyof T]: infer U; -} - ? U - : never; - -type OmitIndexSignature> = Pick>; +type OmitIndexSignature = { + [K in keyof T as string extends K ? never : number extends K ? never : K]: T[K]; +}; export interface TransformerImport { transform: string; @@ -35,6 +31,7 @@ export type CompilerOptions = OmitIndexSignature & { sourceMapTraceback?: boolean; luaPlugins?: LuaPluginImport[]; plugins?: Array; + tstlVerbose?: boolean; [option: string]: any; }; diff --git a/src/transformation/context/context.ts b/src/transformation/context/context.ts index 4ee4515c4..4dbcf9e10 100644 --- a/src/transformation/context/context.ts +++ b/src/transformation/context/context.ts @@ -27,8 +27,7 @@ export interface DiagnosticsProducingTypeChecker extends ts.TypeChecker { export class TransformationContext { public readonly diagnostics: ts.Diagnostic[] = []; - public readonly checker: DiagnosticsProducingTypeChecker = (this - .program as any).getDiagnosticsProducingTypeChecker(); + public readonly checker: DiagnosticsProducingTypeChecker = this.program.getDiagnosticsProducingTypeChecker(); public readonly resolver: EmitResolver; public readonly options: CompilerOptions = this.program.getCompilerOptions(); diff --git a/src/transpilation/resolve.ts b/src/transpilation/resolve.ts index a9b6b4e1a..5334c0ca7 100644 --- a/src/transpilation/resolve.ts +++ b/src/transpilation/resolve.ts @@ -5,9 +5,9 @@ import * as fs from "fs"; import { EmitHost, ProcessedFile } from "./utils"; import { SourceNode } from "source-map"; import { getEmitPathRelativeToOutDir, getProjectRoot, getSourceDir } from "./transpiler"; -import { formatPathToLuaPath, trimExtension } from "../utils"; +import { formatPathToLuaPath, normalizeSlashes, trimExtension } from "../utils"; import { couldNotReadDependency, couldNotResolveRequire } from "./diagnostics"; -import { BuildMode } from "../CompilerOptions"; +import { BuildMode, CompilerOptions } from "../CompilerOptions"; const resolver = resolve.ResolverFactory.createResolver({ extensions: [".lua"], @@ -24,9 +24,14 @@ interface ResolutionResult { export function resolveDependencies(program: ts.Program, files: ProcessedFile[], emitHost: EmitHost): ResolutionResult { const outFiles: ProcessedFile[] = [...files]; const diagnostics: ts.Diagnostic[] = []; + const options = program.getCompilerOptions() as CompilerOptions; // Resolve dependencies for all processed files for (const file of files) { + if (options.tstlVerbose) { + console.log(`Resolving dependencies for ${normalizeSlashes(file.fileName)}`); + } + const resolutionResult = resolveFileDependencies(file, program, emitHost); outFiles.push(...resolutionResult.resolvedFiles); diagnostics.push(...resolutionResult.diagnostics); @@ -38,6 +43,7 @@ export function resolveDependencies(program: ts.Program, files: ProcessedFile[], function resolveFileDependencies(file: ProcessedFile, program: ts.Program, emitHost: EmitHost): ResolutionResult { const dependencies: ProcessedFile[] = []; const diagnostics: ts.Diagnostic[] = []; + const options = program.getCompilerOptions() as CompilerOptions; for (const required of findRequiredPaths(file.code)) { // Do no resolve lualib @@ -57,6 +63,10 @@ function resolveFileDependencies(file: ProcessedFile, program: ts.Program, emitH const fileDir = path.dirname(file.fileName); const resolvedDependency = resolveDependency(fileDir, required, program, emitHost); if (resolvedDependency) { + if (options.tstlVerbose) { + console.log(`Resolved ${required} to ${normalizeSlashes(resolvedDependency)}`); + } + // Figure out resolved require path and dependency output path const resolvedRequire = getEmitPathRelativeToOutDir(resolvedDependency, program); @@ -100,6 +110,11 @@ function resolveDependency( program: ts.Program, emitHost: EmitHost ): string | undefined { + const options = program.getCompilerOptions() as CompilerOptions; + if (options.tstlVerbose) { + console.log(`Resolving "${dependency}" from ${normalizeSlashes(fileDirectory)}`); + } + // Check if file is a file in the project const resolvedPath = path.join(fileDirectory, dependency); diff --git a/src/transpilation/transpile.ts b/src/transpilation/transpile.ts index 0d3071f73..d78162fd8 100644 --- a/src/transpilation/transpile.ts +++ b/src/transpilation/transpile.ts @@ -27,6 +27,10 @@ export function getProgramTranspileResult( ): TranspileResult { const options = program.getCompilerOptions() as CompilerOptions; + if (options.tstlVerbose) { + console.log("Parsing project settings"); + } + const diagnostics = validateOptions(options); let transpiledFiles: ProcessedFile[] = []; @@ -57,13 +61,26 @@ export function getProgramTranspileResult( } const plugins = getPlugins(program, diagnostics, customPlugins); + + if (options.tstlVerbose) { + console.log(`Successfully loaded ${plugins.length} plugins`); + } + const visitorMap = createVisitorMap(plugins.map(p => p.visitors).filter(isNonNull)); const printer = createPrinter(plugins.map(p => p.printer).filter(isNonNull)); const processSourceFile = (sourceFile: ts.SourceFile) => { + if (options.tstlVerbose) { + console.log(`Transforming ${sourceFile.fileName}`); + } + const { file, diagnostics: transformDiagnostics } = transformSourceFile(program, sourceFile, visitorMap); diagnostics.push(...transformDiagnostics); if (!options.noEmit && !options.emitDeclarationOnly) { + if (options.tstlVerbose) { + console.log(`Printing ${sourceFile.fileName}`); + } + const printResult = printer(program, emitHost, sourceFile.fileName, file); transpiledFiles.push({ sourceFiles: [sourceFile], diff --git a/src/transpilation/transpiler.ts b/src/transpilation/transpiler.ts index 2dc5a9fc7..b19790b37 100644 --- a/src/transpilation/transpiler.ts +++ b/src/transpilation/transpiler.ts @@ -1,6 +1,6 @@ import * as path from "path"; import * as ts from "typescript"; -import { isBundleEnabled } from "../CompilerOptions"; +import { CompilerOptions, isBundleEnabled } from "../CompilerOptions"; import { getLuaLibBundle } from "../LuaLib"; import { normalizeSlashes, trimExtension } from "../utils"; import { getBundleResult } from "./bundle"; @@ -29,6 +29,7 @@ export class Transpiler { public emit(emitOptions: EmitOptions): EmitResult { const { program, writeFile = this.emitHost.writeFile } = emitOptions; + const verbose = (program.getCompilerOptions() as CompilerOptions).tstlVerbose; const { diagnostics, transpiledFiles: freshFiles } = getProgramTranspileResult( this.emitHost, writeFile, @@ -37,15 +38,27 @@ export class Transpiler { const { emitPlan } = this.getEmitPlan(program, diagnostics, freshFiles); + if (verbose) { + console.log("Emitting output"); + } + const options = program.getCompilerOptions(); const emitBOM = options.emitBOM ?? false; for (const { outputPath, code, sourceMap, sourceFiles } of emitPlan) { + if (verbose) { + console.log(`Emitting ${normalizeSlashes(outputPath)}`); + } + writeFile(outputPath, code, emitBOM, undefined, sourceFiles); if (options.sourceMap && sourceMap !== undefined) { writeFile(outputPath + ".map", sourceMap, emitBOM, undefined, sourceFiles); } } + if (verbose) { + console.log("Emit finished!"); + } + return { diagnostics, emitSkipped: emitPlan.length === 0 }; } @@ -54,10 +67,18 @@ export class Transpiler { diagnostics: ts.Diagnostic[], files: ProcessedFile[] ): { emitPlan: EmitFile[] } { - const options = program.getCompilerOptions(); + const options = program.getCompilerOptions() as CompilerOptions; + + if (options.tstlVerbose) { + console.log("Constructing emit plan"); + } const lualibRequired = files.some(f => f.code.includes('require("lualib_bundle")')); if (lualibRequired) { + if (options.tstlVerbose) { + console.log("Including lualib bundle"); + } + // Add lualib bundle to source dir 'virtually', will be moved to correct output dir in emitPlan const fileName = normalizeSlashes(path.resolve(getSourceDir(program), "lualib_bundle.lua")); files.unshift({ fileName, code: getLuaLibBundle(this.emitHost) }); diff --git a/src/typescript-internal.d.ts b/src/typescript-internal.d.ts index 7444d89d7..7bfd309ac 100644 --- a/src/typescript-internal.d.ts +++ b/src/typescript-internal.d.ts @@ -1,3 +1,5 @@ +import { DiagnosticsProducingTypeChecker } from "./transformation/context"; + export {}; declare module "typescript" { @@ -14,6 +16,7 @@ declare module "typescript" { interface Program { getCommonSourceDirectory(): string; + getDiagnosticsProducingTypeChecker(): DiagnosticsProducingTypeChecker; } interface CompilerOptions { diff --git a/test/transpile/__snapshots__/project.spec.ts.snap b/test/transpile/__snapshots__/project.spec.ts.snap index 623480405..1effba209 100644 --- a/test/transpile/__snapshots__/project.spec.ts.snap +++ b/test/transpile/__snapshots__/project.spec.ts.snap @@ -1,5 +1,25 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should give verbose output 1`] = ` +Array [ + "Parsing project settings", + "Successfully loaded 0 plugins", + "Transforming /test/transpile/project/otherFile.ts", + "Printing /test/transpile/project/otherFile.ts", + "Transforming /test/transpile/project/index.ts", + "Printing /test/transpile/project/index.ts", + "Constructing emit plan", + "Resolving dependencies for /test/transpile/project/otherFile.ts", + "Resolving dependencies for /test/transpile/project/index.ts", + "Resolving \\"./otherFile\\" from /test/transpile/project", + "Resolved ./otherFile to /test/transpile/project/otherFile.ts", + "Emitting output", + "Emitting /test/transpile/project/otherFile.lua", + "Emitting /test/transpile/project/index.lua", + "Emit finished!", +] +`; + exports[`should transpile 1`] = ` Array [ Object { diff --git a/test/transpile/project.spec.ts b/test/transpile/project.spec.ts index 965c834d7..49234975b 100644 --- a/test/transpile/project.spec.ts +++ b/test/transpile/project.spec.ts @@ -1,4 +1,5 @@ import * as path from "path"; +import { normalizeSlashes } from "../../src/utils"; import * as util from "../util"; test("should transpile", () => { @@ -13,3 +14,22 @@ test("should transpile", () => { transpiledFiles.map(f => ({ filePath: path.relative(projectDir, f.outPath), lua: f.lua })) ).toMatchSnapshot(); }); + +test("should give verbose output", () => { + // Capture console logs + const consoleLogs: string[] = []; + const originalLog = console.log; + console.log = (...args: any[]) => { + consoleLogs.push(args.map(a => a.toString().replace(normalizeSlashes(process.cwd()), "")).join(",")); + }; + + const projectDir = path.join(__dirname, "project"); + util.testProject(path.join(projectDir, "tsconfig.json")) + .setMainFileName(path.join(projectDir, "index.ts")) + .setOptions({ tstlVerbose: true }) + .expectToHaveNoDiagnostics(); + + console.log = originalLog; + + expect(consoleLogs).toMatchSnapshot(); +}); diff --git a/test/transpile/transformers/fixtures.ts b/test/transpile/transformers/fixtures.ts index a69829d5d..dff7667be 100644 --- a/test/transpile/transformers/fixtures.ts +++ b/test/transpile/transformers/fixtures.ts @@ -6,22 +6,25 @@ import { visitAndReplace } from "./utils"; export const program = (program: ts.Program, options: { value: any }): ts.TransformerFactory => checker(program.getTypeChecker(), options); -export const config = ({ value }: { value: any }): ts.TransformerFactory => context => file => - visitAndReplace(context, file, node => { - if (!ts.isReturnStatement(node)) return; - return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); - }); +export const config = + ({ value }: { value: any }): ts.TransformerFactory => + context => + file => + visitAndReplace(context, file, node => { + if (!ts.isReturnStatement(node)) return; + return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); + }); -export const checker = ( - checker: ts.TypeChecker, - { value }: { value: any } -): ts.TransformerFactory => context => file => - visitAndReplace(context, file, node => { - if (!ts.isReturnStatement(node) || !node.expression) return; - const type = checker.getTypeAtLocation(node.expression); - if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) return; - return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); - }); +export const checker = + (checker: ts.TypeChecker, { value }: { value: any }): ts.TransformerFactory => + context => + file => + visitAndReplace(context, file, node => { + if (!ts.isReturnStatement(node) || !node.expression) return; + const type = checker.getTypeAtLocation(node.expression); + if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) return; + return ts.factory.updateReturnStatement(node, value ? ts.factory.createTrue() : ts.factory.createFalse()); + }); export const raw: ts.TransformerFactory = context => file => visitAndReplace(context, file, node => { @@ -29,12 +32,13 @@ export const raw: ts.TransformerFactory = context => file => return ts.factory.updateReturnStatement(node, ts.factory.createTrue()); }); -export const compilerOptions = ( - options: tstl.CompilerOptions -): ts.TransformerFactory => context => file => { - assert(options.plugins?.length === 1); - return visitAndReplace(context, file, node => { - if (!ts.isReturnStatement(node)) return; - return ts.factory.updateReturnStatement(node, ts.factory.createTrue()); - }); -}; +export const compilerOptions = + (options: tstl.CompilerOptions): ts.TransformerFactory => + context => + file => { + assert(options.plugins?.length === 1); + return visitAndReplace(context, file, node => { + if (!ts.isReturnStatement(node)) return; + return ts.factory.updateReturnStatement(node, ts.factory.createTrue()); + }); + }; diff --git a/test/unit/modules/resolution.spec.ts b/test/unit/modules/resolution.spec.ts index 791ae06b5..b2c7194b5 100644 --- a/test/unit/modules/resolution.spec.ts +++ b/test/unit/modules/resolution.spec.ts @@ -3,10 +3,12 @@ import { couldNotResolveRequire } from "../../../src/transpilation/diagnostics"; import * as util from "../../util"; const requireRegex = /require\("(.*?)"\)/; -const expectToRequire = (expected: string): util.TapCallback => builder => { - const [, requiredPath] = builder.getMainLuaCodeChunk().match(requireRegex) ?? []; - expect(requiredPath).toBe(expected); -}; +const expectToRequire = + (expected: string): util.TapCallback => + builder => { + const [, requiredPath] = builder.getMainLuaCodeChunk().match(requireRegex) ?? []; + expect(requiredPath).toBe(expected); + }; test.each([ { diff --git a/test/util.ts b/test/util.ts index 39259e784..b493f43e0 100644 --- a/test/util.ts +++ b/test/util.ts @@ -239,8 +239,9 @@ export abstract class TestBuilder { @memoize public getMainJsCodeChunk(): string { const { transpiledFiles } = this.getJsResult(); - const code = transpiledFiles.find(({ sourceFiles }) => sourceFiles.some(f => f.fileName === this.mainFileName)) - ?.js; + const code = transpiledFiles.find(({ sourceFiles }) => + sourceFiles.some(f => f.fileName === this.mainFileName) + )?.js; assert(code !== undefined); const header = this.jsHeader ? `${this.jsHeader.trimRight()}\n` : ""; @@ -557,25 +558,24 @@ class ProjectTestBuilder extends ModuleTestBuilder { } } -const createTestBuilderFactory = ( - builder: new (_tsCode: string) => T, - serializeSubstitutions: boolean -) => (...args: [string] | [TemplateStringsArray, ...any[]]): T => { - let tsCode: string; - if (typeof args[0] === "string") { - expect(serializeSubstitutions).toBe(false); - tsCode = args[0]; - } else { - let [raw, ...substitutions] = args; - if (serializeSubstitutions) { - substitutions = substitutions.map(s => formatCode(s)); - } +const createTestBuilderFactory = + (builder: new (_tsCode: string) => T, serializeSubstitutions: boolean) => + (...args: [string] | [TemplateStringsArray, ...any[]]): T => { + let tsCode: string; + if (typeof args[0] === "string") { + expect(serializeSubstitutions).toBe(false); + tsCode = args[0]; + } else { + let [raw, ...substitutions] = args; + if (serializeSubstitutions) { + substitutions = substitutions.map(s => formatCode(s)); + } - tsCode = String.raw(Object.assign([], { raw }), ...substitutions); - } + tsCode = String.raw(Object.assign([], { raw }), ...substitutions); + } - return new builder(tsCode); -}; + return new builder(tsCode); + }; export const testBundle = createTestBuilderFactory(BundleTestBuilder, false); export const testModule = createTestBuilderFactory(ModuleTestBuilder, false); From f8667b9c3150ccd5a973ec976b20da21e974f262 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 10 Jul 2021 14:32:24 +0200 Subject: [PATCH 3/3] Add tstlverbose to CLI parser --- src/cli/parse.ts | 5 +++++ test/cli/parse.spec.ts | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/cli/parse.ts b/src/cli/parse.ts index d29bf4e83..b7ed906e3 100644 --- a/src/cli/parse.ts +++ b/src/cli/parse.ts @@ -73,6 +73,11 @@ export const optionDeclarations: CommandLineOption[] = [ description: "List of TypeScriptToLua plugins.", type: "object", }, + { + name: "tstlVerbose", + description: "Provide verbose output useful for diagnosing problems.", + type: "boolean", + }, ]; export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine { diff --git a/test/cli/parse.spec.ts b/test/cli/parse.spec.ts index f66b97766..f157c9a21 100644 --- a/test/cli/parse.spec.ts +++ b/test/cli/parse.spec.ts @@ -104,6 +104,8 @@ describe("command line", () => { ["noHeader", "true", { noHeader: true }], ["sourceMapTraceback", "false", { sourceMapTraceback: false }], ["sourceMapTraceback", "true", { sourceMapTraceback: true }], + ["tstlVerbose", "true", { tstlVerbose: true }], + ["tstlVerbose", "false", { tstlVerbose: false }], ["buildMode", "default", { buildMode: tstl.BuildMode.Default }], ["buildMode", "library", { buildMode: tstl.BuildMode.Library }],