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
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
*.tests.js
*.tests.js.map
*.tests.d.ts
*.spec.js
*.spec.js.map
*.spec.d.ts
src
**/snapshot
**/spec
/*.*
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@types/jest": "^30.0.0",
"@types/node": "^24.3.0",
"@types/pluralize": "^0.0.33",
"@typescript-eslint/eslint-plugin": "^8.41.0",
"@typescript-eslint/parser": "^8.41.0",
"@typespec/http-server-js": "^0.58.0-alpha.18",
Expand All @@ -61,6 +62,7 @@
"@typespec/compiler": "^1.3.0",
"@typespec/http": "^1.3.0",
"@typespec/rest": "^0.73.0",
"basketry": "^0.2.1"
"basketry": "^0.2.1",
"pluralize": "^8.0.0"
}
}
15 changes: 13 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import casePkg from 'case';
import pluralizePkg from 'pluralize';

import * as TSP from '@typespec/compiler';
import { DocNode } from '@typespec/compiler/ast';
Expand All @@ -12,6 +13,7 @@ import pkg from '../package.json' with { type: 'json' };
import { decodeRange } from 'basketry';

const { camel, snake } = casePkg;
const { singular } = pluralizePkg;

export class TypespecParser {
public static async create(
Expand Down Expand Up @@ -173,9 +175,17 @@ export class TypespecParser {
}

private parseInterface(int: TSP.Interface): IR.Interface {
const name = this.parseName(int);

const singularName = {
kind: name.kind,
value: singular(name.value),
loc: name.loc,
};

return {
kind: 'Interface',
name: this.parseName(int),
name: singularName,
description: this.parseDescription(int.node?.docs),
deprecated: undefined, // TODO: parse interface deprecated
methods: this.parseMethods(int),
Expand Down Expand Up @@ -526,6 +536,7 @@ export class TypespecParser {
kind: 'ComplexValue',
typeName: t.name,
isArray: options?.asArray ? trueLiteral() : undefined,
isOptional: options?.isOptional ? trueLiteral(loc) : undefined,
rules: [],
};
case 'ModelProperty':
Expand Down Expand Up @@ -835,7 +846,7 @@ export class TypespecParser {
return {
kind: 'Property',
name: this.parseName(prop),
description: undefined, // TODO: handle description
description: this.parseDescription(prop.node?.docs),
value: this.parseMemberValue(prop.type, {
isOptional: prop.optional,
default: this.parseDefaultValue(prop.defaultValue),
Expand Down
28 changes: 28 additions & 0 deletions src/spec/4.1-structure/4.1.10-property.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expectDefined, parse } from '../test-utils.js';

describe('4.1.10 Property', () => {
describe('kind', () => {
it('parses "Property"', async () => {
// ARRANGE
const tsp = `
import "@typespec/http";

@service
namespace Petstore;

model Widget {
id: string;
}
`;

// ACT
const { service, violations } = await parse(tsp);

// ASSERT
expect(violations).toHaveLength(0);
const method = service?.types[0]?.properties[0];
expectDefined(method);
expect(method.kind).toBe('Property');
});
});
});
37 changes: 37 additions & 0 deletions src/spec/4.1-structure/4.1.13-primitive-value.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,43 @@ describe('4.1.13 PrimitiveValue', () => {
expectDefined(memberValue.isOptional);
expect(memberValue.isOptional.value).toBe(true);
});

it('parses a TrueLiteral for optional complex values', async () => {
// ARRANGE
const tsp = `
import "@typespec/http";

@service
namespace Petstore;

model Gizmo {
foo: string;
}

model Widget {
gizmo?: Gizmo;
}

interface Widgets {
create(widget: Widget): string;
}
`;

// ACT
const { service, violations } = await parse(tsp);

// ASSERT
expect(violations).toHaveLength(0);
const widgetType = service?.types.find(
(t) => t.name.value === 'Widget',
);
expectDefined(widgetType);

const memberValue = widgetType.properties[0]?.value;
expectDefined(memberValue);
expectDefined(memberValue.isOptional);
expect(memberValue.isOptional.value).toBe(true);
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/spec/4.1-structure/4.1.2-interface.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('4.1.2 Interface', () => {
});

describe('name', () => {
it('parses interface name', async () => {
it('parses interface name as singular', async () => {
// ARRANGE
const tsp = `
import "@typespec/http";
Expand All @@ -43,7 +43,7 @@ describe('4.1.2 Interface', () => {
expect(violations).toHaveLength(0);
const int = service?.interfaces[0];
expectDefined(int);
expect(int.name.value).toBe('Widgets');
expect(int.name.value).toBe('Widget');
expect(int.name.loc).toBeDefined();
});
});
Expand Down