From b4fd0a7fb51f3c6dfab0ce6072cab666c30eac87 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 9 Nov 2023 10:37:21 -0800 Subject: [PATCH 1/2] isolatedModules error on alias merging with local value --- src/compiler/checker.ts | 13 +++++++++++ src/compiler/diagnosticMessages.json | 6 ++++- src/testRunner/compilerRunner.ts | 1 + ...alse,verbatimmodulesyntax=true).errors.txt | 19 ++++++++++++++++ ...rue,verbatimmodulesyntax=false).errors.txt | 16 ++++++++++++++ ...true,verbatimmodulesyntax=true).errors.txt | 22 +++++++++++++++++++ .../isolatedModulesSketchyAliasLocalMerge.ts | 15 +++++++++++++ 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt create mode 100644 tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt create mode 100644 tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt create mode 100644 tests/cases/compiler/isolatedModulesSketchyAliasLocalMerge.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9bdb4a3d071b..23121e8fbe101 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -45423,6 +45423,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; error(node, message, symbolToString(symbol)); } + else if (node.kind !== SyntaxKind.ExportSpecifier) { + // Look at 'compilerOptions.isolatedModules' and not 'getIsolatedModules(...)' (which considers 'verbatimModuleSyntax') + // here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'. + const appearsValueyToTranspiler = compilerOptions.isolatedModules && !findAncestor(node, isTypeOnlyImportOrExportDeclaration); + if (appearsValueyToTranspiler && symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue)) { + error( + node, + Diagnostics.Import_0_conflicts_with_local_value_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled, + symbolToString(symbol), + isolatedModulesLikeFlagName, + ); + } + } if ( getIsolatedModules(compilerOptions) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 81fd5981ac8a4..30db63d55b160 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3711,8 +3711,12 @@ "category": "Error", "code": 2864 }, + "Import '{0}' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled.": { + "category": "Error", + "code": 2865 + }, - "Import declaration '{0}' is using private name '{1}'.": { + "Import declaration '{0}' is using private name '{1}'. ": { "category": "Error", "code": 4000 }, diff --git a/src/testRunner/compilerRunner.ts b/src/testRunner/compilerRunner.ts index d287bec994046..b3c86f6390ff8 100644 --- a/src/testRunner/compilerRunner.ts +++ b/src/testRunner/compilerRunner.ts @@ -140,6 +140,7 @@ class CompilerTest { "importHelpers", "downlevelIteration", "isolatedModules", + "verbatimModuleSyntax", "strict", "noImplicitAny", "strictNullChecks", diff --git a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt new file mode 100644 index 0000000000000..6d699b7aaea0b --- /dev/null +++ b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=false,verbatimmodulesyntax=true).errors.txt @@ -0,0 +1,19 @@ +bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + + +==== types.ts (0 errors) ==== + export type FC = () => void; + +==== bad.ts (2 errors) ==== + import { FC } from "./types"; + ~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~ +!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + let FC: FC | null = null; + +==== good.ts (0 errors) ==== + import type { FC } from "./types"; + let FC: FC | null = null; + \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt new file mode 100644 index 0000000000000..04875f8fca464 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=false).errors.txt @@ -0,0 +1,16 @@ +bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled. + + +==== types.ts (0 errors) ==== + export type FC = () => void; + +==== bad.ts (1 errors) ==== + import { FC } from "./types"; + ~~ +!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled. + let FC: FC | null = null; + +==== good.ts (0 errors) ==== + import type { FC } from "./types"; + let FC: FC | null = null; + \ No newline at end of file diff --git a/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt new file mode 100644 index 0000000000000..f87ec7ea8fc17 --- /dev/null +++ b/tests/baselines/reference/isolatedModulesSketchyAliasLocalMerge(isolatedmodules=true,verbatimmodulesyntax=true).errors.txt @@ -0,0 +1,22 @@ +bad.ts(1,10): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. +bad.ts(1,10): error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. +bad.ts(1,10): error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled. + + +==== types.ts (0 errors) ==== + export type FC = () => void; + +==== bad.ts (3 errors) ==== + import { FC } from "./types"; + ~~ +!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled. + ~~ +!!! error TS1484: 'FC' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled. + ~~ +!!! error TS2865: Import 'FC' conflicts with local value, so must be declared with a type-only import when 'isolatedModules' is enabled. + let FC: FC | null = null; + +==== good.ts (0 errors) ==== + import type { FC } from "./types"; + let FC: FC | null = null; + \ No newline at end of file diff --git a/tests/cases/compiler/isolatedModulesSketchyAliasLocalMerge.ts b/tests/cases/compiler/isolatedModulesSketchyAliasLocalMerge.ts new file mode 100644 index 0000000000000..fa5869c0da1c6 --- /dev/null +++ b/tests/cases/compiler/isolatedModulesSketchyAliasLocalMerge.ts @@ -0,0 +1,15 @@ +// @isolatedModules: false, true +// @verbatimModuleSyntax: false, true +// @noEmit: true +// @noTypesAndSymbols: true + +// @Filename: types.ts +export type FC = () => void; + +// @Filename: bad.ts +import { FC } from "./types"; +let FC: FC | null = null; + +// @Filename: good.ts +import type { FC } from "./types"; +let FC: FC | null = null; From 5546b582d422b63184e3321aff88426e14b15f69 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 9 Nov 2023 10:58:23 -0800 Subject: [PATCH 2/2] Remove accidental space from message --- src/compiler/diagnosticMessages.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 30db63d55b160..7ba864791bd35 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3716,7 +3716,7 @@ "code": 2865 }, - "Import declaration '{0}' is using private name '{1}'. ": { + "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", "code": 4000 },