diff --git a/.config/ast-grep/rule-tests/__snapshots__/no-context-snapshot.yml b/.config/ast-grep/rule-tests/__snapshots__/no-context-snapshot.yml new file mode 100644 index 0000000000000..ff36ca0094d1f --- /dev/null +++ b/.config/ast-grep/rule-tests/__snapshots__/no-context-snapshot.yml @@ -0,0 +1,42 @@ +id: no-context +snapshots: + "fn foo(context: ChunkingContext) -> u32 { 5 };": + labels: + - source: context + style: primary + start: 7 + end: 14 + - source: "context: ChunkingContext" + style: secondary + start: 7 + end: 31 + foo(|context| context): + labels: + - source: context + style: primary + start: 5 + end: 12 + - source: "|context|" + style: secondary + start: 4 + end: 13 + let context = ChunkingContext::new();: + labels: + - source: context + style: primary + start: 4 + end: 11 + - source: let context = ChunkingContext::new(); + style: secondary + start: 0 + end: 37 + "struct Foo { context: Context };": + labels: + - source: context + style: primary + start: 13 + end: 20 + - source: "context: Context" + style: secondary + start: 13 + end: 29 diff --git a/.config/ast-grep/rule-tests/no-context-test.yml b/.config/ast-grep/rule-tests/no-context-test.yml new file mode 100644 index 0000000000000..ed8ebf148087b --- /dev/null +++ b/.config/ast-grep/rule-tests/no-context-test.yml @@ -0,0 +1,11 @@ +id: no-context +valid: + - "let chunking_context = ChunkingContext::new();" + - "struct Foo { chunking_context: Context };" + - "foo(|chunking_context| context)" + - "fn foo(chunking_context: ChunkingContext) -> u32 { 5 };" +invalid: + - "let context = ChunkingContext::new();" + - "struct Foo { context: Context };" + - "foo(|context| context)" + - "fn foo(context: ChunkingContext) -> u32 { 5 };" diff --git a/.config/ast-grep/rule-utils/.gitkeep b/.config/ast-grep/rule-utils/.gitkeep new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/.config/ast-grep/rules/no-context.yml b/.config/ast-grep/rules/no-context.yml new file mode 100644 index 0000000000000..b7f496e459454 --- /dev/null +++ b/.config/ast-grep/rules/no-context.yml @@ -0,0 +1,20 @@ +id: no-context +message: Don't name variables `context`. +note: Use a more specific name, such as chunking_context, asset_context, etc. +severity: warning +language: Rust +rule: + regex: \bcontext\b + any: + - all: + - inside: + any: + - kind: closure_parameters + - kind: parameter + - kind: function_type + - kind: let_declaration + - kind: identifier + - all: + - kind: field_identifier + - inside: + kind: field_declaration diff --git a/.gitattributes b/.gitattributes index af8824eb9b77e..bd24553281a47 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ +.config/ast-grep/rule-tests/__snapshots__/** linguist-generated=true crates/turbopack-tests/tests/snapshot/**/output/** linguist-generated=true crates/turborepo-lockfiles/fixtures/*.lock text eol=lf diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d94b321dd3b5..3b55ca38c9740 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -518,6 +518,10 @@ jobs: run: | cargo groups clippy turborepo-libraries --features rustls-tls + - name: Run ast-grep lints + run: | + npx --package @ast-grep/cli -- ast-grep scan $(cargo groups list turborepo-libraries | awk '{ print $2 }' | tr '\n' ' ') + turbopack_rust_check: needs: [determine_jobs] # We test dependency changes only on main @@ -569,6 +573,10 @@ jobs: run: | RUSTFLAGS="-D warnings -A deprecated" cargo groups clippy turbopack --features rustls-tls + - name: Run ast-grep lints + run: | + npx --package @ast-grep/cli -- ast-grep scan $(cargo groups list turbopack | awk '{ print $2 }' | tr '\n' ' ') + next_dev_check: needs: [determine_jobs] if: needs.determine_jobs.outputs.turbopack == 'true' || needs.determine_jobs.outputs.cargo_on_main == 'true' diff --git a/sgconfig.yml b/sgconfig.yml new file mode 100644 index 0000000000000..ae6f77b0db445 --- /dev/null +++ b/sgconfig.yml @@ -0,0 +1,6 @@ +ruleDirs: + - .config/ast-grep/rules +testConfigs: + - testDir: .config/ast-grep/rule-tests +utilDirs: + - .config/ast-grep/rule-utils