-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(type-utils): handle external packages that are not in program.sourceFileToPackageName
#10097
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
Changes from all commits
cddd246
615f0cf
25e5120
99a21ff
89eadc9
95f33ee
567760c
62d2bcd
c3730c4
3c2ffaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { getCanonicalFileName } from '@typescript-eslint/typescript-estree'; | ||
import path from 'node:path'; | ||
import * as ts from 'typescript'; | ||
|
||
function findParentModuleDeclaration( | ||
|
@@ -30,15 +32,36 @@ function typeDeclaredInDeclarationFile( | |
declarationFiles: ts.SourceFile[], | ||
program: ts.Program, | ||
): boolean { | ||
// Handle scoped packages: if the name starts with @, remove it and replace / with __ | ||
const typesPackageName = packageName.replace(/^@([^/]+)\//, '$1__'); | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping | ||
const escapeAlternates = (alternates: string[]): string => | ||
alternates | ||
.map(alternate => alternate.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&')) | ||
.join('|'); | ||
|
||
const matcher = new RegExp(`${packageName}|${typesPackageName}`); | ||
const packageNameRegExp = escapeAlternates([ | ||
packageName, | ||
// Handle scoped packages: if the name starts with @, remove it and replace / with __ | ||
`@types/${packageName.replace(/^@([^/]+)\//, '$1__')}`, | ||
]); | ||
const packageNameMatcher = new RegExp(packageNameRegExp); | ||
const { typeRoots } = program.getCompilerOptions(); | ||
const fileNameMatcher = new RegExp( | ||
`(?:${escapeAlternates( | ||
typeRoots | ||
? typeRoots.map(typeRoot => | ||
getCanonicalFileName( | ||
path.join(program.getCurrentDirectory(), typeRoot), | ||
), | ||
) | ||
: ['node_modules'], | ||
)})/(?:${packageNameRegExp})/`, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://www.typescriptlang.org/tsconfig/#typeRoots - if someone customizes this, then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (moved to https://github.com/typescript-eslint/typescript-eslint/pull/10097/files#r1854146022 to be closer to implementation) |
||
return declarationFiles.some(declaration => { | ||
const packageIdName = program.sourceFileToPackageName.get(declaration.path); | ||
return ( | ||
packageIdName !== undefined && | ||
matcher.test(packageIdName) && | ||
(packageIdName == null | ||
? fileNameMatcher.test(getCanonicalFileName(declaration.fileName)) | ||
: packageNameMatcher.test(packageIdName)) && | ||
program.isSourceFileFromExternalLibrary(declaration) | ||
); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Bug]
@types/
should not be hardcoded to always be considered. From https://www.typescriptlang.org/tsconfig/#typeRoots: