Skip to content

Commit f479de6

Browse files
author
Andy
authored
patternMatcher: Just return 'undefined' for an invalid pattern (#23237)
* patternMatcher: Just return 'undefined' for an invalid pattern * Fix tests
1 parent 22919d5 commit f479de6

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

src/harness/unittests/services/patternMatcher.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,11 @@ describe("PatternMatcher", () => {
343343
});
344344

345345
it("BlankPattern", () => {
346-
const matches = getAllMatches("AddMetadataReference", "");
347-
348-
assert.isTrue(matches === undefined);
346+
assertInvalidPattern("");
349347
});
350348

351349
it("WhitespaceOnlyPattern", () => {
352-
const matches = getAllMatches("AddMetadataReference", " ");
353-
354-
assert.isTrue(matches === undefined);
350+
assertInvalidPattern(" ");
355351
});
356352

357353
it("EachWordSeparately1", () => {
@@ -449,6 +445,10 @@ describe("PatternMatcher", () => {
449445
});
450446
});
451447

448+
function assertInvalidPattern(pattern: string) {
449+
assert.equal(ts.createPatternMatcher(pattern), undefined);
450+
}
451+
452452
function getFirstMatch(candidate: string, pattern: string): ts.PatternMatch {
453453
const matches = ts.createPatternMatcher(pattern).getMatchesForLastSegmentOfPattern(candidate);
454454
return matches ? matches[0] : undefined;

src/services/navigateTo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace ts.NavigateTo {
1010

1111
export function getNavigateToItems(sourceFiles: ReadonlyArray<SourceFile>, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number, excludeDtsFiles: boolean): NavigateToItem[] {
1212
const patternMatcher = createPatternMatcher(searchValue);
13+
if (!patternMatcher) return emptyArray;
1314
let rawItems: RawNavigateToItem[] = [];
1415

1516
// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]

src/services/patternMatcher.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,22 @@ namespace ts {
9797
};
9898
}
9999

100-
export function createPatternMatcher(pattern: string): PatternMatcher {
100+
export function createPatternMatcher(pattern: string): PatternMatcher | undefined {
101101
// We'll often see the same candidate string many times when searching (For example, when
102102
// we see the name of a module that is used everywhere, or the name of an overload). As
103103
// such, we cache the information we compute about the candidate for the life of this
104104
// pattern matcher so we don't have to compute it multiple times.
105105
const stringToWordSpans = createMap<TextSpan[]>();
106106

107-
pattern = pattern.trim();
108-
109-
const dotSeparatedSegments = pattern.split(".").map(p => createSegment(p.trim()));
110-
const invalidPattern = dotSeparatedSegments.length === 0 || forEach(dotSeparatedSegments, segmentIsInvalid);
107+
const dotSeparatedSegments = pattern.trim().split(".").map(p => createSegment(p.trim()));
108+
// A segment is considered invalid if we couldn't find any words in it.
109+
if (dotSeparatedSegments.some(segment => !segment.subWordTextChunks.length)) return undefined;
111110

112111
return {
113-
getMatches: (containers, candidate) => skipMatch(candidate) ? undefined : getMatches(containers, candidate, dotSeparatedSegments, stringToWordSpans),
114-
getMatchesForLastSegmentOfPattern: candidate => skipMatch(candidate) ? undefined : matchSegment(candidate, lastOrUndefined(dotSeparatedSegments), stringToWordSpans),
112+
getMatches: (containers, candidate) => getMatches(containers, candidate, dotSeparatedSegments, stringToWordSpans),
113+
getMatchesForLastSegmentOfPattern: candidate => matchSegment(candidate, last(dotSeparatedSegments), stringToWordSpans),
115114
patternContainsDots: dotSeparatedSegments.length > 1
116115
};
117-
118-
// Quick checks so we can bail out when asked to match a candidate.
119-
function skipMatch(candidate: string) {
120-
return invalidPattern || !candidate;
121-
}
122116
}
123117

124118
function getMatches(candidateContainers: ReadonlyArray<string>, candidate: string, dotSeparatedSegments: ReadonlyArray<Segment>, stringToWordSpans: Map<TextSpan[]>): PatternMatch[] | undefined {
@@ -381,11 +375,6 @@ namespace ts {
381375
};
382376
}
383377

384-
// A segment is considered invalid if we couldn't find any words in it.
385-
function segmentIsInvalid(segment: Segment) {
386-
return segment.subWordTextChunks.length === 0;
387-
}
388-
389378
function isUpperCaseLetter(ch: number) {
390379
// Fast check for the ascii range.
391380
if (ch >= CharacterCodes.A && ch <= CharacterCodes.Z) {

0 commit comments

Comments
 (0)