Skip to content

Commit

Permalink
Update logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Apr 8, 2024
1 parent 9abf24b commit bcbe56f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 60 deletions.
1 change: 1 addition & 0 deletions docs/libraries/azure-core/reference/linter.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
25 changes: 25 additions & 0 deletions docs/libraries/azure-core/rules/use-standard-integer.md
Original file line number Diff line number Diff line change
@@ -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;
}
```
1 change: 1 addition & 0 deletions packages/typespec-azure-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down
18 changes: 17 additions & 1 deletion packages/typespec-azure-core/src/rules/use-standard-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
}
},
};
Expand Down
116 changes: 57 additions & 59 deletions packages/typespec-azure-core/test/rules/use-standard-integer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit bcbe56f

Please sign in to comment.