Added BindingElement to isSomeImportDeclaration (#43387)

* Added BindingElement to isSomeImportDeclaration

* Added tests

* Refactores to use getDeclarationOfAlias
This commit is contained in:
Armando Aguirre 2021-03-31 18:18:16 -07:00 committed by GitHub
parent 62f3ccd9c0
commit 8f8a579eee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 16 deletions

View file

@ -2489,6 +2489,7 @@ namespace ts {
* module.exports = <EntityNameExpression>
* {<Identifier>}
* {name: <EntityNameExpression>}
* const { x } = require ...
*/
function isAliasSymbolDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.ImportEqualsDeclaration
@ -23933,7 +23934,7 @@ namespace ts {
}
}
else if (isAlias) {
declaration = symbol.declarations?.find(isSomeImportDeclaration);
declaration = getDeclarationOfAliasSymbol(symbol);
}
else {
return type;
@ -41950,21 +41951,6 @@ namespace ts {
}
}
function isSomeImportDeclaration(decl: Node): boolean {
switch (decl.kind) {
case SyntaxKind.ImportClause: // For default import
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.NamespaceImport:
case SyntaxKind.ImportSpecifier: // For rename import `x as y`
return true;
case SyntaxKind.Identifier:
// For regular import, `decl` is an Identifier under the ImportSpecifier.
return decl.parent.kind === SyntaxKind.ImportSpecifier;
default:
return false;
}
}
namespace JsxNames {
export const JSX = "JSX" as __String;
export const IntrinsicElements = "IntrinsicElements" as __String;

View file

@ -0,0 +1,20 @@
=== /bar.js ===
const { a } = require("./foo");
>a : Symbol(a, Decl(bar.js, 0, 7))
>require : Symbol(require)
>"./foo" : Symbol("/foo", Decl(foo.d.ts, 0, 0))
if (a) {
>a : Symbol(a, Decl(bar.js, 0, 7))
var x = a + 1;
>x : Symbol(x, Decl(bar.js, 2, 5))
>a : Symbol(a, Decl(bar.js, 0, 7))
}
=== /foo.d.ts ===
// Regresion test for GH#41957
export const a: number | null;
>a : Symbol(a, Decl(foo.d.ts, 3, 12))

View file

@ -0,0 +1,24 @@
=== /bar.js ===
const { a } = require("./foo");
>a : number | null
>require("./foo") : typeof import("/foo")
>require : any
>"./foo" : "./foo"
if (a) {
>a : number | null
var x = a + 1;
>x : number
>a + 1 : number
>a : number
>1 : 1
}
=== /foo.d.ts ===
// Regresion test for GH#41957
export const a: number | null;
>a : number | null
>null : null

View file

@ -0,0 +1,15 @@
// Regresion test for GH#41957
// @allowJs: true
// @checkJs: true
// @strictNullChecks: true
// @noEmit: true
// @Filename: /foo.d.ts
export const a: number | null;
// @Filename: /bar.js
const { a } = require("./foo");
if (a) {
var x = a + 1;
}