Skip to content

import statement from "node" in js source file could produce correct declaration path. #41816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2020
Merged
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
3 changes: 2 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,8 @@ namespace ts {
export function getPackageNameFromTypesPackageName(mangledName: string): string {
const withoutAtTypePrefix = removePrefix(mangledName, "@types/");
if (withoutAtTypePrefix !== mangledName) {
return unmangleScopedPackageName(withoutAtTypePrefix);
const withoutAtTypeNodePrefix = removePrefix(withoutAtTypePrefix, "node/");
return unmangleScopedPackageName(withoutAtTypeNodePrefix);
}
return mangledName;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/importDeclFromTypeNodeInJsSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//// [tests/cases/compiler/importDeclFromTypeNodeInJsSource.ts] ////

//// [index.d.ts]
/// <reference path="events.d.ts" />
//// [events.d.ts]
declare module "events" {
namespace EventEmitter {
class EventEmitter {
constructor();
}
}
export = EventEmitter;
}

//// [b.js]
import { EventEmitter } from 'events';
class Foo extends EventEmitter {
constructor() {
super();
}
}
export default Foo;

//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
class Foo extends events_1.EventEmitter {
constructor() {
super();
}
}
exports.default = Foo;


//// [b.d.ts]
export default Foo;
declare class Foo extends EventEmitter {
}
import { EventEmitter } from "events";
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, so the key point to this test is that this .d.ts doesn't turn into node/events because it's a special case global 👍🏻

Copy link
Member

@weswigham weswigham Dec 8, 2020

Choose a reason for hiding this comment

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

This fix seems incorrect and an odd special case. We expect in cases like this one, to use the module name specified by the ambient module declaration, not one from a filepath. Since the module is declared as declare module "events" we should only be using "events" to refer to it, and should never have looked at a filepath to begin with (notably: there's no guarantee the ambient module has the same name as the containing file!).

Copy link
Member

Choose a reason for hiding this comment

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

This change needs to be reverted and correctly fixed as @weswigham points out correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh.... Sorry for the misleading...
And, I am a little confused, if there is not any special folder, according to the module resolution https://www.typescriptlang.org/docs/handbook/module-resolution.html. How could file find the correct modules? Did I miss something?

Copy link
Contributor Author

@ShuiRuTian ShuiRuTian Dec 9, 2020

Choose a reason for hiding this comment

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

Auh, I find this https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#-reference-ing-a-module and finally understand why it is called "ambient" module......

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#41895 this one!

35 changes: 35 additions & 0 deletions tests/baselines/reference/importDeclFromTypeNodeInJsSource.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== /src/node_modules/@types/node/index.d.ts ===
/// <reference path="events.d.ts" />
No type information for this code.=== /src/node_modules/@types/node/events.d.ts ===
declare module "events" {
>"events" : Symbol("events", Decl(events.d.ts, 0, 0))

namespace EventEmitter {
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 0, 25))

class EventEmitter {
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 1, 28))

constructor();
}
}
export = EventEmitter;
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 0, 25))
}

=== /src/b.js ===
import { EventEmitter } from 'events';
>EventEmitter : Symbol(EventEmitter, Decl(b.js, 0, 8))

class Foo extends EventEmitter {
>Foo : Symbol(Foo, Decl(b.js, 0, 38))
>EventEmitter : Symbol(EventEmitter, Decl(b.js, 0, 8))

constructor() {
super();
>super : Symbol(EventEmitter, Decl(events.d.ts, 1, 28))
}
}
export default Foo;
>Foo : Symbol(Foo, Decl(b.js, 0, 38))

36 changes: 36 additions & 0 deletions tests/baselines/reference/importDeclFromTypeNodeInJsSource.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== /src/node_modules/@types/node/index.d.ts ===
/// <reference path="events.d.ts" />
No type information for this code.=== /src/node_modules/@types/node/events.d.ts ===
declare module "events" {
>"events" : typeof import("events")

namespace EventEmitter {
>EventEmitter : typeof EventEmitter

class EventEmitter {
>EventEmitter : EventEmitter

constructor();
}
}
export = EventEmitter;
>EventEmitter : typeof EventEmitter
}

=== /src/b.js ===
import { EventEmitter } from 'events';
>EventEmitter : typeof EventEmitter

class Foo extends EventEmitter {
>Foo : Foo
>EventEmitter : EventEmitter

constructor() {
super();
>super() : void
>super : typeof EventEmitter
}
}
export default Foo;
>Foo : Foo

26 changes: 26 additions & 0 deletions tests/cases/compiler/importDeclFromTypeNodeInJsSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @target: esnext
// @module: commonjs
// @allowJs: true
// @checkJs: true
// @declaration: true
// @outDir: ./dist
// @filename: /src/node_modules/@types/node/index.d.ts
/// <reference path="events.d.ts" />
// @filename: /src/node_modules/@types/node/events.d.ts
declare module "events" {
namespace EventEmitter {
class EventEmitter {
constructor();
}
}
export = EventEmitter;
}

// @filename: /src/b.js
import { EventEmitter } from 'events';
class Foo extends EventEmitter {
constructor() {
super();
}
}
export default Foo;