Skip to content

Commit 3148739

Browse files
authored
Correctly merge tstl options from extended tsconfig (TypeScriptToLua#1436)
* Correctly merge tstl options from extended tsconfig * Remove useless else statement * Handle tsconfig files with comments
1 parent 2982567 commit 3148739

File tree

9 files changed

+124
-3
lines changed

9 files changed

+124
-3
lines changed

src/cli/tsconfig.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as ts from "typescript";
3-
import { CompilerOptions } from "../CompilerOptions";
3+
import { CompilerOptions, TypeScriptToLuaOptions } from "../CompilerOptions";
44
import { normalizeSlashes } from "../utils";
55
import * as cliDiagnostics from "./diagnostics";
66
import { ParsedCommandLine, updateParsedConfigFile } from "./parse";
@@ -41,17 +41,73 @@ export function parseConfigFileWithSystem(
4141
commandLineOptions?: CompilerOptions,
4242
system = ts.sys
4343
): ParsedCommandLine {
44+
const configRootDir = path.dirname(configFileName);
4445
const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(
4546
ts.readJsonConfigFile(configFileName, system.readFile),
4647
system,
47-
path.dirname(configFileName),
48+
configRootDir,
4849
commandLineOptions,
4950
configFileName
5051
);
5152

53+
const cycleCache = new Set<string>();
54+
const extendedTstlOptions = getExtendedTstlOptions(configFileName, configRootDir, cycleCache, system);
55+
56+
parsedConfigFile.raw.tstl = Object.assign(extendedTstlOptions, parsedConfigFile.raw.tstl ?? {});
57+
5258
return updateParsedConfigFile(parsedConfigFile);
5359
}
5460

61+
function getExtendedTstlOptions(
62+
configFilePath: string,
63+
configRootDir: string,
64+
cycleCache: Set<string>,
65+
system: ts.System
66+
): TypeScriptToLuaOptions {
67+
const absolutePath = path.isAbsolute(configFilePath) ? configFilePath : path.resolve(configRootDir, configFilePath);
68+
const newConfigRoot = path.dirname(absolutePath);
69+
70+
if (cycleCache.has(absolutePath)) {
71+
return {};
72+
}
73+
74+
cycleCache.add(absolutePath);
75+
const fileContent = system.readFile(absolutePath);
76+
const options = {};
77+
78+
if (fileContent) {
79+
const { config: parsedConfig } = ts.parseConfigFileTextToJson(configFilePath, fileContent) as {
80+
config?: {
81+
extends?: string | string[];
82+
tstl?: TypeScriptToLuaOptions;
83+
};
84+
};
85+
86+
if (!parsedConfig) {
87+
return {};
88+
}
89+
90+
if (parsedConfig.extends) {
91+
if (Array.isArray(parsedConfig.extends)) {
92+
for (const extendedConfigFile of parsedConfig.extends) {
93+
Object.assign(
94+
options,
95+
getExtendedTstlOptions(extendedConfigFile, newConfigRoot, cycleCache, system)
96+
);
97+
}
98+
} else {
99+
Object.assign(options, getExtendedTstlOptions(parsedConfig.extends, newConfigRoot, cycleCache, system));
100+
}
101+
}
102+
103+
if (parsedConfig.tstl) {
104+
Object.assign(options, parsedConfig.tstl);
105+
}
106+
}
107+
108+
return options;
109+
}
110+
55111
export function createConfigFileUpdater(
56112
optionsToExtend: CompilerOptions
57113
): (options: ts.CompilerOptions) => ts.Diagnostic[] {

test/cli/tsconfig.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from "fs-extra";
22
import * as os from "os";
33
import * as path from "path";
4-
import { locateConfigFile } from "../../src/cli/tsconfig";
4+
import { locateConfigFile, parseConfigFileWithSystem } from "../../src/cli/tsconfig";
55
import { normalizeSlashes } from "../../src/utils";
66

77
let temp: string;
@@ -91,3 +91,25 @@ describe("errors", () => {
9191
expect([locate("tsconfig.json", [""])]).toHaveDiagnostics();
9292
});
9393
});
94+
95+
describe("tsconfig extends", () => {
96+
test("correctly merges extended tsconfig files", () => {
97+
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.json"));
98+
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.3", noHeader: true });
99+
});
100+
101+
test("can handle multiple extends", () => {
102+
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.multi-extends.json"));
103+
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.4", sourceMapTraceback: true });
104+
});
105+
106+
test("can handle cycles in configs", () => {
107+
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig-cycle1.json"));
108+
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.4" });
109+
});
110+
111+
test("can handle tsconfig files with comments", () => {
112+
const parsedConfig = parseConfigFileWithSystem(path.join(__dirname, "tsconfig", "tsconfig.with-comments.json"));
113+
expect(parsedConfig.options).toMatchObject({ luaTarget: "5.3" });
114+
});
115+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig-cycle2.json",
3+
"tstl": {
4+
"luaTarget": "5.4"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig-cycle1.json",
3+
"tstl": {
4+
"luaTarget": "5.3"
5+
}
6+
}

test/cli/tsconfig/tsconfig.base.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tstl": {
3+
"noHeader": true,
4+
"luaTarget": "jit"
5+
}
6+
}

test/cli/tsconfig/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"tstl": {
4+
"luaTarget": "5.3"
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": ["./tsconfig.json", "./tsconfig2.json"],
3+
"tstl": {
4+
"sourceMapTraceback": true
5+
}
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "./tsconfig.base.json",
3+
"tstl": {
4+
// Can handle comments
5+
/* also of this kind */
6+
"luaTarget": "5.3"
7+
}
8+
}

test/cli/tsconfig/tsconfig2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"tstl": {
3+
"luaTarget": "5.4"
4+
}
5+
}

0 commit comments

Comments
 (0)