Skip to content

Commit 7ee56e9

Browse files
authored
Add code style guide for Angular & TypeScript import statements (#556)
* Add style guide for import statements
1 parent 97bad9b commit 7ee56e9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/contributing/code-style/angular.md

+36
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,39 @@ Some appropriate operators are:
158158
consider carefully if this is the right operator for your use case. mergeMap will flatten
159159
observables but not care about the order. If ordering is important use `concatMap`. If you only
160160
care about the latest value use `switchMap`.
161+
162+
## Import statements
163+
164+
We have a couple of guidelines for import statements, which are enforced using the eslint rules
165+
[`no-restricted-imports`](https://eslint.org/docs/latest/rules/no-restricted-imports),
166+
[`import/order`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md),
167+
and
168+
[`import/no-restricted-paths`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-restricted-paths.md).
169+
170+
These rules aim to:
171+
172+
- Prevent relative imports across package boundaries.
173+
- Restrict packages from importing application specific code.
174+
- Enforce a convention for the order of import statements.
175+
176+
### Imports within the same package
177+
178+
Use relative imports when importing within the same package. For example, `MyNewService` and
179+
`LogService` are both in the `@bitwarden/common` package.
180+
181+
```typescript
182+
import { LogService } from "../../abstractions/log.service";
183+
184+
export class MyNewService {}
185+
```
186+
187+
### Imports from different packages
188+
189+
For imports from different packages, use absolute imports. For example `DifferentPackageService` is
190+
not in `@bitwarden/common` and needs to import `LogService` from `@bitwarden/common`.
191+
192+
```typescript
193+
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
194+
195+
export class DifferentPackageService {}
196+
```

0 commit comments

Comments
 (0)