From bcbe56fbedc63eed374828d98d19a6de0b5fd6ec Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Fri, 5 Apr 2024 11:39:27 -0700 Subject: [PATCH] Update logic. --- docs/libraries/azure-core/reference/linter.md | 1 + .../azure-core/rules/use-standard-integer.md | 25 ++++ packages/typespec-azure-core/README.md | 1 + .../src/rules/use-standard-integer.ts | 18 ++- .../test/rules/use-standard-integer.test.ts | 116 +++++++++--------- 5 files changed, 101 insertions(+), 60 deletions(-) create mode 100644 docs/libraries/azure-core/rules/use-standard-integer.md diff --git a/docs/libraries/azure-core/reference/linter.md b/docs/libraries/azure-core/reference/linter.md index ed61acf75c..e7a79ffac2 100644 --- a/docs/libraries/azure-core/reference/linter.md +++ b/docs/libraries/azure-core/reference/linter.md @@ -59,5 +59,6 @@ Available ruleSets: | `@azure-tools/typespec-azure-core/response-schema-problem` | Warn about operations having multiple non-error response schemas. | | `@azure-tools/typespec-azure-core/rpc-operation-request-body` | Warning for RPC body problems. | | `@azure-tools/typespec-azure-core/spread-discriminated-model` | Check a model with a discriminator has not been used in composition. | +| `@azure-tools/typespec-azure-core/use-standard-integer` | Use only types that map to int32 or int64. | | `@azure-tools/typespec-azure-core/use-standard-names` | Use recommended names for operations. | | `@azure-tools/typespec-azure-core/use-standard-operations` | Operations should be defined using a signature from the Azure.Core namespace. | diff --git a/docs/libraries/azure-core/rules/use-standard-integer.md b/docs/libraries/azure-core/rules/use-standard-integer.md new file mode 100644 index 0000000000..6428c6cb6b --- /dev/null +++ b/docs/libraries/azure-core/rules/use-standard-integer.md @@ -0,0 +1,25 @@ +--- +title: "use-standard-integer" +--- + +```text title="Full name" +@azure-tools/typespec-azure-core/use-standard-integer +``` + +Azure services favor the standard types int32 and int64 (or safeint) when generating code using Autorest. + +#### ❌ Incorrect + +```tsp +model Widget { + id: int8; +} +``` + +#### ✅ Correct + +```tsp +model Widget { + id: int32; +} +``` diff --git a/packages/typespec-azure-core/README.md b/packages/typespec-azure-core/README.md index 423b8016b0..116561509c 100644 --- a/packages/typespec-azure-core/README.md +++ b/packages/typespec-azure-core/README.md @@ -63,6 +63,7 @@ Available ruleSets: | `@azure-tools/typespec-azure-core/response-schema-problem` | Warn about operations having multiple non-error response schemas. | | `@azure-tools/typespec-azure-core/rpc-operation-request-body` | Warning for RPC body problems. | | `@azure-tools/typespec-azure-core/spread-discriminated-model` | Check a model with a discriminator has not been used in composition. | +| `@azure-tools/typespec-azure-core/use-standard-integer` | Use only types that map to int32 or int64. | | `@azure-tools/typespec-azure-core/use-standard-names` | Use recommended names for operations. | | `@azure-tools/typespec-azure-core/use-standard-operations` | Operations should be defined using a signature from the Azure.Core namespace. | diff --git a/packages/typespec-azure-core/src/rules/use-standard-integer.ts b/packages/typespec-azure-core/src/rules/use-standard-integer.ts index 6ca53f92f5..d3a109f65a 100644 --- a/packages/typespec-azure-core/src/rules/use-standard-integer.ts +++ b/packages/typespec-azure-core/src/rules/use-standard-integer.ts @@ -4,14 +4,30 @@ export const useStandardInteger = createRule({ name: "use-standard-integer", description: "Use only types that map to int32 or int64.", severity: "warning", + url: "https://azure.github.io/typespec-azure/docs/libraries/azure-core/rules/use-standard-integer", messages: { default: "Recommended integer types are 'int32', 'int64' or 'safeint'.", }, create(context) { return { model: (model: Model) => { + const nonRecommendedInts = [ + "int64", + "int8", + "int16", + "uint8", + "uint16", + "uint32", + "uint64", + ]; for (const [name, prop] of model.properties) { - const test = "best"; + if (prop.type.kind === "Scalar") { + if (nonRecommendedInts.includes(prop.type.name)) { + context.reportDiagnostic({ + target: prop.type, + }); + } + } } }, }; diff --git a/packages/typespec-azure-core/test/rules/use-standard-integer.test.ts b/packages/typespec-azure-core/test/rules/use-standard-integer.test.ts index 83bf3315a2..5b74156606 100644 --- a/packages/typespec-azure-core/test/rules/use-standard-integer.test.ts +++ b/packages/typespec-azure-core/test/rules/use-standard-integer.test.ts @@ -3,73 +3,71 @@ import { LinterRuleTester, createLinterRuleTester, } from "@typespec/compiler/testing"; -import { beforeEach, describe, it } from "vitest"; +import { beforeEach, it } from "vitest"; import { useStandardInteger } from "../../src/rules/use-standard-integer.js"; import { createAzureCoreTestRunner } from "../test-host.js"; -describe("typespec-azure-core: no-format rule", () => { - let runner: BasicTestRunner; - let tester: LinterRuleTester; +let runner: BasicTestRunner; +let tester: LinterRuleTester; - beforeEach(async () => { - runner = await createAzureCoreTestRunner({ omitServiceNamespace: true }); - tester = createLinterRuleTester(runner, useStandardInteger, "@azure-tools/typespec-azure-core"); - }); +beforeEach(async () => { + runner = await createAzureCoreTestRunner({ omitServiceNamespace: true }); + tester = createLinterRuleTester(runner, useStandardInteger, "@azure-tools/typespec-azure-core"); +}); - it("emits a warning diagnostic for non-standard integer types", async () => { - await tester - .expect( - ` - namespace Azure.Widget; +it("emits a warning diagnostic for non-standard integer types", async () => { + await tester + .expect( + ` + namespace Azure.Widget; - model Widget { - prop1: uint16; - prop2: int8; - prop3: uint32; - } - ` - ) - .toEmitDiagnostics([ - { - code: "@azure-tools/typespec-azure-core/use-standard-integer", - }, - { - code: "@azure-tools/typespec-azure-core/use-standard-integer", - }, - { - code: "@azure-tools/typespec-azure-core/use-standard-integer", - }, - ]); - }); + model Widget { + prop1: uint16; + prop2: int8; + prop3: uint32; + } + ` + ) + .toEmitDiagnostics([ + { + code: "@azure-tools/typespec-azure-core/use-standard-integer", + }, + { + code: "@azure-tools/typespec-azure-core/use-standard-integer", + }, + { + code: "@azure-tools/typespec-azure-core/use-standard-integer", + }, + ]); +}); - it("does not emit a warning diagnostic for standard integer types", async () => { - await tester - .expect( - ` - namespace Azure.Widget; +it("does not emit a warning diagnostic for standard integer types", async () => { + await tester + .expect( + ` + namespace Azure.Widget; - model Widget { - prop1: int32; - prop2: int64; - prop3: safeint; - } - ` - ) - .toBeValid(); - }); + model Widget { + prop1: int32; + prop2: int64; + prop3: safeint; + } + ` + ) + .toBeValid(); +}); - it("does not emit a warning diagnostic for non-standard integer types that map to supported integers in Autorest", async () => { - await tester - .expect( - ` - namespace Azure.Widget; +it("does not emit a warning diagnostic for non-standard integer types that map to supported integers in Autorest", async () => { + await tester + .expect( + ` + namespace Azure.Widget; - model Widget { - prop1: numeric; - prop2: integer - } - ` - ) - .toBeValid(); - }); + model Widget { + prop1: numeric; + prop2: integer + } + ` + ) + .toBeValid(); });