Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(type-utils): allowFileSpecifier
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import path from 'path'; | ||
import type * as ts from 'typescript'; | ||
type FileSpecifier = { | ||
from: 'file'; | ||
name: string | string[]; | ||
} & ( | ||
| { | ||
path?: string; | ||
excludePaths?: undefined; | ||
} | ||
| { | ||
path?: undefined; | ||
excludePaths?: string | string[]; // defaults to `["node_modules"]`. | ||
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. I also wonder if we should call this | ||
} | ||
); | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. 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. Starting a new conversation here: why make {path:"examples",excludePaths:["examples/*/node_modules"]} Proposal: can we switch this back to a straightforward interface? interfaceFileSpecifier{from:'file';name:string|string[];path?:string;excludePaths?:string|string[];// defaults to `["node_modules"]`.} | ||
interface LibSpecifier { | ||
from: 'lib'; | ||
@@ -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(); | ||
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) | ||
JoshuaKGoldberg marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
@@ -164,7 +184,12 @@ export function typeMatchesSpecifier( | ||
?.map(declaration => declaration.getSourceFile()) ?? []; | ||
switch (specifier.from) { | ||
case 'file': | ||
return typeDeclaredInFile( | ||
specifier.path, | ||
specifier.excludePaths, | ||
declarationFiles, | ||
program, | ||
); | ||
case 'lib': | ||
return declarationFiles.some(declaration => | ||
program.isSourceFileDefaultLibrary(declaration), | ||