Skip to content

fix(type-utils): allow FileSpecifier to exclude paths #6860

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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import path from 'path';
import type * as ts from 'typescript';

interface FileSpecifier {
type FileSpecifier = {
from: 'file';
name: string | string[];
path?: string;
}
} & (
| {
path?: string;
excludePaths?: undefined;
}
| {
path?: undefined;
excludePaths?: string | string[]; // defaults to `["node_modules"]`.
Copy link
Member

Choose a reason for hiding this comment

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

I also wonder if we should call this pathExcludes, to visually lump it in with path... 🤔

}
);
Copy link
Member

Choose a reason for hiding this comment

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

Starting a new conversation here: why make excludePaths and path mutually exclusive? What if someone wants to provide both? E.g.:

{
  path: "examples",
  excludePaths: ["examples/*/node_modules"]
}

Proposal: can we switch this back to a straightforward interface?

interface FileSpecifier {
  from: 'file';
  name: string | string[];
  path?: string;
  excludePaths?: string | string[]; // defaults to `["node_modules"]`.
}


interface LibSpecifier {
from: 'lib';
Expand Down Expand Up @@ -129,14 +137,26 @@ function specifierNameMatches(type: ts.Type, name: string | string[]): boolean {

function typeDeclaredInFile(
relativePath: string | undefined,
excludePaths: string | string[] | undefined,
declarationFiles: ts.SourceFile[],
program: ts.Program,
): boolean {
if (relativePath === undefined) {
const cwd = program.getCurrentDirectory().toLowerCase();
return declarationFiles.some(declaration =>
declaration.fileName.toLowerCase().startsWith(cwd),
);
const excludePathsArray =
excludePaths === undefined
? ['node_modules']
: Array.isArray(excludePaths)
? excludePaths
: [excludePaths];

return declarationFiles.some(declaration => {
const fileName = declaration.fileName.toLowerCase();
return (
fileName.startsWith(cwd) &&
excludePathsArray.every(p => !fileName.startsWith(path.join(cwd, p)))
);
});
}
const absolutePath = path
.join(program.getCurrentDirectory(), relativePath)
Expand Down Expand Up @@ -164,7 +184,12 @@ export function typeMatchesSpecifier(
?.map(declaration => declaration.getSourceFile()) ?? [];
switch (specifier.from) {
case 'file':
return typeDeclaredInFile(specifier.path, declarationFiles, program);
return typeDeclaredInFile(
specifier.path,
specifier.excludePaths,
declarationFiles,
program,
);
case 'lib':
return declarationFiles.some(declaration =>
program.isSourceFileDefaultLibrary(declaration),
Expand Down