Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tsc/internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,18 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
}

mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
// We know moduleName resolves to an ambient module provided that moduleName:
// - is in the list of ambient modules locally declared in the current source file.
if slices.Contains(file.AmbientModuleNames, moduleName) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a straight~ port of

if (resolveToOwnAmbientModule) {
const name = nameAndModeGetter.getName(entry);
// We know moduleName resolves to an ambient module provided that moduleName:
// - is in the list of ambient modules locally declared in the current source file.
if (contains(containingSourceFile!.ambientModuleNames, name)) {
if (isTraceEnabled(options, host)) {
trace(
host,
Diagnostics.Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1,
name,
getNormalizedAbsolutePath(containingSourceFile!.originalFileName, currentDirectory),
);
}
(result ??= new Array(entries.length))[i] = emptyResolution;
continue;
}
}

if optionsForFile.TraceResolution.IsTrue() {
resolutionsTrace = append(resolutionsTrace, module.DiagAndArgs{
Message: diagnostics.Module_0_was_resolved_as_locally_declared_ambient_module_in_file_1,
Args: []any{moduleName, file.FileName()},
})
}
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = &module.ResolvedModule{}
continue
}
var resolvedModule *module.ResolvedModule
var trace []module.DiagAndArgs
var err error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/a.ts(2,1): error TS2304: Cannot find name 'fooGlobal'.


==== /a.ts (1 errors) ====
/// <reference path="declarations.d.ts" />
fooGlobal;
~~~~~~~~~
!!! error TS2304: Cannot find name 'fooGlobal'.

==== /node_modules/foo/package.json (0 errors) ====
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }

==== /node_modules/foo/index.d.ts (0 errors) ====
declare global {
var fooGlobal: string;
}
declare function foo(value: unknown): boolean;
export default foo;

==== /declarations.d.ts (0 errors) ====
declare module "foo" {
import _foo from "foo";
export function isFoo(value: unknown): boolean;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////

//// [package.json]
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }

//// [index.d.ts]
declare global {
var fooGlobal: string;
}
declare function foo(value: unknown): boolean;
export default foo;

//// [declarations.d.ts]
declare module "foo" {
import _foo from "foo";
export function isFoo(value: unknown): boolean;
}

//// [a.ts]
/// <reference path="declarations.d.ts" />
fooGlobal;


//// [a.js]
"use strict";
/// <reference path="declarations.d.ts" />
fooGlobal;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////

=== /a.ts ===

/// <reference path="declarations.d.ts" />
fooGlobal;

=== /declarations.d.ts ===
declare module "foo" {
>"foo" : Symbol(_foo, Decl(declarations.d.ts, 0, 0))

import _foo from "foo";
>_foo : Symbol(_foo, Decl(declarations.d.ts, 1, 10))

export function isFoo(value: unknown): boolean;
>isFoo : Symbol(isFoo, Decl(declarations.d.ts, 1, 27))
>value : Symbol(value, Decl(declarations.d.ts, 2, 26))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Module 'foo' was resolved as locally declared ambient module in file '/declarations.d.ts'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/ambientModuleImportingItselfNotResolved.ts] ////

=== /a.ts ===
/// <reference path="declarations.d.ts" />
fooGlobal;
>fooGlobal : any

=== /declarations.d.ts ===
declare module "foo" {
>"foo" : typeof _foo

import _foo from "foo";
>_foo : typeof _foo

export function isFoo(value: unknown): boolean;
>isFoo : (value: unknown) => boolean
>value : unknown
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -1,35 +1,9 @@
======== Resolving module 'ph' from '/a/b/node_modules/@types/node/ph.d.ts'. ========
Module 'ph' was resolved as locally declared ambient module in file '/a/b/node_modules/@types/node/ph.d.ts'.
======== Resolving module 'node:ph' from '/a/b/main.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/a/b/node_modules/@types/node/package.json' does not exist.
File '/a/b/node_modules/@types/package.json' does not exist.
File '/a/b/node_modules/package.json' does not exist.
File '/a/b/package.json' does not exist.
File '/a/package.json' does not exist.
File '/package.json' does not exist.
Loading module 'ph' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/a/b/node_modules/@types/node/node_modules' does not exist, skipping all lookups in it.
Directory '/a/b/node_modules/@types/node_modules' does not exist, skipping all lookups in it.
File '/a/b/node_modules/ph.ts' does not exist.
File '/a/b/node_modules/ph.tsx' does not exist.
File '/a/b/node_modules/ph.d.ts' does not exist.
File '/a/b/node_modules/@types/ph.d.ts' does not exist.
Directory '/a/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
Searching all ancestor node_modules directories for fallback extensions: JavaScript, JSON.
Directory '/a/b/node_modules/@types/node/node_modules' does not exist, skipping all lookups in it.
Directory '/a/b/node_modules/@types/node_modules' does not exist, skipping all lookups in it.
File '/a/b/node_modules/ph.js' does not exist.
File '/a/b/node_modules/ph.jsx' does not exist.
Directory '/a/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
======== Module name 'ph' was not resolved. ========
======== Resolving module 'node:ph' from '/a/b/main.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/a/b/package.json' does not exist according to earlier cached lookups.
File '/a/package.json' does not exist according to earlier cached lookups.
File '/package.json' does not exist according to earlier cached lookups.
Skipping module 'node:ph' that looks like an absolute URI, target file types: TypeScript, JavaScript, Declaration, JSON.
======== Module name 'node:ph' was not resolved. ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @target: esnext
// @module: preserve
// @moduleResolution: bundler
// @noImplicitReferences: true
// @traceResolution: true

// An import inside an ambient module declaration that names the same module
// resolves to that declaration, so the real package must not be pulled in.

// @filename: /node_modules/foo/package.json
{ "name": "foo", "version": "1.0.0", "types": "index.d.ts" }

// @filename: /node_modules/foo/index.d.ts
declare global {
var fooGlobal: string;
}
declare function foo(value: unknown): boolean;
export default foo;

// @filename: /declarations.d.ts
declare module "foo" {
import _foo from "foo";
export function isFoo(value: unknown): boolean;
}

// @filename: /a.ts
/// <reference path="declarations.d.ts" />
fooGlobal;
Loading