Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f4255dd

Browse filesBrowse files
committed
Handle the new js binding element alias symbols in JS declaration emit
1 parent 672861a commit f4255dd
Copy full SHA for f4255dd

5 files changed

+241Lines changed: 241 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6783,6 +6783,37 @@ namespace ts {
67836783
const targetName = getInternalSymbolName(target, verbatimTargetName);
67846784
includePrivateSymbol(target); // the target may be within the same scope - attempt to serialize it first
67856785
switch (node.kind) {
6786+
case SyntaxKind.BindingElement:
6787+
if (node.parent?.parent?.kind === SyntaxKind.VariableDeclaration) {
6788+
// const { SomeClass } = require('./lib');
6789+
const specifier = getSpecifierForModuleSymbol(target.parent || target, context); // 'y'
6790+
addResult(factory.createImportDeclaration(
6791+
/*decorators*/ undefined,
6792+
/*modifiers*/ undefined,
6793+
factory.createImportClause(/*isTypeOnly*/ false, /*name*/ undefined, factory.createNamedImports([factory.createImportSpecifier(
6794+
(node as BindingElement).propertyName && isIdentifier((node as BindingElement).propertyName!) ? factory.createIdentifier(idText((node as BindingElement).propertyName as Identifier)) : undefined,
6795+
factory.createIdentifier(localName)
6796+
)])),
6797+
factory.createStringLiteral(specifier)
6798+
), ModifierFlags.None);
6799+
break;
6800+
}
6801+
// At present, the below case should be entirely unhit, as, generally speaking, the below case is *usually* bound
6802+
// such that the `BinaryExpression` is the declaration rather than the specific, nested binding element
6803+
// (because we don't seek to emit an alias in these forms yet). As such, the `BinaryExpression` switch case
6804+
// will be what actually handles this form. _However_, in anticipation of binding the below with proper
6805+
// alias symbols, I'm _pretty comfortable_ including the case here, even though it is not yet live.
6806+
if (node.parent?.parent?.kind === SyntaxKind.BinaryExpression) {
6807+
// module.exports = { SomeClass }
6808+
serializeExportSpecifier(
6809+
unescapeLeadingUnderscores(symbol.escapedName),
6810+
targetName
6811+
);
6812+
break;
6813+
}
6814+
// We don't know how to serialize this (nested?) binding element
6815+
Debug.failBadSyntaxKind(node.parent?.parent || node, "Unhandled binding element grandparent kind in declaration serialization");
6816+
break;
67866817
case SyntaxKind.VariableDeclaration:
67876818
// commonjs require: const x = require('y')
67886819
if (isPropertyAccessExpression((node as VariableDeclaration).initializer!)) {
Collapse file
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsReexportedCjsAlias.ts] ////
2+
3+
//// [lib.js]
4+
/**
5+
* @param {string} a
6+
*/
7+
function bar(a) {
8+
return a + a;
9+
}
10+
11+
class SomeClass {
12+
a() {
13+
return 1;
14+
}
15+
}
16+
17+
module.exports = {
18+
bar,
19+
SomeClass
20+
}
21+
//// [main.js]
22+
const { SomeClass, SomeClass: Another } = require('./lib');
23+
24+
module.exports = {
25+
SomeClass,
26+
Another
27+
}
28+
29+
//// [lib.js]
30+
/**
31+
* @param {string} a
32+
*/
33+
function bar(a) {
34+
return a + a;
35+
}
36+
var SomeClass = /** @class */ (function () {
37+
function SomeClass() {
38+
}
39+
SomeClass.prototype.a = function () {
40+
return 1;
41+
};
42+
return SomeClass;
43+
}());
44+
module.exports = {
45+
bar: bar,
46+
SomeClass: SomeClass
47+
};
48+
//// [main.js]
49+
var _a = require('./lib'), SomeClass = _a.SomeClass, Another = _a.SomeClass;
50+
module.exports = {
51+
SomeClass: SomeClass,
52+
Another: Another
53+
};
54+
55+
56+
//// [lib.d.ts]
57+
/**
58+
* @param {string} a
59+
*/
60+
export function bar(a: string): string;
61+
export class SomeClass {
62+
a(): number;
63+
}
64+
//// [main.d.ts]
65+
import { SomeClass } from "./lib";
66+
import { SomeClass as Another } from "./lib";
67+
export { SomeClass, Another };
Collapse file
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== tests/cases/conformance/jsdoc/declarations/main.js ===
2+
const { SomeClass, SomeClass: Another } = require('./lib');
3+
>SomeClass : Symbol(SomeClass, Decl(main.js, 0, 7))
4+
>SomeClass : Symbol(SomeClass, Decl(lib.js, 14, 8))
5+
>Another : Symbol(Another, Decl(main.js, 0, 18))
6+
>require : Symbol(require)
7+
>'./lib' : Symbol("tests/cases/conformance/jsdoc/declarations/lib", Decl(lib.js, 0, 0))
8+
9+
module.exports = {
10+
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/main", Decl(main.js, 0, 0))
11+
>module : Symbol(export=, Decl(main.js, 0, 59))
12+
>exports : Symbol(export=, Decl(main.js, 0, 59))
13+
14+
SomeClass,
15+
>SomeClass : Symbol(SomeClass, Decl(main.js, 2, 18))
16+
17+
Another
18+
>Another : Symbol(Another, Decl(main.js, 3, 14))
19+
}
20+
=== tests/cases/conformance/jsdoc/declarations/lib.js ===
21+
/**
22+
* @param {string} a
23+
*/
24+
function bar(a) {
25+
>bar : Symbol(bar, Decl(lib.js, 0, 0))
26+
>a : Symbol(a, Decl(lib.js, 3, 13))
27+
28+
return a + a;
29+
>a : Symbol(a, Decl(lib.js, 3, 13))
30+
>a : Symbol(a, Decl(lib.js, 3, 13))
31+
}
32+
33+
class SomeClass {
34+
>SomeClass : Symbol(SomeClass, Decl(lib.js, 5, 1))
35+
36+
a() {
37+
>a : Symbol(SomeClass.a, Decl(lib.js, 7, 17))
38+
39+
return 1;
40+
}
41+
}
42+
43+
module.exports = {
44+
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/lib", Decl(lib.js, 0, 0))
45+
>module : Symbol(export=, Decl(lib.js, 11, 1))
46+
>exports : Symbol(export=, Decl(lib.js, 11, 1))
47+
48+
bar,
49+
>bar : Symbol(bar, Decl(lib.js, 13, 18))
50+
51+
SomeClass
52+
>SomeClass : Symbol(SomeClass, Decl(lib.js, 14, 8))
53+
}
Collapse file
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
=== tests/cases/conformance/jsdoc/declarations/main.js ===
2+
const { SomeClass, SomeClass: Another } = require('./lib');
3+
>SomeClass : typeof SomeClass
4+
>SomeClass : any
5+
>Another : typeof SomeClass
6+
>require('./lib') : { bar: (a: string) => string; SomeClass: typeof SomeClass; }
7+
>require : any
8+
>'./lib' : "./lib"
9+
10+
module.exports = {
11+
>module.exports = { SomeClass, Another} : { SomeClass: typeof SomeClass; Another: typeof SomeClass; }
12+
>module.exports : { SomeClass: typeof SomeClass; Another: typeof SomeClass; }
13+
>module : { "\"tests/cases/conformance/jsdoc/declarations/main\"": { SomeClass: typeof SomeClass; Another: typeof SomeClass; }; }
14+
>exports : { SomeClass: typeof SomeClass; Another: typeof SomeClass; }
15+
>{ SomeClass, Another} : { SomeClass: typeof SomeClass; Another: typeof SomeClass; }
16+
17+
SomeClass,
18+
>SomeClass : typeof SomeClass
19+
20+
Another
21+
>Another : typeof SomeClass
22+
}
23+
=== tests/cases/conformance/jsdoc/declarations/lib.js ===
24+
/**
25+
* @param {string} a
26+
*/
27+
function bar(a) {
28+
>bar : (a: string) => string
29+
>a : string
30+
31+
return a + a;
32+
>a + a : string
33+
>a : string
34+
>a : string
35+
}
36+
37+
class SomeClass {
38+
>SomeClass : SomeClass
39+
40+
a() {
41+
>a : () => number
42+
43+
return 1;
44+
>1 : 1
45+
}
46+
}
47+
48+
module.exports = {
49+
>module.exports = { bar, SomeClass} : { bar: (a: string) => string; SomeClass: typeof SomeClass; }
50+
>module.exports : { bar: (a: string) => string; SomeClass: typeof SomeClass; }
51+
>module : { "\"tests/cases/conformance/jsdoc/declarations/lib\"": { bar: (a: string) => string; SomeClass: typeof SomeClass; }; }
52+
>exports : { bar: (a: string) => string; SomeClass: typeof SomeClass; }
53+
>{ bar, SomeClass} : { bar: (a: string) => string; SomeClass: typeof SomeClass; }
54+
55+
bar,
56+
>bar : (a: string) => string
57+
58+
SomeClass
59+
>SomeClass : typeof SomeClass
60+
}
Collapse file
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @target: es5
4+
// @outDir: ./out
5+
// @declaration: true
6+
// @filename: lib.js
7+
/**
8+
* @param {string} a
9+
*/
10+
function bar(a) {
11+
return a + a;
12+
}
13+
14+
class SomeClass {
15+
a() {
16+
return 1;
17+
}
18+
}
19+
20+
module.exports = {
21+
bar,
22+
SomeClass
23+
}
24+
// @filename: main.js
25+
const { SomeClass, SomeClass: Another } = require('./lib');
26+
27+
module.exports = {
28+
SomeClass,
29+
Another
30+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.