Skip to content
Draft
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
82 changes: 4 additions & 78 deletions packages/typespec-lintdiff/src/rules/get-in-operation-name.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import {
createTCGCContext,
getClientLocation,
getClientNameOverride,
type TCGCContext,
} from "@azure-tools/typespec-client-generator-core";
import {
createRule,
isGlobalNamespace,
isService,
isTemplateDeclarationOrInstance,
paramMessage,
type Interface,
type Namespace,
type Operation,
type Program,
} from "@typespec/compiler";
import { capitalize } from "@typespec/compiler/casing";
import { createRule, isTemplateDeclarationOrInstance, paramMessage } from "@typespec/compiler";
import { getHttpOperation } from "@typespec/http";
import { getOperationId } from "@typespec/openapi";
import { createAutorestOperationIdResolver } from "./utils/resolve-autorest-operation-id.js";

const validGetOperationId = /^(?:\w+_(?:Get|List)|Get|List)/;

Expand All @@ -29,9 +12,7 @@ export const getInOperationNameRule = createRule({
default: paramMessage`'GET' operation '${"operationId"}' should use method name 'Get' or method name starting with 'List'. Note: If you have already shipped an SDK on top of this spec, fixing this warning may introduce a breaking change.`,
},
create(context) {
const tcgcContext = createTCGCContext(context.program, "@azure-tools/typespec-autorest", {
mutateNamespace: false,
});
const resolveOperationId = createAutorestOperationIdResolver(context.program);

return {
operation: (operation) => {
Expand All @@ -44,7 +25,7 @@ export const getInOperationNameRule = createRule({
return;
}

const operationId = resolveAutorestOperationId(context.program, operation, tcgcContext);
const operationId = resolveOperationId(operation);
if (operationId.length === 0 || validGetOperationId.test(operationId)) {
return;
}
Expand All @@ -57,58 +38,3 @@ export const getInOperationNameRule = createRule({
};
},
});

function resolveAutorestOperationId(
program: Program,
operation: Operation,
tcgcContext: TCGCContext,
): string {
const explicitOperationId = getOperationId(program, operation);
if (explicitOperationId) {
return explicitOperationId;
}

const operationName = getClientName(tcgcContext, operation);
const clientLocation = getClientLocation(tcgcContext, operation);

if (clientLocation) {
if (typeof clientLocation === "string") {
return standardizeOperationId(`${clientLocation}_${operationName}`);
}
if (clientLocation.kind === "Interface") {
return standardizeOperationId(
`${getClientName(tcgcContext, clientLocation)}_${operationName}`,
);
}
if (isGlobalNamespace(program, clientLocation) || isService(program, clientLocation)) {
return standardizeOperationId(operationName);
}
return standardizeOperationId(`${getClientName(tcgcContext, clientLocation)}_${operationName}`);
}

let operationId: string;
if (operation.interface) {
operationId = `${getClientName(tcgcContext, operation.interface)}_${operationName}`;
} else if (
operation.namespace === undefined ||
isGlobalNamespace(program, operation.namespace) ||
isService(program, operation.namespace)
) {
operationId = operationName;
} else {
operationId = `${getClientName(tcgcContext, operation.namespace)}_${operationName}`;
}

return standardizeOperationId(operationId);
}

function standardizeOperationId(operationId: string): string {
return operationId
.split("_")
.map((part) => capitalize(part))
.join("_");
}

function getClientName(tcgcContext: TCGCContext, type: Operation | Interface | Namespace): string {
return getClientNameOverride(tcgcContext, type) ?? type.name;
}
89 changes: 58 additions & 31 deletions packages/typespec-lintdiff/src/rules/list-in-operation-name.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {
createRule,
getPagingOperation,
ignoreDiagnostics,
isArrayModelType,
isList,
isTemplateDeclarationOrInstance,
paramMessage,
type Model,
type Operation,
type Program,
} from "@typespec/compiler";
import { getHttpOperation, type HttpOperationResponse } from "@typespec/http";
import { getHttpOperation, type HttpOperation, type HttpOperationResponse } from "@typespec/http";
import { getExtensions } from "@typespec/openapi";
import { createAutorestOperationIdResolver } from "./utils/resolve-autorest-operation-id.js";

const validListOperationId = /^(?:(?:\w+_List\w*)|List)$/;

export const listInOperationNameRule = createRule({
name: "list-in-operation-name",
Expand All @@ -17,55 +25,55 @@ export const listInOperationNameRule = createRule({
default: paramMessage`Operation '${"operationName"}' returns a list/pageable response and should use method name starting with 'list'. Note: If you have already shipped an SDK on top of this spec, fixing this warning may introduce a breaking change.`,
},
create(context) {
const resolveOperationId = createAutorestOperationIdResolver(context.program);

return {
operation: (operation) => {
const name = operation.name.toLowerCase();
if (name.startsWith("list")) {
if (operation.interface === undefined && isTemplateDeclarationOrInstance(operation)) {
return;
}

const [httpOperation] = getHttpOperation(context.program, operation);
if (httpOperation.verb !== "get" && httpOperation.verb !== "post") {
return;
}

if (!isListOperation(context.program, operation)) {
const operationId = resolveOperationId(operation);
if (validListOperationId.test(operationId)) {
return;
}

if (!emitsListResponse(context.program, operation, httpOperation)) {
return;
}

context.reportDiagnostic({
target: operation,
format: { operationName: operation.name },
target: getDiagnosticTarget(operation),
format: { operationName: operationId },
});
},
};
},
});

function isListOperation(program: any, operation: any): boolean {
// Check for TypeSpec paging metadata
const [pagingOperation] = getPagingOperation(program, operation);
if (pagingOperation !== undefined) {
function emitsListResponse(
program: Program,
operation: Operation,
httpOperation: HttpOperation,
): boolean {
if (getExtensions(program, operation).has("x-ms-pageable")) {
return true;
}

// Check for x-ms-pageable extension
if (getExtensions(program, operation).has("x-ms-pageable")) {
const pagingOperation = ignoreDiagnostics(getPagingOperation(program, operation));
if (isListOperation(program, operation) && pagingOperation?.output.nextLink !== undefined) {
return true;
}

// Check if any success response has a collection-shaped body
// (a model with a "value" array property and at most one other property)
const [httpOperation] = getHttpOperation(program, operation);
return httpOperation.responses.some((response) =>
isCollectionResponse(program, response),
);
return httpOperation.responses.some((response) => isCollectionResponse(program, response));
}

function isCollectionResponse(
program: any,
response: HttpOperationResponse,
): boolean {
const statusCode = response.statusCodes;
if (typeof statusCode === "number" && statusCode >= 300) {
return false;
}

function isCollectionResponse(program: Program, response: HttpOperationResponse): boolean {
for (const content of response.responses) {
const body = content.body;
if (body === undefined || body.type.kind !== "Model") {
Expand All @@ -78,10 +86,7 @@ function isCollectionResponse(
continue;
}

if (
valueProp.type.kind === "Model" &&
isArrayModelType(program, valueProp.type)
) {
if (valueProp.type.kind === "Model" && isArrayModelType(program, valueProp.type)) {
const propCount = model.properties.size;
if (propCount <= 2) {
return true;
Expand All @@ -91,3 +96,25 @@ function isCollectionResponse(

return false;
}

function isListOperation(program: Program, operation: Operation): boolean {
for (let current: Operation | undefined = operation; current; current = current.sourceOperation) {
if (isList(program, current)) {
return true;
}
}
return false;
}

function getDiagnosticTarget(
operation: Operation,
): Operation | NonNullable<Operation["interface"]> {
const operationInterface = operation.interface;
if (
operationInterface?.node !== undefined &&
operation.node?.parent !== operationInterface.node
) {
return operationInterface;
}
return operation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
createTCGCContext,
getClientLocation,
getClientNameOverride,
type TCGCContext,
} from "@azure-tools/typespec-client-generator-core";
import {
isGlobalNamespace,
isService,
type Interface,
type Namespace,
type Operation,
type Program,
} from "@typespec/compiler";
import { capitalize } from "@typespec/compiler/casing";
import { getOperationId } from "@typespec/openapi";

export function createAutorestOperationIdResolver(program: Program) {
const tcgcContext = createTCGCContext(program, "@azure-tools/typespec-autorest", {
mutateNamespace: false,
});

return (operation: Operation) => resolveAutorestOperationId(program, operation, tcgcContext);
}

function resolveAutorestOperationId(
program: Program,
operation: Operation,
tcgcContext: TCGCContext,
): string {
const explicitOperationId = getOperationId(program, operation);
if (explicitOperationId) {
return explicitOperationId;
}

const operationName = getClientName(tcgcContext, operation);
const clientLocation = getClientLocation(tcgcContext, operation);

if (clientLocation) {
if (typeof clientLocation === "string") {
return standardizeOperationId(`${clientLocation}_${operationName}`);
}
if (clientLocation.kind === "Interface") {
return standardizeOperationId(
`${getClientName(tcgcContext, clientLocation)}_${operationName}`,
);
}
if (isGlobalNamespace(program, clientLocation) || isService(program, clientLocation)) {
return standardizeOperationId(operationName);
}
return standardizeOperationId(`${getClientName(tcgcContext, clientLocation)}_${operationName}`);
}

let operationId: string;
if (operation.interface) {
operationId = `${getClientName(tcgcContext, operation.interface)}_${operationName}`;
} else if (
operation.namespace === undefined ||
isGlobalNamespace(program, operation.namespace) ||
isService(program, operation.namespace)
) {
operationId = operationName;
} else {
operationId = `${getClientName(tcgcContext, operation.namespace)}_${operationName}`;
}

return standardizeOperationId(operationId);
}

function standardizeOperationId(operationId: string): string {
return operationId
.split("_")
.map((part) => capitalize(part))
.join("_");
}

function getClientName(tcgcContext: TCGCContext, type: Operation | Interface | Namespace): string {
return getClientNameOverride(tcgcContext, type) ?? type.name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"violation": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "../../lib/imports.tsp";
using TypeSpec.Http;

@service(#{ title: "Test Service" })
namespace TestService;

model Result {
@pageItems
items: string[];

@nextLink
nextLink: url;
}

interface Widgets {
@list
@route("/widgets")
@get
getWidgets(): Result;
}
Loading