Skip to content

Commit cba7725

Browse files
committed
feat: emitExtension can not be .d.ts now
1 parent 3bad1ef commit cba7725

10 files changed

+77
-26
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2721,8 +2721,8 @@ namespace ts {
27212721
const tsExtension = tryExtractTSExtension(moduleReference);
27222722
if (tsExtension) {
27232723
// Support import .ts and .tsx file for environment like deno
2724-
if (fileExtensionIs(moduleReference, Extension.Dts)) {
2725-
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
2724+
if (!compilerOptions.emitExtension || fileExtensionIs(moduleReference, Extension.Dts)) {
2725+
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;
27262726
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
27272727
}
27282728
}

src/compiler/diagnosticMessages.json

+6-14
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,6 @@
10591059
"category": "Error",
10601060
"code": 1360
10611061
},
1062-
10631062
"The types of '{0}' are incompatible between these types.": {
10641063
"category": "Error",
10651064
"code": 2200
@@ -1088,7 +1087,6 @@
10881087
"code": 2205,
10891088
"elidedInCompatabilityPyramid": true
10901089
},
1091-
10921090
"Duplicate identifier '{0}'.": {
10931091
"category": "Error",
10941092
"code": 2300
@@ -2269,7 +2267,6 @@
22692267
"category": "Error",
22702268
"code": 2614
22712269
},
2272-
22732270
"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
22742271
"category": "Error",
22752272
"code": 2649
@@ -2426,7 +2423,7 @@
24262423
"category": "Error",
24272424
"code": 2689
24282425
},
2429-
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
2426+
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.": {
24302427
"category": "Error",
24312428
"code": 2691
24322429
},
@@ -2791,7 +2788,6 @@
27912788
"category": "Error",
27922789
"code": 2781
27932790
},
2794-
27952791
"Import declaration '{0}' is using private name '{1}'.": {
27962792
"category": "Error",
27972793
"code": 4000
@@ -3156,7 +3152,6 @@
31563152
"category": "Error",
31573153
"code": 4110
31583154
},
3159-
31603155
"The current host does not support the '{0}' option.": {
31613156
"category": "Error",
31623157
"code": 5001
@@ -4197,7 +4192,6 @@
41974192
"category": "Message",
41984193
"code": 6302
41994194
},
4200-
42014195
"Composite projects may not disable declaration emit.": {
42024196
"category": "Error",
42034197
"code": 6304
@@ -4358,7 +4352,6 @@
43584352
"category": "Message",
43594353
"code": 6383
43604354
},
4361-
43624355
"The expected type comes from property '{0}' which is declared here on type '{1}'": {
43634356
"category": "Message",
43644357
"code": 6500
@@ -4383,15 +4376,18 @@
43834376
"category": "Message",
43844377
"code": 6505
43854378
},
4386-
"Emit extension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
4379+
"emitExtension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
43874380
"category": "Error",
43884381
"code": 6506
43894382
},
43904383
"emitExtension can only be \".jsx\" when JSX is set to \"preserve\"": {
43914384
"category": "Error",
43924385
"code": 6507
43934386
},
4394-
4387+
"emitExtension can not be \".d.ts\"": {
4388+
"category": "Error",
4389+
"code": 6508
4390+
},
43954391
"Variable '{0}' implicitly has an '{1}' type.": {
43964392
"category": "Error",
43974393
"code": 7005
@@ -4586,7 +4582,6 @@
45864582
"category": "Error",
45874583
"code": 7055
45884584
},
4589-
45904585
"You cannot rename this element.": {
45914586
"category": "Error",
45924587
"code": 8000
@@ -4819,7 +4814,6 @@
48194814
"category": "Error",
48204815
"code": 18003
48214816
},
4822-
48234817
"File is a CommonJS module; it may be converted to an ES6 module.": {
48244818
"category": "Suggestion",
48254819
"code": 80001
@@ -4852,7 +4846,6 @@
48524846
"category": "Suggestion",
48534847
"code": 80008
48544848
},
4855-
48564849
"Add missing 'super()' call": {
48574850
"category": "Message",
48584851
"code": 90001
@@ -5353,7 +5346,6 @@
53535346
"category": "Message",
53545347
"code": 95095
53555348
},
5356-
53575349
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
53585350
"category": "Error",
53595351
"code": 18004

src/compiler/program.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3162,12 +3162,16 @@ namespace ts {
31623162
});
31633163
}
31643164

3165-
if (options.emitExtension && !startsWith(options.emitExtension, ".")) {
3166-
createOptionValueDiagnostic("emitExtension", Diagnostics.Emit_extension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
3167-
}
3168-
3169-
if (options.emitExtension && options.emitExtension !== ".jsx" && options.jsx === JsxEmit.Preserve) {
3170-
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
3165+
if (options.emitExtension) {
3166+
if (!startsWith(options.emitExtension, ".")) {
3167+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
3168+
}
3169+
if (options.emitExtension !== Extension.Jsx && options.jsx === JsxEmit.Preserve) {
3170+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
3171+
}
3172+
if (options.emitExtension === Extension.Dts) {
3173+
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_not_be_d_ts, options.emitExtension);
3174+
}
31713175
}
31723176

31733177
// 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)