Skip to content

Commit

Permalink
Repurpose rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Apr 9, 2024
1 parent a710224 commit 0e7dd0f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 109 deletions.
25 changes: 25 additions & 0 deletions docs/libraries/azure-core/rules/no-generic-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: "no-generic-types"
---

```text title="Full name"
@azure-tools/typespec-azure-core/no-generic-types
```

Azure services should use types which specify the bit-width instead of generic types.

#### ❌ Incorrect

```tsp
model Widget {
id: integer;
}
```

#### ✅ Correct

```tsp
model Widget {
id: int32;
}
```
25 changes: 0 additions & 25 deletions docs/libraries/azure-core/rules/use-standard-integer.md

This file was deleted.

46 changes: 1 addition & 45 deletions packages/typespec-autorest/test/formats.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expectDiagnosticEmpty, expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual } from "assert";
import { describe, it } from "vitest";
import { diagnoseOpenApiFor, emitOpenApiWithDiagnostics, openApiFor } from "./test-host.js";
import { diagnoseOpenApiFor, openApiFor } from "./test-host.js";

describe("typespec-autorest: format", () => {
it("allows supported formats", async () => {
Expand Down Expand Up @@ -71,48 +71,4 @@ describe("typespec-autorest: format", () => {
);
expectDiagnosticEmpty(diagnostics);
});

it("ensures certain scalars emit int32 and int64 formats", async () => {
const [res, _] = await emitOpenApiWithDiagnostics(
`
@service
namespace Test;
model Widget {
intA: int32;
intB: int64;
intC: safeint;
intD: numeric;
intE: integer;
}
`
);
const model = res.definitions!["Widget"]!;
deepStrictEqual(model, {
properties: {
intA: {
format: "int32",
type: "integer",
},
intB: {
format: "int64",
type: "integer",
},
intC: {
format: "int64",
type: "integer",
},
intD: {
format: "int64",
type: "integer",
},
intE: {
format: "int64",
type: "integer",
},
},
required: ["intA", "intB", "intC", "intD", "intE"],
type: "object",
});
});
});
4 changes: 2 additions & 2 deletions packages/typespec-azure-core/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { noEnumRule } from "./rules/no-enum.js";
import { noErrorStatusCodesRule } from "./rules/no-error-status-codes.js";
import { noExplicitRoutesResourceOps } from "./rules/no-explicit-routes-resource-ops.js";
import { noFixedEnumDiscriminatorRule } from "./rules/no-fixed-enum-discriminator.js";
import { noGenericTypesRule } from "./rules/no-generic-types.js";
import { noNullableRule } from "./rules/no-nullable.js";
import { noOffsetDateTimeRule } from "./rules/no-offsetdatetime.js";
import { operationIdRule } from "./rules/no-operation-id.js";
Expand All @@ -32,7 +33,6 @@ import { requireKeyVisibility } from "./rules/require-key-visibility.js";
import { responseSchemaMultiStatusCodeRule } from "./rules/response-schema-multi-status-code.js";
import { rpcOperationRequestBodyRule } from "./rules/rpc-operation-request-body.js";
import { spreadDiscriminatedModelRule } from "./rules/spread-discriminated-model.js";
import { useStandardInteger } from "./rules/use-standard-integer.js";
import { useStandardNames } from "./rules/use-standard-names.js";
import { useStandardOperations } from "./rules/use-standard-ops.js";

Expand All @@ -52,6 +52,7 @@ const rules = [
noExplicitRoutesResourceOps,
noFixedEnumDiscriminatorRule,
nonBreakingVersioningRule,
noGenericTypesRule,
noNullableRule,
noOffsetDateTimeRule,
noResponseBodyRule,
Expand All @@ -69,7 +70,6 @@ const rules = [
responseSchemaMultiStatusCodeRule,
rpcOperationRequestBodyRule,
spreadDiscriminatedModelRule,
useStandardInteger,
useStandardNames,
useStandardOperations,
];
Expand Down
38 changes: 38 additions & 0 deletions packages/typespec-azure-core/src/rules/no-generic-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Model, createRule, paramMessage } from "@typespec/compiler";

const disallowList = new Set(["integer", "numeric", "float", "decimal"]);
const alternatives = new Map([
["integer", "int32"],
["numeric", "int32"],
["float", "float32"],
["decimal", "float32"],
]);

export const noGenericTypesRule = createRule({
name: "no-generic-types",
description: "Don't use generic types. Use more specific types instead.",
severity: "warning",
url: "https://azure.github.io/typespec-azure/docs/libraries/azure-core/rules/no-generic-types",
messages: {
default: paramMessage`Don't use generic type '${"name"}'. Use a more specific type that specifies the bit size, such as '${"alternative"}' instead.`,
},
create(context) {
return {
model: (model: Model) => {
for (const [_, prop] of model.properties) {
if (prop.type.kind === "Scalar") {
if (disallowList.has(prop.type.name)) {
context.reportDiagnostic({
target: prop.type,
format: {
name: prop.type.name,
alternative: alternatives.get(prop.type.name)!,
},
});
}
}
}
},
};
},
});
35 changes: 0 additions & 35 deletions packages/typespec-azure-core/src/rules/use-standard-integer.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
createLinterRuleTester,
} from "@typespec/compiler/testing";
import { beforeEach, it } from "vitest";
import { useStandardInteger } from "../../src/rules/use-standard-integer.js";
import { noGenericTypesRule } from "../../src/rules/no-generic-types.js";
import { createAzureCoreTestRunner } from "../test-host.js";

let runner: BasicTestRunner;
let tester: LinterRuleTester;

beforeEach(async () => {
runner = await createAzureCoreTestRunner({ omitServiceNamespace: true });
tester = createLinterRuleTester(runner, useStandardInteger, "@azure-tools/typespec-azure-core");
tester = createLinterRuleTester(runner, noGenericTypesRule, "@azure-tools/typespec-azure-core");
});

it("emits a warning diagnostic for non-standard integer types", async () => {
Expand Down

0 comments on commit 0e7dd0f

Please sign in to comment.