Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag for not overwrite js files by default without generating errors #11980

Merged
merged 6 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,21 +1537,24 @@ namespace ts {
const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName);
// Report error if the output overwrites input file
if (filesByName.contains(emitFilePath)) {
let chain: DiagnosticMessageChain;
if (!options.configFilePath) {
// The program is from either an inferred project or an external project
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);
if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) {
Copy link
Member

@billti billti Nov 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if 'noEmitOverwrittenFiles' is true, then it must be an inferred project, so the other options are never going to be set, right? What's the value in checking them? (Or as we adding 'noEmitOverwritenFiles' as a general config file option?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true for now, though the relationship might be loose. It is possible that in the future this option can be enabled for other projects, while this checking guarantees correctness without costing much

blockEmittingOfFile(emitFileName);
}
else {
let chain: DiagnosticMessageChain;
if (!options.configFilePath) {
// The program is from either an inferred project or an external project
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);
}
chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName);
blockEmittingOfFile(emitFileName, createCompilerDiagnosticFromMessageChain(chain));
}
chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName);
const diagnostic = createCompilerDiagnosticFromMessageChain(chain);
createEmitBlockingDiagnostics(emitFileName, diagnostic);
}

// Report error if multiple files write into same file
if (emitFilesSeen.contains(emitFilePath)) {
// Already seen the same emit file - report error
const diagnostic = createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName);
createEmitBlockingDiagnostics(emitFileName, diagnostic);
blockEmittingOfFile(emitFileName, createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName));
}
else {
emitFilesSeen.set(emitFilePath, true);
Expand All @@ -1560,9 +1563,11 @@ namespace ts {
}
}

function createEmitBlockingDiagnostics(emitFileName: string, diag: Diagnostic) {
function blockEmittingOfFile(emitFileName: string, diag?: Diagnostic) {
hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true);
programDiagnostics.add(diag);
if (diag) {
programDiagnostics.add(diag);
}
}
}
}
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,7 @@ namespace ts {
moduleResolution?: ModuleResolutionKind;
newLine?: NewLineKind;
noEmit?: boolean;
/*@internal*/noEmitOverwritenFiles?: boolean;
noEmitHelpers?: boolean;
noEmitOnError?: boolean;
noErrorTruncation?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ namespace ts.server {
this.compilerOptions.allowNonTsExtensions = true;
}

if (this.projectKind === ProjectKind.Inferred) {
this.compilerOptions.noEmitOverwritenFiles = true;
}

if (languageServiceEnabled) {
this.enableLanguageService();
}
Expand Down