Skip to content

Commit 0ddfa56

Browse files
committed
feat: emitExtension can not be .d.ts now
1 parent 1ff53b0 commit 0ddfa56

10 files changed

+78
-29
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2725,8 +2725,8 @@ namespace ts {
27252725
const tsExtension = tryExtractTSExtension(moduleReference);
27262726
if (tsExtension) {
27272727
// Support import .ts and .tsx file for environment like deno
2728-
if (fileExtensionIs(moduleReference, Extension.Dts)) {
2729-
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
2728+
if (!compilerOptions.emitExtension || fileExtensionIs(moduleReference, Extension.Dts)) {
2729+
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead_If_the_extension_is_expected_please_set_emitExtension_in_the_compilerConfig;
27302730
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
27312731
}
27322732
}

src/compiler/diagnosticMessages.json

+7-17
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,6 @@
10511051
"category": "Error",
10521052
"code": 1360
10531053
},
1054-
10551054
"The types of '{0}' are incompatible between these types.": {
10561055
"category": "Error",
10571056
"code": 2200
@@ -1080,7 +1079,6 @@
10801079
"code": 2205,
10811080
"elidedInCompatabilityPyramid": true
10821081
},
1083-
10841082
"Duplicate identifier '{0}'.": {
10851083
"category": "Error",
10861084
"code": 2300
@@ -2261,7 +2259,6 @@
22612259
"category": "Error",
22622260
"code": 2614
22632261
},
2264-
22652262
"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
22662263
"category": "Error",
22672264
"code": 2649
@@ -2418,7 +2415,7 @@
24182415
"category": "Error",
24192416
"code": 2689
24202417
},
2421-
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
2418+
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.": {
24222419
"category": "Error",
24232420
"code": 2691
24242421
},
@@ -2783,7 +2780,6 @@
27832780
"category": "Error",
27842781
"code": 2781
27852782
},
2786-
27872783
"Import declaration '{0}' is using private name '{1}'.": {
27882784
"category": "Error",
27892785
"code": 4000
@@ -3148,7 +3144,6 @@
31483144
"category": "Error",
31493145
"code": 4110
31503146
},
3151-
31523147
"The current host does not support the '{0}' option.": {
31533148
"category": "Error",
31543149
"code": 5001
@@ -3305,7 +3300,6 @@
33053300
"category": "Error",
33063301
"code": 5077
33073302
},
3308-
33093303
"Generates a sourcemap for each corresponding '.d.ts' file.": {
33103304
"category": "Message",
33113305
"code": 6000
@@ -4060,7 +4054,7 @@
40604054
"category": "Message",
40614055
"code": 6201
40624056
},
4063-
"Project references may not form a circular graph. Cycle detected: {0}": {
4057+
"Project references may not form a circular graph. Cycle detected: {0}": {
40644058
"category": "Error",
40654059
"code": 6202
40664060
},
@@ -4148,7 +4142,6 @@
41484142
"category": "Message",
41494143
"code": 6223
41504144
},
4151-
41524145
"Projects to reference": {
41534146
"category": "Message",
41544147
"code": 6300
@@ -4157,7 +4150,6 @@
41574150
"category": "Message",
41584151
"code": 6302
41594152
},
4160-
41614153
"Composite projects may not disable declaration emit.": {
41624154
"category": "Error",
41634155
"code": 6304
@@ -4318,7 +4310,6 @@
43184310
"category": "Message",
43194311
"code": 6383
43204312
},
4321-
43224313
"The expected type comes from property '{0}' which is declared here on type '{1}'": {
43234314
"category": "Message",
43244315
"code": 6500
@@ -4343,15 +4334,18 @@
43434334
"category": "Message",
43444335
"code": 6505
43454336
},
4346-
"Emit extension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
4337+
"emitExtension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
43474338
"category": "Error",
43484339
"code": 6506
43494340
},
43504341
"emitExtension can only be \".jsx\" when JSX is set to \"preserve\"": {
43514342
"category": "Error",
43524343
"code": 6507
43534344
},
4354-
4345+
"emitExtension can not be \".d.ts\"": {
4346+
"category": "Error",
4347+
"code": 6508
4348+
},
43554349
"Variable '{0}' implicitly has an '{1}' type.": {
43564350
"category": "Error",
43574351
"code": 7005
@@ -4546,7 +4540,6 @@
45464540
"category": "Error",
45474541
"code": 7055
45484542
},
4549-
45504543
"You cannot rename this element.": {
45514544
"category": "Error",
45524545
"code": 8000
@@ -4787,7 +4780,6 @@
47874780
"category": "Error",
47884781
"code": 18003
47894782
},
4790-
47914783
"File is a CommonJS module; it may be converted to an ES6 module.": {
47924784
"category": "Suggestion",
47934785
"code": 80001
@@ -4820,7 +4812,6 @@
48204812
"category": "Suggestion",
48214813
"code": 80008
48224814
},
4823-
48244815
"Add missing 'super()' call": {
48254816
"category": "Message",
48264817
"code": 90001
@@ -5321,7 +5312,6 @@
53215312
"category": "Message",
53225313
"code": 95095
53235314
},
5324-
53255315
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
53265316
"category": "Error",
53275317
"code": 18004

src/compiler/program.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3137,12 +3137,16 @@ namespace ts {
31373137
});
31383138
}
31393139

3140-
if (options.emitExtension && !startsWith(options.emitExtension, ".")) {
3141-
createOptionValueDiagnostic("emitExtension", Diagnostics.Emit_extension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
3142-
}
3143-
3144-
if (options.emitExtension && options.emitExtension !== ".jsx" && options.jsx === JsxEmit.Preserve) {
3145-
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
3140+
if (options.emitExtension) {
3141+
if (!startsWith(options.emitExtension, ".")) {
3142+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
3143+
}
3144+
if (options.emitExtension !== Extension.Jsx && options.jsx === JsxEmit.Preserve) {
3145+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
3146+
}
3147+
if (options.emitExtension === Extension.Dts) {
3148+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_not_be_d_ts, options.emitExtension);
3149+
}
31463150
}
31473151

31483152
// Verify that all the emit files are unique and don't overwrite input files
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/emitExtensionDTS.ts(2,20): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './file' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.
2+
3+
4+
==== tests/cases/compiler/emitExtensionDTS.ts (1 errors) ====
5+
// @Filename 1.ts
6+
import * as t from './file.d.ts'
7+
~~~~~~~~~~~~~
8+
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './file' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.
9+
console.log(t)
10+
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [emitExtensionDTS.ts]
2+
// @Filename 1.ts
3+
import * as t from './file.d.ts'
4+
console.log(t)
5+
6+
7+
8+
//// [emitExtensionDTS.mjs]
9+
"use strict";
10+
exports.__esModule = true;
11+
// @Filename 1.ts
12+
var t = require("./file.d.ts");
13+
console.log(t);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/emitExtensionDTS.ts ===
2+
// @Filename 1.ts
3+
import * as t from './file.d.ts'
4+
>t : Symbol(t, Decl(emitExtensionDTS.ts, 1, 6))
5+
6+
console.log(t)
7+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
8+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
9+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
10+
>t : Symbol(t, Decl(emitExtensionDTS.ts, 1, 6))
11+
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/emitExtensionDTS.ts ===
2+
// @Filename 1.ts
3+
import * as t from './file.d.ts'
4+
>t : any
5+
6+
console.log(t)
7+
>console.log(t) : void
8+
>console.log : (message?: any, ...optionalParams: any[]) => void
9+
>console : Console
10+
>log : (message?: any, ...optionalParams: any[]) => void
11+
>t : any
12+
13+

tests/baselines/reference/emitExtensionInvalidValue.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
error TS6506: Emit extension must start with '.', but here has 'ts', try to replace it with '.ts'.
1+
error TS6506: emitExtension must start with '.', but here has 'ts', try to replace it with '.ts'.
22

33

4-
!!! error TS6506: Emit extension must start with '.', but here has 'ts', try to replace it with '.ts'.
4+
!!! error TS6506: emitExtension must start with '.', but here has 'ts', try to replace it with '.ts'.
55
==== tests/cases/compiler/0.ts (0 errors) ====
66
export default 1
77

tests/baselines/reference/moduleResolutionNoTs.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
1+
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.
22

33

44
==== tests/cases/compiler/z.d.ts (0 errors) ====
@@ -8,7 +8,7 @@ tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with
88
==== tests/cases/compiler/user.ts (1 errors) ====
99
import z from "./z.d.ts";
1010
~~~~~~~~~~
11-
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
11+
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.
1212

1313
// Making sure the suggested fixes are valid:
1414
import z2 from "./z";
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @emitExtension: .mjs
2+
3+
// @Filename 1.ts
4+
import * as t from './file.d.ts'
5+
console.log(t)
6+

0 commit comments

Comments
 (0)