Skip to content

Commit 8f8a579

Browse files
authored
Added BindingElement to isSomeImportDeclaration (#43387)
* Added BindingElement to isSomeImportDeclaration * Added tests * Refactores to use getDeclarationOfAlias
1 parent 62f3ccd commit 8f8a579

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,7 @@ namespace ts {
24892489
* module.exports = <EntityNameExpression>
24902490
* {<Identifier>}
24912491
* {name: <EntityNameExpression>}
2492+
* const { x } = require ...
24922493
*/
24932494
function isAliasSymbolDeclaration(node: Node): boolean {
24942495
return node.kind === SyntaxKind.ImportEqualsDeclaration
@@ -23933,7 +23934,7 @@ namespace ts {
2393323934
}
2393423935
}
2393523936
else if (isAlias) {
23936-
declaration = symbol.declarations?.find(isSomeImportDeclaration);
23937+
declaration = getDeclarationOfAliasSymbol(symbol);
2393723938
}
2393823939
else {
2393923940
return type;
@@ -41950,21 +41951,6 @@ namespace ts {
4195041951
}
4195141952
}
4195241953

41953-
function isSomeImportDeclaration(decl: Node): boolean {
41954-
switch (decl.kind) {
41955-
case SyntaxKind.ImportClause: // For default import
41956-
case SyntaxKind.ImportEqualsDeclaration:
41957-
case SyntaxKind.NamespaceImport:
41958-
case SyntaxKind.ImportSpecifier: // For rename import `x as y`
41959-
return true;
41960-
case SyntaxKind.Identifier:
41961-
// For regular import, `decl` is an Identifier under the ImportSpecifier.
41962-
return decl.parent.kind === SyntaxKind.ImportSpecifier;
41963-
default:
41964-
return false;
41965-
}
41966-
}
41967-
4196841954
namespace JsxNames {
4196941955
export const JSX = "JSX" as __String;
4197041956
export const IntrinsicElements = "IntrinsicElements" as __String;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== /bar.js ===
2+
const { a } = require("./foo");
3+
>a : Symbol(a, Decl(bar.js, 0, 7))
4+
>require : Symbol(require)
5+
>"./foo" : Symbol("/foo", Decl(foo.d.ts, 0, 0))
6+
7+
if (a) {
8+
>a : Symbol(a, Decl(bar.js, 0, 7))
9+
10+
var x = a + 1;
11+
>x : Symbol(x, Decl(bar.js, 2, 5))
12+
>a : Symbol(a, Decl(bar.js, 0, 7))
13+
}
14+
=== /foo.d.ts ===
15+
// Regresion test for GH#41957
16+
17+
18+
export const a: number | null;
19+
>a : Symbol(a, Decl(foo.d.ts, 3, 12))
20+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== /bar.js ===
2+
const { a } = require("./foo");
3+
>a : number | null
4+
>require("./foo") : typeof import("/foo")
5+
>require : any
6+
>"./foo" : "./foo"
7+
8+
if (a) {
9+
>a : number | null
10+
11+
var x = a + 1;
12+
>x : number
13+
>a + 1 : number
14+
>a : number
15+
>1 : 1
16+
}
17+
=== /foo.d.ts ===
18+
// Regresion test for GH#41957
19+
20+
21+
export const a: number | null;
22+
>a : number | null
23+
>null : null
24+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regresion test for GH#41957
2+
3+
// @allowJs: true
4+
// @checkJs: true
5+
// @strictNullChecks: true
6+
// @noEmit: true
7+
8+
// @Filename: /foo.d.ts
9+
export const a: number | null;
10+
11+
// @Filename: /bar.js
12+
const { a } = require("./foo");
13+
if (a) {
14+
var x = a + 1;
15+
}

0 commit comments

Comments
 (0)