Skip to content

Commit 3063638

Browse files
committed
Redo as noEraslingImportedNames
1 parent 126069d commit 3063638

File tree

39 files changed

+545
-117
lines changed

39 files changed

+545
-117
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38081,7 +38081,7 @@ namespace ts {
3808138081
}
3808238082

3808338083
const isDeclaredTypeOnly = isTypeOnlyImportOrExportDeclaration(node);
38084-
if ((compilerOptions.isolatedModules || compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact)
38084+
if (compilerOptions.isolatedModules
3808538085
&& !isDeclaredTypeOnly
3808638086
&& !(node.flags & NodeFlags.Ambient)
3808738087
&& (!(target.flags & SymbolFlags.Value) || getTypeOnlyAliasDeclaration(symbol))) {
@@ -38092,10 +38092,10 @@ namespace ts {
3809238092
case SyntaxKind.ImportClause:
3809338093
case SyntaxKind.ImportSpecifier:
3809438094
case SyntaxKind.ImportEqualsDeclaration: {
38095-
if (compilerOptions.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) {
38095+
if (compilerOptions.noErasingImportedNames) {
3809638096
const message = isType
38097-
? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact
38098-
: Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_importsNotUsedAsValues_is_set_to_preserve_exact;
38097+
? Diagnostics._0_is_a_type_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled
38098+
: Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_with_a_type_only_import_when_noErasingImportedNames_and_isolatedModules_are_both_enabled;
3809938099
const name = idText(node.kind === SyntaxKind.ImportSpecifier ? node.propertyName || node.name : node.name!);
3810038100
addTypeOnlyDeclarationRelatedInfo(
3810138101
error(node, message, name),
@@ -38106,29 +38106,21 @@ namespace ts {
3810638106
break;
3810738107
}
3810838108
case SyntaxKind.ExportSpecifier: {
38109-
// Don't allow re-exporting an export that will be elided when `--isolatedModules` is set
38110-
// or when `--importsNotUsedAsValues` is `preserve-exact`.
38111-
if (compilerOptions.isolatedModules &&
38112-
compilerOptions.importsNotUsedAsValues !== ImportsNotUsedAsValues.PreserveExact &&
38113-
typeOnlyAlias && getSourceFileOfNode(typeOnlyAlias) === getSourceFileOfNode(node)) {
38114-
// In `isolatedModules` alone, `import type { A } from './a'; export { A }` is allowed
38115-
// because single-file analysis can determine that the export should be dropped.
38116-
// `--importsNotUsedAsValues=preserv-exact` is stricter; it refuses to touch non-type-only
38117-
// imports and exports, so `export { A }` is not allowed if 'A' is not emitted.
38109+
// Don't allow re-exporting an export that will be elided when `--isolatedModules` is set.
38110+
// The exception is that `import type { A } from './a'; export { A }` is allowed
38111+
// because single-file analysis can determine that the export should be dropped.
38112+
if (getSourceFileOfNode(typeOnlyAlias) !== getSourceFileOfNode(node)) {
38113+
const message = isType
38114+
? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type
38115+
: Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_re_exported_with_a_type_only_re_export_when_isolatedModules_is_enabled;
38116+
const name = idText(node.propertyName || node.name);
38117+
addTypeOnlyDeclarationRelatedInfo(
38118+
error(node, message, name),
38119+
isType ? undefined : typeOnlyAlias,
38120+
name
38121+
);
3811838122
return;
3811938123
}
38120-
const message =
38121-
compilerOptions.isolatedModules && isType ? Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type :
38122-
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 :
38123-
isType ? Diagnostics._0_is_a_type_and_must_be_re_exported_with_a_type_only_re_export_when_importsNotUsedAsValues_is_set_to_preserve_exact :
38124-
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;
38125-
const name = idText(node.propertyName || node.name);
38126-
addTypeOnlyDeclarationRelatedInfo(
38127-
error(node, message, name),
38128-
isType ? undefined : typeOnlyAlias,
38129-
name
38130-
);
38131-
return;
3813238124
}
3813338125
}
3813438126
}

src/compiler/commandLineParser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ namespace ts {
539539
"remove": ImportsNotUsedAsValues.Remove,
540540
"preserve": ImportsNotUsedAsValues.Preserve,
541541
"error": ImportsNotUsedAsValues.Error,
542-
"preserve-exact": ImportsNotUsedAsValues.PreserveExact,
543542
})),
544543
affectsEmit: true,
545544
affectsSemanticDiagnostics: true,
@@ -1085,6 +1084,13 @@ namespace ts {
10851084
category: Diagnostics.Advanced_Options,
10861085
description: Diagnostics.Emit_class_fields_with_Define_instead_of_Set,
10871086
},
1087+
{
1088+
name: "noErasingImportedNames",
1089+
type: "boolean",
1090+
affectsEmit: true,
1091+
category: Diagnostics.Advanced_Options,
1092+
description: Diagnostics.Disable_the_removal_of_unused_imported_identifiers_from_the_JavaScript_output
1093+
},
10881094

10891095
{
10901096
name: "keyofStringsOnly",

src/compiler/diagnosticMessages.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,26 +1364,22 @@
13641364
"category": "Error",
13651365
"code": 1433
13661366
},
1367-
"'{0}' is a type and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": {
1367+
"'{0}' is a type and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": {
13681368
"category": "Error",
13691369
"code": 1434
13701370
},
1371-
"'{0}' is a type and must be re-exported with a type-only re-export when 'importsNotUsedAsValues' is set to 'preserve-exact'.": {
1372-
"category": "Error",
1373-
"code": 1435
1374-
},
1375-
"'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'importsNotUsedAsValues' is set to 'preserve-exact'.": {
1371+
"'{0}' resolves to a type-only declaration and must be imported with a type-only import when 'noErasingImportedNames' and 'isolatedModules' are both enabled.": {
13761372
"category": "Error",
13771373
"code": 1436
13781374
},
1379-
"'{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'.": {
1380-
"category": "Error",
1381-
"code": 1437
1382-
},
13831375
"'{0}' resolves to a type-only declaration and must be re-exported with a type-only re-export when 'isolatedModules' is enabled.": {
13841376
"category": "Error",
13851377
"code": 1438
13861378
},
1379+
"Disable the removal of unused imported identifiers from the JavaScript output.": {
1380+
"category": "Message",
1381+
"code": 1439
1382+
},
13871383

13881384
"The types of '{0}' are incompatible between these types.": {
13891385
"category": "Error",
@@ -3962,7 +3958,7 @@
39623958
"category": "Error",
39633959
"code": 5094
39643960
},
3965-
"Option 'importsNotUsedAsValues' may be set to 'preserve-exact' only when 'module' is set to 'es2015' or later.": {
3961+
"Option 'noErasingImportedNames' may be enabled only when 'module' is set to 'es2015' or later.": {
39663962
"category": "Error",
39673963
"code": 5095
39683964
},

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,8 +3295,8 @@ namespace ts {
32953295
}
32963296
}
32973297

3298-
if (options.importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact && getEmitModuleKind(options) < ModuleKind.ES2015) {
3299-
createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_importsNotUsedAsValues_may_be_set_to_preserve_exact_only_when_module_is_set_to_es2015_or_later);
3298+
if (options.noErasingImportedNames && getEmitModuleKind(options) < ModuleKind.ES2015) {
3299+
createOptionValueDiagnostic("importsNotUsedAsValues", Diagnostics.Option_noErasingImportedNames_may_be_enabled_only_when_module_is_set_to_es2015_or_later);
33003300
}
33013301

33023302
// If the emit is enabled make sure that every output file is unique and not overwriting any of the input files

src/compiler/transformers/ts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,7 @@ namespace ts {
28032803
}
28042804

28052805
/**
2806-
* Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' or 'preserve-exact'.
2806+
* Visits an import declaration, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve' and `noErasingImportedNames` is not set.
28072807
*
28082808
* @param node The import declaration node.
28092809
*/
@@ -2821,8 +2821,8 @@ namespace ts {
28212821
// Elide the declaration if the import clause was elided.
28222822
const importClause = visitNode(node.importClause, visitImportClause, isImportClause);
28232823
return importClause ||
2824+
compilerOptions.noErasingImportedNames ||
28242825
importsNotUsedAsValues === ImportsNotUsedAsValues.Preserve ||
2825-
importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact ||
28262826
importsNotUsedAsValues === ImportsNotUsedAsValues.Error
28272827
? factory.updateImportDeclaration(
28282828
node,
@@ -2834,13 +2834,13 @@ namespace ts {
28342834
}
28352835

28362836
/**
2837-
* Visits an import clause, eliding it if it is not referenced and `importsNotUsedAsValues` is not 'preserve-exact'.
2837+
* Visits an import clause, eliding it if it is not referenced and `noErasingImportedNames` is not set.
28382838
*
28392839
* @param node The import clause node.
28402840
*/
28412841
function visitImportClause(node: ImportClause): VisitResult<ImportClause> {
28422842
Debug.assert(!node.isTypeOnly);
2843-
if (importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) {
2843+
if (compilerOptions.noErasingImportedNames) {
28442844
return node;
28452845
}
28462846
// Elide the import clause if we elide both its name and its named bindings.
@@ -2891,7 +2891,7 @@ namespace ts {
28912891

28922892
/**
28932893
* Visits an export declaration, eliding it if it does not contain a clause that resolves
2894-
* to a value and if `importsNotUsedAsValues` is not 'preserve-exact'.
2894+
* to a value and if `noErasingImportedNames` is not set.
28952895
*
28962896
* @param node The export declaration node.
28972897
*/
@@ -2900,7 +2900,7 @@ namespace ts {
29002900
return undefined;
29012901
}
29022902

2903-
if (!node.exportClause || isNamespaceExport(node.exportClause) || importsNotUsedAsValues === ImportsNotUsedAsValues.PreserveExact) {
2903+
if (!node.exportClause || isNamespaceExport(node.exportClause) || compilerOptions.noErasingImportedNames) {
29042904
// never elide `export <whatever> from <whereever>` declarations -
29052905
// they should be kept for sideffects/untyped exports, even when the
29062906
// type checker doesn't know about any exports

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5954,6 +5954,7 @@ namespace ts {
59545954
/*@internal*/noEmitForJsFiles?: boolean;
59555955
noEmitHelpers?: boolean;
59565956
noEmitOnError?: boolean;
5957+
noErasingImportedNames?: boolean;
59575958
noErrorTruncation?: boolean;
59585959
noFallthroughCasesInSwitch?: boolean;
59595960
noImplicitAny?: boolean; // Always combine with strict property
@@ -6071,7 +6072,6 @@ namespace ts {
60716072
Remove,
60726073
Preserve,
60736074
Error,
6074-
PreserveExact,
60756075
}
60766076

60776077
export const enum NewLineKind {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,7 @@ declare namespace ts {
28502850
noEmit?: boolean;
28512851
noEmitHelpers?: boolean;
28522852
noEmitOnError?: boolean;
2853+
noErasingImportedNames?: boolean;
28532854
noErrorTruncation?: boolean;
28542855
noFallthroughCasesInSwitch?: boolean;
28552856
noImplicitAny?: boolean;
@@ -2946,8 +2947,7 @@ declare namespace ts {
29462947
export enum ImportsNotUsedAsValues {
29472948
Remove = 0,
29482949
Preserve = 1,
2949-
Error = 2,
2950-
PreserveExact = 3
2950+
Error = 2
29512951
}
29522952
export enum NewLineKind {
29532953
CarriageReturnLineFeed = 0,

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,7 @@ declare namespace ts {
28502850
noEmit?: boolean;
28512851
noEmitHelpers?: boolean;
28522852
noEmitOnError?: boolean;
2853+
noErasingImportedNames?: boolean;
28532854
noErrorTruncation?: boolean;
28542855
noFallthroughCasesInSwitch?: boolean;
28552856
noImplicitAny?: boolean;
@@ -2946,8 +2947,7 @@ declare namespace ts {
29462947
export enum ImportsNotUsedAsValues {
29472948
Remove = 0,
29482949
Preserve = 1,
2949-
Error = 2,
2950-
PreserveExact = 3
2950+
Error = 2
29512951
}
29522952
export enum NewLineKind {
29532953
CarriageReturnLineFeed = 0,

tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=amd).errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/baselines/reference/importsNotUsedAsValues_preserve-exact_module(module=commonjs).errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)