From 162ad55052493fb2ebb5e9cfd441d0543512c1bb Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 7 May 2021 09:44:01 -0700 Subject: [PATCH 01/19] Add compiler option --- src/compiler/commandLineParser.ts | 7 ++++--- src/compiler/types.ts | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 253e38a5295b5..d79e7e180ef2c 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -536,9 +536,10 @@ namespace ts { { name: "importsNotUsedAsValues", type: new Map(getEntries({ - remove: ImportsNotUsedAsValues.Remove, - preserve: ImportsNotUsedAsValues.Preserve, - error: ImportsNotUsedAsValues.Error + "remove": ImportsNotUsedAsValues.Remove, + "preserve": ImportsNotUsedAsValues.Preserve, + "error": ImportsNotUsedAsValues.Error, + "preserve-exact": ImportsNotUsedAsValues.PreserveExact, })), affectsEmit: true, affectsSemanticDiagnostics: true, diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f7183d7dd6c27..0fc47a278363b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6070,7 +6070,8 @@ namespace ts { export const enum ImportsNotUsedAsValues { Remove, Preserve, - Error + Error, + PreserveExact, } export const enum NewLineKind { From da436399c5036d3d1c1226895772508711b29d7f Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 7 May 2021 09:44:27 -0700 Subject: [PATCH 02/19] Require es2015+ --- src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/program.ts | 10 +++++++--- ...es_preserve-exact_module(module=amd).errors.txt | 7 +++++++ ...edAsValues_preserve-exact_module(module=amd).js | 9 +++++++++ ...eserve-exact_module(module=commonjs).errors.txt | 7 +++++++ ...alues_preserve-exact_module(module=commonjs).js | 7 +++++++ ...sValues_preserve-exact_module(module=es2015).js | 6 ++++++ ...preserve-exact_module(module=system).errors.txt | 7 +++++++ ...sValues_preserve-exact_module(module=system).js | 14 ++++++++++++++ ...importsNotUsedAsValues_preserve-exact_module.ts | 4 ++++ 10 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js create mode 100644 tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f4fbe932e93f9..cf7c1878d81ab 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3942,6 +3942,10 @@ "category": "Error", "code": 5094 }, + "Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later.": { + "category": "Error", + "code": 5095 + }, "Generates a sourcemap for each corresponding '.d.ts' file.": { "category": "Message", diff --git a/src/compiler/program.ts b/src/compiler/program.ts index a50b60a3171b2..c7af1680fe108 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -3295,6 +3295,10 @@ namespace ts { } } + if (options.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact && getEmitModuleKind(options) < ModuleKind.ES2015) { + createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_importsNotUsedAsValues_may_only_be_set_to_preserve_exact_when_module_is_set_to_es2015_or_later); + } + // If the emit is enabled make sure that every output file is unique and not overwriting any of the input files if (!options.noEmit && !options.suppressOutputPathCheck) { const emitHost = getEmitHost(); @@ -3565,7 +3569,7 @@ namespace ts { createDiagnosticForOption(/*onKey*/ true, option1, option2, message, option1, option2, option3); } - function createOptionValueDiagnostic(option1: string, message: DiagnosticMessage, arg0: string) { + function createOptionValueDiagnostic(option1: string, message: DiagnosticMessage, arg0?: string) { createDiagnosticForOption(/*onKey*/ false, option1, /*option2*/ undefined, message, arg0); } @@ -3580,7 +3584,7 @@ namespace ts { } } - function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage, arg0: string | number, arg1?: string | number, arg2?: string | number) { + function createDiagnosticForOption(onKey: boolean, option1: string, option2: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number) { const compilerOptionsObjectLiteralSyntax = getCompilerOptionsObjectLiteralSyntax(); const needCompilerDiagnostic = !compilerOptionsObjectLiteralSyntax || !createOptionDiagnosticInObjectLiteralSyntax(compilerOptionsObjectLiteralSyntax, onKey, option1, option2, message, arg0, arg1, arg2); @@ -3606,7 +3610,7 @@ namespace ts { return _compilerOptionsObjectLiteralSyntax || undefined; } - function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage, arg0: string | number, arg1?: string | number, arg2?: string | number): boolean { + function createOptionDiagnosticInObjectLiteralSyntax(objectLiteral: ObjectLiteralExpression, onKey: boolean, key1: string, key2: string | undefined, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): boolean { const props = getPropertyAssignment(objectLiteral, key1, key2); for (const prop of props) { programDiagnostics.add(createDiagnosticForNodeInSourceFile(options.configFile!, onKey ? prop.name : prop.initializer, message, arg0, arg1, arg2)); diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt new file mode 100644 index 0000000000000..78cda02c61432 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js new file mode 100644 index 0000000000000..e09b7569742cb --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js @@ -0,0 +1,9 @@ +//// [importsNotUsedAsValues_preserve-exact_module.ts] +export {}; + + +//// [importsNotUsedAsValues_preserve-exact_module.js] +define(["require", "exports"], function (require, exports) { + "use strict"; + exports.__esModule = true; +}); diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt new file mode 100644 index 0000000000000..78cda02c61432 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js new file mode 100644 index 0000000000000..0ab551b173b93 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js @@ -0,0 +1,7 @@ +//// [importsNotUsedAsValues_preserve-exact_module.ts] +export {}; + + +//// [importsNotUsedAsValues_preserve-exact_module.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js new file mode 100644 index 0000000000000..3abff4aa677b1 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js @@ -0,0 +1,6 @@ +//// [importsNotUsedAsValues_preserve-exact_module.ts] +export {}; + + +//// [importsNotUsedAsValues_preserve-exact_module.js] +export {}; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt new file mode 100644 index 0000000000000..78cda02c61432 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js new file mode 100644 index 0000000000000..87284733faf13 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js @@ -0,0 +1,14 @@ +//// [importsNotUsedAsValues_preserve-exact_module.ts] +export {}; + + +//// [importsNotUsedAsValues_preserve-exact_module.js] +System.register([], function (exports_1, context_1) { + "use strict"; + var __moduleName = context_1 && context_1.id; + return { + setters: [], + execute: function () { + } + }; +}); diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts new file mode 100644 index 0000000000000..9dde082758e95 --- /dev/null +++ b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts @@ -0,0 +1,4 @@ +// @importsNotUsedAsValues: preserve-exact +// @module: amd,system,commonjs,es2015 +// @noTypesAndSymbols: true +export {}; From a755070e45a5aef610d03f452e7c8e34395fc131 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 7 May 2021 10:13:43 -0700 Subject: [PATCH 03/19] Do not elide any imports or exports in preserve-exact --- src/compiler/transformers/ts.ts | 21 +++++---- ...sNotUsedAsValues_preserve-exact.errors.txt | 35 ++++++++++++++ .../importsNotUsedAsValues_preserve-exact.js | 43 +++++++++++++++++ ...ortsNotUsedAsValues_preserve-exact.symbols | 42 +++++++++++++++++ ...mportsNotUsedAsValues_preserve-exact.types | 47 +++++++++++++++++++ .../importsNotUsedAsValues_preserve-exact.ts | 26 ++++++++++ 6 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types create mode 100644 tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0d24f332c15bd..4cdb1df730944 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -46,6 +46,7 @@ namespace ts { const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks"); const languageVersion = getEmitScriptTarget(compilerOptions); const moduleKind = getEmitModuleKind(compilerOptions); + const { importsNotUsedAsValues } = compilerOptions; // Save the previous transformation hooks. const previousOnEmitNode = context.onEmitNode; @@ -2802,7 +2803,7 @@ namespace ts { } /** - * Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve'. + * Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' or 'preserve-exact'. * * @param node The import declaration node. */ @@ -2820,8 +2821,9 @@ namespace ts { // Elide the declaration if the import clause was elided. const importClause = visitNode(node.importClause, visitImportClause, isImportClause); return importClause || - compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || - compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error + importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || + importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact || + importsNotUsedAsValues === ImportsNotUsedAsValues.Error ? factory.updateImportDeclaration( node, /*decorators*/ undefined, @@ -2832,13 +2834,14 @@ namespace ts { } /** - * Visits an import clause, eliding it if it is not referenced. + * Visits an import clause, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve-exact'. * * @param node The import clause node. */ function visitImportClause(node: ImportClause): VisitResult { - if (node.isTypeOnly) { - return undefined; + Debug.assert(!node.isTypeOnly); + if (importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { + return node; } // Elide the import clause if we elide both its name and its named bindings. const name = resolver.isReferencedAliasDeclaration(node) ? node.name : undefined; @@ -2888,7 +2891,7 @@ namespace ts { /** * Visits an export declaration, eliding it if it does not contain a clause that resolves - * to a value. + * to a value and if `importsNotUsedAsValues` is not 'preserve-exact'. * * @param node The export declaration node. */ @@ -2897,7 +2900,7 @@ namespace ts { return undefined; } - if (!node.exportClause || isNamespaceExport(node.exportClause)) { + if (!node.exportClause || isNamespaceExport(node.exportClause) || importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { // never elide `export from ` declarations - // they should be kept for sideffects/untyped exports, even when the // type checker doesn't know about any exports @@ -2980,7 +2983,7 @@ namespace ts { if (isExternalModuleImportEqualsDeclaration(node)) { const isReferenced = resolver.isReferencedAliasDeclaration(node); // If the alias is unreferenced but we want to keep the import, replace with 'import "mod"'. - if (!isReferenced && compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve) { + if (!isReferenced && importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve) { return setOriginalNode( setTextRange( factory.createImportDeclaration( diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt new file mode 100644 index 0000000000000..c90a89d4bda8c --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt @@ -0,0 +1,35 @@ +tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. +tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + + +==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== + export default {}; + export const b = 0; + export const c = 1; + +==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ==== + import a, { b, c } from "./a"; + +==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== + import * as a from "./a"; + +==== tests/cases/conformance/externalModules/typeOnly/d.ts (1 errors) ==== + export = {}; + ~~~~~~~~~~~~ +!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. + +==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== + import D = require("./d"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + import DD = require("./d"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + DD; + +==== tests/cases/conformance/externalModules/typeOnly/f.ts (0 errors) ==== + import type a from "./a"; + import { b, c } from "./a"; + b; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js new file mode 100644 index 0000000000000..20050b1c3d625 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js @@ -0,0 +1,43 @@ +//// [tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts] //// + +//// [a.ts] +export default {}; +export const b = 0; +export const c = 1; + +//// [b.ts] +import a, { b, c } from "./a"; + +//// [c.ts] +import * as a from "./a"; + +//// [d.ts] +export = {}; + +//// [e.ts] +import D = require("./d"); +import DD = require("./d"); +DD; + +//// [f.ts] +import type a from "./a"; +import { b, c } from "./a"; +b; + + +//// [a.js] +export default {}; +export var b = 0; +export var c = 1; +//// [b.js] +import a, { b, c } from "./a"; +//// [c.js] +import * as a from "./a"; +//// [d.js] +export {}; +//// [e.js] +DD; +export {}; +//// [f.js] +import { b, c } from "./a"; +b; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols new file mode 100644 index 0000000000000..c1fe6176415a8 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols @@ -0,0 +1,42 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export default {}; +export const b = 0; +>b : Symbol(b, Decl(a.ts, 1, 12)) + +export const c = 1; +>c : Symbol(c, Decl(a.ts, 2, 12)) + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +import a, { b, c } from "./a"; +>a : Symbol(a, Decl(b.ts, 0, 6)) +>b : Symbol(b, Decl(b.ts, 0, 11)) +>c : Symbol(c, Decl(b.ts, 0, 14)) + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import * as a from "./a"; +>a : Symbol(a, Decl(c.ts, 0, 6)) + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export = {}; +No type information for this code. +No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import D = require("./d"); +>D : Symbol(D, Decl(e.ts, 0, 0)) + +import DD = require("./d"); +>DD : Symbol(DD, Decl(e.ts, 0, 26)) + +DD; +>DD : Symbol(DD, Decl(e.ts, 0, 26)) + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type a from "./a"; +>a : Symbol(a, Decl(f.ts, 0, 6)) + +import { b, c } from "./a"; +>b : Symbol(b, Decl(f.ts, 1, 8)) +>c : Symbol(c, Decl(f.ts, 1, 11)) + +b; +>b : Symbol(b, Decl(f.ts, 1, 8)) + diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types new file mode 100644 index 0000000000000..ed58db4b453d4 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types @@ -0,0 +1,47 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export default {}; +>{} : {} + +export const b = 0; +>b : 0 +>0 : 0 + +export const c = 1; +>c : 1 +>1 : 1 + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +import a, { b, c } from "./a"; +>a : {} +>b : 0 +>c : 1 + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import * as a from "./a"; +>a : typeof a + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export = {}; +>{} : {} + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import D = require("./d"); +>D : {} + +import DD = require("./d"); +>DD : {} + +DD; +>DD : {} + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type a from "./a"; +>a : any + +import { b, c } from "./a"; +>b : 0 +>c : 1 + +b; +>b : 0 + diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts new file mode 100644 index 0000000000000..ceab3ba2dfdfc --- /dev/null +++ b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts @@ -0,0 +1,26 @@ +// @importsNotUsedAsValues: preserve-exact +// @module: esnext + +// @Filename: a.ts +export default {}; +export const b = 0; +export const c = 1; + +// @Filename: b.ts +import a, { b, c } from "./a"; + +// @Filename: c.ts +import * as a from "./a"; + +// @Filename: d.ts +export = {}; + +// @Filename: e.ts +import D = require("./d"); +import DD = require("./d"); +DD; + +// @Filename: f.ts +import type a from "./a"; +import { b, c } from "./a"; +b; From 8dd24559a63f60fca2a5a687f1384fd8450febdd Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 17 May 2021 16:14:19 -0700 Subject: [PATCH 04/19] Add errors for writing imports/exports that reference elided names --- src/compiler/checker.ts | 76 ++++++++++++--- src/compiler/diagnosticMessages.json | 20 ++++ ...dAsValues_preserve-exact_errors.errors.txt | 81 ++++++++++++++++ ...tsNotUsedAsValues_preserve-exact_errors.js | 77 +++++++++++++++ ...UsedAsValues_preserve-exact_errors.symbols | 95 +++++++++++++++++++ ...otUsedAsValues_preserve-exact_errors.types | 95 +++++++++++++++++++ .../isolatedModulesReExportType.errors.txt | 17 +++- .../reference/isolatedModulesReExportType.js | 14 +++ .../isolatedModulesReExportType.symbols | 20 ++++ .../isolatedModulesReExportType.types | 22 ++++- .../compiler/isolatedModulesReExportType.ts | 11 +++ ...tsNotUsedAsValues_preserve-exact_errors.ts | 46 +++++++++ 12 files changed, 560 insertions(+), 14 deletions(-) create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols create mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types create mode 100644 tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c9cb3fd285482..2c6a6d1076fd4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2166,17 +2166,25 @@ namespace ts { const message = isExport ? Diagnostics._0_cannot_be_used_as_a_value_because_it_was_exported_using_export_type : Diagnostics._0_cannot_be_used_as_a_value_because_it_was_imported_using_import_type; - const relatedMessage = isExport - ? Diagnostics._0_was_exported_here - : Diagnostics._0_was_imported_here; const unescapedName = unescapeLeadingUnderscores(name); - addRelatedInfo( + addTypeOnlyDeclarationRelatedInfo( error(useSite, message, unescapedName), - createDiagnosticForNode(typeOnlyDeclaration, relatedMessage, unescapedName)); + typeOnlyDeclaration, + unescapedName); } } } + function addTypeOnlyDeclarationRelatedInfo(diagnostic: Diagnostic, typeOnlyDeclaration: TypeOnlyCompatibleAliasDeclaration | undefined, unescapedName: string) { + if (!typeOnlyDeclaration) return diagnostic; + return addRelatedInfo( + diagnostic, + createDiagnosticForNode( + typeOnlyDeclaration, + typeOnlyDeclarationIsExport(typeOnlyDeclaration) ? Diagnostics._0_was_exported_here : Diagnostics._0_was_imported_here, + unescapedName)); + } + function getIsDeferredContext(location: Node, lastLocation: Node | undefined): boolean { if (location.kind !== SyntaxKind.ArrowFunction && location.kind !== SyntaxKind.FunctionExpression) { // initializers in instance property declaration of class like entities are executed in constructor and thus deferred @@ -38072,13 +38080,57 @@ namespace ts { error(node, message, symbolToString(symbol)); } - // Don't allow to re-export something with no value side when `--isolatedModules` is set. - if (compilerOptions.isolatedModules - && node.kind === SyntaxKind.ExportSpecifier - && !node.parent.parent.isTypeOnly - && !(target.flags & SymbolFlags.Value) - && !(node.flags & NodeFlags.Ambient)) { - error(node, Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type); + const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node); + if ((compilerOptions.isolatedModules || compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) + && !isDeclaredTypeOnly + && !(node.flags & NodeFlags.Ambient) + && (!(target.flags & SymbolFlags.Value) || getTypeOnlyAliasDeclaration(symbol))) { + const isType = !(target.flags & SymbolFlags.Value); + const typeOnlyAlias = getTypeOnlyAliasDeclaration(symbol); + + switch (node.kind) { + case SyntaxKind.ImportClause: + case SyntaxKind.ImportSpecifier: + case SyntaxKind.ImportEqualsDeclaration: { + if (compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { + const message = isType + ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact + : Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact; + const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); + addTypeOnlyDeclarationRelatedInfo( + error(node, message, name), + isType ? undefined : typeOnlyAlias, + name + ); + } + break; + } + case SyntaxKind.ExportSpecifier: { + // Don't allow re-exporting an export that will be elided when `--isolatedModules` is set + // or when `--importsNotUsedAsValues` is `preserve-exact`. + if (compilerOptions.isolatedModules && + compilerOptions.importsNotUsedAsValues !== ImportsNotUsedAsValues.PreserveExact && + typeOnlyAlias && getSourceFileOfNode(typeOnlyAlias) === getSourceFileOfNode(node)) { + // In `isolatedModules` alone, `import type { A } from './a'; export { A }` is allowed + // because single-file analysis can determine that the export should be dropped. + // `--importsNotUsedAsValues=preserv-exact` is stricter; it refuses to touch non-type-only + // imports and exports, so `export { A }` is not allowed if 'A' is not emitted. + return; + } + const message = + compilerOptions.isolatedModules && isType ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type : + compilerOptions.isolatedModules && !isType ? Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled : + isType ? Diagnostics._0_is_a_type_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact : + Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact; + const name = idText(node.propertyName || node.name); + addTypeOnlyDeclarationRelatedInfo( + error(node, message, name), + isType ? undefined : typeOnlyAlias, + name + ); + return; + } + } } if (isImportSpecifier(node) && target.declarations?.every(d => !!(getCombinedNodeFlags(d) & NodeFlags.Deprecated))) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index cf7c1878d81ab..e8232cd2a7d47 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1364,6 +1364,26 @@ "category": "Error", "code": 1433 }, + "'{0}' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "category": "Error", + "code": 1434 + }, + "'{0}' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "category": "Error", + "code": 1435 + }, + "'{0}' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "category": "Error", + "code": 1436 + }, + "'{0}' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "category": "Error", + "code": 1437 + }, + "'{0}' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": { + "category": "Error", + "code": 1438 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt new file mode 100644 index 0000000000000..52342ce13a8e9 --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt @@ -0,0 +1,81 @@ +tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/f.ts(3,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. + + +==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== + export type A = {}; + export type { A as default }; + +==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ==== + class B {}; + export type { B, B as default }; + +==== tests/cases/conformance/externalModules/typeOnly/c.ts (4 errors) ==== + import DefaultA from "./a"; + ~~~~~~~~ +!!! error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. + import { A } from "./a"; + ~ +!!! error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. + import DefaultB from "./b"; + ~~~~~~~~ +!!! error TS1436: 'DefaultB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. + import { B } from "./b"; + ~ +!!! error TS1436: 'B' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. + +==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== + import type DefaultA from "./a"; + import type { A } from "./a"; + import type DefaultB from "./b"; + import type { B } from "./b"; + +==== tests/cases/conformance/externalModules/typeOnly/d.ts (2 errors) ==== + export { A as AA } from "./a"; + ~~~~~~~ +!!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. + export { B as BB } from "./b"; + ~~~~~~~ +!!! error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. + +==== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts (0 errors) ==== + export type { A as AA } from "./a"; + export type { B as BB } from "./b"; + +==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== + import { AA, BB } from "./d"; + ~~ +!!! error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. + ~~ +!!! error TS1436: 'BB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. + +==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== + import type { AA, BB } from "./d"; + +==== tests/cases/conformance/externalModules/typeOnly/f.ts (2 errors) ==== + import type { A } from "./a"; + import type { B } from "./b"; + export { A, B as BB }; + ~ +!!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. + ~~~~~~~ +!!! error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! related TS1376 tests/cases/conformance/externalModules/typeOnly/f.ts:2:15: 'B' was imported here. + +==== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts (0 errors) ==== + import type { A } from "./a"; + import type { B } from "./b"; + export type { A, B as BB }; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js new file mode 100644 index 0000000000000..f564805995fdf --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js @@ -0,0 +1,77 @@ +//// [tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts] //// + +//// [a.ts] +export type A = {}; +export type { A as default }; + +//// [b.ts] +class B {}; +export type { B, B as default }; + +//// [c.ts] +import DefaultA from "./a"; +import { A } from "./a"; +import DefaultB from "./b"; +import { B } from "./b"; + +//// [c.fixed.ts] +import type DefaultA from "./a"; +import type { A } from "./a"; +import type DefaultB from "./b"; +import type { B } from "./b"; + +//// [d.ts] +export { A as AA } from "./a"; +export { B as BB } from "./b"; + +//// [d.fixed.ts] +export type { A as AA } from "./a"; +export type { B as BB } from "./b"; + +//// [e.ts] +import { AA, BB } from "./d"; + +//// [e.fixed.ts] +import type { AA, BB } from "./d"; + +//// [f.ts] +import type { A } from "./a"; +import type { B } from "./b"; +export { A, B as BB }; + +//// [f.fixed.ts] +import type { A } from "./a"; +import type { B } from "./b"; +export type { A, B as BB }; + + +//// [a.js] +export {}; +//// [b.js] +var B = /** @class */ (function () { + function B() { + } + return B; +}()); +; +export {}; +//// [c.js] +import DefaultA from "./a"; +import { A } from "./a"; +import DefaultB from "./b"; +import { B } from "./b"; +//// [c.fixed.js] +export {}; +//// [d.js] +export { A as AA } from "./a"; +export { B as BB } from "./b"; +//// [d.fixed.js] +export {}; +//// [e.js] +import { AA, BB } from "./d"; +//// [e.fixed.js] +export {}; +//// [f.js] +export { A, B as BB }; +//// [f.fixed.js] +export {}; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols new file mode 100644 index 0000000000000..0dd64238caf2a --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols @@ -0,0 +1,95 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export type A = {}; +>A : Symbol(A, Decl(a.ts, 0, 0)) + +export type { A as default }; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>default : Symbol(default, Decl(a.ts, 1, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +class B {}; +>B : Symbol(B, Decl(b.ts, 0, 0)) + +export type { B, B as default }; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>B : Symbol(B, Decl(b.ts, 0, 0)) +>default : Symbol(default, Decl(b.ts, 1, 16)) + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import DefaultA from "./a"; +>DefaultA : Symbol(DefaultA, Decl(c.ts, 0, 6)) + +import { A } from "./a"; +>A : Symbol(A, Decl(c.ts, 1, 8)) + +import DefaultB from "./b"; +>DefaultB : Symbol(DefaultB, Decl(c.ts, 2, 6)) + +import { B } from "./b"; +>B : Symbol(B, Decl(c.ts, 3, 8)) + +=== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts === +import type DefaultA from "./a"; +>DefaultA : Symbol(DefaultA, Decl(c.fixed.ts, 0, 6)) + +import type { A } from "./a"; +>A : Symbol(A, Decl(c.fixed.ts, 1, 13)) + +import type DefaultB from "./b"; +>DefaultB : Symbol(DefaultB, Decl(c.fixed.ts, 2, 6)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(c.fixed.ts, 3, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export { A as AA } from "./a"; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>AA : Symbol(AA, Decl(d.ts, 0, 8)) + +export { B as BB } from "./b"; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>BB : Symbol(BB, Decl(d.ts, 1, 8)) + +=== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts === +export type { A as AA } from "./a"; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>AA : Symbol(AA, Decl(d.fixed.ts, 0, 13)) + +export type { B as BB } from "./b"; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>BB : Symbol(BB, Decl(d.fixed.ts, 1, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import { AA, BB } from "./d"; +>AA : Symbol(AA, Decl(e.ts, 0, 8)) +>BB : Symbol(BB, Decl(e.ts, 0, 12)) + +=== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts === +import type { AA, BB } from "./d"; +>AA : Symbol(AA, Decl(e.fixed.ts, 0, 13)) +>BB : Symbol(BB, Decl(e.fixed.ts, 0, 17)) + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type { A } from "./a"; +>A : Symbol(A, Decl(f.ts, 0, 13)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(f.ts, 1, 13)) + +export { A, B as BB }; +>A : Symbol(A, Decl(f.ts, 2, 8)) +>B : Symbol(B, Decl(f.ts, 1, 13)) +>BB : Symbol(BB, Decl(f.ts, 2, 11)) + +=== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts === +import type { A } from "./a"; +>A : Symbol(A, Decl(f.fixed.ts, 0, 13)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(f.fixed.ts, 1, 13)) + +export type { A, B as BB }; +>A : Symbol(A, Decl(f.fixed.ts, 2, 13)) +>B : Symbol(B, Decl(f.fixed.ts, 1, 13)) +>BB : Symbol(BB, Decl(f.fixed.ts, 2, 16)) + diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types new file mode 100644 index 0000000000000..1e8a912ed7ced --- /dev/null +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types @@ -0,0 +1,95 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export type A = {}; +>A : A + +export type { A as default }; +>A : any +>default : A + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +class B {}; +>B : B + +export type { B, B as default }; +>B : B +>B : typeof B +>default : B + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import DefaultA from "./a"; +>DefaultA : any + +import { A } from "./a"; +>A : any + +import DefaultB from "./b"; +>DefaultB : typeof DefaultB + +import { B } from "./b"; +>B : typeof DefaultB + +=== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts === +import type DefaultA from "./a"; +>DefaultA : DefaultA + +import type { A } from "./a"; +>A : DefaultA + +import type DefaultB from "./b"; +>DefaultB : DefaultB + +import type { B } from "./b"; +>B : DefaultB + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export { A as AA } from "./a"; +>A : any +>AA : any + +export { B as BB } from "./b"; +>B : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B +>BB : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B + +=== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts === +export type { A as AA } from "./a"; +>A : any +>AA : import("tests/cases/conformance/externalModules/typeOnly/a").A + +export type { B as BB } from "./b"; +>B : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B +>BB : import("tests/cases/conformance/externalModules/typeOnly/b").B + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import { AA, BB } from "./d"; +>AA : any +>BB : typeof BB + +=== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts === +import type { AA, BB } from "./d"; +>AA : AA +>BB : BB + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type { A } from "./a"; +>A : A + +import type { B } from "./b"; +>B : B + +export { A, B as BB }; +>A : any +>B : typeof B +>BB : typeof B + +=== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts === +import type { A } from "./a"; +>A : A + +import type { B } from "./b"; +>B : B + +export type { A, B as BB }; +>A : A +>B : typeof B +>BB : B + diff --git a/tests/baselines/reference/isolatedModulesReExportType.errors.txt b/tests/baselines/reference/isolatedModulesReExportType.errors.txt index 299bd0fcf88e6..0b4b3df52077c 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.errors.txt +++ b/tests/baselines/reference/isolatedModulesReExportType.errors.txt @@ -1,8 +1,9 @@ /user.ts(2,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. /user.ts(17,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. +/user.ts(25,10): error TS1438: 'CC' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. -==== /user.ts (2 errors) ==== +==== /user.ts (3 errors) ==== // Error, can't re-export something that's only a type. export { T } from "./exportT"; ~ @@ -25,6 +26,17 @@ ~~~~~~~ !!! error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. + // Ok, type-only import indicates that the export can be elided. + import type { T as TT } from "./exportT"; + export { TT }; + + // Error, type-only declaration is in a different file. + import { C as CC } from "./reExportValueAsTypeOnly"; + export { CC }; + ~~ +!!! error TS1438: 'CC' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! related TS1377 /reExportValueAsTypeOnly.ts:1:15: 'CC' was exported here. + ==== /exportT.ts (0 errors) ==== export type T = number; @@ -45,4 +57,7 @@ declare module "baz" { export { T } from "foo"; // Also allowed. } + +==== /reExportValueAsTypeOnly.ts (0 errors) ==== + export type { C } from "./exportValue"; \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesReExportType.js b/tests/baselines/reference/isolatedModulesReExportType.js index 9908e6701dffe..2e3828fcfbb8d 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.js +++ b/tests/baselines/reference/isolatedModulesReExportType.js @@ -21,6 +21,9 @@ declare module "baz" { export { T } from "foo"; // Also allowed. } +//// [reExportValueAsTypeOnly.ts] +export type { C } from "./exportValue"; + //// [user.ts] // Error, can't re-export something that's only a type. export { T } from "./exportT"; @@ -39,6 +42,14 @@ export type T3 = T; // Error, not clear (to an isolated module) whether `T4` is a type. import { T } from "./exportT"; export { T as T4 }; + +// Ok, type-only import indicates that the export can be elided. +import type { T as TT } from "./exportT"; +export { TT }; + +// Error, type-only declaration is in a different file. +import { C as CC } from "./reExportValueAsTypeOnly"; +export { CC }; //// [exportT.js] @@ -57,6 +68,9 @@ var C = /** @class */ (function () { return C; }()); exports.C = C; +//// [reExportValueAsTypeOnly.js] +"use strict"; +exports.__esModule = true; //// [user.js] "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { diff --git a/tests/baselines/reference/isolatedModulesReExportType.symbols b/tests/baselines/reference/isolatedModulesReExportType.symbols index b64f0cae4626a..e52acba684931 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.symbols +++ b/tests/baselines/reference/isolatedModulesReExportType.symbols @@ -30,6 +30,22 @@ export { T as T4 }; >T : Symbol(T, Decl(user.ts, 15, 8)) >T4 : Symbol(T4, Decl(user.ts, 16, 8)) +// Ok, type-only import indicates that the export can be elided. +import type { T as TT } from "./exportT"; +>T : Symbol(NS.T, Decl(exportT.ts, 0, 0)) +>TT : Symbol(TT, Decl(user.ts, 19, 13)) + +export { TT }; +>TT : Symbol(TT, Decl(user.ts, 20, 8)) + +// Error, type-only declaration is in a different file. +import { C as CC } from "./reExportValueAsTypeOnly"; +>C : Symbol(C, Decl(reExportValueAsTypeOnly.ts, 0, 13)) +>CC : Symbol(CC, Decl(user.ts, 23, 8)) + +export { CC }; +>CC : Symbol(CC, Decl(user.ts, 24, 8)) + === /exportT.ts === export type T = number; >T : Symbol(T, Decl(exportT.ts, 0, 0)) @@ -45,3 +61,7 @@ declare type T = number; export = T; >T : Symbol(T, Decl(exportEqualsT.ts, 0, 0)) +=== /reExportValueAsTypeOnly.ts === +export type { C } from "./exportValue"; +>C : Symbol(C, Decl(reExportValueAsTypeOnly.ts, 0, 13)) + diff --git a/tests/baselines/reference/isolatedModulesReExportType.types b/tests/baselines/reference/isolatedModulesReExportType.types index 55c4cdfb7b778..370853c76aee5 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.types +++ b/tests/baselines/reference/isolatedModulesReExportType.types @@ -8,7 +8,7 @@ export import T2 = require("./exportEqualsT"); // OK, has a value side export { C } from "./exportValue"; ->C : typeof import("/exportValue").C +>C : typeof CC // OK, even though the namespace it exports is only types. import * as NS from "./exportT"; @@ -29,6 +29,22 @@ export { T as T4 }; >T : any >T4 : any +// Ok, type-only import indicates that the export can be elided. +import type { T as TT } from "./exportT"; +>T : any +>TT : number + +export { TT }; +>TT : any + +// Error, type-only declaration is in a different file. +import { C as CC } from "./reExportValueAsTypeOnly"; +>C : typeof CC +>CC : typeof CC + +export { CC }; +>CC : typeof CC + === /exportT.ts === export type T = number; >T : number @@ -44,3 +60,7 @@ declare type T = number; export = T; >T : number +=== /reExportValueAsTypeOnly.ts === +export type { C } from "./exportValue"; +>C : import("/exportValue").C + diff --git a/tests/cases/compiler/isolatedModulesReExportType.ts b/tests/cases/compiler/isolatedModulesReExportType.ts index 6d82fd3f180d1..8bb10bbf22931 100644 --- a/tests/cases/compiler/isolatedModulesReExportType.ts +++ b/tests/cases/compiler/isolatedModulesReExportType.ts @@ -21,6 +21,9 @@ declare module "baz" { export { T } from "foo"; // Also allowed. } +// @Filename: /reExportValueAsTypeOnly.ts +export type { C } from "./exportValue"; + // @Filename: /user.ts // Error, can't re-export something that's only a type. export { T } from "./exportT"; @@ -39,3 +42,11 @@ export type T3 = T; // Error, not clear (to an isolated module) whether `T4` is a type. import { T } from "./exportT"; export { T as T4 }; + +// Ok, type-only import indicates that the export can be elided. +import type { T as TT } from "./exportT"; +export { TT }; + +// Error, type-only declaration is in a different file. +import { C as CC } from "./reExportValueAsTypeOnly"; +export { CC }; diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts new file mode 100644 index 0000000000000..fcfcb70ea9b8f --- /dev/null +++ b/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts @@ -0,0 +1,46 @@ +// @importsNotUsedAsValues: preserve-exact +// @module: esnext + +// @Filename: a.ts +export type A = {}; +export type { A as default }; + +// @Filename: b.ts +class B {}; +export type { B, B as default }; + +// @Filename: c.ts +import DefaultA from "./a"; +import { A } from "./a"; +import DefaultB from "./b"; +import { B } from "./b"; + +// @Filename: c.fixed.ts +import type DefaultA from "./a"; +import type { A } from "./a"; +import type DefaultB from "./b"; +import type { B } from "./b"; + +// @Filename: d.ts +export { A as AA } from "./a"; +export { B as BB } from "./b"; + +// @Filename: d.fixed.ts +export type { A as AA } from "./a"; +export type { B as BB } from "./b"; + +// @Filename: e.ts +import { AA, BB } from "./d"; + +// @Filename: e.fixed.ts +import type { AA, BB } from "./d"; + +// @Filename: f.ts +import type { A } from "./a"; +import type { B } from "./b"; +export { A, B as BB }; + +// @Filename: f.fixed.ts +import type { A } from "./a"; +import type { B } from "./b"; +export type { A, B as BB }; From e6df1e5e5f4a73fc93000af51e5deb860992dd7f Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 17 May 2021 17:21:22 -0700 Subject: [PATCH 05/19] Improve diagnostics wording --- src/compiler/checker.ts | 6 +++--- src/compiler/diagnosticMessages.json | 8 ++++---- src/compiler/program.ts | 2 +- ...dAsValues_preserve-exact_errors.errors.txt | 20 +++++++++---------- ...eserve-exact_module(module=amd).errors.txt | 4 ++-- ...e-exact_module(module=commonjs).errors.txt | 4 ++-- ...rve-exact_module(module=system).errors.txt | 4 ++-- .../isolatedModulesReExportType.errors.txt | 4 ++-- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2c6a6d1076fd4..e89ca27d2de19 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38095,7 +38095,7 @@ namespace ts { if (compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { const message = isType ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact - : Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact; + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact; const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); addTypeOnlyDeclarationRelatedInfo( error(node, message, name), @@ -38119,9 +38119,9 @@ namespace ts { } const message = compilerOptions.isolatedModules && isType ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type : - compilerOptions.isolatedModules && !isType ? Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled : + compilerOptions.isolatedModules && !isType ? Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled : isType ? Diagnostics._0_is_a_type_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact : - Diagnostics._0_resolves_to_a_type_only_reference_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact; + Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact; const name = idText(node.propertyName || node.name); addTypeOnlyDeclarationRelatedInfo( error(node, message, name), diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e8232cd2a7d47..579dfc851918a 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1372,15 +1372,15 @@ "category": "Error", "code": 1435 }, - "'{0}' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { "category": "Error", "code": 1436 }, - "'{0}' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { "category": "Error", "code": 1437 }, - "'{0}' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": { + "'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": { "category": "Error", "code": 1438 }, @@ -3962,7 +3962,7 @@ "category": "Error", "code": 5094 }, - "Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later.": { + "Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later.": { "category": "Error", "code": 5095 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c7af1680fe108..0a0fc2a226bf5 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -3296,7 +3296,7 @@ namespace ts { } if (options.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact && getEmitModuleKind(options) < ModuleKind.ES2015) { - createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_importsNotUsedAsValues_may_only_be_set_to_preserve_exact_when_module_is_set_to_es2015_or_later); + createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_importsNotUsedAsValues_may_be_set_to_preserve_exact_only_when_module_is_set_to_es2015_or_later); } // If the emit is enabled make sure that every output file is unique and not overwriting any of the input files diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt index 52342ce13a8e9..22e14296a4b16 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt @@ -1,13 +1,13 @@ tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. tests/cases/conformance/externalModules/typeOnly/f.ts(3,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. ==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== @@ -27,11 +27,11 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r !!! error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. import DefaultB from "./b"; ~~~~~~~~ -!!! error TS1436: 'DefaultB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. import { B } from "./b"; ~ -!!! error TS1436: 'B' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== @@ -46,7 +46,7 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r !!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. export { B as BB } from "./b"; ~~~~~~~ -!!! error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts (0 errors) ==== @@ -58,7 +58,7 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r ~~ !!! error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. ~~ -!!! error TS1436: 'BB' resolves to a type-only reference and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== @@ -71,7 +71,7 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r ~ !!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. ~~~~~~~ -!!! error TS1437: 'B' resolves to a type-only reference and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. !!! related TS1376 tests/cases/conformance/externalModules/typeOnly/f.ts:2:15: 'B' was imported here. ==== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts (0 errors) ==== diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt index 78cda02c61432..b52a3b9677bff 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt index 78cda02c61432..b52a3b9677bff 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt index 78cda02c61432..b52a3b9677bff 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt +++ b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'importsNotUsedAsValues' may only be set to 'preserve-exact' when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesReExportType.errors.txt b/tests/baselines/reference/isolatedModulesReExportType.errors.txt index 0b4b3df52077c..6fa10e49d3520 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.errors.txt +++ b/tests/baselines/reference/isolatedModulesReExportType.errors.txt @@ -1,6 +1,6 @@ /user.ts(2,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. /user.ts(17,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. -/user.ts(25,10): error TS1438: 'CC' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +/user.ts(25,10): error TS1438: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. ==== /user.ts (3 errors) ==== @@ -34,7 +34,7 @@ import { C as CC } from "./reExportValueAsTypeOnly"; export { CC }; ~~ -!!! error TS1438: 'CC' resolves to a type-only reference and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! error TS1438: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 /reExportValueAsTypeOnly.ts:1:15: 'CC' was exported here. ==== /exportT.ts (0 errors) ==== From 126069dd938f9c8d25779797470643658c5b0d79 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 17 May 2021 17:40:13 -0700 Subject: [PATCH 06/19] Update API baselines --- tests/baselines/reference/api/tsserverlibrary.d.ts | 3 ++- tests/baselines/reference/api/typescript.d.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index e94b1da823f34..073b62909746f 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2946,7 +2946,8 @@ declare namespace ts { export enum ImportsNotUsedAsValues { Remove = 0, Preserve = 1, - Error = 2 + Error = 2, + PreserveExact = 3 } export enum NewLineKind { CarriageReturnLineFeed = 0, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 7b8a24ba088b4..206dd09a2b39b 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2946,7 +2946,8 @@ declare namespace ts { export enum ImportsNotUsedAsValues { Remove = 0, Preserve = 1, - Error = 2 + Error = 2, + PreserveExact = 3 } export enum NewLineKind { CarriageReturnLineFeed = 0, From 30636381e2259f1213e021ee75ec5f166ccecdc8 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 16 Jun 2021 15:36:40 -0500 Subject: [PATCH 07/19] Redo as noEraslingImportedNames --- src/compiler/checker.ts | 42 ++++---- src/compiler/commandLineParser.ts | 8 +- src/compiler/diagnosticMessages.json | 18 ++-- src/compiler/program.ts | 4 +- src/compiler/transformers/ts.ts | 12 +-- src/compiler/types.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 4 +- tests/baselines/reference/api/typescript.d.ts | 4 +- ...eserve-exact_module(module=amd).errors.txt | 7 -- ...e-exact_module(module=commonjs).errors.txt | 7 -- ..._preserve-exact_module(module=commonjs).js | 7 -- ...es_preserve-exact_module(module=es2015).js | 6 -- ...rve-exact_module(module=system).errors.txt | 7 -- ...edNames(isolatedmodules=false).errors.txt} | 0 ...ngImportedNames(isolatedmodules=false).js} | 2 +- ...ortedNames(isolatedmodules=false).symbols} | 0 ...mportedNames(isolatedmodules=false).types} | 0 ...rtedNames(isolatedmodules=true).errors.txt | 35 +++++++ ...singImportedNames(isolatedmodules=true).js | 43 +++++++++ ...mportedNames(isolatedmodules=true).symbols | 42 ++++++++ ...gImportedNames(isolatedmodules=true).types | 47 +++++++++ ...tedNames_errors(isolatedmodules=false).js} | 2 +- ...mes_errors(isolatedmodules=false).symbols} | 0 ...Names_errors(isolatedmodules=false).types} | 0 ...s_errors(isolatedmodules=true).errors.txt} | 41 ++++---- ...ortedNames_errors(isolatedmodules=true).js | 77 +++++++++++++++ ...Names_errors(isolatedmodules=true).symbols | 95 +++++++++++++++++++ ...edNames_errors(isolatedmodules=true).types | 95 +++++++++++++++++++ ...mportedNames_module(module=amd).errors.txt | 7 ++ ...rasingImportedNames_module(module=amd).js} | 4 +- ...edNames_module(module=commonjs).errors.txt | 7 ++ ...ngImportedNames_module(module=commonjs).js | 7 ++ ...singImportedNames_module(module=es2015).js | 6 ++ ...rtedNames_module(module=system).errors.txt | 7 ++ ...ingImportedNames_module(module=system).js} | 4 +- .../noErasingImportedNames/tsconfig.json | 5 + ...rve-exact.ts => noErasingImportedNames.ts} | 3 +- ...rs.ts => noErasingImportedNames_errors.ts} | 3 +- ...le.ts => noErasingImportedNames_module.ts} | 2 +- 39 files changed, 545 insertions(+), 117 deletions(-) delete mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt delete mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt delete mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js delete mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js delete mode 100644 tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact.errors.txt => noErasingImportedNames(isolatedmodules=false).errors.txt} (100%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact.js => noErasingImportedNames(isolatedmodules=false).js} (80%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact.symbols => noErasingImportedNames(isolatedmodules=false).symbols} (100%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact.types => noErasingImportedNames(isolatedmodules=false).types} (100%) create mode 100644 tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt create mode 100644 tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js create mode 100644 tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols create mode 100644 tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_errors.js => noErasingImportedNames_errors(isolatedmodules=false).js} (89%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_errors.symbols => noErasingImportedNames_errors(isolatedmodules=false).symbols} (100%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_errors.types => noErasingImportedNames_errors(isolatedmodules=false).types} (100%) rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_errors.errors.txt => noErasingImportedNames_errors(isolatedmodules=true).errors.txt} (56%) create mode 100644 tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js create mode 100644 tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols create mode 100644 tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types create mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_module(module=amd).js => noErasingImportedNames_module(module=amd).js} (51%) create mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt create mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js create mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=es2015).js create mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt rename tests/baselines/reference/{importsNotUsedAsValues_preserve-exact_module(module=system).js => noErasingImportedNames_module(module=system).js} (64%) create mode 100644 tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json rename tests/cases/conformance/externalModules/typeOnly/{importsNotUsedAsValues_preserve-exact.ts => noErasingImportedNames.ts} (85%) rename tests/cases/conformance/externalModules/typeOnly/{importsNotUsedAsValues_preserve-exact_errors.ts => noErasingImportedNames_errors.ts} (93%) rename tests/cases/conformance/externalModules/typeOnly/{importsNotUsedAsValues_preserve-exact_module.ts => noErasingImportedNames_module.ts} (64%) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e89ca27d2de19..437d105464c55 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38081,7 +38081,7 @@ namespace ts { } const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node); - if ((compilerOptions.isolatedModules || compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) + if (compilerOptions.isolatedModules && !isDeclaredTypeOnly && !(node.flags & NodeFlags.Ambient) && (!(target.flags & SymbolFlags.Value) || getTypeOnlyAliasDeclaration(symbol))) { @@ -38092,10 +38092,10 @@ namespace ts { case SyntaxKind.ImportClause: case SyntaxKind.ImportSpecifier: case SyntaxKind.ImportEqualsDeclaration: { - if (compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { + if (compilerOptions.noErasingImportedNames) { const message = isType - ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact - : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact; + ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled; const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); addTypeOnlyDeclarationRelatedInfo( error(node, message, name), @@ -38106,29 +38106,21 @@ namespace ts { break; } case SyntaxKind.ExportSpecifier: { - // Don't allow re-exporting an export that will be elided when `--isolatedModules` is set - // or when `--importsNotUsedAsValues` is `preserve-exact`. - if (compilerOptions.isolatedModules && - compilerOptions.importsNotUsedAsValues !== ImportsNotUsedAsValues.PreserveExact && - typeOnlyAlias && getSourceFileOfNode(typeOnlyAlias) === getSourceFileOfNode(node)) { - // In `isolatedModules` alone, `import type { A } from './a'; export { A }` is allowed - // because single-file analysis can determine that the export should be dropped. - // `--importsNotUsedAsValues=preserv-exact` is stricter; it refuses to touch non-type-only - // imports and exports, so `export { A }` is not allowed if 'A' is not emitted. + // Don't allow re-exporting an export that will be elided when `--isolatedModules` is set. + // The exception is that `import type { A } from './a'; export { A }` is allowed + // because single-file analysis can determine that the export should be dropped. + if (getSourceFileOfNode(typeOnlyAlias) !== getSourceFileOfNode(node)) { + const message = isType + ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled; + const name = idText(node.propertyName || node.name); + addTypeOnlyDeclarationRelatedInfo( + error(node, message, name), + isType ? undefined : typeOnlyAlias, + name + ); return; } - const message = - compilerOptions.isolatedModules && isType ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type : - compilerOptions.isolatedModules && !isType ? Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled : - isType ? Diagnostics._0_is_a_type_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact : - Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact; - const name = idText(node.propertyName || node.name); - addTypeOnlyDeclarationRelatedInfo( - error(node, message, name), - isType ? undefined : typeOnlyAlias, - name - ); - return; } } } diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d79e7e180ef2c..d0d0f95d5502a 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -539,7 +539,6 @@ namespace ts { "remove": ImportsNotUsedAsValues.Remove, "preserve": ImportsNotUsedAsValues.Preserve, "error": ImportsNotUsedAsValues.Error, - "preserve-exact": ImportsNotUsedAsValues.PreserveExact, })), affectsEmit: true, affectsSemanticDiagnostics: true, @@ -1085,6 +1084,13 @@ namespace ts { category: Diagnostics.Advanced_Options, description: Diagnostics.Emit_class_fields_with_Define_instead_of_Set, }, + { + name: "noErasingImportedNames", + type: "boolean", + affectsEmit: true, + category: Diagnostics.Advanced_Options, + description: Diagnostics.Disable_the_removal_of_unused_imported_identifiers_from_the_JavaScript_output + }, { name: "keyofStringsOnly", diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 579dfc851918a..125686d4b5436 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1364,26 +1364,22 @@ "category": "Error", "code": 1433 }, - "'{0}' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "'{0}' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1434 }, - "'{0}' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { - "category": "Error", - "code": 1435 - }, - "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { + "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1436 }, - "'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": { - "category": "Error", - "code": 1437 - }, "'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": { "category": "Error", "code": 1438 }, + "Disable the removal of unused imported identifiers from the JavaScript output.": { + "category": "Message", + "code": 1439 + }, "The types of '{0}' are incompatible between these types.": { "category": "Error", @@ -3962,7 +3958,7 @@ "category": "Error", "code": 5094 }, - "Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later.": { + "Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later.": { "category": "Error", "code": 5095 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 0a0fc2a226bf5..8ceecda324c39 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -3295,8 +3295,8 @@ namespace ts { } } - if (options.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact && getEmitModuleKind(options) < ModuleKind.ES2015) { - createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_importsNotUsedAsValues_may_be_set_to_preserve_exact_only_when_module_is_set_to_es2015_or_later); + if (options.noErasingImportedNames && getEmitModuleKind(options) < ModuleKind.ES2015) { + createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_noErasingImportedNames_may_be_enabled_only_when_module_is_set_to_es2015_or_later); } // If the emit is enabled make sure that every output file is unique and not overwriting any of the input files diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 4cdb1df730944..bec4a5a2af55b 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2803,7 +2803,7 @@ namespace ts { } /** - * Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' or 'preserve-exact'. + * Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' and `noErasingImportedNames` is not set. * * @param node The import declaration node. */ @@ -2821,8 +2821,8 @@ namespace ts { // Elide the declaration if the import clause was elided. const importClause = visitNode(node.importClause, visitImportClause, isImportClause); return importClause || + compilerOptions.noErasingImportedNames || importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || - importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact || importsNotUsedAsValues === ImportsNotUsedAsValues.Error ? factory.updateImportDeclaration( node, @@ -2834,13 +2834,13 @@ namespace ts { } /** - * Visits an import clause, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve-exact'. + * Visits an import clause, eliding it if it is not referenced and `noErasingImportedNames` is not set. * * @param node The import clause node. */ function visitImportClause(node: ImportClause): VisitResult { Debug.assert(!node.isTypeOnly); - if (importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { + if (compilerOptions.noErasingImportedNames) { return node; } // Elide the import clause if we elide both its name and its named bindings. @@ -2891,7 +2891,7 @@ namespace ts { /** * Visits an export declaration, eliding it if it does not contain a clause that resolves - * to a value and if `importsNotUsedAsValues` is not 'preserve-exact'. + * to a value and if `noErasingImportedNames` is not set. * * @param node The export declaration node. */ @@ -2900,7 +2900,7 @@ namespace ts { return undefined; } - if (!node.exportClause || isNamespaceExport(node.exportClause) || importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) { + if (!node.exportClause || isNamespaceExport(node.exportClause) || compilerOptions.noErasingImportedNames) { // never elide `export from ` declarations - // they should be kept for sideffects/untyped exports, even when the // type checker doesn't know about any exports diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 0fc47a278363b..0ea779a311350 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5954,6 +5954,7 @@ namespace ts { /*@internal*/noEmitForJsFiles?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; + noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; // Always combine with strict property @@ -6071,7 +6072,6 @@ namespace ts { Remove, Preserve, Error, - PreserveExact, } export const enum NewLineKind { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 073b62909746f..4b6b6af43cdbc 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2850,6 +2850,7 @@ declare namespace ts { noEmit?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; + noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; @@ -2946,8 +2947,7 @@ declare namespace ts { export enum ImportsNotUsedAsValues { Remove = 0, Preserve = 1, - Error = 2, - PreserveExact = 3 + Error = 2 } export enum NewLineKind { CarriageReturnLineFeed = 0, diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 206dd09a2b39b..1ff2c9e9302d7 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2850,6 +2850,7 @@ declare namespace ts { noEmit?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; + noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; @@ -2946,8 +2947,7 @@ declare namespace ts { export enum ImportsNotUsedAsValues { Remove = 0, Preserve = 1, - Error = 2, - PreserveExact = 3 + Error = 2 } export enum NewLineKind { CarriageReturnLineFeed = 0, diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt deleted file mode 100644 index b52a3b9677bff..0000000000000 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt deleted file mode 100644 index b52a3b9677bff..0000000000000 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js deleted file mode 100644 index 0ab551b173b93..0000000000000 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).js +++ /dev/null @@ -1,7 +0,0 @@ -//// [importsNotUsedAsValues_preserve-exact_module.ts] -export {}; - - -//// [importsNotUsedAsValues_preserve-exact_module.js] -"use strict"; -exports.__esModule = true; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js deleted file mode 100644 index 3abff4aa677b1..0000000000000 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=es2015).js +++ /dev/null @@ -1,6 +0,0 @@ -//// [importsNotUsedAsValues_preserve-exact_module.ts] -export {}; - - -//// [importsNotUsedAsValues_preserve-exact_module.js] -export {}; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt b/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt deleted file mode 100644 index b52a3b9677bff..0000000000000 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt similarity index 100% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact.errors.txt rename to tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js similarity index 80% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js rename to tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js index 20050b1c3d625..6e10323c626be 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.js +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts] //// //// [a.ts] export default {}; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols similarity index 100% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact.symbols rename to tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types similarity index 100% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact.types rename to tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt new file mode 100644 index 0000000000000..c90a89d4bda8c --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt @@ -0,0 +1,35 @@ +tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. +tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + + +==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== + export default {}; + export const b = 0; + export const c = 1; + +==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ==== + import a, { b, c } from "./a"; + +==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== + import * as a from "./a"; + +==== tests/cases/conformance/externalModules/typeOnly/d.ts (1 errors) ==== + export = {}; + ~~~~~~~~~~~~ +!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. + +==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== + import D = require("./d"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + import DD = require("./d"); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. + DD; + +==== tests/cases/conformance/externalModules/typeOnly/f.ts (0 errors) ==== + import type a from "./a"; + import { b, c } from "./a"; + b; + \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js new file mode 100644 index 0000000000000..6e10323c626be --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js @@ -0,0 +1,43 @@ +//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts] //// + +//// [a.ts] +export default {}; +export const b = 0; +export const c = 1; + +//// [b.ts] +import a, { b, c } from "./a"; + +//// [c.ts] +import * as a from "./a"; + +//// [d.ts] +export = {}; + +//// [e.ts] +import D = require("./d"); +import DD = require("./d"); +DD; + +//// [f.ts] +import type a from "./a"; +import { b, c } from "./a"; +b; + + +//// [a.js] +export default {}; +export var b = 0; +export var c = 1; +//// [b.js] +import a, { b, c } from "./a"; +//// [c.js] +import * as a from "./a"; +//// [d.js] +export {}; +//// [e.js] +DD; +export {}; +//// [f.js] +import { b, c } from "./a"; +b; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols new file mode 100644 index 0000000000000..c1fe6176415a8 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols @@ -0,0 +1,42 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export default {}; +export const b = 0; +>b : Symbol(b, Decl(a.ts, 1, 12)) + +export const c = 1; +>c : Symbol(c, Decl(a.ts, 2, 12)) + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +import a, { b, c } from "./a"; +>a : Symbol(a, Decl(b.ts, 0, 6)) +>b : Symbol(b, Decl(b.ts, 0, 11)) +>c : Symbol(c, Decl(b.ts, 0, 14)) + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import * as a from "./a"; +>a : Symbol(a, Decl(c.ts, 0, 6)) + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export = {}; +No type information for this code. +No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import D = require("./d"); +>D : Symbol(D, Decl(e.ts, 0, 0)) + +import DD = require("./d"); +>DD : Symbol(DD, Decl(e.ts, 0, 26)) + +DD; +>DD : Symbol(DD, Decl(e.ts, 0, 26)) + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type a from "./a"; +>a : Symbol(a, Decl(f.ts, 0, 6)) + +import { b, c } from "./a"; +>b : Symbol(b, Decl(f.ts, 1, 8)) +>c : Symbol(c, Decl(f.ts, 1, 11)) + +b; +>b : Symbol(b, Decl(f.ts, 1, 8)) + diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types new file mode 100644 index 0000000000000..ed58db4b453d4 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types @@ -0,0 +1,47 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export default {}; +>{} : {} + +export const b = 0; +>b : 0 +>0 : 0 + +export const c = 1; +>c : 1 +>1 : 1 + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +import a, { b, c } from "./a"; +>a : {} +>b : 0 +>c : 1 + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import * as a from "./a"; +>a : typeof a + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export = {}; +>{} : {} + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import D = require("./d"); +>D : {} + +import DD = require("./d"); +>DD : {} + +DD; +>DD : {} + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type a from "./a"; +>a : any + +import { b, c } from "./a"; +>b : 0 +>c : 1 + +b; +>b : 0 + diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js similarity index 89% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js rename to tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js index f564805995fdf..5be35c9ad0182 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.js +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts] //// //// [a.ts] export type A = {}; diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).symbols similarity index 100% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.symbols rename to tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).symbols diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).types similarity index 100% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.types rename to tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).types diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt similarity index 56% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt rename to tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt index 22e14296a4b16..ed97be1020f38 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_errors.errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt @@ -1,13 +1,11 @@ -tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/f.ts(3,10): error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1434: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. +tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1438: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1434: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== @@ -21,17 +19,17 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r ==== tests/cases/conformance/externalModules/typeOnly/c.ts (4 errors) ==== import DefaultA from "./a"; ~~~~~~~~ -!!! error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. import { A } from "./a"; ~ -!!! error TS1434: 'A' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1434: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. import DefaultB from "./b"; ~~~~~~~~ -!!! error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. import { B } from "./b"; ~ -!!! error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== @@ -43,10 +41,10 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r ==== tests/cases/conformance/externalModules/typeOnly/d.ts (2 errors) ==== export { A as AA } from "./a"; ~~~~~~~ -!!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. export { B as BB } from "./b"; ~~~~~~~ -!!! error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1438: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts (0 errors) ==== @@ -56,23 +54,18 @@ tests/cases/conformance/externalModules/typeOnly/f.ts(3,13): error TS1437: 'B' r ==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== import { AA, BB } from "./d"; ~~ -!!! error TS1434: 'AA' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1434: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ~~ -!!! error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'. +!!! error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== import type { AA, BB } from "./d"; -==== tests/cases/conformance/externalModules/typeOnly/f.ts (2 errors) ==== +==== tests/cases/conformance/externalModules/typeOnly/f.ts (0 errors) ==== import type { A } from "./a"; import type { B } from "./b"; export { A, B as BB }; - ~ -!!! error TS1435: 'A' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. - ~~~~~~~ -!!! error TS1437: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'. -!!! related TS1376 tests/cases/conformance/externalModules/typeOnly/f.ts:2:15: 'B' was imported here. ==== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts (0 errors) ==== import type { A } from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js new file mode 100644 index 0000000000000..5be35c9ad0182 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js @@ -0,0 +1,77 @@ +//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts] //// + +//// [a.ts] +export type A = {}; +export type { A as default }; + +//// [b.ts] +class B {}; +export type { B, B as default }; + +//// [c.ts] +import DefaultA from "./a"; +import { A } from "./a"; +import DefaultB from "./b"; +import { B } from "./b"; + +//// [c.fixed.ts] +import type DefaultA from "./a"; +import type { A } from "./a"; +import type DefaultB from "./b"; +import type { B } from "./b"; + +//// [d.ts] +export { A as AA } from "./a"; +export { B as BB } from "./b"; + +//// [d.fixed.ts] +export type { A as AA } from "./a"; +export type { B as BB } from "./b"; + +//// [e.ts] +import { AA, BB } from "./d"; + +//// [e.fixed.ts] +import type { AA, BB } from "./d"; + +//// [f.ts] +import type { A } from "./a"; +import type { B } from "./b"; +export { A, B as BB }; + +//// [f.fixed.ts] +import type { A } from "./a"; +import type { B } from "./b"; +export type { A, B as BB }; + + +//// [a.js] +export {}; +//// [b.js] +var B = /** @class */ (function () { + function B() { + } + return B; +}()); +; +export {}; +//// [c.js] +import DefaultA from "./a"; +import { A } from "./a"; +import DefaultB from "./b"; +import { B } from "./b"; +//// [c.fixed.js] +export {}; +//// [d.js] +export { A as AA } from "./a"; +export { B as BB } from "./b"; +//// [d.fixed.js] +export {}; +//// [e.js] +import { AA, BB } from "./d"; +//// [e.fixed.js] +export {}; +//// [f.js] +export { A, B as BB }; +//// [f.fixed.js] +export {}; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols new file mode 100644 index 0000000000000..0dd64238caf2a --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols @@ -0,0 +1,95 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export type A = {}; +>A : Symbol(A, Decl(a.ts, 0, 0)) + +export type { A as default }; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>default : Symbol(default, Decl(a.ts, 1, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +class B {}; +>B : Symbol(B, Decl(b.ts, 0, 0)) + +export type { B, B as default }; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>B : Symbol(B, Decl(b.ts, 0, 0)) +>default : Symbol(default, Decl(b.ts, 1, 16)) + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import DefaultA from "./a"; +>DefaultA : Symbol(DefaultA, Decl(c.ts, 0, 6)) + +import { A } from "./a"; +>A : Symbol(A, Decl(c.ts, 1, 8)) + +import DefaultB from "./b"; +>DefaultB : Symbol(DefaultB, Decl(c.ts, 2, 6)) + +import { B } from "./b"; +>B : Symbol(B, Decl(c.ts, 3, 8)) + +=== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts === +import type DefaultA from "./a"; +>DefaultA : Symbol(DefaultA, Decl(c.fixed.ts, 0, 6)) + +import type { A } from "./a"; +>A : Symbol(A, Decl(c.fixed.ts, 1, 13)) + +import type DefaultB from "./b"; +>DefaultB : Symbol(DefaultB, Decl(c.fixed.ts, 2, 6)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(c.fixed.ts, 3, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export { A as AA } from "./a"; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>AA : Symbol(AA, Decl(d.ts, 0, 8)) + +export { B as BB } from "./b"; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>BB : Symbol(BB, Decl(d.ts, 1, 8)) + +=== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts === +export type { A as AA } from "./a"; +>A : Symbol(A, Decl(a.ts, 0, 0)) +>AA : Symbol(AA, Decl(d.fixed.ts, 0, 13)) + +export type { B as BB } from "./b"; +>B : Symbol(B, Decl(b.ts, 1, 13)) +>BB : Symbol(BB, Decl(d.fixed.ts, 1, 13)) + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import { AA, BB } from "./d"; +>AA : Symbol(AA, Decl(e.ts, 0, 8)) +>BB : Symbol(BB, Decl(e.ts, 0, 12)) + +=== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts === +import type { AA, BB } from "./d"; +>AA : Symbol(AA, Decl(e.fixed.ts, 0, 13)) +>BB : Symbol(BB, Decl(e.fixed.ts, 0, 17)) + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type { A } from "./a"; +>A : Symbol(A, Decl(f.ts, 0, 13)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(f.ts, 1, 13)) + +export { A, B as BB }; +>A : Symbol(A, Decl(f.ts, 2, 8)) +>B : Symbol(B, Decl(f.ts, 1, 13)) +>BB : Symbol(BB, Decl(f.ts, 2, 11)) + +=== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts === +import type { A } from "./a"; +>A : Symbol(A, Decl(f.fixed.ts, 0, 13)) + +import type { B } from "./b"; +>B : Symbol(B, Decl(f.fixed.ts, 1, 13)) + +export type { A, B as BB }; +>A : Symbol(A, Decl(f.fixed.ts, 2, 13)) +>B : Symbol(B, Decl(f.fixed.ts, 1, 13)) +>BB : Symbol(BB, Decl(f.fixed.ts, 2, 16)) + diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types new file mode 100644 index 0000000000000..1e8a912ed7ced --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types @@ -0,0 +1,95 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export type A = {}; +>A : A + +export type { A as default }; +>A : any +>default : A + +=== tests/cases/conformance/externalModules/typeOnly/b.ts === +class B {}; +>B : B + +export type { B, B as default }; +>B : B +>B : typeof B +>default : B + +=== tests/cases/conformance/externalModules/typeOnly/c.ts === +import DefaultA from "./a"; +>DefaultA : any + +import { A } from "./a"; +>A : any + +import DefaultB from "./b"; +>DefaultB : typeof DefaultB + +import { B } from "./b"; +>B : typeof DefaultB + +=== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts === +import type DefaultA from "./a"; +>DefaultA : DefaultA + +import type { A } from "./a"; +>A : DefaultA + +import type DefaultB from "./b"; +>DefaultB : DefaultB + +import type { B } from "./b"; +>B : DefaultB + +=== tests/cases/conformance/externalModules/typeOnly/d.ts === +export { A as AA } from "./a"; +>A : any +>AA : any + +export { B as BB } from "./b"; +>B : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B +>BB : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B + +=== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts === +export type { A as AA } from "./a"; +>A : any +>AA : import("tests/cases/conformance/externalModules/typeOnly/a").A + +export type { B as BB } from "./b"; +>B : typeof import("tests/cases/conformance/externalModules/typeOnly/b").B +>BB : import("tests/cases/conformance/externalModules/typeOnly/b").B + +=== tests/cases/conformance/externalModules/typeOnly/e.ts === +import { AA, BB } from "./d"; +>AA : any +>BB : typeof BB + +=== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts === +import type { AA, BB } from "./d"; +>AA : AA +>BB : BB + +=== tests/cases/conformance/externalModules/typeOnly/f.ts === +import type { A } from "./a"; +>A : A + +import type { B } from "./b"; +>B : B + +export { A, B as BB }; +>A : any +>B : typeof B +>BB : typeof B + +=== tests/cases/conformance/externalModules/typeOnly/f.fixed.ts === +import type { A } from "./a"; +>A : A + +import type { B } from "./b"; +>B : B + +export type { A, B as BB }; +>A : A +>B : typeof B +>BB : B + diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt new file mode 100644 index 0000000000000..66585372f6425 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js b/tests/baselines/reference/noErasingImportedNames_module(module=amd).js similarity index 51% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js rename to tests/baselines/reference/noErasingImportedNames_module(module=amd).js index e09b7569742cb..515cb2c6e4b48 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).js +++ b/tests/baselines/reference/noErasingImportedNames_module(module=amd).js @@ -1,8 +1,8 @@ -//// [importsNotUsedAsValues_preserve-exact_module.ts] +//// [noErasingImportedNames_module.ts] export {}; -//// [importsNotUsedAsValues_preserve-exact_module.js] +//// [noErasingImportedNames_module.js] define(["require", "exports"], function (require, exports) { "use strict"; exports.__esModule = true; diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt new file mode 100644 index 0000000000000..66585372f6425 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js new file mode 100644 index 0000000000000..bb184e4ed3fab --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js @@ -0,0 +1,7 @@ +//// [noErasingImportedNames_module.ts] +export {}; + + +//// [noErasingImportedNames_module.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js b/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js new file mode 100644 index 0000000000000..b8dc5f6404361 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js @@ -0,0 +1,6 @@ +//// [noErasingImportedNames_module.ts] +export {}; + + +//// [noErasingImportedNames_module.js] +export {}; diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt new file mode 100644 index 0000000000000..66585372f6425 --- /dev/null +++ b/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js b/tests/baselines/reference/noErasingImportedNames_module(module=system).js similarity index 64% rename from tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js rename to tests/baselines/reference/noErasingImportedNames_module(module=system).js index 87284733faf13..ff71b0d2e0029 100644 --- a/tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=system).js +++ b/tests/baselines/reference/noErasingImportedNames_module(module=system).js @@ -1,8 +1,8 @@ -//// [importsNotUsedAsValues_preserve-exact_module.ts] +//// [noErasingImportedNames_module.ts] export {}; -//// [importsNotUsedAsValues_preserve-exact_module.js] +//// [noErasingImportedNames_module.js] System.register([], function (exports_1, context_1) { "use strict"; var __moduleName = context_1 && context_1.id; diff --git a/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json b/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json new file mode 100644 index 0000000000000..af93b63749114 --- /dev/null +++ b/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "noErasingImportedNames": true + } +} diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts similarity index 85% rename from tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts rename to tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts index ceab3ba2dfdfc..e4fe1817cf2b5 100644 --- a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts @@ -1,4 +1,5 @@ -// @importsNotUsedAsValues: preserve-exact +// @noErasingImportedNames: true +// @isolatedModules: true,false // @module: esnext // @Filename: a.ts diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts similarity index 93% rename from tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts rename to tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts index fcfcb70ea9b8f..c76f2a672136a 100644 --- a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_errors.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts @@ -1,4 +1,5 @@ -// @importsNotUsedAsValues: preserve-exact +// @noErasingImportedNames: true +// @isolatedModules: true,false // @module: esnext // @Filename: a.ts diff --git a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts similarity index 64% rename from tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts rename to tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts index 9dde082758e95..3076ab6602763 100644 --- a/tests/cases/conformance/externalModules/typeOnly/importsNotUsedAsValues_preserve-exact_module.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts @@ -1,4 +1,4 @@ -// @importsNotUsedAsValues: preserve-exact +// @noErasingImportedNames: true // @module: amd,system,commonjs,es2015 // @noTypesAndSymbols: true export {}; From 30ffc15f99b3e6ecfe748cda204dd6ef3399e8d8 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 16 Jun 2021 15:41:05 -0500 Subject: [PATCH 08/19] Update option category --- src/compiler/commandLineParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 8ab3ce0098611..e98bf2d26a972 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1173,7 +1173,7 @@ namespace ts { name: "noErasingImportedNames", type: "boolean", affectsEmit: true, - category: Diagnostics.Advanced_Options, + category: Diagnostics.Emit, description: Diagnostics.Disable_the_removal_of_unused_imported_identifiers_from_the_JavaScript_output }, From 0b02873d484c871dd28ac7f8e6272a2221e5c6ce Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 16 Jun 2021 15:48:42 -0500 Subject: [PATCH 09/19] Update baselines --- .../tsConfig/Default initialized TSConfig/tsconfig.json | 1 + .../Initialized TSConfig with advanced options/tsconfig.json | 1 + .../tsconfig.json | 1 + .../tsconfig.json | 1 + .../Initialized TSConfig with files options/tsconfig.json | 1 + .../tsconfig.json | 1 + .../tsconfig.json | 1 + .../tsconfig.json | 1 + .../tsconfig.json | 1 + .../declarationDir-is-specified.js | 1 + .../when-outDir-and-declarationDir-is-specified.js | 1 + .../when-outDir-is-specified.js | 1 + .../with-outFile.js | 1 + ...ut-outDir-or-outFile-is-specified-with-declaration-enabled.js | 1 + .../without-outDir-or-outFile-is-specified.js | 1 + 15 files changed, 15 insertions(+) diff --git a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json index e1c5ef2bcd17d..6f3d44a468223 100644 --- a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json index 4dac607128f16..62266fb7fce10 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "lib", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json index 22b2990e991d9..4005f0ac6b543 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json index a2e520b658ec1..ace7c2c6326a9 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json index 65948b3da0b4c..80c97926f959c 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json index 8d3e008e22ca3..59835bfc6bcef 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json index e1c5ef2bcd17d..6f3d44a468223 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json index 4f4aa82160934..4a86ad8379582 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json index 0ffa10335be50..77187bec34060 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json @@ -64,6 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js index 8f95fdf16180a..c63254433d812 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js index d02d550323450..633a70c29e73b 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js index 85180bd89d9ce..8ebfa02b1500f 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js index 8de7b095812c2..dd5357b7f6ddc 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js index f26ee9895a71e..8a69028fe1554 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js index cf5107b973bfd..21875e0076fd2 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js @@ -85,6 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ From bc850fac77883e2ee7fad555bf2b13ae4b63f2b1 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Aug 2021 10:11:52 -0700 Subject: [PATCH 10/19] Lint --- src/compiler/commandLineParser.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index e98bf2d26a972..3c5b40bd88e9d 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -568,9 +568,9 @@ namespace ts { { name: "importsNotUsedAsValues", type: new Map(getEntries({ - "remove": ImportsNotUsedAsValues.Remove, - "preserve": ImportsNotUsedAsValues.Preserve, - "error": ImportsNotUsedAsValues.Error, + remove: ImportsNotUsedAsValues.Remove, + preserve: ImportsNotUsedAsValues.Preserve, + error: ImportsNotUsedAsValues.Error, })), affectsEmit: true, affectsSemanticDiagnostics: true, From 71f4e71d58a552fb72e3f02d589a36eaeea69dd4 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Aug 2021 15:24:14 -0700 Subject: [PATCH 11/19] Fix up transformer comments --- src/compiler/checker.ts | 3 ++ src/compiler/transformers/ts.ts | 34 +++++++++---------- ...tedNames(isolatedmodules=false).errors.txt | 3 +- ...ingImportedNames(isolatedmodules=false).js | 3 +- ...portedNames(isolatedmodules=false).symbols | 6 +++- ...ImportedNames(isolatedmodules=false).types | 5 ++- ...rtedNames(isolatedmodules=true).errors.txt | 8 +++-- ...singImportedNames(isolatedmodules=true).js | 3 +- ...mportedNames(isolatedmodules=true).symbols | 6 +++- ...gImportedNames(isolatedmodules=true).types | 5 ++- ...rtedNames_errors(isolatedmodules=false).js | 7 ++-- ...ortedNames_errors(isolatedmodules=true).js | 7 ++-- .../typeOnly/noErasingImportedNames.ts | 3 +- 13 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6a83fc752935a..12ca5925e2beb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -40069,6 +40069,9 @@ namespace ts { } function isAliasResolvedToValue(symbol: Symbol): boolean { + if (symbol === unknownSymbol) { + return false; + } const target = resolveAlias(symbol); if (target === unknownSymbol) { return true; diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index ace98771cff89..45db96f73ffae 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2804,7 +2804,7 @@ namespace ts { } /** - * Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' and `noErasingImportedNames` is not set. + * Visits an import declaration, eliding it if it is type-only or if it has an import clause that may be elided. * * @param node The import declaration node. */ @@ -2822,7 +2822,6 @@ namespace ts { // Elide the declaration if the import clause was elided. const importClause = visitNode(node.importClause, visitImportClause, isImportClause); return importClause || - compilerOptions.noErasingImportedNames || importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || importsNotUsedAsValues === ImportsNotUsedAsValues.Error ? factory.updateImportDeclaration( @@ -2835,30 +2834,27 @@ namespace ts { } /** - * Visits an import clause, eliding it if it is not referenced and `noErasingImportedNames` is not set. + * Visits an import clause, eliding it if its `name` and `namedBindings` may both be elided. * * @param node The import clause node. */ function visitImportClause(node: ImportClause): VisitResult { Debug.assert(!node.isTypeOnly); - if (compilerOptions.noErasingImportedNames) { - return node; - } // Elide the import clause if we elide both its name and its named bindings. - const name = resolver.isReferencedAliasDeclaration(node) ? node.name : undefined; + const name = shouldEmitAliasDeclaration(node) ? node.name : undefined; const namedBindings = visitNode(node.namedBindings, visitNamedImportBindings, isNamedImportBindings); return (name || namedBindings) ? factory.updateImportClause(node, /*isTypeOnly*/ false, name, namedBindings) : undefined; } /** - * Visits named import bindings, eliding it if it is not referenced. + * Visits named import bindings, eliding them if their targets, their references, and the compilation settings allow. * * @param node The named import bindings node. */ function visitNamedImportBindings(node: NamedImportBindings): VisitResult { if (node.kind === SyntaxKind.NamespaceImport) { // Elide a namespace import if it is not referenced. - return resolver.isReferencedAliasDeclaration(node) ? node : undefined; + return shouldEmitAliasDeclaration(node) ? node : undefined; } else { // Elide named imports if all of its import specifiers are elided. @@ -2868,13 +2864,12 @@ namespace ts { } /** - * Visits an import specifier, eliding it if it is not referenced. + * Visits an import specifier, eliding it if its target, its references, and the compilation settings allow. * * @param node The import specifier node. */ function visitImportSpecifier(node: ImportSpecifier): VisitResult { - // Elide an import specifier if it is not referenced. - return resolver.isReferencedAliasDeclaration(node) ? node : undefined; + return shouldEmitAliasDeclaration(node) ? node : undefined; } /** @@ -2891,8 +2886,7 @@ namespace ts { } /** - * Visits an export declaration, eliding it if it does not contain a clause that resolves - * to a value and if `noErasingImportedNames` is not set. + * Visits an export declaration, eliding it if it does not contain a clause that resolves to a value. * * @param node The export declaration node. */ @@ -2901,7 +2895,7 @@ namespace ts { return undefined; } - if (!node.exportClause || isNamespaceExport(node.exportClause) || compilerOptions.noErasingImportedNames) { + if (!node.exportClause || isNamespaceExport(node.exportClause)) { // never elide `export from ` declarations - // they should be kept for sideffects/untyped exports, even when the // type checker doesn't know about any exports @@ -2965,7 +2959,7 @@ namespace ts { // preserve old compiler's behavior: emit 'var' for import declaration (even if we do not consider them referenced) when // - current file is not external module // - import declaration is top level and target is value imported by entity name - return resolver.isReferencedAliasDeclaration(node) + return shouldEmitAliasDeclaration(node) || (!isExternalModule(currentSourceFile) && resolver.isTopLevelValueImportEqualsWithEntityName(node)); } @@ -2982,7 +2976,7 @@ namespace ts { } if (isExternalModuleImportEqualsDeclaration(node)) { - const isReferenced = resolver.isReferencedAliasDeclaration(node); + const isReferenced = shouldEmitAliasDeclaration(node); // If the alias is unreferenced but we want to keep the import, replace with 'import "mod"'. if (!isReferenced && importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve) { return setOriginalNode( @@ -3373,5 +3367,11 @@ namespace ts { return isPropertyAccessExpression(node) || isElementAccessExpression(node) ? resolver.getConstantValue(node) : undefined; } + + function shouldEmitAliasDeclaration(node: Node): boolean { + return compilerOptions.noErasingImportedNames + ? resolver.isValueAliasDeclaration(node) + : resolver.isReferencedAliasDeclaration(node); + } } } diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt index c90a89d4bda8c..681847a43c64a 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt @@ -7,9 +7,10 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import export default {}; export const b = 0; export const c = 1; + export interface D {} ==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ==== - import a, { b, c } from "./a"; + import a, { b, c, D } from "./a"; ==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js index 6e10323c626be..3b04ffb76db43 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js @@ -4,9 +4,10 @@ export default {}; export const b = 0; export const c = 1; +export interface D {} //// [b.ts] -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; //// [c.ts] import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols index c1fe6176415a8..d8d8fcc5927e5 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols @@ -6,11 +6,15 @@ export const b = 0; export const c = 1; >c : Symbol(c, Decl(a.ts, 2, 12)) +export interface D {} +>D : Symbol(D, Decl(a.ts, 2, 19)) + === tests/cases/conformance/externalModules/typeOnly/b.ts === -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; >a : Symbol(a, Decl(b.ts, 0, 6)) >b : Symbol(b, Decl(b.ts, 0, 11)) >c : Symbol(c, Decl(b.ts, 0, 14)) +>D : Symbol(D, Decl(b.ts, 0, 17)) === tests/cases/conformance/externalModules/typeOnly/c.ts === import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types index ed58db4b453d4..d36e2a2a0f9f2 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types @@ -10,11 +10,14 @@ export const c = 1; >c : 1 >1 : 1 +export interface D {} + === tests/cases/conformance/externalModules/typeOnly/b.ts === -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; >a : {} >b : 0 >c : 1 +>D : any === tests/cases/conformance/externalModules/typeOnly/c.ts === import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt index c90a89d4bda8c..82d34719c6544 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt @@ -1,3 +1,4 @@ +tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1434: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. @@ -7,9 +8,12 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import export default {}; export const b = 0; export const c = 1; + export interface D {} -==== tests/cases/conformance/externalModules/typeOnly/b.ts (0 errors) ==== - import a, { b, c } from "./a"; +==== tests/cases/conformance/externalModules/typeOnly/b.ts (1 errors) ==== + import a, { b, c, D } from "./a"; + ~ +!!! error TS1434: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js index 6e10323c626be..3b04ffb76db43 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js @@ -4,9 +4,10 @@ export default {}; export const b = 0; export const c = 1; +export interface D {} //// [b.ts] -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; //// [c.ts] import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols index c1fe6176415a8..d8d8fcc5927e5 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols @@ -6,11 +6,15 @@ export const b = 0; export const c = 1; >c : Symbol(c, Decl(a.ts, 2, 12)) +export interface D {} +>D : Symbol(D, Decl(a.ts, 2, 19)) + === tests/cases/conformance/externalModules/typeOnly/b.ts === -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; >a : Symbol(a, Decl(b.ts, 0, 6)) >b : Symbol(b, Decl(b.ts, 0, 11)) >c : Symbol(c, Decl(b.ts, 0, 14)) +>D : Symbol(D, Decl(b.ts, 0, 17)) === tests/cases/conformance/externalModules/typeOnly/c.ts === import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types index ed58db4b453d4..d36e2a2a0f9f2 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types @@ -10,11 +10,14 @@ export const c = 1; >c : 1 >1 : 1 +export interface D {} + === tests/cases/conformance/externalModules/typeOnly/b.ts === -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; >a : {} >b : 0 >c : 1 +>D : any === tests/cases/conformance/externalModules/typeOnly/c.ts === import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js index 5be35c9ad0182..abc11b5fd7cff 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js @@ -56,10 +56,7 @@ var B = /** @class */ (function () { ; export {}; //// [c.js] -import DefaultA from "./a"; -import { A } from "./a"; -import DefaultB from "./b"; -import { B } from "./b"; +export {}; //// [c.fixed.js] export {}; //// [d.js] @@ -68,7 +65,7 @@ export { B as BB } from "./b"; //// [d.fixed.js] export {}; //// [e.js] -import { AA, BB } from "./d"; +export {}; //// [e.fixed.js] export {}; //// [f.js] diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js index 5be35c9ad0182..abc11b5fd7cff 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js @@ -56,10 +56,7 @@ var B = /** @class */ (function () { ; export {}; //// [c.js] -import DefaultA from "./a"; -import { A } from "./a"; -import DefaultB from "./b"; -import { B } from "./b"; +export {}; //// [c.fixed.js] export {}; //// [d.js] @@ -68,7 +65,7 @@ export { B as BB } from "./b"; //// [d.fixed.js] export {}; //// [e.js] -import { AA, BB } from "./d"; +export {}; //// [e.fixed.js] export {}; //// [f.js] diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts index e4fe1817cf2b5..401569ecfd3da 100644 --- a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts @@ -6,9 +6,10 @@ export default {}; export const b = 0; export const c = 1; +export interface D {} // @Filename: b.ts -import a, { b, c } from "./a"; +import a, { b, c, D } from "./a"; // @Filename: c.ts import * as a from "./a"; From b5ed171f0bcd700e9728d7888c2fc3c5895ca2bf Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Aug 2021 16:29:36 -0700 Subject: [PATCH 12/19] Fix errors from merge --- src/compiler/checker.ts | 5 +++- ...rtedNames(isolatedmodules=true).errors.txt | 4 +-- ...rtedNames_errors(isolatedmodules=false).js | 5 ++-- ...es_errors(isolatedmodules=true).errors.txt | 28 +++++++++---------- ...ortedNames_errors(isolatedmodules=true).js | 5 ++-- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f18dff5dfc572..d010e8846eefa 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38607,7 +38607,10 @@ namespace ts { error(node, message, symbolToString(symbol)); } - const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node); + // Type assertion to defeat the type predicate aliasing of `node` here because + // `isTypeOnlyImportOrExportDeclaration` is a one-sided type guard so `!isDeclaredTypeOnly` + // does not actually imply any narrowing effect on `node`. + const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node as never); if (compilerOptions.isolatedModules && !isDeclaredTypeOnly && !(node.flags & NodeFlags.Ambient) diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt index 82d34719c6544..ee162434c50eb 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1434: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1444: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. @@ -13,7 +13,7 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import ==== tests/cases/conformance/externalModules/typeOnly/b.ts (1 errors) ==== import a, { b, c, D } from "./a"; ~ -!!! error TS1434: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js index abc11b5fd7cff..60e8a79b68e9f 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js @@ -60,8 +60,7 @@ export {}; //// [c.fixed.js] export {}; //// [d.js] -export { A as AA } from "./a"; -export { B as BB } from "./b"; +export {}; //// [d.fixed.js] export {}; //// [e.js] @@ -69,6 +68,6 @@ export {}; //// [e.fixed.js] export {}; //// [f.js] -export { A, B as BB }; +export {}; //// [f.fixed.js] export {}; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt index ed97be1020f38..5b835b2377a50 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt @@ -1,11 +1,11 @@ -tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1434: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1444: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. -tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1438: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1434: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1448: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1444: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== @@ -19,17 +19,17 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/c.ts (4 errors) ==== import DefaultA from "./a"; ~~~~~~~~ -!!! error TS1434: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. import { A } from "./a"; ~ -!!! error TS1434: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. import DefaultB from "./b"; ~~~~~~~~ -!!! error TS1436: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. import { B } from "./b"; ~ -!!! error TS1436: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== @@ -44,7 +44,7 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' !!! error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. export { B as BB } from "./b"; ~~~~~~~ -!!! error TS1438: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! error TS1448: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts (0 errors) ==== @@ -54,9 +54,9 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1436: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== import { AA, BB } from "./d"; ~~ -!!! error TS1434: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. ~~ -!!! error TS1436: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js index abc11b5fd7cff..60e8a79b68e9f 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js @@ -60,8 +60,7 @@ export {}; //// [c.fixed.js] export {}; //// [d.js] -export { A as AA } from "./a"; -export { B as BB } from "./b"; +export {}; //// [d.fixed.js] export {}; //// [e.js] @@ -69,6 +68,6 @@ export {}; //// [e.fixed.js] export {}; //// [f.js] -export { A, B as BB }; +export {}; //// [f.fixed.js] export {}; From bc91a258a3d12a3ec9fa52c05549dea0714f90f2 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Aug 2021 17:35:12 -0700 Subject: [PATCH 13/19] Update other error code baseline --- .../reference/isolatedModulesReExportType.errors.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/isolatedModulesReExportType.errors.txt b/tests/baselines/reference/isolatedModulesReExportType.errors.txt index 6fa10e49d3520..21562d0ab648f 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.errors.txt +++ b/tests/baselines/reference/isolatedModulesReExportType.errors.txt @@ -1,6 +1,6 @@ /user.ts(2,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. /user.ts(17,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. -/user.ts(25,10): error TS1438: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +/user.ts(25,10): error TS1448: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. ==== /user.ts (3 errors) ==== @@ -34,7 +34,7 @@ import { C as CC } from "./reExportValueAsTypeOnly"; export { CC }; ~~ -!!! error TS1438: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! error TS1448: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 /reExportValueAsTypeOnly.ts:1:15: 'CC' was exported here. ==== /exportT.ts (0 errors) ==== From f8c686974525a4f7be9d0a5e17f2d63b1058ea0f Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 25 Aug 2021 17:29:15 -0700 Subject: [PATCH 14/19] Rename to "preserveValueImports" --- src/compiler/checker.ts | 6 ++--- src/compiler/commandLineParser.ts | 2 +- src/compiler/diagnosticMessages.json | 6 ++--- src/compiler/program.ts | 4 ++-- src/compiler/transformers/ts.ts | 2 +- src/compiler/types.ts | 2 +- .../reference/api/tsserverlibrary.d.ts | 2 +- tests/baselines/reference/api/typescript.d.ts | 2 +- ...rtedNames(isolatedmodules=true).errors.txt | 4 ++-- ...es_errors(isolatedmodules=true).errors.txt | 24 +++++++++---------- ...mportedNames_module(module=amd).errors.txt | 4 ++-- ...edNames_module(module=commonjs).errors.txt | 4 ++-- ...rtedNames_module(module=system).errors.txt | 4 ++-- .../noErasingImportedNames/tsconfig.json | 5 ---- .../preserveValueImports/tsconfig.json | 5 ++++ .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../declarationDir-is-specified.js | 2 +- ...-outDir-and-declarationDir-is-specified.js | 2 +- .../when-outDir-is-specified.js | 2 +- .../with-outFile.js | 2 +- ...e-is-specified-with-declaration-enabled.js | 2 +- .../without-outDir-or-outFile-is-specified.js | 2 +- .../typeOnly/noErasingImportedNames.ts | 2 +- .../typeOnly/noErasingImportedNames_errors.ts | 2 +- .../typeOnly/noErasingImportedNames_module.ts | 2 +- 33 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json create mode 100644 tests/baselines/reference/showConfig/Shows tsconfig for single option/preserveValueImports/tsconfig.json diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d010e8846eefa..d6b8d29a28847 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38622,10 +38622,10 @@ namespace ts { case SyntaxKind.ImportClause: case SyntaxKind.ImportSpecifier: case SyntaxKind.ImportEqualsDeclaration: { - if (compilerOptions.noErasingImportedNames) { + if (compilerOptions.preserveValueImports) { const message = isType - ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled - : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled; + ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled; const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); addTypeOnlyDeclarationRelatedInfo( error(node, message, name), diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index c8d078b14e6cd..d8ca3dfe14cc2 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1169,7 +1169,7 @@ namespace ts { defaultValueDescription: "false" }, { - name: "noErasingImportedNames", + name: "preserveValueImports", type: "boolean", affectsEmit: true, category: Diagnostics.Emit, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 402a9aeb02a1e..3ca57e6c683c1 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1408,11 +1408,11 @@ "category": "Error", "code": 1443 }, - "'{0}' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": { + "'{0}' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1444 }, - "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": { + "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1446 }, @@ -4054,7 +4054,7 @@ "category": "Error", "code": 5094 }, - "Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later.": { + "Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later.": { "category": "Error", "code": 5095 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index edfe22b41c44c..1c9ff1e8e8446 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -3307,8 +3307,8 @@ namespace ts { } } - if (options.noErasingImportedNames && getEmitModuleKind(options) < ModuleKind.ES2015) { - createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_noErasingImportedNames_may_be_enabled_only_when_module_is_set_to_es2015_or_later); + if (options.preserveValueImports && getEmitModuleKind(options) < ModuleKind.ES2015) { + createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_preserveValueImports_may_be_enabled_only_when_module_is_set_to_es2015_or_later); } // If the emit is enabled make sure that every output file is unique and not overwriting any of the input files diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index b57ae647b604b..c5a94842a3bf5 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -3357,7 +3357,7 @@ namespace ts { } function shouldEmitAliasDeclaration(node: Node): boolean { - return compilerOptions.noErasingImportedNames + return compilerOptions.preserveValueImports ? resolver.isValueAliasDeclaration(node) : resolver.isReferencedAliasDeclaration(node); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 3da06dcda0b8a..074debe7a707c 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6025,7 +6025,6 @@ namespace ts { /*@internal*/noEmitForJsFiles?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; - noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; // Always combine with strict property @@ -6050,6 +6049,7 @@ namespace ts { preserveConstEnums?: boolean; noImplicitOverride?: boolean; preserveSymlinks?: boolean; + preserveValueImports?: boolean; /* @internal */ preserveWatchOutput?: boolean; project?: string; /* @internal */ pretty?: boolean; diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 6c9904ea07340..4c1cb12f86aa3 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2883,7 +2883,6 @@ declare namespace ts { noEmit?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; - noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; @@ -2905,6 +2904,7 @@ declare namespace ts { preserveConstEnums?: boolean; noImplicitOverride?: boolean; preserveSymlinks?: boolean; + preserveValueImports?: boolean; project?: string; reactNamespace?: string; jsxFactory?: string; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 6594a6b4db854..6b6cf85439676 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2883,7 +2883,6 @@ declare namespace ts { noEmit?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; - noErasingImportedNames?: boolean; noErrorTruncation?: boolean; noFallthroughCasesInSwitch?: boolean; noImplicitAny?: boolean; @@ -2905,6 +2904,7 @@ declare namespace ts { preserveConstEnums?: boolean; noImplicitOverride?: boolean; preserveSymlinks?: boolean; + preserveValueImports?: boolean; project?: string; reactNamespace?: string; jsxFactory?: string; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt index ee162434c50eb..b71214af2b4fe 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1444: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1444: 'D' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. @@ -13,7 +13,7 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import ==== tests/cases/conformance/externalModules/typeOnly/b.ts (1 errors) ==== import a, { b, c, D } from "./a"; ~ -!!! error TS1444: 'D' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'D' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt index 5b835b2377a50..4a58bcc6ea241 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt @@ -1,11 +1,11 @@ -tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1444: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1444: 'A' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1448: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1444: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1444: 'AA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== @@ -19,17 +19,17 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/c.ts (4 errors) ==== import DefaultA from "./a"; ~~~~~~~~ -!!! error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. import { A } from "./a"; ~ -!!! error TS1444: 'A' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'A' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. import DefaultB from "./b"; ~~~~~~~~ -!!! error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. import { B } from "./b"; ~ -!!! error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== @@ -54,9 +54,9 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== import { AA, BB } from "./d"; ~~ -!!! error TS1444: 'AA' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1444: 'AA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ~~ -!!! error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled. +!!! error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt index 66585372f6425..872bd2312c715 100644 --- a/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt index 66585372f6425..872bd2312c715 100644 --- a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt index 66585372f6425..872bd2312c715 100644 --- a/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt +++ b/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt @@ -1,7 +1,7 @@ -error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -!!! error TS5095: Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later. +!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. ==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== export {}; \ No newline at end of file diff --git a/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json b/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json deleted file mode 100644 index af93b63749114..0000000000000 --- a/tests/baselines/reference/showConfig/Shows tsconfig for single option/noErasingImportedNames/tsconfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "compilerOptions": { - "noErasingImportedNames": true - } -} diff --git a/tests/baselines/reference/showConfig/Shows tsconfig for single option/preserveValueImports/tsconfig.json b/tests/baselines/reference/showConfig/Shows tsconfig for single option/preserveValueImports/tsconfig.json new file mode 100644 index 0000000000000..2c37ccb9b5c70 --- /dev/null +++ b/tests/baselines/reference/showConfig/Shows tsconfig for single option/preserveValueImports/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "preserveValueImports": true + } +} diff --git a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json index d629d0e524243..24c858e7298fa 100644 --- a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json index e641f4b579869..00af67bae39cb 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "lib", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json index bee439889ce2a..4026611000379 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json index 0d99fcf7933d8..9ada4098b5c5b 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json index 6241fdc391e56..1882db254fc3f 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json index 2faa55e9ecfe5..959bdfc78c997 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json index d629d0e524243..24c858e7298fa 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json index cf4d6eb136c20..044febb86952b 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json index 5d4fbafd0c6aa..abbff9e06e914 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js index 19c4242de695f..f2150a384dc5d 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js index 281547ff55a9e..757077d932d7c 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js index 6877407c1ff1a..57d1083592907 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js index 81a5103145e4e..610afa7d7f7c6 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js index 580713fc0271d..022f9e1ca5f98 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js index c6a883f0bee96..a78ea7dd523f8 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "noErasingImportedNames": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts index 401569ecfd3da..66326cb98bda4 100644 --- a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts @@ -1,4 +1,4 @@ -// @noErasingImportedNames: true +// @preserveValueImports: true // @isolatedModules: true,false // @module: esnext diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts index c76f2a672136a..fc2a3612e7f75 100644 --- a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts @@ -1,4 +1,4 @@ -// @noErasingImportedNames: true +// @preserveValueImports: true // @isolatedModules: true,false // @module: esnext diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts index 3076ab6602763..27f78f707c5db 100644 --- a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts +++ b/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts @@ -1,4 +1,4 @@ -// @noErasingImportedNames: true +// @preserveValueImports: true // @module: amd,system,commonjs,es2015 // @noTypesAndSymbols: true export {}; From 9e2e4dc39d67fcc9674001586c8af3f9ac0921f3 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 7 Sep 2021 17:12:25 -0700 Subject: [PATCH 15/19] Clean up, reword diagnostics --- src/compiler/checker.ts | 88 +++++++++---------- src/compiler/commandLineParser.ts | 2 +- src/compiler/diagnosticMessages.json | 10 +-- src/compiler/program.ts | 2 +- src/compiler/transformers/ts.ts | 7 +- src/compiler/types.ts | 8 ++ src/compiler/utilitiesPublic.ts | 2 +- ...mportedNames_module(module=amd).errors.txt | 7 -- ...edNames_module(module=commonjs).errors.txt | 7 -- ...ngImportedNames_module(module=commonjs).js | 7 -- ...singImportedNames_module(module=es2015).js | 6 -- ...rtedNames_module(module=system).errors.txt | 7 -- ...Imports(isolatedmodules=false).errors.txt} | 0 ...rveValueImports(isolatedmodules=false).js} | 2 +- ...lueImports(isolatedmodules=false).symbols} | 0 ...ValueImports(isolatedmodules=false).types} | 0 ...eImports(isolatedmodules=true).errors.txt} | 4 +- ...erveValueImports(isolatedmodules=true).js} | 2 +- ...alueImports(isolatedmodules=true).symbols} | 0 ...eValueImports(isolatedmodules=true).types} | 0 ...eImports_errors(isolatedmodules=false).js} | 2 +- ...rts_errors(isolatedmodules=false).symbols} | 0 ...ports_errors(isolatedmodules=false).types} | 0 ...s_errors(isolatedmodules=true).errors.txt} | 28 +++--- ...ueImports_errors(isolatedmodules=true).js} | 2 +- ...orts_errors(isolatedmodules=true).symbols} | 0 ...mports_errors(isolatedmodules=true).types} | 0 ...ValueImports_module(module=amd).errors.txt | 7 ++ ...reserveValueImports_module(module=amd).js} | 4 +- ...Imports_module(module=commonjs).errors.txt | 7 ++ ...rveValueImports_module(module=commonjs).js | 7 ++ ...serveValueImports_module(module=es2015).js | 6 ++ ...ueImports_module(module=system).errors.txt | 7 ++ ...erveValueImports_module(module=system).js} | 4 +- ...portedNames.ts => preserveValueImports.ts} | 0 ...rors.ts => preserveValueImports_errors.ts} | 0 ...dule.ts => preserveValueImports_module.ts} | 0 37 files changed, 119 insertions(+), 116 deletions(-) delete mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt delete mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt delete mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js delete mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=es2015).js delete mode 100644 tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=false).errors.txt => preserveValueImports(isolatedmodules=false).errors.txt} (100%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=false).js => preserveValueImports(isolatedmodules=false).js} (83%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=false).symbols => preserveValueImports(isolatedmodules=false).symbols} (100%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=false).types => preserveValueImports(isolatedmodules=false).types} (100%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=true).errors.txt => preserveValueImports(isolatedmodules=true).errors.txt} (87%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=true).js => preserveValueImports(isolatedmodules=true).js} (83%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=true).symbols => preserveValueImports(isolatedmodules=true).symbols} (100%) rename tests/baselines/reference/{noErasingImportedNames(isolatedmodules=true).types => preserveValueImports(isolatedmodules=true).types} (100%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=true).js => preserveValueImports_errors(isolatedmodules=false).js} (89%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=false).symbols => preserveValueImports_errors(isolatedmodules=false).symbols} (100%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=false).types => preserveValueImports_errors(isolatedmodules=false).types} (100%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=true).errors.txt => preserveValueImports_errors(isolatedmodules=true).errors.txt} (73%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=false).js => preserveValueImports_errors(isolatedmodules=true).js} (89%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=true).symbols => preserveValueImports_errors(isolatedmodules=true).symbols} (100%) rename tests/baselines/reference/{noErasingImportedNames_errors(isolatedmodules=true).types => preserveValueImports_errors(isolatedmodules=true).types} (100%) create mode 100644 tests/baselines/reference/preserveValueImports_module(module=amd).errors.txt rename tests/baselines/reference/{noErasingImportedNames_module(module=amd).js => preserveValueImports_module(module=amd).js} (59%) create mode 100644 tests/baselines/reference/preserveValueImports_module(module=commonjs).errors.txt create mode 100644 tests/baselines/reference/preserveValueImports_module(module=commonjs).js create mode 100644 tests/baselines/reference/preserveValueImports_module(module=es2015).js create mode 100644 tests/baselines/reference/preserveValueImports_module(module=system).errors.txt rename tests/baselines/reference/{noErasingImportedNames_module(module=system).js => preserveValueImports_module(module=system).js} (70%) rename tests/cases/conformance/externalModules/typeOnly/{noErasingImportedNames.ts => preserveValueImports.ts} (100%) rename tests/cases/conformance/externalModules/typeOnly/{noErasingImportedNames_errors.ts => preserveValueImports_errors.ts} (100%) rename tests/cases/conformance/externalModules/typeOnly/{noErasingImportedNames_module.ts => preserveValueImports_module.ts} (100%) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d6b8d29a28847..5bcb6d479a904 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38607,49 +38607,45 @@ namespace ts { error(node, message, symbolToString(symbol)); } - // Type assertion to defeat the type predicate aliasing of `node` here because - // `isTypeOnlyImportOrExportDeclaration` is a one-sided type guard so `!isDeclaredTypeOnly` - // does not actually imply any narrowing effect on `node`. - const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node as never); if (compilerOptions.isolatedModules - && !isDeclaredTypeOnly - && !(node.flags & NodeFlags.Ambient) - && (!(target.flags & SymbolFlags.Value) || getTypeOnlyAliasDeclaration(symbol))) { - const isType = !(target.flags & SymbolFlags.Value); + && !isTypeOnlyImportOrExportDeclaration(node) + && !(node.flags & NodeFlags.Ambient)) { const typeOnlyAlias = getTypeOnlyAliasDeclaration(symbol); - - switch (node.kind) { - case SyntaxKind.ImportClause: - case SyntaxKind.ImportSpecifier: - case SyntaxKind.ImportEqualsDeclaration: { - if (compilerOptions.preserveValueImports) { - const message = isType - ? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled - : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled; - const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); - addTypeOnlyDeclarationRelatedInfo( - error(node, message, name), - isType ? undefined : typeOnlyAlias, - name - ); + const isType = !(target.flags & SymbolFlags.Value); + if (isType || typeOnlyAlias) { + switch (node.kind) { + case SyntaxKind.ImportClause: + case SyntaxKind.ImportSpecifier: + case SyntaxKind.ImportEqualsDeclaration: { + if (compilerOptions.preserveValueImports) { + const message = isType + ? Diagnostics._0_is_a_type_and_must_be_imported_using_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_using_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled; + const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); + addTypeOnlyDeclarationRelatedInfo( + error(node, message, name), + isType ? undefined : typeOnlyAlias, + name + ); + } + break; } - break; - } - case SyntaxKind.ExportSpecifier: { - // Don't allow re-exporting an export that will be elided when `--isolatedModules` is set. - // The exception is that `import type { A } from './a'; export { A }` is allowed - // because single-file analysis can determine that the export should be dropped. - if (getSourceFileOfNode(typeOnlyAlias) !== getSourceFileOfNode(node)) { - const message = isType - ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type - : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled; - const name = idText(node.propertyName || node.name); - addTypeOnlyDeclarationRelatedInfo( - error(node, message, name), - isType ? undefined : typeOnlyAlias, - name - ); - return; + case SyntaxKind.ExportSpecifier: { + // Don't allow re-exporting an export that will be elided when `--isolatedModules` is set. + // The exception is that `import type { A } from './a'; export { A }` is allowed + // because single-file analysis can determine that the export should be dropped. + if (getSourceFileOfNode(typeOnlyAlias) !== getSourceFileOfNode(node)) { + const message = isType + ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type + : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_using_a_type_only_re_export_when_isolatedModules_is_enabled; + const name = idText(node.propertyName || node.name); + addTypeOnlyDeclarationRelatedInfo( + error(node, message, name), + isType ? undefined : typeOnlyAlias, + name + ); + return; + } } } } @@ -40433,13 +40429,13 @@ namespace ts { function isValueAliasDeclaration(node: Node): boolean { switch (node.kind) { case SyntaxKind.ImportEqualsDeclaration: - return isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol); + return isAliasResolvedToValue(getSymbolOfNode(node)); case SyntaxKind.ImportClause: case SyntaxKind.NamespaceImport: case SyntaxKind.ImportSpecifier: case SyntaxKind.ExportSpecifier: - const symbol = getSymbolOfNode(node) || unknownSymbol; - return isAliasResolvedToValue(symbol) && !getTypeOnlyAliasDeclaration(symbol); + const symbol = getSymbolOfNode(node); + return !!symbol && isAliasResolvedToValue(symbol) && !getTypeOnlyAliasDeclaration(symbol); case SyntaxKind.ExportDeclaration: const exportClause = (node as ExportDeclaration).exportClause; return !!exportClause && ( @@ -40448,7 +40444,7 @@ namespace ts { ); case SyntaxKind.ExportAssignment: return (node as ExportAssignment).expression && (node as ExportAssignment).expression.kind === SyntaxKind.Identifier ? - isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol) : + isAliasResolvedToValue(getSymbolOfNode(node)) : true; } return false; @@ -40465,8 +40461,8 @@ namespace ts { return isValue && node.moduleReference && !nodeIsMissing(node.moduleReference); } - function isAliasResolvedToValue(symbol: Symbol): boolean { - if (symbol === unknownSymbol) { + function isAliasResolvedToValue(symbol: Symbol | undefined): boolean { + if (!symbol) { return false; } const target = resolveAlias(symbol); diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d8ca3dfe14cc2..22e784391a1c7 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1173,7 +1173,7 @@ namespace ts { type: "boolean", affectsEmit: true, category: Diagnostics.Emit, - description: Diagnostics.Disable_the_removal_of_unused_imported_identifiers_from_the_JavaScript_output + description: Diagnostics.Preserve_unused_imported_values_in_the_JavaScript_output_that_would_otherwise_be_removed, }, { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3ca57e6c683c1..a6bb05d949ea3 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1408,19 +1408,19 @@ "category": "Error", "code": 1443 }, - "'{0}' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { + "'{0}' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1444 }, - "'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { + "'{0}' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled.": { "category": "Error", "code": 1446 }, - "'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": { + "'{0}' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled.": { "category": "Error", "code": 1448 }, - "Disable the removal of unused imported identifiers from the JavaScript output.": { + "Preserve unused imported values in the JavaScript output that would otherwise be removed.": { "category": "Message", "code": 1449 }, @@ -4054,7 +4054,7 @@ "category": "Error", "code": 5094 }, - "Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later.": { + "Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later.": { "category": "Error", "code": 5095 }, diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 1c9ff1e8e8446..d20ac90d49e68 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -3308,7 +3308,7 @@ namespace ts { } if (options.preserveValueImports && getEmitModuleKind(options) < ModuleKind.ES2015) { - createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_preserveValueImports_may_be_enabled_only_when_module_is_set_to_es2015_or_later); + createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_preserveValueImports_can_only_be_used_when_module_is_set_to_es2015_or_later); } // If the emit is enabled make sure that every output file is unique and not overwriting any of the input files diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index c5a94842a3bf5..012be39dadbdd 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -46,7 +46,6 @@ namespace ts { const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks"); const languageVersion = getEmitScriptTarget(compilerOptions); const moduleKind = getEmitModuleKind(compilerOptions); - const { importsNotUsedAsValues } = compilerOptions; // Save the previous transformation hooks. const previousOnEmitNode = context.onEmitNode; @@ -2810,8 +2809,8 @@ namespace ts { // Elide the declaration if the import clause was elided. const importClause = visitNode(node.importClause, visitImportClause, isImportClause); return importClause || - importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || - importsNotUsedAsValues === ImportsNotUsedAsValues.Error + compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve || + compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Error ? factory.updateImportDeclaration( node, /*decorators*/ undefined, @@ -2966,7 +2965,7 @@ namespace ts { if (isExternalModuleImportEqualsDeclaration(node)) { const isReferenced = shouldEmitAliasDeclaration(node); // If the alias is unreferenced but we want to keep the import, replace with 'import "mod"'. - if (!isReferenced && importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve) { + if (!isReferenced && compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve) { return setOriginalNode( setTextRange( factory.createImportDeclaration( diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 074debe7a707c..544fc01580fa3 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3104,6 +3104,14 @@ namespace ts { | ImportOrExportSpecifier ; + export type TypeOnlyAliasDeclaration = + | ImportClause & { readonly isTypeOnly: true, readonly name: Identifier } + | ImportEqualsDeclaration & { readonly isTypeOnly: true } + | NamespaceImport & { readonly parent: ImportClause & { readonly isTypeOnly: true } } + | ImportSpecifier & { readonly parent: NamedImports & { readonly parent: ImportClause & { readonly isTypeOnly: true } } } + | ExportSpecifier & { readonly parent: NamedExports & { readonly parent: ExportDeclaration & { readonly isTypeOnly: true } } } + ; + /** * This is either an `export =` or an `export default` declaration. * Unless `isExportEquals` is set, this node was parsed as an `export default`. diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index b9236521cba67..91b0a1cc9a9cc 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -1126,7 +1126,7 @@ namespace ts { return isImportSpecifier(node) || isExportSpecifier(node); } - export function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyCompatibleAliasDeclaration { + export function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyAliasDeclaration { switch (node.kind) { case SyntaxKind.ImportSpecifier: case SyntaxKind.ExportSpecifier: diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt deleted file mode 100644 index 872bd2312c715..0000000000000 --- a/tests/baselines/reference/noErasingImportedNames_module(module=amd).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt deleted file mode 100644 index 872bd2312c715..0000000000000 --- a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js b/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js deleted file mode 100644 index bb184e4ed3fab..0000000000000 --- a/tests/baselines/reference/noErasingImportedNames_module(module=commonjs).js +++ /dev/null @@ -1,7 +0,0 @@ -//// [noErasingImportedNames_module.ts] -export {}; - - -//// [noErasingImportedNames_module.js] -"use strict"; -exports.__esModule = true; diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js b/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js deleted file mode 100644 index b8dc5f6404361..0000000000000 --- a/tests/baselines/reference/noErasingImportedNames_module(module=es2015).js +++ /dev/null @@ -1,6 +0,0 @@ -//// [noErasingImportedNames_module.ts] -export {}; - - -//// [noErasingImportedNames_module.js] -export {}; diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt b/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt deleted file mode 100644 index 872bd2312c715..0000000000000 --- a/tests/baselines/reference/noErasingImportedNames_module(module=system).errors.txt +++ /dev/null @@ -1,7 +0,0 @@ -error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. - - -!!! error TS5095: Option 'preserveValueImports' may be enabled only when 'module' is set to 'es2015' or later. -==== tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts (0 errors) ==== - export {}; - \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt b/tests/baselines/reference/preserveValueImports(isolatedmodules=false).errors.txt similarity index 100% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).errors.txt rename to tests/baselines/reference/preserveValueImports(isolatedmodules=false).errors.txt diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js b/tests/baselines/reference/preserveValueImports(isolatedmodules=false).js similarity index 83% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js rename to tests/baselines/reference/preserveValueImports(isolatedmodules=false).js index 3b04ffb76db43..9b26f77b33145 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).js +++ b/tests/baselines/reference/preserveValueImports(isolatedmodules=false).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/preserveValueImports.ts] //// //// [a.ts] export default {}; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols b/tests/baselines/reference/preserveValueImports(isolatedmodules=false).symbols similarity index 100% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).symbols rename to tests/baselines/reference/preserveValueImports(isolatedmodules=false).symbols diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types b/tests/baselines/reference/preserveValueImports(isolatedmodules=false).types similarity index 100% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=false).types rename to tests/baselines/reference/preserveValueImports(isolatedmodules=false).types diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).errors.txt similarity index 87% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt rename to tests/baselines/reference/preserveValueImports(isolatedmodules=true).errors.txt index b71214af2b4fe..aac66e08f6458 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1444: 'D' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/b.ts(1,19): error TS1444: 'D' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(1,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead. @@ -13,7 +13,7 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(2,1): error TS1202: Import ==== tests/cases/conformance/externalModules/typeOnly/b.ts (1 errors) ==== import a, { b, c, D } from "./a"; ~ -!!! error TS1444: 'D' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1444: 'D' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/c.ts (0 errors) ==== import * as a from "./a"; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).js similarity index 83% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js rename to tests/baselines/reference/preserveValueImports(isolatedmodules=true).js index 3b04ffb76db43..9b26f77b33145 100644 --- a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).js +++ b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/preserveValueImports.ts] //// //// [a.ts] export default {}; diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).symbols similarity index 100% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).symbols rename to tests/baselines/reference/preserveValueImports(isolatedmodules=true).symbols diff --git a/tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types b/tests/baselines/reference/preserveValueImports(isolatedmodules=true).types similarity index 100% rename from tests/baselines/reference/noErasingImportedNames(isolatedmodules=true).types rename to tests/baselines/reference/preserveValueImports(isolatedmodules=true).types diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).js similarity index 89% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).js index 60e8a79b68e9f..36507e663afc9 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).js +++ b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/preserveValueImports_errors.ts] //// //// [a.ts] export type A = {}; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).symbols b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).symbols similarity index 100% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).symbols rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).symbols diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).types b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).types similarity index 100% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).types rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=false).types diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).errors.txt similarity index 73% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).errors.txt index 4a58bcc6ea241..9823c1d1ae459 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).errors.txt +++ b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).errors.txt @@ -1,11 +1,11 @@ -tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1444: 'A' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(1,8): error TS1444: 'DefaultA' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(2,10): error TS1444: 'A' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(3,8): error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/c.ts(4,10): error TS1446: 'B' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. tests/cases/conformance/externalModules/typeOnly/d.ts(1,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. -tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1448: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1444: 'AA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. -tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/d.ts(2,10): error TS1448: 'B' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,10): error TS1444: 'AA' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== @@ -19,17 +19,17 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/c.ts (4 errors) ==== import DefaultA from "./a"; ~~~~~~~~ -!!! error TS1444: 'DefaultA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1444: 'DefaultA' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. import { A } from "./a"; ~ -!!! error TS1444: 'A' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1444: 'A' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. import DefaultB from "./b"; ~~~~~~~~ -!!! error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1446: 'DefaultB' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:18: 'DefaultB' was exported here. import { B } from "./b"; ~ -!!! error TS1446: 'B' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1446: 'B' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/c.fixed.ts (0 errors) ==== @@ -44,7 +44,7 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' !!! error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. export { B as BB } from "./b"; ~~~~~~~ -!!! error TS1448: 'B' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! error TS1448: 'B' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'B' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/d.fixed.ts (0 errors) ==== @@ -54,9 +54,9 @@ tests/cases/conformance/externalModules/typeOnly/e.ts(1,14): error TS1446: 'BB' ==== tests/cases/conformance/externalModules/typeOnly/e.ts (2 errors) ==== import { AA, BB } from "./d"; ~~ -!!! error TS1444: 'AA' is a type and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1444: 'AA' is a type and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. ~~ -!!! error TS1446: 'BB' resolves to a type-only declaration and must be imported with a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. +!!! error TS1446: 'BB' resolves to a type-only declaration and must be imported using a type-only import when 'preserveValueImports' and 'isolatedModules' are both enabled. !!! related TS1377 tests/cases/conformance/externalModules/typeOnly/b.ts:2:15: 'BB' was exported here. ==== tests/cases/conformance/externalModules/typeOnly/e.fixed.ts (0 errors) ==== diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).js similarity index 89% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).js index 60e8a79b68e9f..36507e663afc9 100644 --- a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=false).js +++ b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).js @@ -1,4 +1,4 @@ -//// [tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts] //// +//// [tests/cases/conformance/externalModules/typeOnly/preserveValueImports_errors.ts] //// //// [a.ts] export type A = {}; diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).symbols similarity index 100% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).symbols rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).symbols diff --git a/tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types b/tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).types similarity index 100% rename from tests/baselines/reference/noErasingImportedNames_errors(isolatedmodules=true).types rename to tests/baselines/reference/preserveValueImports_errors(isolatedmodules=true).types diff --git a/tests/baselines/reference/preserveValueImports_module(module=amd).errors.txt b/tests/baselines/reference/preserveValueImports_module(module=amd).errors.txt new file mode 100644 index 0000000000000..204342959ca1b --- /dev/null +++ b/tests/baselines/reference/preserveValueImports_module(module=amd).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/preserveValueImports_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=amd).js b/tests/baselines/reference/preserveValueImports_module(module=amd).js similarity index 59% rename from tests/baselines/reference/noErasingImportedNames_module(module=amd).js rename to tests/baselines/reference/preserveValueImports_module(module=amd).js index 515cb2c6e4b48..f51632425da99 100644 --- a/tests/baselines/reference/noErasingImportedNames_module(module=amd).js +++ b/tests/baselines/reference/preserveValueImports_module(module=amd).js @@ -1,8 +1,8 @@ -//// [noErasingImportedNames_module.ts] +//// [preserveValueImports_module.ts] export {}; -//// [noErasingImportedNames_module.js] +//// [preserveValueImports_module.js] define(["require", "exports"], function (require, exports) { "use strict"; exports.__esModule = true; diff --git a/tests/baselines/reference/preserveValueImports_module(module=commonjs).errors.txt b/tests/baselines/reference/preserveValueImports_module(module=commonjs).errors.txt new file mode 100644 index 0000000000000..204342959ca1b --- /dev/null +++ b/tests/baselines/reference/preserveValueImports_module(module=commonjs).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/preserveValueImports_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/preserveValueImports_module(module=commonjs).js b/tests/baselines/reference/preserveValueImports_module(module=commonjs).js new file mode 100644 index 0000000000000..790566516ac13 --- /dev/null +++ b/tests/baselines/reference/preserveValueImports_module(module=commonjs).js @@ -0,0 +1,7 @@ +//// [preserveValueImports_module.ts] +export {}; + + +//// [preserveValueImports_module.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/preserveValueImports_module(module=es2015).js b/tests/baselines/reference/preserveValueImports_module(module=es2015).js new file mode 100644 index 0000000000000..c01c03cf616b8 --- /dev/null +++ b/tests/baselines/reference/preserveValueImports_module(module=es2015).js @@ -0,0 +1,6 @@ +//// [preserveValueImports_module.ts] +export {}; + + +//// [preserveValueImports_module.js] +export {}; diff --git a/tests/baselines/reference/preserveValueImports_module(module=system).errors.txt b/tests/baselines/reference/preserveValueImports_module(module=system).errors.txt new file mode 100644 index 0000000000000..204342959ca1b --- /dev/null +++ b/tests/baselines/reference/preserveValueImports_module(module=system).errors.txt @@ -0,0 +1,7 @@ +error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. + + +!!! error TS5095: Option 'preserveValueImports' can only be used when 'module' is set to 'es2015' or later. +==== tests/cases/conformance/externalModules/typeOnly/preserveValueImports_module.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/noErasingImportedNames_module(module=system).js b/tests/baselines/reference/preserveValueImports_module(module=system).js similarity index 70% rename from tests/baselines/reference/noErasingImportedNames_module(module=system).js rename to tests/baselines/reference/preserveValueImports_module(module=system).js index ff71b0d2e0029..f8d108572f495 100644 --- a/tests/baselines/reference/noErasingImportedNames_module(module=system).js +++ b/tests/baselines/reference/preserveValueImports_module(module=system).js @@ -1,8 +1,8 @@ -//// [noErasingImportedNames_module.ts] +//// [preserveValueImports_module.ts] export {}; -//// [noErasingImportedNames_module.js] +//// [preserveValueImports_module.js] System.register([], function (exports_1, context_1) { "use strict"; var __moduleName = context_1 && context_1.id; diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts b/tests/cases/conformance/externalModules/typeOnly/preserveValueImports.ts similarity index 100% rename from tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames.ts rename to tests/cases/conformance/externalModules/typeOnly/preserveValueImports.ts diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts b/tests/cases/conformance/externalModules/typeOnly/preserveValueImports_errors.ts similarity index 100% rename from tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_errors.ts rename to tests/cases/conformance/externalModules/typeOnly/preserveValueImports_errors.ts diff --git a/tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts b/tests/cases/conformance/externalModules/typeOnly/preserveValueImports_module.ts similarity index 100% rename from tests/cases/conformance/externalModules/typeOnly/noErasingImportedNames_module.ts rename to tests/cases/conformance/externalModules/typeOnly/preserveValueImports_module.ts From bff0093b470ac0b9166c84c1c1ffe177e7d65925 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 7 Sep 2021 17:40:26 -0700 Subject: [PATCH 16/19] Update API baselines --- .../reference/api/tsserverlibrary.d.ts | 24 ++++++++++++++++++- tests/baselines/reference/api/typescript.d.ts | 24 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 4c1cb12f86aa3..3516993b2d5ae 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -1688,6 +1688,28 @@ declare namespace ts { } export type ImportOrExportSpecifier = ImportSpecifier | ExportSpecifier; export type TypeOnlyCompatibleAliasDeclaration = ImportClause | ImportEqualsDeclaration | NamespaceImport | ImportOrExportSpecifier; + export type TypeOnlyAliasDeclaration = ImportClause & { + readonly isTypeOnly: true; + readonly name: Identifier; + } | ImportEqualsDeclaration & { + readonly isTypeOnly: true; + } | NamespaceImport & { + readonly parent: ImportClause & { + readonly isTypeOnly: true; + }; + } | ImportSpecifier & { + readonly parent: NamedImports & { + readonly parent: ImportClause & { + readonly isTypeOnly: true; + }; + }; + } | ExportSpecifier & { + readonly parent: NamedExports & { + readonly parent: ExportDeclaration & { + readonly isTypeOnly: true; + }; + }; + }; /** * This is either an `export =` or an `export default` declaration. * Unless `isExportEquals` is set, this node was parsed as an `export default`. @@ -4320,7 +4342,7 @@ declare namespace ts { function isTemplateLiteralToken(node: Node): node is TemplateLiteralToken; function isTemplateMiddleOrTemplateTail(node: Node): node is TemplateMiddle | TemplateTail; function isImportOrExportSpecifier(node: Node): node is ImportSpecifier | ExportSpecifier; - function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyCompatibleAliasDeclaration; + function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyAliasDeclaration; function isStringTextContainingNode(node: Node): node is StringLiteral | TemplateLiteralToken; function isModifier(node: Node): node is Modifier; function isEntityName(node: Node): node is EntityName; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 6b6cf85439676..76913e5b2a49b 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -1688,6 +1688,28 @@ declare namespace ts { } export type ImportOrExportSpecifier = ImportSpecifier | ExportSpecifier; export type TypeOnlyCompatibleAliasDeclaration = ImportClause | ImportEqualsDeclaration | NamespaceImport | ImportOrExportSpecifier; + export type TypeOnlyAliasDeclaration = ImportClause & { + readonly isTypeOnly: true; + readonly name: Identifier; + } | ImportEqualsDeclaration & { + readonly isTypeOnly: true; + } | NamespaceImport & { + readonly parent: ImportClause & { + readonly isTypeOnly: true; + }; + } | ImportSpecifier & { + readonly parent: NamedImports & { + readonly parent: ImportClause & { + readonly isTypeOnly: true; + }; + }; + } | ExportSpecifier & { + readonly parent: NamedExports & { + readonly parent: ExportDeclaration & { + readonly isTypeOnly: true; + }; + }; + }; /** * This is either an `export =` or an `export default` declaration. * Unless `isExportEquals` is set, this node was parsed as an `export default`. @@ -4320,7 +4342,7 @@ declare namespace ts { function isTemplateLiteralToken(node: Node): node is TemplateLiteralToken; function isTemplateMiddleOrTemplateTail(node: Node): node is TemplateMiddle | TemplateTail; function isImportOrExportSpecifier(node: Node): node is ImportSpecifier | ExportSpecifier; - function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyCompatibleAliasDeclaration; + function isTypeOnlyImportOrExportDeclaration(node: Node): node is TypeOnlyAliasDeclaration; function isStringTextContainingNode(node: Node): node is StringLiteral | TemplateLiteralToken; function isModifier(node: Node): node is Modifier; function isEntityName(node: Node): node is EntityName; From dd5ab20186725e383466cb006d2b53f76d67badd Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 7 Sep 2021 17:42:16 -0700 Subject: [PATCH 17/19] Update other baseline affected by error message reword --- .../reference/isolatedModulesReExportType.errors.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baselines/reference/isolatedModulesReExportType.errors.txt b/tests/baselines/reference/isolatedModulesReExportType.errors.txt index 21562d0ab648f..3d89ddf487a53 100644 --- a/tests/baselines/reference/isolatedModulesReExportType.errors.txt +++ b/tests/baselines/reference/isolatedModulesReExportType.errors.txt @@ -1,6 +1,6 @@ /user.ts(2,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. /user.ts(17,10): error TS1205: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'. -/user.ts(25,10): error TS1448: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +/user.ts(25,10): error TS1448: 'CC' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled. ==== /user.ts (3 errors) ==== @@ -34,7 +34,7 @@ import { C as CC } from "./reExportValueAsTypeOnly"; export { CC }; ~~ -!!! error TS1448: 'CC' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled. +!!! error TS1448: 'CC' resolves to a type-only declaration and must be re-exported using a type-only re-export when 'isolatedModules' is enabled. !!! related TS1377 /reExportValueAsTypeOnly.ts:1:15: 'CC' was exported here. ==== /exportT.ts (0 errors) ==== From dc9e02d7d4dcf201f254c1eea4ba4f27107b87c5 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 7 Sep 2021 17:43:43 -0700 Subject: [PATCH 18/19] Update tsconfig baselines --- .../tsConfig/Default initialized TSConfig/tsconfig.json | 2 +- .../Initialized TSConfig with advanced options/tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../Initialized TSConfig with files options/tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../tsconfig.json | 2 +- .../declarationDir-is-specified.js | 2 +- .../when-outDir-and-declarationDir-is-specified.js | 2 +- .../when-outDir-is-specified.js | 2 +- .../with-outFile.js | 2 +- ...t-outDir-or-outFile-is-specified-with-declaration-enabled.js | 2 +- .../without-outDir-or-outFile-is-specified.js | 2 +- 15 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json index 24c858e7298fa..96b9d1be78cd4 100644 --- a/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json index 00af67bae39cb..9c26f5a029727 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "lib", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json index 4026611000379..a75d5adbedb87 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json index 9ada4098b5c5b..9f8f66fa059c2 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json index 1882db254fc3f..8d75086de68ea 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json index 959bdfc78c997..f66c53cd98f16 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json index 24c858e7298fa..96b9d1be78cd4 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json index 044febb86952b..85c5394078dc6 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json index abbff9e06e914..763d33c30bb04 100644 --- a/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json +++ b/tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json @@ -64,7 +64,7 @@ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js index f2150a384dc5d..ed84c9e43922b 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/declarationDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js index 757077d932d7c..109062bb84503 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-and-declarationDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ "declarationDir": "decls", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js index 57d1083592907..4255c9ffedd7f 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/when-outDir-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js index 610afa7d7f7c6..2e6117272664a 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/with-outFile.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js index 022f9e1ca5f98..28924e1e61c1d 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified-with-declaration-enabled.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ diff --git a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js index a78ea7dd523f8..20fd0c695b90d 100644 --- a/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js +++ b/tests/baselines/reference/tscWatch/programUpdates/should-not-trigger-recompilation-because-of-program-emit/without-outDir-or-outFile-is-specified.js @@ -85,7 +85,7 @@ interface Array { length: number; [n: number]: T; } // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ - // "preserveValueImports": true, /* Disable the removal of unused imported identifiers from the JavaScript output. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ From c27d59a0e66624a06b7f9fd7fc032e2b992d894e Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 8 Sep 2021 15:49:11 -0700 Subject: [PATCH 19/19] Add debug assertion instead of ! --- src/compiler/checker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5bcb6d479a904..a1c2edb860a46 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -38618,10 +38618,11 @@ namespace ts { case SyntaxKind.ImportSpecifier: case SyntaxKind.ImportEqualsDeclaration: { if (compilerOptions.preserveValueImports) { + Debug.assertIsDefined(node.name, "An ImportClause with a symbol should have a name"); const message = isType ? Diagnostics._0_is_a_type_and_must_be_imported_using_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled : Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_using_a_type_only_import_when_preserveValueImports_and_isolatedModules_are_both_enabled; - const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!); + const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name); addTypeOnlyDeclarationRelatedInfo( error(node, message, name), isType ? undefined : typeOnlyAlias,