diff --git a/docs/libraries/azure-core/reference/linter.md b/docs/libraries/azure-core/reference/linter.md index 6bb1608adf..65ab133690 100644 --- a/docs/libraries/azure-core/reference/linter.md +++ b/docs/libraries/azure-core/reference/linter.md @@ -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. | diff --git a/packages/typespec-azure-core/src/rules/no-generic-numeric.ts b/packages/typespec-azure-core/src/rules/no-generic-numeric.ts index ecf1253567..a7766777f0 100644 --- a/packages/typespec-azure-core/src/rules/no-generic-numeric.ts +++ b/packages/typespec-azure-core/src/rules/no-generic-numeric.ts @@ -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)!, @@ -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; diff --git a/packages/typespec-azure-core/test/rules/no-generic-numeric.test.ts b/packages/typespec-azure-core/test/rules/no-generic-numeric.test.ts index a383ec7525..9bfffb5dc3 100644 --- a/packages/typespec-azure-core/test/rules/no-generic-numeric.test.ts +++ b/packages/typespec-azure-core/test/rules/no-generic-numeric.test.ts @@ -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.", }, ]); }); @@ -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.", }, ]); });