Allow autoImportFileExcludePatterns to match anywhere in the file path (#57090)

This commit is contained in:
Andrew Branch 2024-01-19 12:40:47 -08:00 committed by GitHub
parent 6cb6fc94ae
commit 9ba8c7a51c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 3229 additions and 4 deletions

View File

@ -9140,7 +9140,8 @@ export const commonPackageFolders: readonly string[] = ["node_modules", "bower_c
const implicitExcludePathRegexPattern = `(?!(${commonPackageFolders.join("|")})(/|$))`;
interface WildcardMatcher {
/** @internal */
export interface WildcardMatcher {
singleAsteriskRegexFragment: string;
doubleAsteriskRegexFragment: string;
replaceWildcardCharacter: (match: string) => string;
@ -9222,7 +9223,13 @@ export function getPatternFromSpec(spec: string, basePath: string, usage: "files
return pattern && `^(${pattern})${usage === "exclude" ? "($|/)" : "$"}`;
}
function getSubPatternFromSpec(spec: string, basePath: string, usage: "files" | "directories" | "exclude", { singleAsteriskRegexFragment, doubleAsteriskRegexFragment, replaceWildcardCharacter }: WildcardMatcher): string | undefined {
/** @internal */
export function getSubPatternFromSpec(
spec: string,
basePath: string,
usage: "files" | "directories" | "exclude",
{ singleAsteriskRegexFragment, doubleAsteriskRegexFragment, replaceWildcardCharacter }: WildcardMatcher = wildcardMatchers[usage],
): string | undefined {
let subpattern = "";
let hasWrittenComponent = false;
const components = getNormalizedPathComponents(spec, basePath);

View File

@ -21,8 +21,8 @@ import {
getNamesForExportedSymbol,
getNodeModulePathParts,
getPackageNameFromTypesPackageName,
getPatternFromSpec,
getRegexFromPattern,
getSubPatternFromSpec,
getSymbolId,
hostGetCanonicalFileName,
hostUsesCaseSensitiveFileNames,
@ -425,7 +425,7 @@ export function forEachExternalModuleToImportFrom(
const excludePatterns = preferences.autoImportFileExcludePatterns && mapDefined(preferences.autoImportFileExcludePatterns, spec => {
// The client is expected to send rooted path specs since we don't know
// what directory a relative path is relative to.
const pattern = getPatternFromSpec(spec, "", "exclude");
const pattern = getSubPatternFromSpec(spec, "", "exclude");
return pattern ? getRegexFromPattern(pattern, useCaseSensitiveFileNames) : undefined;
});

View File

@ -0,0 +1,31 @@
/// <reference path="../fourslash.ts"/>
// @module: commonjs
// @Filename: /project/node_modules/aws-sdk/package.json
//// { "name": "aws-sdk", "version": "2.0.0", "main": "index.js" }
// @Filename: /project/node_modules/aws-sdk/index.d.ts
//// export * from "./clients/s3";
// @Filename: /project/node_modules/aws-sdk/clients/s3.d.ts
//// export declare class S3 {}
// @Filename: /project/package.json
//// { "dependencies": { "aws-sdk": "*" } }
// @Filename: /project/index.ts
//// S3/**/
const autoImportFileExcludePatterns = ["**/node_modules/aws-sdk"];
verify.completions({
marker: "",
excludes: "S3",
preferences: {
includeCompletionsForModuleExports: true,
autoImportFileExcludePatterns,
}
});
verify.importFixAtPosition([], /*errorCode*/ undefined, { autoImportFileExcludePatterns });

View File

@ -0,0 +1,31 @@
/// <reference path="../fourslash.ts"/>
// @module: commonjs
// @Filename: //tsclient/project/node_modules/aws-sdk/package.json
//// { "name": "aws-sdk", "version": "2.0.0", "main": "index.js" }
// @Filename: //tsclient/project/node_modules/aws-sdk/index.d.ts
//// export * from "./clients/s3";
// @Filename: //tsclient/project/node_modules/aws-sdk/clients/s3.d.ts
//// export declare class S3 {}
// @Filename: //tsclient/project/package.json
//// { "dependencies": { "aws-sdk": "*" } }
// @Filename: //tsclient/project/index.ts
//// S3/**/
const autoImportFileExcludePatterns = ["\\\\tsclient\\project\\node_modules\\aws-sdk"];
verify.completions({
marker: "",
excludes: "S3",
preferences: {
includeCompletionsForModuleExports: true,
autoImportFileExcludePatterns,
}
});
verify.importFixAtPosition([], /*errorCode*/ undefined, { autoImportFileExcludePatterns });

View File

@ -0,0 +1,31 @@
/// <reference path="../fourslash.ts"/>
// @module: commonjs
// @Filename: c:/project/node_modules/aws-sdk/package.json
//// { "name": "aws-sdk", "version": "2.0.0", "main": "index.js" }
// @Filename: c:/project/node_modules/aws-sdk/index.d.ts
//// export * from "./clients/s3";
// @Filename: c:/project/node_modules/aws-sdk/clients/s3.d.ts
//// export declare class S3 {}
// @Filename: c:/project/package.json
//// { "dependencies": { "aws-sdk": "*" } }
// @Filename: c:/project/index.ts
//// S3/**/
const autoImportFileExcludePatterns = ["**\\node_modules\\aws-sdk"];
verify.completions({
marker: "",
excludes: "S3",
preferences: {
includeCompletionsForModuleExports: true,
autoImportFileExcludePatterns,
}
});
verify.importFixAtPosition([], /*errorCode*/ undefined, { autoImportFileExcludePatterns });