-
Notifications
You must be signed in to change notification settings - Fork 324
Add style guide #1430
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
Merged
Merged
Add style guide #1430
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0819296
Add style guide
timotheeguerin 1aec19e
Add more guidance
timotheeguerin 005a437
Merge branch 'main' into style-guide
timotheeguerin bd15c97
Fix try it no override
timotheeguerin e7b3c5f
Merge branch 'style-guide' of https://github.com/timotheeguerin/cadl …
timotheeguerin 48595b5
Merge branch 'main' into style-guide
timotheeguerin 32256bb
Merge branch 'main' into style-guide
timotheeguerin c523609
Update docs/introduction/style-guide.md
timotheeguerin a7fb37c
Merge branch 'main' into style-guide
timotheeguerin 821ee56
Merge branch 'main' into style-guide
timotheeguerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| --- | ||
| title: Style guide | ||
| --- | ||
|
|
||
| # Cadl Language Style Guide | ||
|
|
||
| This is a guide providing a recommended set of naming convention to use when writing a Cadl spec. | ||
|
|
||
| :::info | ||
| The guidelines in this article are used in Cadl Core libraries. You can use them, or adapt them to your needs. The primary objectives are consistency and readability within your project, team, organization, or company source code. | ||
| ::: | ||
|
|
||
| ## Naming convention | ||
|
|
||
| | Type | Naming | Example | | ||
| | ---------------- | -------------------------------------------- | ------------------------------------------------ | | ||
| | scalar | camelCase | `scalar uuid extends string;` | | ||
| | model | PascalCase | `model Pet {}` | | ||
| | model property | camelCase | `model Pet {furColor: string}` | | ||
| | enum | PascalCase | `model Pet {furColor: string}` | | ||
| | enum member | camelCase | `enum Direction {up, down}` | | ||
| | namespace | PascalCase | `namespace Org.PetStore` | | ||
| | interface | PascalCase | `interface Stores {}` | | ||
| | operation | camelCase | `op listPets(): Pet[];` | | ||
| | operation params | camelCase | `op getPet(petId: string): Pet;` | | ||
| | unions | PascalCase | `union Pet {cat: Cat, dog: Dog}` | | ||
| | unions variants | camelCase | `union Pet {cat: Cat, dog: Dog}` | | ||
| | alias | camelCase or PascalCase depending on context | `alias myString = string` or `alias MyPet = Pet` | | ||
| | decorators | camelCase | `@format`, `@resourceCollection` | | ||
| | functions | camelCase | `addedAfter` | | ||
| | file name | kebab-case | `my-lib.cadl` | | ||
|
|
||
| ## Layout convention | ||
|
|
||
| Cadl has a built-in formatter. See [formatter section](./formatter.md) for more information on how to use it. | ||
|
|
||
| - Use 2 space indenting | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| model Pet { | ||
| name: string; | ||
| } | ||
|
|
||
| // good | ||
| model Pet { | ||
| name: string; | ||
| } | ||
| ``` | ||
|
|
||
| - Place a space before an opening curly brace | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| model Pet{ | ||
| name: string; | ||
| } | ||
|
|
||
| // good | ||
| model Pet { | ||
| name: string; | ||
| } | ||
| ``` | ||
|
|
||
| - Block opening curly brace `{` should be on the same line | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| model Pet | ||
| { | ||
| name: string; | ||
| } | ||
|
|
||
| // good | ||
| model Pet { | ||
| name: string; | ||
| } | ||
| ``` | ||
|
|
||
| - Add a newline after blocks | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| model Pet { | ||
| name: string; | ||
| } | ||
| model Cat extends Pet {} | ||
|
|
||
| // good | ||
| model Pet { | ||
| name: string; | ||
| } | ||
|
|
||
| model Cat extends Pet {} | ||
| ``` | ||
|
|
||
| - Place no space between an operation/decorator/function name and the parmaeter list | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| op list (filter: string): Pet[]; | ||
|
|
||
| // bad | ||
| @doc ("This is a pet") | ||
|
|
||
| // good | ||
| op list(filter: string): Pet[]; | ||
|
|
||
| // good | ||
| @doc("This is a pet") | ||
| ``` | ||
|
|
||
| - Do not add spaces inside parentheses | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| op list( filter: string ): Pet[]; | ||
|
|
||
| // good | ||
| op list(filter: string): Pet[]; | ||
|
|
||
| ``` | ||
|
|
||
| - Add spaces inside curly braces. | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| alias foo = {type: "cat"}; | ||
|
|
||
| // good | ||
| alias foo = { type: "cat" }; | ||
| ``` | ||
|
|
||
| - Do not add space inside square brackets | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| // bad | ||
| alias foo = [ 1, 2, 3 ]; | ||
|
|
||
| // good | ||
| alias foo = [1, 2, 3]; | ||
| ``` | ||
|
|
||
| - Start all comments with a space | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```cadl | ||
| //bad | ||
|
|
||
| // good | ||
| ``` | ||
|
|
||
| - Avoid trailing spaces at the end of lines. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.