Skip to content

Commit 5819402

Browse files
authored
Merge pull request #15255 from Microsoft/extendCompileOnSave
Use base tsconfig's compileOnSave option if tsconfig.json doesnt have it specified
2 parents a859399 + 3f7b53e commit 5819402

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,10 +1111,11 @@ namespace ts {
11111111
const jsonOptions = json["typeAcquisition"] || json["typingOptions"];
11121112
const typeAcquisition: TypeAcquisition = convertTypeAcquisitionFromJsonWorker(jsonOptions, basePath, errors, configFileName);
11131113

1114+
let baseCompileOnSave: boolean;
11141115
if (json["extends"]) {
11151116
let [include, exclude, files, baseOptions]: [string[], string[], string[], CompilerOptions] = [undefined, undefined, undefined, {}];
11161117
if (typeof json["extends"] === "string") {
1117-
[include, exclude, files, baseOptions] = (tryExtendsName(json["extends"]) || [include, exclude, files, baseOptions]);
1118+
[include, exclude, files, baseCompileOnSave, baseOptions] = (tryExtendsName(json["extends"]) || [include, exclude, files, baseCompileOnSave, baseOptions]);
11181119
}
11191120
else {
11201121
errors.push(createCompilerDiagnostic(Diagnostics.Compiler_option_0_requires_a_value_of_type_1, "extends", "string"));
@@ -1135,7 +1136,10 @@ namespace ts {
11351136
options.configFilePath = configFileName;
11361137

11371138
const { fileNames, wildcardDirectories } = getFileNames(errors);
1138-
const compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors);
1139+
let compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors);
1140+
if (baseCompileOnSave && json[compileOnSaveCommandLineOption.name] === undefined) {
1141+
compileOnSave = baseCompileOnSave;
1142+
}
11391143

11401144
return {
11411145
options,
@@ -1147,7 +1151,7 @@ namespace ts {
11471151
compileOnSave
11481152
};
11491153

1150-
function tryExtendsName(extendedConfig: string): [string[], string[], string[], CompilerOptions] {
1154+
function tryExtendsName(extendedConfig: string): [string[], string[], string[], boolean, CompilerOptions] {
11511155
// If the path isn't a rooted or relative path, don't try to resolve it (we reserve the right to special case module-id like paths in the future)
11521156
if (!(isRootedDiskPath(extendedConfig) || startsWith(normalizeSlashes(extendedConfig), "./") || startsWith(normalizeSlashes(extendedConfig), "../"))) {
11531157
errors.push(createCompilerDiagnostic(Diagnostics.A_path_in_an_extends_option_must_be_relative_or_rooted_but_0_is_not, extendedConfig));
@@ -1177,7 +1181,7 @@ namespace ts {
11771181
return map(extendedResult.config[key], updatePath);
11781182
}
11791183
});
1180-
return [include, exclude, files, result.options];
1184+
return [include, exclude, files, result.compileOnSave, result.options];
11811185
}
11821186

11831187
function getFileNames(errors: Diagnostic[]): ExpandResult {
@@ -1245,7 +1249,7 @@ namespace ts {
12451249
}
12461250
}
12471251

1248-
export function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean {
1252+
export function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean | undefined {
12491253
if (!hasProperty(jsonOption, compileOnSaveCommandLineOption.name)) {
12501254
return false;
12511255
}

src/harness/unittests/compileOnSave.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,29 @@ namespace ts.projectSystem {
317317
sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, []);
318318
});
319319

320+
it("should save when compileOnSave is enabled in base tsconfig.json", () => {
321+
configFile = {
322+
path: "/a/b/tsconfig.json",
323+
content: `{
324+
"extends": "/a/tsconfig.json"
325+
}`
326+
};
327+
328+
const configFile2: FileOrFolder = {
329+
path: "/a/tsconfig.json",
330+
content: `{
331+
"compileOnSave": true
332+
}`
333+
};
334+
335+
const host = createServerHost([moduleFile1, file1Consumer1, file1Consumer2, configFile2, configFile, libFile]);
336+
const typingsInstaller = createTestTypingsInstaller(host);
337+
const session = createSession(host, typingsInstaller);
338+
339+
openFilesForSession([moduleFile1, file1Consumer1], session);
340+
sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer1, file1Consumer2] }]);
341+
});
342+
320343
it("should always return the file itself if '--isolatedModules' is specified", () => {
321344
configFile = {
322345
path: "/a/b/tsconfig.json",

0 commit comments

Comments
 (0)