Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tcgc] add linter rulesets framework #1208

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

add linter rulesets to TCGC, for both generic and language-specific linter rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: "Linter usage"
toc_min_heading_level: 2
toc_max_heading_level: 3
---

# Linter

## Usage

Add the following in `tspconfig.yaml`:

```yaml
linter:
extends:
- "@azure-tools/typespec-client-generator-core/all"
```

## RuleSets

Available ruleSets:

- `@azure-tools/typespec-client-generator-core/all`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure all would ever be useful when considering non tier 1 languages as well as required rules vs best practices? Should we be a bit more granular on what all means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all isn't a specificly-defined ruleset, it's the standard name for all of the rules existing in a package

- `@azure-tools/typespec-client-generator-core/best-practices:csharp`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does best practice mean here? Does it mean this is optional? If so is there another category which is not optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best practices is what I've called the ruleset for now, open to suggestions! I think the advice from the typespec team has been to use the word "best-practices", @timotheeguerin is this correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these rulesets optional and can be opted out?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I don't think we have a strong opinion on that name, we just have a placeholder @typespec/best-practices package that would contain misc linter to write good typespec.

I would say name this however makes the most sense. But I would note that I don't think this really should spend too much time on this as for all of azure they MUST use either the data-plane or resource-manager ruleset in teh spec repo. And I assume all those rules for all those language will be enabled.

I expect those more targeted ruleset might be more useful when we have the non azure version of tcgc(e.g. @typespec/client) where external customer might decide which language they care about.


## Rules

| Name | Description |
| -------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| `@azure-tools/typespec-client-generator-core/require-client-suffix` | Client names should end with 'Client'. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we specify which ruleset each of these are in?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That file is auto generated with the list so would need to update the ref doc generator. If you have some idea on what'd you like to see feel free to suggest a new style but this should work for a generic linter

| `@azure-tools/typespec-client-generator-core/property-name-conflict` | Avoid naming conflicts between a property and a model of the same name. |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a required rule or is it a recommendation to avoid property and model having the same name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all rules are in a sense optional its up to the user to enable them(directly or via a ruleset). For azure specs we require that the data-plane or resource-manager ruleset is used.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Contributing Linter Rules to the @azure-tools/typespec-client-generator-core Package

For information about linter rules in typespec in general, view [here][generic-linter]

In the `@azure-tools/typespec-client-generator-core` library, we have two main types of rules:

1. Generic rule that applies to all emitted languages
2. Specific rule that applies to a subset of all language: it can even apply to just one language
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. Specific rule that applies to a subset of all language: it can even apply to just one language
2. Specific rule that applies to a subset of all languages: it can even apply to just one language


The process for adding a rule starts off the same for both: for language-specific rules, there is an extra step

1. Write the rule in `typespec-azure/packages/typespec-client-generator-core/src/rules/[rule-name].rule.ts
2. Add reference to the rule in the `rules` array in [`typespec-azure/packages/typespec-client-generator-core/src/linter.ts`][tcgc-linter]
- This will automatically add it to a ruleset called `:all` for `@azure-tools/typespec-client-generator-core`
3. Add the rule to the enable list for [`data-plane.ts`][data-plane-ruleset] and/or [`resource-manager.ts`][resource-manager-ruleset] in the [rulesets][rulesets] package. You can set `enable` to `false` here, if you want to delay enabling
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to have different rules for data plane and mgmt plane? I know we are less strict with mgmt plane, but aren't the recommendations the same for both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the current setup of azure-core-rulesets, where you explicitly list rules for the data plane and mgmt rulesets

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is some rules that management plane specific and some that shouldn't apply to mamangement plane. But the majority should apply to both

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a single place to add a rule that applies to both or do we need to add the rule explicitly to both lists?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see step 3 done in this PR. Will this be added in a follow-up PR?


**If you are adding a language-specific rule**, you will also need this extra step 4. Add reference to the rule in the `[language]Rules` array in [`typespec-azure/packages/typespec-client-generator-core/src/linter.ts`][tcgc-linter]

For Azure generations then, all rules, including all language-specific rules, will be run on the specs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to limit this to the Tier 1 languages not necessarily all languages? Should we specify this? I assume Go could add their own rules in here and those wouldn't apply unless generating Go even for azure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, let me update this to say that language-agnostic rules, and tier-1 languages will be run

For unbranded generations, since we've added the rules into specific `best-practices:[language]` rulesets, you can explicitly specify a subset of rules in your `tsp-config.yaml`, i.e. if I only want Python best-practices, I could add this in my `tsp-config.yaml`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might create a bad situation where someone can turn off csharp rules but in reality they cannot. I think we might want to consider what flexibility a user would have over tcgc rules when running in azure context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every azure service is enforced to have one of the 2 rules set enabled by the spec repo pipeline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With rulesets, we have to give users the ability to turn them off because they are ultimately warnings. After talking with @timotheeguerin, you can't have error linter rules, only warnings. If there is a case where you explicitly want to error in csharp for a csharp-specific issue, the csharp emitter itself can throw a fatal error with reportDiagnostic, but the fatal error should have an analog in the linter warnings in tcgc that says "if you do this in csharp and ignore me, the csharp emitter will fail"


```yaml
linter:
extends:
- best-practices: python
```

Finally, we recommend that every warning or error you throw in your language emitter has a corresponding warning in TCGC. This is part of our shift-left policy, so tsp authors can catch these potential pitfalls earlier in the process.

### Links

[generic-linter]: https://typespec.io/docs/next/extending-typespec/linters "Generic Linter Docs"
[tcgc-linter]: https://github.com/typespec-azure/packages/typespec-client-generator-core/src/linter.ts "Linter TS File"
[rulesets]: https://github.com/typespec-azure/packages/typespec-azure-rulesets "Rulesets package"
[data-plane-ruleset]: https://github.com/typespec-azure/packages/typespec-azure-rulesets/src/rulesets/data-plane.ts "Data Plane Ruleset"
[resource-manager-ruleset]: https://github.com/typespec-azure/packages/typespec-azure-rulesets/src/rulesets/resource-manager.ts "Resource Manager Ruleset"
Comment on lines +33 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These links don't resolve. They are missing /Azure/ after https://github.com and also blob/main/ after typespec-azure/

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: "property-name-conflict"
---

```text title="Full name"
@azure-tools/typespec-client-generator-core/property-name-conflict
```

Verify that there isn't a name conflict between a property in the model, and the name of the model itself

#### ❌ Incorrect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Invalid over Incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm following the current format which uses Incorrect over Invalid, @timotheeguerin should this be switched?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just pattern from how eslint document its rules, e.g. https://typescript-eslint.io/rules/class-literal-property-style

But if we feel like changing then we can discuss the common format we want


```ts
model Widget {
widget: string;
}
```

#### ✅ Ok

Using items from a private namespace within the same library is allowed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description doesn't seem to match the sample.


```ts
model Widget {
widgetName: string;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: "require-client-suffix"
---

```text title="Full name"
@azure-tools/typespec-client-generator-core/require-client-suffix
```

Verify that the generated client's name will have the suffix `Client` in its name

#### ❌ Incorrect

```ts
// main.tsp
namespace MyService;
```

```ts
// client.tsp
namespace MyCustomizations;

@@client(MyService);
```

#### ✅ Recommended

If you would not like to make any changes to the generated client besides its name, you can rely on the [`@clientName`][client-name] decorator to rename the main namespace of the service.

```ts
@@clientName(MyService, "MyClient");
```

#### ✅ Ok

If you are completely recreating a client namespace in your `client.tsp`, you can rely on that namespace to end in `Client`

```ts
// client.tsp
@client({service: MyService})
namespace MyClient {
iscai-msft marked this conversation as resolved.
Show resolved Hide resolved
}
```

### Links

[client-name]: https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/reference/decorators#@Azure.ClientGenerator.Core.clientName "@clientName Decorator"
26 changes: 26 additions & 0 deletions packages/typespec-client-generator-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ TypeSpec Data Plane Generation library
npm install @azure-tools/typespec-client-generator-core
```

## Linter

### Usage

Add the following in `tspconfig.yaml`:

```yaml
linter:
extends:
- "@azure-tools/typespec-client-generator-core/all"
```

### RuleSets

Available ruleSets:

- `@azure-tools/typespec-client-generator-core/all`
- `@azure-tools/typespec-client-generator-core/best-practices:csharp`

### Rules

| Name | Description |
| -------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| `@azure-tools/typespec-client-generator-core/require-client-suffix` | Client names should end with 'Client'. |
| `@azure-tools/typespec-client-generator-core/property-name-conflict` | Avoid naming conflicts between a property and a model of the same name. |

## Decorators

### Azure.ClientGenerator.Core
Expand Down
11 changes: 2 additions & 9 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ export const $client: ClientDecorator = (
explicitService?.kind === "Namespace"
? explicitService
: findClientService(context.program, target) ?? (target as any);
if (!name.endsWith("Client")) {
reportDiagnostic(context.program, {
code: "client-name",
format: { name },
target: context.decoratorTarget,
});
}

if (!isService(context.program, service)) {
reportDiagnostic(context.program, {
Expand Down Expand Up @@ -791,7 +784,7 @@ export const $clientFormat: ClientFormatDecorator = (
setScopedDecoratorData(context, $clientFormat, clientFormatKey, target, format, scope); // eslint-disable-line deprecation/deprecation
} else {
reportDiagnostic(context.program, {
code: "incorrect-client-format",
code: "invalid-client-format",
format: { format, expectedTargetTypes: expectedTargetTypes.join('", "') },
target: context.decoratorTarget,
});
Expand Down Expand Up @@ -939,7 +932,7 @@ export const $access: AccessDecorator = (
) => {
if (typeof value.value !== "string" || (value.value !== "public" && value.value !== "internal")) {
reportDiagnostic(context.program, {
code: "access",
code: "invalid-access",
format: {},
target: entity,
});
Expand Down
1 change: 1 addition & 0 deletions packages/typespec-client-generator-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./decorators.js";
export * from "./interfaces.js";
export * from "./lib.js";
export { $linter } from "./linter.js";
export * from "./public-utils.js";
export * from "./types.js";
export { $onValidate } from "./validate.js";
38 changes: 2 additions & 36 deletions packages/typespec-client-generator-core/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@ import { createTypeSpecLibrary, paramMessage } from "@typespec/compiler";
export const $lib = createTypeSpecLibrary({
name: "@azure-tools/typespec-client-generator-core",
diagnostics: {
"client-name": {
severity: "warning",
messages: {
default: paramMessage`Client name "${"name"}" must end with Client. Use @client({name: "...Client"}`,
},
},
"client-service": {
severity: "warning",
messages: {
default: paramMessage`Client "${"name"}" is not inside a service namespace. Use @client({service: MyServiceNS}`,
},
},
"unknown-client-format": {
severity: "error",
messages: {
default: paramMessage`Client format "${"format"}" is unknown. Known values are "${"knownValues"}"`,
},
},
"incorrect-client-format": {
"invalid-client-format": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used when we're checking the input of @clientFormat. Is this valid?

severity: "error",
messages: {
default: paramMessage`Format "${"format"}" can only apply to "${"expectedTargetTypes"}"`,
Expand All @@ -33,22 +21,7 @@ export const $lib = createTypeSpecLibrary({
default: "Cannot have a union containing only null types.",
},
},
"union-unsupported": {
severity: "error",
messages: {
default:
"Unions cannot be emitted by our language generators unless all options are literals of the same type.",
null: "Unions containing multiple model types cannot be emitted unless the union is between one model type and 'null'.",
},
},
"use-enum-instead": {
severity: "warning",
messages: {
default:
"Use enum instead of union of string or number literals. Falling back to the literal type.",
},
},
access: {
"invalid-access": {
severity: "error",
messages: {
default: `Access decorator value must be "public" or "internal".`,
Expand All @@ -60,13 +33,6 @@ export const $lib = createTypeSpecLibrary({
default: `Usage decorator value must be 2 ("input") or 4 ("output").`,
},
},
"invalid-encode": {
severity: "error",
messages: {
default: "Invalid encoding",
wrongType: paramMessage`Encoding '${"encoding"}' cannot be used on type '${"type"}'`,
},
},
"conflicting-multipart-model-usage": {
severity: "error",
messages: {
Expand Down
23 changes: 23 additions & 0 deletions packages/typespec-client-generator-core/src/linter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineLinter } from "@typespec/compiler";
import { propertyNameConflictRule } from "./rules/property-name-conflict.rule.js";
import { requireClientSuffixRule } from "./rules/require-client-suffix.rule.js";

const rules = [requireClientSuffixRule, propertyNameConflictRule];

const csharpRules = [propertyNameConflictRule];

export const $linter = defineLinter({
rules,
ruleSets: {
"best-practices:csharp": {
enable: {
...Object.fromEntries(
csharpRules.map((rule) => [
`@azure-tools/typespec-client-generator-core/${rule.name}`,
true,
])
),
},
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ModelProperty, createRule, paramMessage } from "@typespec/compiler";

export const propertyNameConflictRule = createRule({
name: "property-name-conflict",
description: "Avoid naming conflicts between a property and a model of the same name.",
severity: "warning",
url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/property-name-conflict",
messages: {
default: paramMessage`Property '${"propertyName"}' having the same name as its enclosing model will cause problems with C# code generation. Consider renaming the property directly or using the @clientName("newName", "csharp") decorator to rename the property for C#.`,
},
create(context) {
return {
modelProperty: (property: ModelProperty) => {
const modelName = property.model?.name.toLocaleLowerCase();
const propertyName = property.name.toLocaleLowerCase();
if (propertyName === modelName) {
context.reportDiagnostic({
format: { propertyName: property.name },
target: property,
});
}
},
};
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createRule, Interface, Namespace, paramMessage } from "@typespec/compiler";
import { createTCGCContext, getClient } from "../decorators.js";

export const requireClientSuffixRule = createRule({
name: "require-client-suffix",
description: "Client names should end with 'Client'.",
severity: "warning",
url: "https://azure.github.io/typespec-azure/docs/libraries/typespec-client-generator-core/rules/require-client-suffix",
messages: {
default: paramMessage`Client name "${"name"}" must end with Client. Use @client({name: "...Client"}`,
},
create(context) {
const tcgcContext = createTCGCContext(
context.program,
"@azure-tools/typespec-client-generator-core"
);
return {
namespace: (namespace: Namespace) => {
const sdkClient = getClient(tcgcContext, namespace);
if (sdkClient && !sdkClient.name.endsWith("Client")) {
context.reportDiagnostic({
target: namespace,
format: {
name: sdkClient.name,
},
});
}
},
interface: (interfaceType: Interface) => {
const sdkClient = getClient(tcgcContext, interfaceType);
if (sdkClient && !sdkClient.name.endsWith("Client")) {
context.reportDiagnostic({
target: interfaceType,
format: {
name: sdkClient.name,
},
});
}
},
};
},
});
Loading
Loading