Skip to content

Commit 182bc77

Browse files
authored
Add diagnostics to remind adding tsconfig file for certain external project (#11932)
* Add diagnostics for certain external project * Show tsconfig suggestion * fix lint error * Address pr * fix comment * Update error message
1 parent ad9c148 commit 182bc77

File tree

40 files changed

+119
-12
lines changed

40 files changed

+119
-12
lines changed

src/compiler/core.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,18 @@ namespace ts {
10631063
};
10641064
}
10651065

1066+
export function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessageChain): Diagnostic {
1067+
return {
1068+
file: undefined,
1069+
start: undefined,
1070+
length: undefined,
1071+
1072+
code: chain.code,
1073+
category: chain.category,
1074+
messageText: chain.next ? chain : chain.messageText
1075+
};
1076+
}
1077+
10661078
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage, ...args: any[]): DiagnosticMessageChain;
10671079
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage): DiagnosticMessageChain {
10681080
let text = getLocaleSpecificMessage(message);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,5 +3118,9 @@
31183118
"Implement inherited abstract class": {
31193119
"category": "Message",
31203120
"code": 90007
3121+
},
3122+
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
3123+
"category": "Error",
3124+
"code": 90009
31213125
}
31223126
}

src/compiler/program.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ namespace ts {
239239
const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
240240
const fileName = diagnostic.file.fileName;
241241
const relativeFileName = convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName));
242-
output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `;
242+
output += `${relativeFileName}(${line + 1},${character + 1}): `;
243243
}
244244

245245
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
246-
output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }${ host.getNewLine() }`;
246+
output += `${category} TS${diagnostic.code}: ${flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine())}${host.getNewLine()}`;
247247
}
248248
return output;
249249
}
@@ -1316,11 +1316,11 @@ namespace ts {
13161316
}
13171317
else if (shouldAddFile) {
13181318
findSourceFile(resolution.resolvedFileName,
1319-
toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName),
1319+
toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName),
13201320
/*isDefaultLib*/ false,
1321-
file,
1322-
skipTrivia(file.text, file.imports[i].pos),
1323-
file.imports[i].end);
1321+
file,
1322+
skipTrivia(file.text, file.imports[i].pos),
1323+
file.imports[i].end);
13241324
}
13251325

13261326
if (isFromNodeModulesSearch) {
@@ -1537,13 +1537,21 @@ namespace ts {
15371537
const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName);
15381538
// Report error if the output overwrites input file
15391539
if (filesByName.contains(emitFilePath)) {
1540-
createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file);
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);
1544+
}
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);
15411548
}
15421549

15431550
// Report error if multiple files write into same file
15441551
if (emitFilesSeen.contains(emitFilePath)) {
15451552
// Already seen the same emit file - report error
1546-
createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files);
1553+
const diagnostic = createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName);
1554+
createEmitBlockingDiagnostics(emitFileName, diagnostic);
15471555
}
15481556
else {
15491557
emitFilesSeen.set(emitFilePath, true);
@@ -1552,9 +1560,9 @@ namespace ts {
15521560
}
15531561
}
15541562

1555-
function createEmitBlockingDiagnostics(emitFileName: string, message: DiagnosticMessage) {
1563+
function createEmitBlockingDiagnostics(emitFileName: string, diag: Diagnostic) {
15561564
hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true);
1557-
programDiagnostics.add(createCompilerDiagnostic(message, emitFileName));
1565+
programDiagnostics.add(diag);
15581566
}
15591567
}
15601568
}

src/harness/fourslash.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,19 @@ namespace FourSlash {
12741274
resultString += "Diagnostics:" + Harness.IO.newLine();
12751275
const diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram());
12761276
for (let i = 0, n = diagnostics.length; i < n; i++) {
1277-
resultString += " " + diagnostics[0].messageText + Harness.IO.newLine();
1277+
const diagnostic = diagnostics[i];
1278+
if (typeof diagnostic.messageText !== "string") {
1279+
let chainedMessage = <ts.DiagnosticMessageChain>diagnostic.messageText;
1280+
let indentation = " ";
1281+
while (chainedMessage) {
1282+
resultString += indentation + chainedMessage.messageText + Harness.IO.newLine();
1283+
chainedMessage = chainedMessage.next;
1284+
indentation = indentation + " ";
1285+
}
1286+
}
1287+
else {
1288+
resultString += " " + diagnostic.messageText + Harness.IO.newLine();
1289+
}
12781290
}
12791291
}
12801292

tests/baselines/reference/compileOnSaveWorksWhenEmitBlockingErrorOnOtherFile.baseline

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
EmitSkipped: true
22
Diagnostics:
3-
Cannot write file '/tests/cases/fourslash/b.js' because it would overwrite input file.
3+
Cannot write file '/tests/cases/fourslash/b.js' because it would overwrite input file.
4+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
45

56
EmitSkipped: false
67
FileName : /tests/cases/fourslash/a.js

tests/baselines/reference/declarationFileOverwriteError.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.d.ts' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.d.ts' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/compiler/a.d.ts (0 errors) ====
68

79
declare class c {

tests/baselines/reference/declarationFileOverwriteErrorWithOut.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/compiler/out.d.ts (0 errors) ====
68

79
declare class c {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile01.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile01.js' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/conformance/salsa/myFile01.js (0 errors) ====
68

79
export default "hello";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile02.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile02.js' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/conformance/salsa/myFile02.js (0 errors) ====
68

79
export default "hello";

tests/baselines/reference/jsFileCompilationAmbientVarDeclarationSyntax.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,1): error TS8009: 'declare' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
declare var v;
810
~~~~~~~

0 commit comments

Comments
 (0)