Skip to content

Commit

Permalink
Add ignoreInterpolations util to fourslash for fuzzy diagnostic mat…
Browse files Browse the repository at this point in the history
…ching (#35652)

* Add ignoreInterpolations util to fourslash for fuzzy diagnostic matching

* Simplify

* It’s not Swift

* Fix regexp

* Remove unnecessary type assertion
  • Loading branch information
andrewbranch authored Dec 17, 2019
1 parent 53c8b95 commit c73af61
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ namespace FourSlash {
throw new Error("Operation should be cancelled");
}

export function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations {
return { template: typeof diagnostic === "string" ? diagnostic : diagnostic.message };
}

// This function creates IScriptSnapshot object for testing getPreProcessedFileInfo
// Return object may lack some functionalities for other purposes.
function createScriptSnapShot(sourceText: string): ts.IScriptSnapshot {
Expand Down Expand Up @@ -2483,7 +2487,12 @@ namespace FourSlash {

const action = actions[index];

assert.equal(action.description, options.description);
if (typeof options.description === "string") {
assert.equal(action.description, options.description);
}
else {
assert.match(action.description, templateToRegExp(options.description.template));
}
assert.deepEqual(action.commands, options.commands);

if (options.applyChanges) {
Expand Down Expand Up @@ -3829,4 +3838,8 @@ ${code}
const actualString = quoted ? "\"" + actual + "\"" : actual;
return `\n${expectMsg}:\n${expectedString}\n\n${actualMsg}:\n${actualString}`;
}

function templateToRegExp(template: string) {
return new RegExp(`^${ts.regExpEscape(template).replace(/\\\{\d+\\\}/g, ".*?")}$`);
}
}
5 changes: 4 additions & 1 deletion src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ namespace FourSlashInterface {
}

export interface VerifyCodeFixOptions extends NewContentOptions {
readonly description: string;
readonly description: string | DiagnosticIgnoredInterpolations;
readonly errorCode?: number;
readonly index?: number;
readonly preferences?: ts.UserPreferences;
Expand Down Expand Up @@ -1605,5 +1605,8 @@ namespace FourSlashInterface {
readonly ranges: readonly RenameLocationOptions[];
readonly providePrefixAndSuffixTextForRename?: boolean;
};
export interface DiagnosticIgnoredInterpolations {
template: string
};
export type RenameLocationOptions = FourSlash.Range | { readonly range: FourSlash.Range, readonly prefixText?: string, readonly suffixText?: string };
}
6 changes: 3 additions & 3 deletions tests/cases/fourslash/codeFixInPropertyAccess_js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//// return false;
//// }

verify.codeFixAll({
fixId: "correctQualifiedNameToIndexedAccessType",
fixAllDescription: "Rewrite all as indexed access types",
verify.codeFix({
index: 0,
description: ignoreInterpolations(ts.Diagnostics.Rewrite_as_the_indexed_access_type_0),
newFileContent:
`/**
* @typedef Foo
Expand Down
5 changes: 4 additions & 1 deletion tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ declare namespace FourSlashInterface {
jsxClosingTag(map: { [markerName: string]: { readonly newText: string } | undefined }): void;
isInCommentAtPosition(onlyMultiLineDiverges?: boolean): void;
codeFix(options: {
description: string,
description: string | DiagnosticIgnoredInterpolations,
newFileContent?: NewFileContent,
newRangeContent?: string,
errorCode?: number,
Expand Down Expand Up @@ -720,7 +720,10 @@ declare namespace FourSlashInterface {
readonly providePrefixAndSuffixTextForRename?: boolean;
};
type RenameLocationOptions = Range | { readonly range: Range, readonly prefixText?: string, readonly suffixText?: string };
type DiagnosticIgnoredInterpolations = { template: string }
}
/** Wraps a diagnostic message to be compared ignoring interpolated strings */
declare function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations;
declare function verifyOperationIsCancelled(f: any): void;
declare var test: FourSlashInterface.test_;
declare var plugins: FourSlashInterface.plugins;
Expand Down

0 comments on commit c73af61

Please sign in to comment.