Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Mar 6, 2023
1 parent 2f5207d commit 3f9ba2f
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 61 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ tools/oav-traffic-converter/build/
src/swift/*.xcworkspace/xcuserdata/
src/swift/**/*.xcodeproj/xcuserdata/

# Cadl APIView
cadl-output/
tools/apiview/emitters/cadl-apiview/temp/
# TypeSpec APIView
tools/apiview/emitters/cadl-apiview/cadl-output/
tools/apiview/emitters/cadl-apiview/temp/
tools/apiview/emitters/typespec-apiview/tsp-output/
tools/apiview/emitters/typespec-apiview/temp/
1 change: 1 addition & 0 deletions tools/apiview/emitters/typespec-apiview/cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ words:
- vswhere
- westus
- xplat
- deindent
ignorePaths:
- "**/node_modules/**"
- "**/dist/**"
Expand Down
3 changes: 2 additions & 1 deletion tools/apiview/emitters/typespec-apiview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
]
}
},
"typespecMain": "dist/src/index.js",
"tspMain": "dist/src/index.js",
"engines": {
"node": ">=16.0.0"
},
Expand All @@ -57,6 +57,7 @@
"@azure-tools/typespec-azure-core": "0.27.0",
"@azure-tools/typespec-autorest": "0.27.0",
"@typespec/compiler": "0.41.0",
"@typespec/http": "0.41.0",
"@typespec/rest": "0.41.0",
"@typespec/versioning": "0.41.0"
},
Expand Down
6 changes: 3 additions & 3 deletions tools/apiview/emitters/typespec-apiview/src/apiview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export class ApiView {
case SyntaxKind.BlockComment:
throw new Error(`Case "BlockComment" not implemented`);
case SyntaxKind.TypeSpecScript:
throw new Error(`Case "CadlScript" not implemented`);
throw new Error(`Case "TypeSpecScript" not implemented`);
case SyntaxKind.DecoratorExpression:
obj = node as DecoratorExpressionNode;
this.punctuation("@", false, false);
Expand Down Expand Up @@ -997,15 +997,15 @@ export class ApiView {
}
}

private renderPunctuation(punc: string) {
private renderPunctuation(punctuation: string) {
const last = this.tokens.pop()!;
if (last?.Kind === ApiViewTokenKind.Whitespace) {
// hacky workaround to ensure comma is after trailing bracket for expanded anonymous models
this.tokens.pop();
} else {
this.tokens.push(last);
}
this.punctuation(punc, false, true);
this.punctuation(punctuation, false, true);
}

resolveMissingTypeReferences() {
Expand Down
18 changes: 9 additions & 9 deletions tools/apiview/emitters/typespec-apiview/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ export function resolveOptions(context: EmitContext<ApiViewEmitterOptions>): Res
function resolveNamespaceString(namespace: Namespace): string | undefined {
// FIXME: Fix this wonky workaround when getNamespaceString is fixed.
const value = getNamespaceFullName(namespace);
return value == "" ? undefined : value;
return value === "" ? undefined : value;
}

// TODO: Up-level this logic?
function resolveAllowedVersions(program: Program, service: Service): string[] {
const allowed: string[] = [];
const serviceVersion = service.version;
const versions = getVersion(program, service.type)?.getVersions();
if (serviceVersion != undefined && versions != undefined) {
if (serviceVersion !== undefined && versions !== undefined) {
throw new Error("Cannot have serviceVersion with multi-API.");
}
if (serviceVersion != undefined) {
if (serviceVersion !== undefined) {
allowed.push(serviceVersion);
} else if (versions != undefined) {
} else if (versions !== undefined) {
for (const item of versions) {
allowed.push(item.name);
}
Expand All @@ -71,7 +71,7 @@ function resolveAllowedVersions(program: Program, service: Service): string[] {
function resolveVersionValue(program: Program, namespace: Namespace, version: string): string {
try {
const versions = getVersion(program, namespace)!.getVersions();
return versions.filter((item) => item.name == version).map((item) => item.value)[0];
return versions.filter((item) => item.name === version).map((item) => item.value)[0];
} catch {
return version;
}
Expand All @@ -82,8 +82,8 @@ function resolveProgramForVersion(program: Program, namespace: Namespace, versio
return program;
}
const version = resolveVersionValue(program, namespace, versionKey);
const projections = buildVersionProjections(program, namespace).filter((item) => item.version == version);
if (projections.length == 0) {
const projections = buildVersionProjections(program, namespace).filter((item) => item.version === version);
if (projections.length === 0) {
// non-multi-version scenario. Return original program.
return program;
} else {
Expand Down Expand Up @@ -154,7 +154,7 @@ function createApiViewEmitter(program: Program, options: ResolvedApiViewEmitterO
return;
}
// applies the default "apiview.json" filename if not provided and there's only a single service
if (services.length == 1) {
if (services.length === 1) {
options.outputFile = options.outputFile ?? "apiview.json"
}
validateMultiServiceOptions(program, services, options);
Expand All @@ -166,7 +166,7 @@ function createApiViewEmitter(program: Program, options: ResolvedApiViewEmitterO
const serviceTitle = service.title ? service.title : namespaceString;
const allowedVersions = resolveAllowedVersions(program, service);
if (versionString) {
if (allowedVersions.filter((version) => version == versionString).length == 0) {
if (allowedVersions.filter((version) => version === versionString).length === 0) {
reportDiagnostic(program, {
code: "version-not-found",
target: NoTarget,
Expand Down
4 changes: 2 additions & 2 deletions tools/apiview/emitters/typespec-apiview/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createCadlLibrary, JSONSchemaType, paramMessage } from "@typespec/compiler";
import { createTypeSpecLibrary, JSONSchemaType, paramMessage } from "@typespec/compiler";

export interface ApiViewEmitterOptions {
"output-file"?: string;
Expand All @@ -20,7 +20,7 @@ const ApiViewEmitterOptionsSchema: JSONSchemaType<ApiViewEmitterOptions> = {
};


export const $lib = createCadlLibrary({
export const $lib = createTypeSpecLibrary({
name: "@azure-tools/typespec-apiview",
diagnostics: {
"no-services-found": {
Expand Down
35 changes: 7 additions & 28 deletions tools/apiview/emitters/typespec-apiview/src/namespace-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export class NamespaceModel {

// Gather models and resources
for (const [modelName, model] of ns.models) {
if (model.node != undefined) {
if (model.node !== undefined) {
let isResource = false;
for (const dec of model.decorators) {
if (dec.decorator.name == "$resource") {
if (dec.decorator.name === "$resource") {
isResource = true;
break;
}
Expand Down Expand Up @@ -156,7 +156,7 @@ function inNamespace(node: Node, program: Program, namespace: Namespace): boolea

export function generateId(obj: BaseNode | NamespaceModel | undefined): string | undefined {
let node;
if (obj == undefined) {
if (obj === undefined) {
return undefined;
}
if (obj instanceof NamespaceModel) {
Expand All @@ -181,14 +181,7 @@ export function generateId(obj: BaseNode | NamespaceModel | undefined): string |
break;
case SyntaxKind.EnumMember:
node = obj as EnumMemberNode;
switch (node.id.kind) {
case SyntaxKind.Identifier:
name = node.id.sv;
break;
case SyntaxKind.StringLiteral:
name = node.id.value;
break;
}
name = node.id.sv;
parentId = generateId(node.parent);
break;
case SyntaxKind.EnumSpreadMember:
Expand All @@ -214,14 +207,7 @@ export function generateId(obj: BaseNode | NamespaceModel | undefined): string |
break;
case SyntaxKind.ModelProperty:
node = obj as ModelPropertyNode;
switch (node.id.kind) {
case SyntaxKind.Identifier:
name = node.id.sv;
break;
case SyntaxKind.StringLiteral:
name = node.id.value;
break;
}
name = node.id.sv;
parentId = generateId(node.parent);
break;
case SyntaxKind.ModelSpreadProperty:
Expand Down Expand Up @@ -253,20 +239,13 @@ export function generateId(obj: BaseNode | NamespaceModel | undefined): string |
break;
case SyntaxKind.UnionVariant:
node = obj as UnionVariantNode;
switch (node.id.kind) {
case SyntaxKind.Identifier:
name = node.id.sv;
break;
case SyntaxKind.StringLiteral:
name = node.id.value;
break;
}
name = node.id.sv;
parentId = generateId(node.parent);
break;
default:
return undefined;
}
if (parentId != undefined) {
if (parentId !== undefined) {
return `${parentId}.${name}`;
} else {
return name;
Expand Down
4 changes: 2 additions & 2 deletions tools/apiview/emitters/typespec-apiview/src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolvePath } from "@typespec/compiler";
import { CadlTestLibrary } from "@typespec/compiler/testing";
import { TypeSpecTestLibrary } from "@typespec/compiler/testing";
import { fileURLToPath } from "url";

export const ApiViewTestLibrary: CadlTestLibrary = {
export const ApiViewTestLibrary: TypeSpecTestLibrary = {
name: "@azure-tools/typespec-apiview",
packageRoot: resolvePath(fileURLToPath(import.meta.url), "../../../../"),
files: [
Expand Down
2 changes: 1 addition & 1 deletion tools/apiview/emitters/typespec-apiview/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const LIB_VERSION = "0.3.5";
export const LIB_VERSION = "0.4.0";
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Diagnostic, logDiagnostics, resolvePath } from "@typespec/compiler";
import { expectDiagnosticEmpty, expectDiagnostics } from "@typespec/compiler/testing";
import { strictEqual } from "assert";
import { apiViewFor, apiViewText, compare, createApiViewTestRunner, diagnosticsFor } from "./test-host.js";
import { expectDiagnostics } from "@typespec/compiler/testing";
import { apiViewFor, apiViewText, compare, diagnosticsFor } from "./test-host.js";

describe("apiview-options: tests", () => {

Expand Down
10 changes: 5 additions & 5 deletions tools/apiview/emitters/typespec-apiview/test/apiview.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert, { fail, strictEqual } from "assert";
import assert, { fail } from "assert";
import { ApiViewDocument, ApiViewTokenKind } from "../src/apiview.js";
import { apiViewFor, apiViewText, compare } from "./test-host.js";

Expand All @@ -11,25 +11,25 @@ describe("apiview: tests", () => {
defIdsPerLine[index] = new Array<string>();
for (const token of apiview.Tokens) {
// ensure that there are no repeated definition IDs.
if (token.DefinitionId != undefined) {
if (token.DefinitionId !== undefined) {
if (definitionIds.has(token.DefinitionId)) {
fail(`Duplicate defintion ID ${token.DefinitionId}.`);
}
definitionIds.add(token.DefinitionId);
}
// Collect the definition IDs that exist on each line
if (token.DefinitionId != undefined) {
if (token.DefinitionId !== undefined) {
defIdsPerLine[index].push(token.DefinitionId);
}
if (token.Kind == ApiViewTokenKind.Newline) {
if (token.Kind === ApiViewTokenKind.Newline) {
index++;
defIdsPerLine[index] = new Array<string>();
}
}
// ensure that each line has either 0 or 1 definition ID.
for (let x = 0; x < defIdsPerLine.length; x++) {
const row = defIdsPerLine[x];
assert(row.length == 0 || row.length == 1, `Too many definition IDs (${row.length}) on line ${x}`);
assert(row.length === 0 || row.length === 1, `Too many definition IDs (${row.length}) on line ${x}`);
}
}

Expand Down
7 changes: 4 additions & 3 deletions tools/apiview/emitters/typespec-apiview/test/test-host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createTestHost, createTestWrapper } from "@typespec/compiler/testing";
import { RestTestLibrary } from "@typespec/rest/testing";
import { HttpTestLibrary } from "@typespec/http/testing";
import { VersioningTestLibrary } from "@typespec/versioning/testing";
import { AzureCoreTestLibrary } from "@azure-tools/typespec-azure-core/testing";
import { ApiViewTestLibrary } from "../src/testing/index.js";
Expand All @@ -11,7 +12,7 @@ import { strictEqual } from "assert";

export async function createApiViewTestHost() {
return createTestHost({
libraries: [ApiViewTestLibrary, RestTestLibrary, VersioningTestLibrary, AzureCoreTestLibrary],
libraries: [ApiViewTestLibrary, RestTestLibrary, HttpTestLibrary, VersioningTestLibrary, AzureCoreTestLibrary],
});
}

Expand Down Expand Up @@ -67,7 +68,7 @@ export function apiViewText(apiview: ApiViewDocument): string[] {
vals.push("\n");
break;
default:
if (token.Value != undefined) {
if (token.Value !== undefined) {
vals.push(token.Value);
}
break;
Expand All @@ -90,7 +91,7 @@ function trimLines(lines: string[]): string[] {
const trimmed: string[] = [];
const indent = getIndex(lines);
for (const line of lines) {
if (line.trim() == '') {
if (line.trim() === '') {
// skip blank lines
continue;
} else {
Expand Down

0 comments on commit 3f9ba2f

Please sign in to comment.