Skip to content

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

Closed
wants to merge 10 commits into from
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(
Expand Down Expand Up @@ -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__')}`,
Copy link
Member

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:

If typeRoots is specified, only packages under typeRoots will be included.

]);
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})/`,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.typescriptlang.org/tsconfig/#typeRoots - if someone customizes this, then fileNameMatcher will be wrong. This'll need to reflect the project's compilerOptions.typeRoots.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
);
});
Expand Down
13 changes: 12 additions & 1 deletion packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parseForESLint } from '@typescript-eslint/parser';
import Ajv from 'ajv';
import path from 'node:path';

import type { TypeOrValueSpecifier } from '../src/TypeOrValueSpecifier';
import type { TypeOrValueSpecifier } from '../src';

import { typeMatchesSpecifier, typeOrValueSpecifiersSchema } from '../src';

Expand Down Expand Up @@ -366,6 +366,17 @@ describe('TypeOrValueSpecifier', () => {
package: 'node:test',
},
],
[
`
import { Buffer } from 'node:buffer';
type Test = Buffer;
`,
{ from: 'package', name: 'Buffer', package: 'node' },
],
[
'type Test = Buffer;',
{ from: 'package', name: 'Buffer', package: 'node' },
],
])('matches a matching package specifier: %s', runTestPositive);

it.each<[string, TypeOrValueSpecifier]>([
Expand Down
Loading