From 5ff6ac0427434aabc9badaff70c28b40a5a0325a Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Sat, 8 Apr 2023 23:54:51 +1200 Subject: [PATCH 1/3] fix(type-utils): use typescript's `isSourceFileFromExternalLibrary` --- .../type-utils/src/TypeOrValueSpecifier.ts | 28 ++++++++++++------- packages/type-utils/typings/typescript.d.ts | 11 ++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index be426297d0ac..584bf942dc9c 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -1,6 +1,6 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'; import path from 'path'; -import type * as ts from 'typescript'; +import * as ts from 'typescript'; interface FileSpecifier { from: 'file'; @@ -150,16 +150,20 @@ function typeDeclaredInFile( function typeDeclaredInPackage( packageName: string, declarationFiles: ts.SourceFile[], + program: ts.Program, ): boolean { // Handle scoped packages - if the name starts with @, remove it and replace / with __ - const typesPackageName = - '@types/' + packageName.replace(/^@([^/]+)\//, '$1__'); - const matcher = new RegExp( - `node_modules/(?:${packageName}|${typesPackageName})/`, - ); - return declarationFiles.some(declaration => - matcher.test(declaration.fileName), - ); + const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__'); + + const matcher = new RegExp(`${packageName}|${typesPackageName}`); + return declarationFiles.some(declaration => { + const packageIdName = program.sourceFileToPackageName.get(declaration.path); + return ( + packageIdName !== undefined && + matcher.test(packageIdName) && + program.isSourceFileFromExternalLibrary(declaration) + ); + }); } export function typeMatchesSpecifier( @@ -186,6 +190,10 @@ export function typeMatchesSpecifier( program.isSourceFileDefaultLibrary(declaration), ); case 'package': - return typeDeclaredInPackage(specifier.package, declarationFiles); + return typeDeclaredInPackage( + specifier.package, + declarationFiles, + program, + ); } } diff --git a/packages/type-utils/typings/typescript.d.ts b/packages/type-utils/typings/typescript.d.ts index 73304155ee74..335b12b86fe5 100644 --- a/packages/type-utils/typings/typescript.d.ts +++ b/packages/type-utils/typings/typescript.d.ts @@ -30,4 +30,15 @@ declare module 'typescript' { */ intrinsicName?: string; } + + interface Program { + /** + * Maps from a SourceFile's `.path` to the name of the package it was imported with. + */ + readonly sourceFileToPackageName: ReadonlyMap; + } + + interface SourceFile extends Declaration, LocalsContainer { + path: Path; + } } From ca5ae81a1a6a6c109ecee349f60cbcd836c0c2c4 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Fri, 7 Jul 2023 16:15:57 +1200 Subject: [PATCH 2/3] style: fix linting issues --- packages/type-utils/src/TypeOrValueSpecifier.ts | 4 ++-- packages/type-utils/src/isTypeReadonly.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index 584bf942dc9c..8cbb4eedf25a 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -1,6 +1,6 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'; import path from 'path'; -import * as ts from 'typescript'; +import type * as ts from 'typescript'; interface FileSpecifier { from: 'file'; @@ -125,7 +125,7 @@ function specifierNameMatches(type: ts.Type, name: string | string[]): boolean { if (symbol === undefined) { return false; } - return name.some(item => item === symbol.escapedName); + return name.some(item => item === (symbol.escapedName as string)); } function typeDeclaredInFile( diff --git a/packages/type-utils/src/isTypeReadonly.ts b/packages/type-utils/src/isTypeReadonly.ts index 63a02ea4464e..189bef6e12c6 100644 --- a/packages/type-utils/src/isTypeReadonly.ts +++ b/packages/type-utils/src/isTypeReadonly.ts @@ -85,7 +85,7 @@ function isTypeReadonlyArrayOrTuple( type.getSymbol(), ESLintUtils.NullThrowsReasons.MissingToken('symbol', 'array type'), ); - const escapedName = symbol.getEscapedName(); + const escapedName = symbol.getEscapedName() as string; if (escapedName === 'Array') { return Readonlyness.Mutable; } From 189d04a0ef48399a580f44f4b136534b2ab417be Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 17 Oct 2023 15:18:23 -0400 Subject: [PATCH 3/3] Undo merge conflict-y change --- packages/type-utils/src/TypeOrValueSpecifier.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index 403f1e959181..16958370f57b 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -130,7 +130,7 @@ function specifierNameMatches(type: ts.Type, name: string[] | string): boolean { if (symbol === undefined) { return false; } - return name.some(item => item === (symbol.escapedName as string)); + return name.some(item => (item as ts.__String) === symbol.escapedName); } function typeDeclaredInFile( pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy