Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not Ready] [typespec-vscode] Add code fix for json to values #5473

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CharCode } from "../charcode.js";
import { defineCodeFix, getSourceLocation } from "../diagnostics.js";
import { DiagnosticTarget } from "../types.js";

export function createJsonToValuesCodeFix(node: DiagnosticTarget) {
return defineCodeFix({
id: "json-to-values",
label: `Convert json to values`,
fix: (context) => {
const location = getSourceLocation(node);
const text = location.file.text;
let pos = location.pos;
const end = location.end;
const range: number[] = [];
while (pos < end) {
if (text.charCodeAt(pos) === CharCode.DoubleQuote) {
range.push(pos);
}
if (range.length === 2) {
break;
}
pos++;
}
const replaceText = text.slice(range[0] + 1, range[1]);
return context.replaceText({ ...location, pos: range[0], end: range[1] + 1 }, replaceText);
},
});
}
15 changes: 15 additions & 0 deletions packages/compiler/src/lib/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ import type {
WithoutDefaultValuesDecorator,
WithoutOmittedPropertiesDecorator,
} from "../../generated-defs/TypeSpec.js";
import { createJsonToValuesCodeFix } from "../core/compiler-code-fixes/json-to-values.codefix.js";
import {
getPropertyType,
isIntrinsicType,
validateDecoratorNotOnType,
} from "../core/decorator-utils.js";
import { getDeprecationDetails, markDeprecated } from "../core/deprecation.js";
import {
NoTarget,
Numeric,
StdTypeName,
compilerAssert,
Expand Down Expand Up @@ -98,6 +100,7 @@ import {
UnionVariant,
Value,
} from "../core/types.js";
import { mutate } from "../utils/misc.js";
import { setKey } from "./key.js";
import { useStateMap, useStateSet } from "./utils.js";

Expand Down Expand Up @@ -1396,6 +1399,18 @@ function checkExampleValid(
diagnosticTarget,
);
if (!assignable) {
// add a codefix to convert json to values , issue #4612
for (const diagnostic of diagnostics) {
if (
diagnostic.severity === "error" &&
diagnostic.target !== NoTarget &&
diagnostic.code === "missing-property"
) {
mutate(diagnostic).codefixes ??= [];
mutate(diagnostic.codefixes).push(createJsonToValuesCodeFix(diagnostic.target));
}
}

program.reportDiagnostics(diagnostics);
}
return assignable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { strictEqual } from "assert";
import { describe, it } from "vitest";
import { createJsonToValuesCodeFix } from "../../../src/core/compiler-code-fixes/json-to-values.codefix.js";
import { SyntaxKind } from "../../../src/index.js";
import { expectCodeFixOnAst } from "../../../src/testing/code-fix-testing.js";

describe("CodeFix: convert-json-to-value", () => {
it("it's missing properties, remove the quotes around property names that contain quotes", async () => {
await expectCodeFixOnAst(
`
@example(┆#{"Baz": "Hello"})
model FooBar { Baz : string; }
`,
(node) => {
strictEqual(node.kind, SyntaxKind.ObjectLiteral);
return createJsonToValuesCodeFix(node);
},
).toChangeTo(`
@example(#{Baz: "Hello"})
model FooBar { Baz : string; }
`);
});
});
Loading