Skip to content

Commit 73e2328

Browse files
authored
Flag for not overwrite js files by default without generating errors (#11980)
* WIP * Properly naming things * refactor * apply the option to all files and check out options * Fix typo
1 parent 182bc77 commit 73e2328

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/compiler/program.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,21 +1537,24 @@ namespace ts {
15371537
const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName);
15381538
// Report error if the output overwrites input file
15391539
if (filesByName.contains(emitFilePath)) {
1540-
let chain: DiagnosticMessageChain;
1541-
if (!options.configFilePath) {
1542-
// The program is from either an inferred project or an external project
1543-
chain = chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig);
1540+
if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) {
1541+
blockEmittingOfFile(emitFileName);
1542+
}
1543+
else {
1544+
let chain: DiagnosticMessageChain;
1545+
if (!options.configFilePath) {
1546+
// The program is from either an inferred project or an external project
1547+
chain = chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig);
1548+
}
1549+
chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName);
1550+
blockEmittingOfFile(emitFileName, createCompilerDiagnosticFromMessageChain(chain));
15441551
}
1545-
chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName);
1546-
const diagnostic = createCompilerDiagnosticFromMessageChain(chain);
1547-
createEmitBlockingDiagnostics(emitFileName, diagnostic);
15481552
}
15491553

15501554
// Report error if multiple files write into same file
15511555
if (emitFilesSeen.contains(emitFilePath)) {
15521556
// Already seen the same emit file - report error
1553-
const diagnostic = createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName);
1554-
createEmitBlockingDiagnostics(emitFileName, diagnostic);
1557+
blockEmittingOfFile(emitFileName, createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName));
15551558
}
15561559
else {
15571560
emitFilesSeen.set(emitFilePath, true);
@@ -1560,9 +1563,11 @@ namespace ts {
15601563
}
15611564
}
15621565

1563-
function createEmitBlockingDiagnostics(emitFileName: string, diag: Diagnostic) {
1566+
function blockEmittingOfFile(emitFileName: string, diag?: Diagnostic) {
15641567
hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true);
1565-
programDiagnostics.add(diag);
1568+
if (diag) {
1569+
programDiagnostics.add(diag);
1570+
}
15661571
}
15671572
}
15681573
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,7 @@ namespace ts {
29842984
moduleResolution?: ModuleResolutionKind;
29852985
newLine?: NewLineKind;
29862986
noEmit?: boolean;
2987+
/*@internal*/noEmitOverwritenFiles?: boolean;
29872988
noEmitHelpers?: boolean;
29882989
noEmitOnError?: boolean;
29892990
noErrorTruncation?: boolean;

src/server/project.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ namespace ts.server {
161161
this.compilerOptions.allowNonTsExtensions = true;
162162
}
163163

164+
if (this.projectKind === ProjectKind.Inferred) {
165+
this.compilerOptions.noEmitOverwritenFiles = true;
166+
}
167+
164168
if (languageServiceEnabled) {
165169
this.enableLanguageService();
166170
}

0 commit comments

Comments
 (0)