Skip to content

Commit ea777eb

Browse files
catalog: add kotlin example
fix #663
1 parent 000d803 commit ea777eb

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

website/catalog/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
2020
* [Extract i18n Keys](/catalog/html/#extract-i18n-keys)
2121
* [Java](/catalog/java/)
2222
* [No Unused Vars](/catalog/java/#no-unused-vars)
23+
* [Kotlin](/catalog/kotlin/)
24+
* [Ensure Clean Architecture](/catalog/kotlin/#ensure-clean-architecture)
2325
* [Python](/catalog/python/)
2426
* [Migrate OpenAi SDK](/catalog/python/#migrate-openai-sdk)
2527
* [Use Walrus Operator in `if` statement](/catalog/python/#use-walrus-operator-in-if-statement)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!-- Remove Badge if it does not have fix-->
2+
## Ensure Clean Architecture
3+
4+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImtvdGxpbiIsInF1ZXJ5IjoiIiwicmV3cml0ZSI6IiIsInN0cmljdG5lc3MiOiJyZWxheGVkIiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogaW1wb3J0LWRlcGVuZGVuY3ktdmlvbGF0aW9uXG5tZXNzYWdlOiBJbXBvcnQgRGVwZW5kZW5jeSBWaW9sYXRpb24gXG5ub3RlczogRW5zdXJlcyB0aGF0IGltcG9ydHMgY29tcGx5IHdpdGggYXJjaGl0ZWN0dXJhbCBydWxlcy4gXG5zZXZlcml0eTogZXJyb3JcbnJ1bGU6XG4gIHBhdHRlcm46IGltcG9ydCAkUEFUSFxuY29uc3RyYWludHM6XG4gIFBBVEg6XG4gICAgYW55OlxuICAgIC0gcmVnZXg6IGNvbVxcLmV4YW1wbGUoXFwuXFx3KykqXFwuZGF0YVxuICAgIC0gcmVnZXg6IGNvbVxcLmV4YW1wbGUoXFwuXFx3KykqXFwucHJlc2VudGF0aW9uXG5maWxlczpcbi0gY29tL2V4YW1wbGUvZG9tYWluLyoqLyoua3QiLCJzb3VyY2UiOiJpbXBvcnQgYW5kcm9pZHgubGlmZWN5Y2xlLlZpZXdNb2RlbFxuaW1wb3J0IGFuZHJvaWR4LmxpZmVjeWNsZS5WaWV3TW9kZWxTY29wZVxuaW1wb3J0IGNvbS5leGFtcGxlLmN1c3RvbWxpbnRleGFtcGxlLmRhdGEubW9kZWxzLlVzZXJEdG9cbmltcG9ydCBjb20uZXhhbXBsZS5jdXN0b21saW50ZXhhbXBsZS5kb21haW4udXNlY2FzZXMuR2V0VXNlclVzZUNhc2VcbmltcG9ydCBjb20uZXhhbXBsZS5jdXN0b21saW50ZXhhbXBsZS5wcmVzZW50YXRpb24uc3RhdGVzLk1haW5TdGF0ZVxuaW1wb3J0IGRhZ2dlci5oaWx0LmFuZHJvaWQubGlmZWN5Y2xlLkhpbHRWaWV3TW9kZWwifQ==)
5+
6+
### Description
7+
8+
This ast-grep rule ensures that the **domain** package in a [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) project does not import classes from the **data** or **presentation** packages. It enforces the separation of concerns by preventing the domain layer from depending on other layers, maintaining the integrity of the architecture.
9+
10+
For example, the rule will trigger an error if an import statement like `import com.example.data.SomeClass` or `import com.example.presentation.AnotherClass` is found within the domain package.
11+
12+
The rule uses the [`files`](/reference/yaml.html#files) field to apply only to the domain package.
13+
14+
### YAML
15+
16+
```yaml
17+
id: import-dependency-violation
18+
message: Import Dependency Violation
19+
notes: Ensures that imports comply with architectural rules.
20+
severity: error
21+
rule:
22+
pattern: import $PATH # capture the import statement
23+
constraints:
24+
PATH: # find specific package imports
25+
any:
26+
- regex: com\.example(\.\w+)*\.data
27+
- regex: com\.example(\.\w+)*\.presentation
28+
files: # apply only to domain package
29+
- com/example/domain/**/*.kt
30+
```
31+
32+
### Example
33+
34+
<!-- highlight matched code in curly-brace {lineNum} -->
35+
```kotlin {3,5}
36+
import androidx.lifecycle.ViewModel
37+
import androidx.lifecycle.ViewModelScope
38+
import com.example.customlintexample.data.models.UserDto
39+
import com.example.customlintexample.domain.usecases.GetUserUseCase
40+
import com.example.customlintexample.presentation.states.MainState
41+
import dagger.hilt.android.lifecycle.HiltViewModel
42+
```
43+
44+
### Contributed by
45+
Inspired by the post [Custom Lint Task Configuration in Gradle with Kotlin DSL](https://www.sngular.com/insights/320/custom-lint-task-configuration-in-gradle-with-kotlin-dsl)

website/catalog/kotlin/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Kotlin
2+
3+
This page curates a list of example ast-grep rules to check and to rewrite Kotlin code.
4+
5+
<!--@include: ./ensure-clean-architecture.md-->

0 commit comments

Comments
 (0)