Skip to content

Commit dedee12

Browse files
authored
Added luaOptions tsconfig object, fixed boolean parsing bug (TypeScriptToLua#463)
* Added luaOptions tsconfig object, fixed boolean parsing bug * Added missing quotes * Fixed mistake reading tsconfig file * Renamed tsconfig object and removed deprectation warning message for now * Adjusted tests I forgot * Really fixed all the stuff I forgot this time
1 parent 210449e commit dedee12

2 files changed

Lines changed: 70 additions & 18 deletions

File tree

src/CommandLineParser.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,55 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
157157
}
158158

159159
const configPath = options.project;
160-
const configContents = fs.readFileSync(configPath).toString();
161-
const configJson = ts.parseConfigFileTextToJson(configPath, configContents);
162-
const parsedJsonConfig = ts.parseJsonConfigFileContent(
163-
configJson.config,
164-
ts.sys,
165-
path.dirname(configPath),
166-
options
167-
);
168-
169-
for (const key in parsedJsonConfig.raw) {
160+
const parsedJsonConfig = parseTsConfigFile(configPath, options);
161+
162+
return parsedJsonConfig;
163+
}
164+
return { isValid: true, result: parsedCommandLine };
165+
}
166+
167+
export function parseTsConfigFile(filePath: string, existingOptions?: ts.CompilerOptions): CLIParseResult {
168+
const configContents = fs.readFileSync(filePath).toString();
169+
return parseTsConfigString(configContents, filePath, existingOptions);
170+
}
171+
172+
export function parseTsConfigString(
173+
tsConfigString: string,
174+
configPath: string,
175+
existingOptions?: ts.CompilerOptions
176+
): CLIParseResult {
177+
const configJson = ts.parseConfigFileTextToJson(configPath, tsConfigString);
178+
const parsedJsonConfig = ts.parseJsonConfigFileContent(
179+
configJson.config,
180+
ts.sys,
181+
path.dirname(configPath),
182+
existingOptions
183+
);
184+
185+
for (const key in parsedJsonConfig.raw) {
186+
const option = optionDeclarations[key];
187+
if (option !== undefined) {
188+
const value = readValue(parsedJsonConfig.raw[key], option.type, key);
189+
if (option.choices) {
190+
if (option.choices.indexOf(value) < 0) {
191+
return {
192+
isValid: false,
193+
errorMessage: `Unknown ${key} value '${value}'.\nAccepted values: ${option.choices}`,
194+
};
195+
}
196+
}
197+
// console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to `
198+
// + `look like { "compilerOptions": { <typescript options> }, "tstl": { <tstl options> } }`);
199+
parsedJsonConfig.options[key] = value;
200+
}
201+
}
202+
203+
// Eventually we will only look for the tstl object for tstl options
204+
if (parsedJsonConfig.raw.tstl) {
205+
for (const key in parsedJsonConfig.raw.tstl) {
170206
const option = optionDeclarations[key];
171207
if (option !== undefined) {
172-
const value = readValue(parsedJsonConfig.raw[key], option.type, key);
208+
const value = readValue(parsedJsonConfig.raw.tstl[key], option.type, key);
173209
if (option.choices) {
174210
if (option.choices.indexOf(value) < 0) {
175211
return {
@@ -182,9 +218,9 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
182218
parsedJsonConfig.options[key] = value;
183219
}
184220
}
185-
return { isValid: true, result: parsedJsonConfig };
186221
}
187-
return { isValid: true, result: parsedCommandLine };
222+
223+
return { isValid: true, result: parsedJsonConfig };
188224
}
189225

190226
function parseTSTLOptions(commandLine: ts.ParsedCommandLine, args: string[]): CLIParseResult {
@@ -270,15 +306,15 @@ function getArgumentValue(
270306
return { isValid: true, result: value };
271307
}
272308

273-
function readValue(valueString: string, valueType: string, parameterName: string): string | boolean {
309+
function readValue(value: string | boolean, valueType: string, parameterName: string): string | boolean {
274310
if (valueType === "boolean") {
275-
return valueString === "true" || valueString === "t"
311+
return value === true || value === "true" || value === "t"
276312
? true
277313
: false;
278314
} else if (valueType === "enum") {
279-
return valueString.toLowerCase();
315+
return value.toString().toLowerCase();
280316
} else {
281-
return valueString;
317+
return value;
282318
}
283319
}
284320

test/unit/commandLineParser.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Expect, Test, TestCase } from "alsatian";
22

3-
import { findConfigFile, parseCommandLine } from "../../src/CommandLineParser";
3+
import { findConfigFile, parseCommandLine, parseTsConfigString } from "../../src/CommandLineParser";
44
import { LuaTarget, LuaLibImportKind } from "../../src/CompilerOptions";
55

66
export class CommandLineParserTests
@@ -226,4 +226,20 @@ export class CommandLineParserTests
226226
const result = findConfigFile({ options: {}, fileNames: [], errors: [] });
227227
Expect(result.isValid).toBe(false);
228228
}
229+
230+
@TestCase("{}", undefined)
231+
@TestCase(`{ noHeader: true }`, true)
232+
@TestCase(`{ noHeader: "true" }`, true)
233+
@TestCase(`{ tstl: { noHeader: true } }`, true)
234+
@TestCase(`{ tstl: { noHeader: "true" } }`, true)
235+
@Test("TsConfig noHeader")
236+
public tsConfigNoHeader(tsConfig: string, expected: boolean): void {
237+
const result = parseTsConfigString(tsConfig, "");
238+
239+
if (result.isValid) {
240+
Expect(result.result.options.noHeader).toBe(expected);
241+
} else {
242+
Expect(result.isValid).toBeTruthy();
243+
}
244+
}
229245
}

0 commit comments

Comments
 (0)