Skip to content

Commit

Permalink
Fix up test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Apr 18, 2024
1 parent 2bb0e78 commit 5468d01
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/libraries/azure-core/reference/linter.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Available ruleSets:
| `@azure-tools/typespec-azure-core/no-explicit-routes-resource-ops` | The @route decorator should not be used on standard resource operation signatures. |
| `@azure-tools/typespec-azure-core/no-fixed-enum-discriminator` | Discriminator shouldn't be a fixed enum. |
| [`@azure-tools/typespec-azure-core/non-breaking-versioning`](/libraries/azure-core/rules/non-breaking-versioning.md) | Check that only backward compatible versioning change are done to a service. |
| [`@azure-tools/typespec-azure-core/no-generic-numeric`](/libraries/azure-core/rules/no-generic-numeric.md) | Don't use generic types. Use more specific types instead. |
| [`@azure-tools/typespec-azure-core/no-generic-numeric`](/libraries/azure-core/rules/no-generic-numeric.md) | Don't use generic types. Use more specific types instead. |
| `@azure-tools/typespec-azure-core/no-nullable` | Use `?` for optional properties. |
| `@azure-tools/typespec-azure-core/no-offsetdatetime` | Prefer using `utcDateTime` when representing a datetime unless an offset is necessary. |
| `@azure-tools/typespec-azure-core/no-response-body` | Ensure that the body is set correctly for the response type. |
Expand Down
7 changes: 6 additions & 1 deletion packages/typespec-azure-core/src/rules/no-generic-numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const noGenericNumericRule = createRule({
if (prop.type.kind === "Scalar") {
if (disallowList.has(prop.type.name)) {
context.reportDiagnostic({
target: prop.type,
target: prop,
format: {
name: prop.type.name,
alternative: alternatives.get(prop.type.name)!,
Expand All @@ -35,6 +35,11 @@ export const noGenericNumericRule = createRule({
}
},
scalar: (scalar: Scalar) => {
// if the scalar is the base scalar, then we don't need to check it as it will surface
// in usage (for example: as a model property)
if (disallowList.has(scalar.name)) {
return;
}
let baseScalar: Scalar | undefined = undefined;
while (scalar.baseScalar !== undefined) {
baseScalar = scalar.baseScalar;
Expand Down
16 changes: 16 additions & 0 deletions packages/typespec-azure-core/test/rules/no-generic-numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ it("emits a warning for generic numeric types", async () => {
.toEmitDiagnostics([
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't use generic type 'integer'. Use a more specific type that specifies the bit size, such as 'int32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't use generic type 'numeric'. Use a more specific type that specifies the bit size, such as 'int32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't use generic type 'float'. Use a more specific type that specifies the bit size, such as 'float32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't use generic type 'decimal'. Use a more specific type that specifies the bit size, such as 'float32' instead.",
},
]);
});
Expand All @@ -67,15 +75,23 @@ it("emits a warning when extending generic numeric types", async () => {
.toEmitDiagnostics([
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't extend generic type 'integer'. Use a more specific type that specifies the bit size, such as 'int32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't extend generic type 'numeric'. Use a more specific type that specifies the bit size, such as 'int32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't extend generic type 'float'. Use a more specific type that specifies the bit size, such as 'float32' instead.",
},
{
code: "@azure-tools/typespec-azure-core/no-generic-numeric",
message:
"Don't extend generic type 'decimal'. Use a more specific type that specifies the bit size, such as 'float32' instead.",
},
]);
});

0 comments on commit 5468d01

Please sign in to comment.