Skip to content

Commit c73af61

Browse files
authored
Add ignoreInterpolations util to fourslash for fuzzy diagnostic matching (#35652)
* Add ignoreInterpolations util to fourslash for fuzzy diagnostic matching * Simplify * It’s not Swift * Fix regexp * Remove unnecessary type assertion
1 parent 53c8b95 commit c73af61

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/harness/fourslashImpl.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ namespace FourSlash {
137137
throw new Error("Operation should be cancelled");
138138
}
139139

140+
export function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations {
141+
return { template: typeof diagnostic === "string" ? diagnostic : diagnostic.message };
142+
}
143+
140144
// This function creates IScriptSnapshot object for testing getPreProcessedFileInfo
141145
// Return object may lack some functionalities for other purposes.
142146
function createScriptSnapShot(sourceText: string): ts.IScriptSnapshot {
@@ -2483,7 +2487,12 @@ namespace FourSlash {
24832487

24842488
const action = actions[index];
24852489

2486-
assert.equal(action.description, options.description);
2490+
if (typeof options.description === "string") {
2491+
assert.equal(action.description, options.description);
2492+
}
2493+
else {
2494+
assert.match(action.description, templateToRegExp(options.description.template));
2495+
}
24872496
assert.deepEqual(action.commands, options.commands);
24882497

24892498
if (options.applyChanges) {
@@ -3829,4 +3838,8 @@ ${code}
38293838
const actualString = quoted ? "\"" + actual + "\"" : actual;
38303839
return `\n${expectMsg}:\n${expectedString}\n\n${actualMsg}:\n${actualString}`;
38313840
}
3841+
3842+
function templateToRegExp(template: string) {
3843+
return new RegExp(`^${ts.regExpEscape(template).replace(/\\\{\d+\\\}/g, ".*?")}$`);
3844+
}
38323845
}

src/harness/fourslashInterfaceImpl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ namespace FourSlashInterface {
15471547
}
15481548

15491549
export interface VerifyCodeFixOptions extends NewContentOptions {
1550-
readonly description: string;
1550+
readonly description: string | DiagnosticIgnoredInterpolations;
15511551
readonly errorCode?: number;
15521552
readonly index?: number;
15531553
readonly preferences?: ts.UserPreferences;
@@ -1605,5 +1605,8 @@ namespace FourSlashInterface {
16051605
readonly ranges: readonly RenameLocationOptions[];
16061606
readonly providePrefixAndSuffixTextForRename?: boolean;
16071607
};
1608+
export interface DiagnosticIgnoredInterpolations {
1609+
template: string
1610+
};
16081611
export type RenameLocationOptions = FourSlash.Range | { readonly range: FourSlash.Range, readonly prefixText?: string, readonly suffixText?: string };
16091612
}

tests/cases/fourslash/codeFixInPropertyAccess_js.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
//// return false;
1717
//// }
1818

19-
verify.codeFixAll({
20-
fixId: "correctQualifiedNameToIndexedAccessType",
21-
fixAllDescription: "Rewrite all as indexed access types",
19+
verify.codeFix({
20+
index: 0,
21+
description: ignoreInterpolations(ts.Diagnostics.Rewrite_as_the_indexed_access_type_0),
2222
newFileContent:
2323
`/**
2424
* @typedef Foo

tests/cases/fourslash/fourslash.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ declare namespace FourSlashInterface {
226226
jsxClosingTag(map: { [markerName: string]: { readonly newText: string } | undefined }): void;
227227
isInCommentAtPosition(onlyMultiLineDiverges?: boolean): void;
228228
codeFix(options: {
229-
description: string,
229+
description: string | DiagnosticIgnoredInterpolations,
230230
newFileContent?: NewFileContent,
231231
newRangeContent?: string,
232232
errorCode?: number,
@@ -720,7 +720,10 @@ declare namespace FourSlashInterface {
720720
readonly providePrefixAndSuffixTextForRename?: boolean;
721721
};
722722
type RenameLocationOptions = Range | { readonly range: Range, readonly prefixText?: string, readonly suffixText?: string };
723+
type DiagnosticIgnoredInterpolations = { template: string }
723724
}
725+
/** Wraps a diagnostic message to be compared ignoring interpolated strings */
726+
declare function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations;
724727
declare function verifyOperationIsCancelled(f: any): void;
725728
declare var test: FourSlashInterface.test_;
726729
declare var plugins: FourSlashInterface.plugins;

0 commit comments

Comments
 (0)