Skip to content

Commit

Permalink
look for local ~Value instead of Type symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
frigus02 committed Dec 12, 2023
1 parent 006cb6a commit dd563e8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3609,9 +3609,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// here because 'verbatimModuleSyntax' will already have an error for importing a type without 'import type'.
if (compilerOptions.isolatedModules && result && isInExternalModule && meaning & SymbolFlags.Value) {
const isGlobal = lookup(globals, name, meaning) === result;
const typeSymbol = isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, SymbolFlags.Type);
if (isGlobal && typeSymbol) {
const importDecl = typeSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration);
const nonValueSymbol = isGlobal && isSourceFile(lastLocation!) && lastLocation.locals && lookup(lastLocation.locals, name, ~SymbolFlags.Value);
if (nonValueSymbol) {
const importDecl = nonValueSymbol.declarations?.find(d => d.kind === SyntaxKind.ImportSpecifier || d.kind === SyntaxKind.ImportClause || d.kind === SyntaxKind.NamespaceImport || d.kind === SyntaxKind.ImportEqualsDeclaration);
if (importDecl && !isTypeOnlyImportDeclaration(importDecl)) {
error(importDecl, Diagnostics.Import_0_conflicts_with_global_value_used_in_this_file_so_must_be_declared_with_a_type_only_import_when_isolatedModules_is_enabled, unescapeLeadingUnderscores(name));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.


==== ./types.ts (0 errors) ====
Expand All @@ -9,21 +11,35 @@ bad.ts(1,10): error TS1484: 'Date' is a type and must be imported using a type-o
year: number;
}

==== ./bad.ts (2 errors) ====
import { Date } from './types';
export namespace Event {
export type T = any;
}

==== ./bad.ts (4 errors) ====
import { Date, Event } from './types';
~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~~~
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~~~~
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

==== ./good.ts (0 errors) ====
import type { Date } from './types';
import type { Date, Event } from './types';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.


==== ./types.ts (0 errors) ====
Expand All @@ -8,19 +9,31 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th
year: number;
}

==== ./bad.ts (1 errors) ====
import { Date } from './types';
export namespace Event {
export type T = any;
}

==== ./bad.ts (2 errors) ====
import { Date, Event } from './types';
~~~~
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
~~~~~
!!! error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

==== ./good.ts (0 errors) ====
import type { Date } from './types';
import type { Date, Event } from './types';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
bad.ts(1,16): error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
bad.ts(1,16): error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.


==== ./types.ts (0 errors) ====
Expand All @@ -10,23 +13,39 @@ bad.ts(1,10): error TS2866: Import 'Date' conflicts with global value used in th
year: number;
}

==== ./bad.ts (3 errors) ====
import { Date } from './types';
export namespace Event {
export type T = any;
}

==== ./bad.ts (6 errors) ====
import { Date, Event } from './types';
~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~~~
!!! error TS1484: 'Date' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~
!!! error TS2866: Import 'Date' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
~~~~~
!!! error TS1286: ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.
~~~~~
!!! error TS1484: 'Event' is a type and must be imported using a type-only import when 'verbatimModuleSyntax' is enabled.
~~~~~
!!! error TS2866: Import 'Event' conflicts with global value used in this file, so must be declared with a type-only import when 'isolatedModules' is enabled.
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

==== ./good.ts (0 errors) ====
import type { Date } from './types';
import type { Date, Event } from './types';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

14 changes: 12 additions & 2 deletions tests/cases/compiler/isolatedModulesShadowGlobalTypeNotValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ export interface Date {
year: number;
}

export namespace Event {
export type T = any;
}

// @filename: ./bad.ts
import { Date } from './types';
import { Date, Event } from './types';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

// @filename: ./good.ts
import type { Date } from './types';
import type { Date, Event } from './types';
function foo(a: Date) {
const b = new Date(a.year, a.month, a.day);
return b.getTime();
}
function bar() {
return new Event('bar') as Event.T;
}

0 comments on commit dd563e8

Please sign in to comment.