From 6d2ca2e9a6f2a9d8d44dd726ba451c51daabd558 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Fri, 1 Mar 2019 22:38:47 +0100 Subject: [PATCH 1/6] Added luaOptions tsconfig object, fixed boolean parsing bug --- src/CommandLineParser.ts | 70 ++++++++++++++++++++++------- test/unit/commandLineParser.spec.ts | 18 +++++++- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index 4881adc4d..47fc842c1 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -157,19 +157,55 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult } const configPath = options.project; - const configContents = fs.readFileSync(configPath).toString(); - const configJson = ts.parseConfigFileTextToJson(configPath, configContents); - const parsedJsonConfig = ts.parseJsonConfigFileContent( - configJson.config, - ts.sys, - path.dirname(configPath), - options - ); - - for (const key in parsedJsonConfig.raw) { + const parsedJsonConfig = parseTsConfigFile(configPath); + + return parsedJsonConfig; + } + return { isValid: true, result: parsedCommandLine }; +} + +export function parseTsConfigFile(filePath: string): CLIParseResult { + const configContents = fs.readFileSync(filePath).toString(); + return parseTsConfigFile(configContents); +} + +export function parseTsConfigString( + tsConfigString: string, + configPath: string, + existingOptions?: ts.CompilerOptions +): CLIParseResult { + const configJson = ts.parseConfigFileTextToJson(configPath, tsConfigString); + const parsedJsonConfig = ts.parseJsonConfigFileContent( + configJson.config, + ts.sys, + path.dirname(configPath), + existingOptions + ); + + for (const key in parsedJsonConfig.raw) { + const option = optionDeclarations[key]; + if (option !== undefined) { + const value = readValue(parsedJsonConfig.raw[key], option.type, key); + if (option.choices) { + if (option.choices.indexOf(value) < 0) { + return { + isValid: false, + errorMessage: `Unknown ${key} value '${value}'.\nAccepted values: ${option.choices}`, + }; + } + } + console.warn("[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to look " + + "like { compilerOptions: { }, luaOptions: { } }"); + parsedJsonConfig.options[key] = value; + } + } + + // Eventually we will only look for the luaOptions object for tstl options + if (parsedJsonConfig.raw.luaOptions) { + for (const key in parsedJsonConfig.raw.luaOptions) { const option = optionDeclarations[key]; if (option !== undefined) { - const value = readValue(parsedJsonConfig.raw[key], option.type, key); + const value = readValue(parsedJsonConfig.raw.luaOptions[key], option.type, key); if (option.choices) { if (option.choices.indexOf(value) < 0) { return { @@ -182,9 +218,9 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult parsedJsonConfig.options[key] = value; } } - return { isValid: true, result: parsedJsonConfig }; } - return { isValid: true, result: parsedCommandLine }; + + return { isValid: true, result: parsedJsonConfig }; } function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CLIParseResult { @@ -270,15 +306,15 @@ function getArgumentValue( return { isValid: true, result: value }; } -function readValue(valueString: string, valueType: string, parameterName: string): string | boolean { +function readValue(value: string | boolean, valueType: string, parameterName: string): string | boolean { if (valueType === "boolean") { - return valueString === "true" || valueString === "t" + return value === true || value === "true" || value === "t" ? true : false; } else if (valueType === "enum") { - return valueString.toLowerCase(); + return value.toString().toLowerCase(); } else { - return valueString; + return value; } } diff --git a/test/unit/commandLineParser.spec.ts b/test/unit/commandLineParser.spec.ts index ab20b33d2..d5969bbd6 100644 --- a/test/unit/commandLineParser.spec.ts +++ b/test/unit/commandLineParser.spec.ts @@ -1,6 +1,6 @@ import { Expect, Test, TestCase } from "alsatian"; -import { findConfigFile, parseCommandLine } from "../../src/CommandLineParser"; +import { findConfigFile, parseCommandLine, parseTsConfigString } from "../../src/CommandLineParser"; import { LuaTarget, LuaLibImportKind } from "../../src/CompilerOptions"; export class CommandLineParserTests @@ -226,4 +226,20 @@ export class CommandLineParserTests const result = findConfigFile({ options: {}, fileNames: [], errors: [] }); Expect(result.isValid).toBe(false); } + + @TestCase("{}", undefined) + @TestCase("{ noHeader: true }", true) + @TestCase("{ noHeader: \"true\" }", true) + @TestCase("{ luaOptions: { noHeader: true } }", true) + @TestCase("{ luaOptions: { noHeader: \"true\" } }", true) + @Test("TsConfig noHeader") + public tsConfigNoHeader(tsConfig: string, expected: boolean): void { + const result = parseTsConfigString(tsConfig, ""); + + if (result.isValid) { + Expect(result.result.options.noHeader).toBe(expected); + } else { + Expect(result.isValid).toBeTruthy(); + } + } } From cc269b085eb5c37c4ea8e81426d151793b09ab8b Mon Sep 17 00:00:00 2001 From: Perryvw Date: Fri, 1 Mar 2019 22:41:52 +0100 Subject: [PATCH 2/6] Added missing quotes --- src/CommandLineParser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index 47fc842c1..aa008c7cc 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -194,8 +194,8 @@ export function parseTsConfigString( }; } } - console.warn("[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to look " - + "like { compilerOptions: { }, luaOptions: { } }"); + console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to look ` + + `like { "compilerOptions": { }, "luaOptions": { } }`); parsedJsonConfig.options[key] = value; } } From 15e20426ee6e826c482b21251d0229e5202af030 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Fri, 1 Mar 2019 23:07:01 +0100 Subject: [PATCH 3/6] Fixed mistake reading tsconfig file --- src/CommandLineParser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index aa008c7cc..ac354b475 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -157,16 +157,16 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult } const configPath = options.project; - const parsedJsonConfig = parseTsConfigFile(configPath); + const parsedJsonConfig = parseTsConfigFile(configPath, options); return parsedJsonConfig; } return { isValid: true, result: parsedCommandLine }; } -export function parseTsConfigFile(filePath: string): CLIParseResult { +export function parseTsConfigFile(filePath: string, existingOptions?: ts.CompilerOptions): CLIParseResult { const configContents = fs.readFileSync(filePath).toString(); - return parseTsConfigFile(configContents); + return parseTsConfigString(configContents, filePath, existingOptions); } export function parseTsConfigString( From 2cc9f47f55d89b4b5816b0c721826e14d75c5708 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 3 Mar 2019 19:53:59 +0100 Subject: [PATCH 4/6] Renamed tsconfig object and removed deprectation warning message for now --- src/CommandLineParser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index ac354b475..575b2de25 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -194,15 +194,15 @@ export function parseTsConfigString( }; } } - console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to look ` - + `like { "compilerOptions": { }, "luaOptions": { } }`); + // console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to ` + // + `look like { "compilerOptions": { }, "luaOptions": { } }`); parsedJsonConfig.options[key] = value; } } // Eventually we will only look for the luaOptions object for tstl options if (parsedJsonConfig.raw.luaOptions) { - for (const key in parsedJsonConfig.raw.luaOptions) { + for (const key in parsedJsonConfig.raw.tstl) { const option = optionDeclarations[key]; if (option !== undefined) { const value = readValue(parsedJsonConfig.raw.luaOptions[key], option.type, key); From 46b162b5d088914c2c3d03fdb9a45896c437171f Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 3 Mar 2019 22:34:33 +0100 Subject: [PATCH 5/6] Adjusted tests I forgot --- test/unit/commandLineParser.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/commandLineParser.spec.ts b/test/unit/commandLineParser.spec.ts index d5969bbd6..4e98948b5 100644 --- a/test/unit/commandLineParser.spec.ts +++ b/test/unit/commandLineParser.spec.ts @@ -230,8 +230,8 @@ export class CommandLineParserTests @TestCase("{}", undefined) @TestCase("{ noHeader: true }", true) @TestCase("{ noHeader: \"true\" }", true) - @TestCase("{ luaOptions: { noHeader: true } }", true) - @TestCase("{ luaOptions: { noHeader: \"true\" } }", true) + @TestCase("{ tstl: { noHeader: true } }", true) + @TestCase("{ tstl: { noHeader: \"true\" } }", true) @Test("TsConfig noHeader") public tsConfigNoHeader(tsConfig: string, expected: boolean): void { const result = parseTsConfigString(tsConfig, ""); From f3a0ca72a0a43677b879ee4c5160d69e84d1fb45 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 3 Mar 2019 23:22:58 +0100 Subject: [PATCH 6/6] Really fixed all the stuff I forgot this time --- src/CommandLineParser.ts | 8 ++++---- test/unit/commandLineParser.spec.ts | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/CommandLineParser.ts b/src/CommandLineParser.ts index 575b2de25..2286bfae8 100644 --- a/src/CommandLineParser.ts +++ b/src/CommandLineParser.ts @@ -195,17 +195,17 @@ export function parseTsConfigString( } } // console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to ` - // + `look like { "compilerOptions": { }, "luaOptions": { } }`); + // + `look like { "compilerOptions": { }, "tstl": { } }`); parsedJsonConfig.options[key] = value; } } - // Eventually we will only look for the luaOptions object for tstl options - if (parsedJsonConfig.raw.luaOptions) { + // Eventually we will only look for the tstl object for tstl options + if (parsedJsonConfig.raw.tstl) { for (const key in parsedJsonConfig.raw.tstl) { const option = optionDeclarations[key]; if (option !== undefined) { - const value = readValue(parsedJsonConfig.raw.luaOptions[key], option.type, key); + const value = readValue(parsedJsonConfig.raw.tstl[key], option.type, key); if (option.choices) { if (option.choices.indexOf(value) < 0) { return { diff --git a/test/unit/commandLineParser.spec.ts b/test/unit/commandLineParser.spec.ts index 4e98948b5..000cdd4b3 100644 --- a/test/unit/commandLineParser.spec.ts +++ b/test/unit/commandLineParser.spec.ts @@ -228,10 +228,10 @@ export class CommandLineParserTests } @TestCase("{}", undefined) - @TestCase("{ noHeader: true }", true) - @TestCase("{ noHeader: \"true\" }", true) - @TestCase("{ tstl: { noHeader: true } }", true) - @TestCase("{ tstl: { noHeader: \"true\" } }", true) + @TestCase(`{ noHeader: true }`, true) + @TestCase(`{ noHeader: "true" }`, true) + @TestCase(`{ tstl: { noHeader: true } }`, true) + @TestCase(`{ tstl: { noHeader: "true" } }`, true) @Test("TsConfig noHeader") public tsConfigNoHeader(tsConfig: string, expected: boolean): void { const result = parseTsConfigString(tsConfig, "");