Skip to content

Commit 6259a65

Browse files
authored
Implement Json Schema emitter (microsoft#1601)
1 parent 452cee9 commit 6259a65

32 files changed

+1973
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@typespec/compiler",
5+
"comment": "Emitter framework: uppercase type argument type names when constructing a declaration name from a template instantiation.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@typespec/compiler"
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@typespec/json-schema",
5+
"comment": "Add @typespec/json-schema for defining and emitting TypeSpec to standard JSON Schema",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@typespec/json-schema"
10+
}

common/config/rush/pnpm-lock.yaml

+52-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compiler/emitter-framework/type-emitter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ export class TypeEmitter<T, TOptions extends object = Record<string, never>> {
670670
case "Operation":
671671
case "Enum":
672672
case "Union":
673-
return this.emitter.emitDeclarationName(t);
673+
const declName = this.emitter.emitDeclarationName(t);
674+
return declName[0].toUpperCase() + declName.slice(1);
674675
default:
675676
compilerAssert(
676677
false,

packages/compiler/test/emitter-framework/emitter.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ describe("emitter-framework: typescript emitter", () => {
120120
`);
121121

122122
assert.match(contents, /interface Test1/);
123-
assert.match(contents, /interface Templateint32/);
123+
assert.match(contents, /interface TemplateInt32/);
124124
assert.match(contents, /interface Test2/);
125-
assert.match(contents, /prop: Templateint32/);
125+
assert.match(contents, /prop: TemplateInt32/);
126126
});
127127

128128
it("emits literal types", async () => {
@@ -260,9 +260,9 @@ describe("emitter-framework: typescript emitter", () => {
260260
`);
261261

262262
assert.match(contents, /a: 1 \| 2 \| SomeModel/);
263-
assert.match(contents, /b: TUstring/);
263+
assert.match(contents, /b: TUString/);
264264
assert.match(contents, /export type U = 1 \| "hello" \| SomeModel/);
265-
assert.match(contents, /export type TUstring = string \| null/);
265+
assert.match(contents, /export type TUString = string \| null/);
266266
});
267267

268268
it("emits tuple types", async () => {

packages/compiler/test/emitter-framework/typescript-emitter.ts

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export class TypeScriptInterfaceEmitter extends CodeTypeEmitter {
110110
}
111111

112112
modelInstantiation(model: Model, name: string): EmitterOutput<string> {
113+
if (this.emitter.getProgram().checker.isStdType(model, "Record")) {
114+
const indexerValue = model.indexer!.value;
115+
return code`Record<string, ${this.emitter.emitTypeReference(indexerValue)}>`;
116+
}
113117
return this.modelDeclaration(model, name);
114118
}
115119

packages/json-schema/.eslintrc.cjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require("@typespec/eslint-config-typespec/patch/modern-module-resolution");
2+
3+
module.exports = {
4+
plugins: ["@typespec/eslint-plugin"],
5+
extends: ["@typespec/eslint-config-typespec", "plugin:@typespec/eslint-plugin/recommended"],
6+
parserOptions: { tsconfigRootDir: __dirname },
7+
};

packages/json-schema/.mocharc.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
timeout: 5000
2+
require: source-map-support/register
3+
spec: "dist/test/**/*.test.js"
4+
ignore: "dist/test/manual/**/*.js"
5+
6+
# Config for https://www.npmjs.com/package/mocha-multi-reporters
7+
reporterOptions: "configFile=mocha.reporter.config.json"

0 commit comments

Comments
 (0)