Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 167 additions & 6 deletions internal/fourslash/_scripts/convertFourslash.mts
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,30 @@ function parseFourslashStatement(statement: ts.Statement): Cmd[] | undefined {
}
const namespace = callExpression.expression.expression;
const func = callExpression.expression.name;
if (!ts.isIdentifier(namespace) || !ts.isIdentifier(func)) {
if (!(ts.isIdentifier(namespace) || namespace.getText() === "verify.not") || !ts.isIdentifier(func)) {
console.error(`Expected identifiers for namespace and function, got ${namespace.getText()} and ${func.getText()}`);
return undefined;
}
if (!ts.isIdentifier(namespace)) {
switch (func.text) {
case "quickInfoExists":
return parseQuickInfoArgs("notQuickInfoExists", callExpression.arguments);
}
console.error(`Unrecognized fourslash statement: ${statement.getText()}`);
return undefined;
}
// `verify.(...)`
if (namespace.text === "verify") {
switch (func.text) {
case "completions":
// `verify.completions(...)`
return parseVerifyCompletionsArgs(callExpression.arguments);
case "quickInfoAt":
case "quickInfoExists":
case "quickInfoIs":
case "quickInfos":
// `verify.quickInfo...(...)`
return parseQuickInfoArgs(func.text, callExpression.arguments);
case "baselineFindAllReferences":
// `verify.baselineFindAllReferences(...)`
return [parseBaselineFindAllReferencesArgs(callExpression.arguments)];
Expand Down Expand Up @@ -721,6 +735,127 @@ function parseBaselineQuickInfo(args: ts.NodeArray<ts.Expression>): Cmd {
};
}

function parseQuickInfoArgs(funcName: string, args: readonly ts.Expression[]): VerifyQuickInfoCmd[] | undefined {
// We currently don't support 'expectedTags'.
switch (funcName) {
case "quickInfoAt": {
if (args.length < 1 || args.length > 3) {
console.error(`Expected 1 or 2 arguments in quickInfoIs, got ${args.map(arg => arg.getText()).join(", ")}`);
return undefined;
}
if (!ts.isStringLiteralLike(args[0])) {
console.error(`Expected string literal for first argument in quickInfoAt, got ${args[0].getText()}`);
return undefined;
}
const marker = getGoStringLiteral(args[0].text);
let text: string | undefined;
if (args[1]) {
if (!ts.isStringLiteralLike(args[1])) {
console.error(`Expected string literal for second argument in quickInfoAt, got ${args[1].getText()}`);
return undefined;
}
text = getGoStringLiteral(args[1].text);
}
let docs: string | undefined;
if (args[2]) {
if (!ts.isStringLiteralLike(args[2]) && args[2].getText() !== "undefined") {
console.error(`Expected string literal or undefined for third argument in quickInfoAt, got ${args[2].getText()}`);
return undefined;
}
if (ts.isStringLiteralLike(args[2])) {
docs = getGoStringLiteral(args[1].text);
}
}
return [{
kind: "quickInfoAt",
marker,
text,
docs,
}];
}
case "quickInfos": {
const cmds: VerifyQuickInfoCmd[] = [];
if (args.length !== 1 || !ts.isObjectLiteralExpression(args[0])) {
console.error(`Expected a single object literal argument in quickInfos, got ${args.map(arg => arg.getText()).join(", ")}`);
return undefined;
}
for (const prop of args[0].properties) {
if (!ts.isPropertyAssignment(prop)) {
console.error(`Expected property assignment in quickInfos, got ${prop.getText()}`);
return undefined;
}
if (!(ts.isIdentifier(prop.name) || ts.isStringLiteralLike(prop.name) || ts.isNumericLiteral(prop.name))) {
console.error(`Expected identifier or literal for property name in quickInfos, got ${prop.name.getText()}`);
return undefined;
}
const marker = getGoStringLiteral(prop.name.text);
let text: string | undefined;
let docs: string | undefined;
if (ts.isArrayLiteralExpression(prop.initializer)) {
if (prop.initializer.elements.length !== 2) {
console.error(`Expected two elements in array literal for quickInfos property, got ${prop.initializer.getText()}`);
return undefined;
}
if (!ts.isStringLiteralLike(prop.initializer.elements[0]) || !ts.isStringLiteralLike(prop.initializer.elements[1])) {
console.error(`Expected string literals in array literal for quickInfos property, got ${prop.initializer.getText()}`);
return undefined;
}
text = getGoStringLiteral(prop.initializer.elements[0].text);
docs = getGoStringLiteral(prop.initializer.elements[1].text);
}
else if (ts.isStringLiteralLike(prop.initializer)) {
text = getGoStringLiteral(prop.initializer.text);
}
else {
console.error(`Expected string literal or array literal for quickInfos property, got ${prop.initializer.getText()}`);
return undefined;
}
cmds.push({
kind: "quickInfoAt",
marker,
text,
docs,
});
}
return cmds;
}
case "quickInfoExists":
return [{
kind: "quickInfoExists",
}];
case "notQuickInfoExists":
return [{
kind: "notQuickInfoExists",
}];
case "quickInfoIs": {
if (args.length < 1 || args.length > 2) {
console.error(`Expected 1 or 2 arguments in quickInfoIs, got ${args.map(arg => arg.getText()).join(", ")}`);
return undefined;
}
if (!ts.isStringLiteralLike(args[0])) {
console.error(`Expected string literal for first argument in quickInfoIs, got ${args[0].getText()}`);
return undefined;
}
const text = getGoStringLiteral(args[0].text);
let docs: string | undefined;
if (args[1]) {
if (!ts.isStringLiteralLike(args[1])) {
console.error(`Expected string literal for second argument in quickInfoIs, got ${args[1].getText()}`);
return undefined;
}
docs = getGoStringLiteral(args[1].text);
}
return [{
kind: "quickInfoIs",
text,
docs,
}];
}
}
console.error(`Unrecognized quick info function: ${funcName}`);
return undefined;
}

function parseKind(expr: ts.Expression): string | undefined {
if (!ts.isStringLiteral(expr)) {
console.error(`Expected string literal for kind, got ${expr.getText()}`);
Expand Down Expand Up @@ -884,13 +1019,21 @@ interface EditCmd {
goStatement: string;
}

interface VerifyQuickInfoCmd {
kind: "quickInfoIs" | "quickInfoAt" | "quickInfoExists" | "notQuickInfoExists";
marker?: string;
text?: string;
docs?: string;
}

type Cmd =
| VerifyCompletionsCmd
| VerifyBaselineFindAllReferencesCmd
| VerifyBaselineGoToDefinitionCmd
| VerifyBaselineQuickInfoCmd
| GoToCmd
| EditCmd;
| EditCmd
| VerifyQuickInfoCmd;

function generateVerifyCompletions({ marker, args, isNewIdentifierLocation }: VerifyCompletionsCmd): string {
let expectedList: string;
Expand Down Expand Up @@ -938,21 +1081,39 @@ function generateGoToCommand({ funcName, args }: GoToCmd): string {
return `f.GoTo${funcNameCapitalized}(t, ${args.join(", ")})`;
}

function generateQuickInfoCommand({ kind, marker, text, docs }: VerifyQuickInfoCmd): string {
switch (kind) {
case "quickInfoIs":
return `f.VerifyQuickInfoIs(t, ${text!}, ${docs ? docs : `""`})`;
case "quickInfoAt":
return `f.VerifyQuickInfoAt(t, ${marker!}, ${text ? text : `""`}, ${docs ? docs : `""`})`;
case "quickInfoExists":
return `f.VerifyQuickInfoExists(t)`;
case "notQuickInfoExists":
return `f.VerifyNotQuickInfoExists(t)`;
}
}

function generateCmd(cmd: Cmd): string {
switch (cmd.kind) {
case "verifyCompletions":
return generateVerifyCompletions(cmd as VerifyCompletionsCmd);
return generateVerifyCompletions(cmd);
case "verifyBaselineFindAllReferences":
return generateBaselineFindAllReferences(cmd as VerifyBaselineFindAllReferencesCmd);
return generateBaselineFindAllReferences(cmd);
case "verifyBaselineGoToDefinition":
return generateBaselineGoToDefinition(cmd as VerifyBaselineGoToDefinitionCmd);
return generateBaselineGoToDefinition(cmd);
case "verifyBaselineQuickInfo":
// Quick Info -> Hover
return `f.VerifyBaselineHover(t)`;
case "goTo":
return generateGoToCommand(cmd as GoToCmd);
return generateGoToCommand(cmd);
case "edit":
return cmd.goStatement;
case "quickInfoAt":
case "quickInfoIs":
case "quickInfoExists":
case "notQuickInfoExists":
return generateQuickInfoCommand(cmd);
default:
let neverCommand: never = cmd;
throw new Error(`Unknown command kind: ${neverCommand as Cmd["kind"]}`);
Expand Down
Loading
Loading