Skip to content

Commit 9a3ec5f

Browse files
lukahartwigOrta
authored and
Orta
committed
Improve error message when compiling a .js file (microsoft#34861)
* Improve error message when compiling a .js file * Add dedicated error message for .json and .js files * Update missing baseline tests * Remove error hint for .json files
1 parent ba5e86f commit 9a3ec5f

11 files changed

+36
-24
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3478,7 +3478,7 @@
34783478
"category": "Error",
34793479
"code": 6053
34803480
},
3481-
"File '{0}' has unsupported extension. The only supported extensions are {1}.": {
3481+
"File '{0}' has an unsupported extension. The only supported extensions are {1}.": {
34823482
"category": "Error",
34833483
"code": 6054
34843484
},
@@ -4323,6 +4323,10 @@
43234323
"category": "Message",
43244324
"code": 6503
43254325
},
4326+
"File '{0}' is a JavaScript file. Did you mean to enable the 'allowJs' option?": {
4327+
"category": "Error",
4328+
"code": 6504
4329+
},
43264330

43274331
"Variable '{0}' implicitly has an '{1}' type.": {
43284332
"category": "Error",

src/compiler/program.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,8 +2189,16 @@ namespace ts {
21892189
refFile?: SourceFile): SourceFile | undefined {
21902190

21912191
if (hasExtension(fileName)) {
2192-
if (!options.allowNonTsExtensions && !forEach(supportedExtensionsWithJsonIfResolveJsonModule, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
2193-
if (fail) fail(Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1, fileName, "'" + supportedExtensions.join("', '") + "'");
2192+
const canonicalFileName = host.getCanonicalFileName(fileName);
2193+
if (!options.allowNonTsExtensions && !forEach(supportedExtensionsWithJsonIfResolveJsonModule, extension => fileExtensionIs(canonicalFileName, extension))) {
2194+
if (fail) {
2195+
if (hasJSFileExtension(canonicalFileName)) {
2196+
fail(Diagnostics.File_0_is_a_JavaScript_file_Did_you_mean_to_enable_the_allowJs_option, fileName);
2197+
}
2198+
else {
2199+
fail(Diagnostics.File_0_has_an_unsupported_extension_The_only_supported_extensions_are_1, fileName, "'" + supportedExtensions.join("', '") + "'");
2200+
}
2201+
}
21942202
return undefined;
21952203
}
21962204

@@ -2205,7 +2213,7 @@ namespace ts {
22052213
fail(Diagnostics.File_0_not_found, fileName);
22062214
}
22072215
}
2208-
else if (refFile && host.getCanonicalFileName(fileName) === host.getCanonicalFileName(refFile.fileName)) {
2216+
else if (refFile && canonicalFileName === host.getCanonicalFileName(refFile.fileName)) {
22092217
fail(Diagnostics.A_file_cannot_have_a_reference_to_itself);
22102218
}
22112219
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
2-
error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
2+
error TS6504: File 'tests/cases/compiler/a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
33

44

55
!!! error TS5052: Option 'checkJs' cannot be specified without specifying option 'allowJs'.
6-
!!! error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
6+
!!! error TS6504: File 'tests/cases/compiler/a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
77
==== tests/cases/compiler/a.js (0 errors) ====
88
var x;

tests/baselines/reference/jsFileCompilationWithMapFileAsJs.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3-
error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
3+
error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
44

55

66
!!! error TS5055: Cannot write file 'tests/cases/compiler/b.js' because it would overwrite input file.
77
!!! 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.
8-
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
8+
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
99
==== tests/cases/compiler/a.ts (0 errors) ====
1010
class c {
1111
}

tests/baselines/reference/jsFileCompilationWithMapFileAsJsWithInlineSourceMap.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3-
error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
3+
error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
44

55

66
!!! error TS5055: Cannot write file 'tests/cases/compiler/b.js' because it would overwrite input file.
77
!!! 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.
8-
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
8+
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts', '.js', '.jsx'.
99
==== tests/cases/compiler/a.ts (0 errors) ====
1010
class c {
1111
}

tests/baselines/reference/jsFileCompilationWithMapFileAsJsWithOutDir.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error TS6054: File 'tests/cases/compiler/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
2-
error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
1+
error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
2+
error TS6504: File 'tests/cases/compiler/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
33

44

5-
!!! error TS6054: File 'tests/cases/compiler/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
6-
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
5+
!!! error TS6054: File 'tests/cases/compiler/b.js.map' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
6+
!!! error TS6504: File 'tests/cases/compiler/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
77
==== tests/cases/compiler/a.ts (0 errors) ====
88
class c {
99
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
1+
error TS6504: File 'tests/cases/compiler/a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
22

33

4-
!!! error TS6054: File 'tests/cases/compiler/a.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
4+
!!! error TS6504: File 'tests/cases/compiler/a.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
55
==== tests/cases/compiler/a.js (0 errors) ====
66
declare var v;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error TS6053: File 'a.ts' not found.
2-
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
2+
error TS6054: File 'a.t' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
33

44

55
!!! error TS6053: File 'a.ts' not found.
6-
!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
6+
!!! error TS6054: File 'a.t' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error TS6053: File 'a.ts' not found.
2-
error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
2+
error TS6054: File 'a.t' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
33

44

55
!!! error TS6053: File 'a.ts' not found.
6-
!!! error TS6054: File 'a.t' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
6+
!!! error TS6054: File 'a.t' has an unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.

tests/baselines/reference/project/jsFileCompilationDifferentNamesSpecified/amd/jsFileCompilationDifferentNamesSpecified.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
error TS6054: File 'DifferentNamesSpecified/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
1+
error TS6504: File 'DifferentNamesSpecified/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
22

33

4-
!!! error TS6054: File 'DifferentNamesSpecified/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
4+
!!! error TS6504: File 'DifferentNamesSpecified/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
55
==== DifferentNamesSpecified/tsconfig.json (0 errors) ====
66
{
77
"compilerOptions": { "out": "test.js" },

tests/baselines/reference/project/jsFileCompilationDifferentNamesSpecified/node/jsFileCompilationDifferentNamesSpecified.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error TS6054: File 'DifferentNamesSpecified/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
1+
error TS6504: File 'DifferentNamesSpecified/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
22
DifferentNamesSpecified/tsconfig.json(2,24): error TS6082: Only 'amd' and 'system' modules are supported alongside --out.
33

44

5-
!!! error TS6054: File 'DifferentNamesSpecified/b.js' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
5+
!!! error TS6504: File 'DifferentNamesSpecified/b.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
66
==== DifferentNamesSpecified/tsconfig.json (1 errors) ====
77
{
88
"compilerOptions": { "out": "test.js" },

0 commit comments

Comments
 (0)