Skip to content

Commit

Permalink
feat: emitExtension can not be .d.ts now
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Nov 17, 2019
1 parent 1ff53b0 commit 0ddfa56
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2725,8 +2725,8 @@ namespace ts {
const tsExtension = tryExtractTSExtension(moduleReference);
if (tsExtension) {
// Support import .ts and .tsx file for environment like deno
if (fileExtensionIs(moduleReference, Extension.Dts)) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
if (!compilerOptions.emitExtension || fileExtensionIs(moduleReference, Extension.Dts)) {
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;
error(errorNode, diag, tsExtension, removeExtension(moduleReference, tsExtension));
}
}
Expand Down
24 changes: 7 additions & 17 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,6 @@
"category": "Error",
"code": 1360
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
"code": 2200
Expand Down Expand Up @@ -1080,7 +1079,6 @@
"code": 2205,
"elidedInCompatabilityPyramid": true
},

"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2300
Expand Down Expand Up @@ -2261,7 +2259,6 @@
"category": "Error",
"code": 2614
},

"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
"category": "Error",
"code": 2649
Expand Down Expand Up @@ -2418,7 +2415,7 @@
"category": "Error",
"code": 2689
},
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead.": {
"An import path cannot end with a '{0}' extension. Consider importing '{1}' instead. If the extension is expected, please set 'emitExtension' in the compilerConfig.": {
"category": "Error",
"code": 2691
},
Expand Down Expand Up @@ -2783,7 +2780,6 @@
"category": "Error",
"code": 2781
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down Expand Up @@ -3148,7 +3144,6 @@
"category": "Error",
"code": 4110
},

"The current host does not support the '{0}' option.": {
"category": "Error",
"code": 5001
Expand Down Expand Up @@ -3305,7 +3300,6 @@
"category": "Error",
"code": 5077
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
"code": 6000
Expand Down Expand Up @@ -4060,7 +4054,7 @@
"category": "Message",
"code": 6201
},
"Project references may not form a circular graph. Cycle detected: {0}": {
"Project references may not form a circular graph. Cycle detected: {0}": {
"category": "Error",
"code": 6202
},
Expand Down Expand Up @@ -4148,7 +4142,6 @@
"category": "Message",
"code": 6223
},

"Projects to reference": {
"category": "Message",
"code": 6300
Expand All @@ -4157,7 +4150,6 @@
"category": "Message",
"code": 6302
},

"Composite projects may not disable declaration emit.": {
"category": "Error",
"code": 6304
Expand Down Expand Up @@ -4318,7 +4310,6 @@
"category": "Message",
"code": 6383
},

"The expected type comes from property '{0}' which is declared here on type '{1}'": {
"category": "Message",
"code": 6500
Expand All @@ -4343,15 +4334,18 @@
"category": "Message",
"code": 6505
},
"Emit extension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
"emitExtension must start with '.', but here has '{0}', try to replace it with '.{0}'.": {
"category": "Error",
"code": 6506
},
"emitExtension can only be \".jsx\" when JSX is set to \"preserve\"": {
"category": "Error",
"code": 6507
},

"emitExtension can not be \".d.ts\"": {
"category": "Error",
"code": 6508
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
Expand Down Expand Up @@ -4546,7 +4540,6 @@
"category": "Error",
"code": 7055
},

"You cannot rename this element.": {
"category": "Error",
"code": 8000
Expand Down Expand Up @@ -4787,7 +4780,6 @@
"category": "Error",
"code": 18003
},

"File is a CommonJS module; it may be converted to an ES6 module.": {
"category": "Suggestion",
"code": 80001
Expand Down Expand Up @@ -4820,7 +4812,6 @@
"category": "Suggestion",
"code": 80008
},

"Add missing 'super()' call": {
"category": "Message",
"code": 90001
Expand Down Expand Up @@ -5321,7 +5312,6 @@
"category": "Message",
"code": 95095
},

"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",
"code": 18004
Expand Down
16 changes: 10 additions & 6 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3137,12 +3137,16 @@ namespace ts {
});
}

if (options.emitExtension && !startsWith(options.emitExtension, ".")) {
createOptionValueDiagnostic("emitExtension", Diagnostics.Emit_extension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
}

if (options.emitExtension && options.emitExtension !== ".jsx" && options.jsx === JsxEmit.Preserve) {
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
if (options.emitExtension) {
if (!startsWith(options.emitExtension, ".")) {
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_must_start_with_but_here_has_0_try_to_replace_it_with_0, options.emitExtension);
}
if (options.emitExtension !== Extension.Jsx && options.jsx === JsxEmit.Preserve) {
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_only_be_jsx_when_JSX_is_set_to_preserve, options.emitExtension);
}
if (options.emitExtension === Extension.Dts) {
createOptionValueDiagnostic("emitExtension", Diagnostics.emitExtension_can_not_be_d_ts, options.emitExtension);
}
}

// Verify that all the emit files are unique and don't overwrite input files
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/emitExtensionDTS.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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.


==== tests/cases/compiler/emitExtensionDTS.ts (1 errors) ====
// @Filename 1.ts
import * as t from './file.d.ts'
~~~~~~~~~~~~~
!!! 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.
console.log(t)


13 changes: 13 additions & 0 deletions tests/baselines/reference/emitExtensionDTS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [emitExtensionDTS.ts]
// @Filename 1.ts
import * as t from './file.d.ts'
console.log(t)



//// [emitExtensionDTS.mjs]
"use strict";
exports.__esModule = true;
// @Filename 1.ts
var t = require("./file.d.ts");
console.log(t);
12 changes: 12 additions & 0 deletions tests/baselines/reference/emitExtensionDTS.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/emitExtensionDTS.ts ===
// @Filename 1.ts
import * as t from './file.d.ts'
>t : Symbol(t, Decl(emitExtensionDTS.ts, 1, 6))

console.log(t)
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>t : Symbol(t, Decl(emitExtensionDTS.ts, 1, 6))


13 changes: 13 additions & 0 deletions tests/baselines/reference/emitExtensionDTS.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
=== tests/cases/compiler/emitExtensionDTS.ts ===
// @Filename 1.ts
import * as t from './file.d.ts'
>t : any

console.log(t)
>console.log(t) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>t : any


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


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

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/moduleResolutionNoTs.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
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.


==== tests/cases/compiler/z.d.ts (0 errors) ====
Expand All @@ -8,7 +8,7 @@ tests/cases/compiler/user.ts(1,15): error TS2691: An import path cannot end with
==== tests/cases/compiler/user.ts (1 errors) ====
import z from "./z.d.ts";
~~~~~~~~~~
!!! error TS2691: An import path cannot end with a '.d.ts' extension. Consider importing './z' instead.
!!! 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.

// Making sure the suggested fixes are valid:
import z2 from "./z";
Expand Down
6 changes: 6 additions & 0 deletions tests/cases/compiler/emitExtensionDTS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @emitExtension: .mjs

// @Filename 1.ts
import * as t from './file.d.ts'
console.log(t)

0 comments on commit 0ddfa56

Please sign in to comment.