Skip to content

Commit

Permalink
add more ResolvedVc best-practices lints (#73331)
Browse files Browse the repository at this point in the history
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
  • Loading branch information
arlyon authored Dec 3, 2024
1 parent 13880d3 commit 2e65412
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .config/ast-grep/rule-tests/resolved-vc-in-return-type-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
id: resolved-vc-in-return-type
valid:
- |
pub async fn ignore_this(x: ResolvedVc<MyType>) -> Result<ResolvedVc<MyType>> {}
- |
pub trait OtherTrait() {
fn ignore_this() -> ResolvedVc<MyType>;
}
invalid:
- |
#[turbo_tasks::function]
pub async fn flag_this(x: ResolvedVc<MyType>) -> ResolvedVc<MyType> {}
- |
#[turbo_tasks::function]
pub async fn flag_this_too(x: ResolvedVc<MyType>) -> Result<ResolvedVc<MyType>> {}
- |
#[turbo_tasks::value_trait]
pub trait ValueTrait() {
fn flag_this() -> ResolvedVc<MyType>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: resolved-vc-in-trait-arguments
valid:
- |
#[turbo_tasks::value_trait]
pub trait Example {
fn write_to_disk(self: Vc<Self>);
fn write_to_disk(self: ResolvedVc<Self>) {}
}
invalid:
- |
#[turbo_tasks::value_trait]
pub trait Example {
fn write_to_disk(self: ResolvedVc<Self>);
}
52 changes: 52 additions & 0 deletions .config/ast-grep/rules/resolved-vc-in-return-type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json

id: resolved-vc-in-return-type
message: Returning a `ResolvedVc` in a turbo task is not recommended. Consider replacing `$TYPE` in `$FN_NAME` with `$RESOLVED_VC` instead.
severity: warning # error, warning, info, hint
language: Rust
rule:
any:
# turbo tasks function
- all:
- pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
- pattern: $TYPE
inside:
kind: function_item
field: return_type
stopBy: end
follows:
pattern: '#[turbo_tasks::function]'
has:
kind: identifier
pattern: $FN_NAME
# turbo tasks value trait
- all:
- pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
- pattern: $TYPE
inside:
kind: function_signature_item
field: return_type
stopBy: end
inside:
inside:
kind: trait_item
follows:
pattern: '#[turbo_tasks::value_trait]'
has:
kind: identifier
pattern: $FN_NAME
rewriters:
- id: rewrite-vc
rule:
pattern: $TYPE
fix: Vc<$A>
transform:
RESOLVED_VC:
rewrite:
rewriters: [rewrite-vc]
source: $TYPE
fix: $RESOLVED_VC
20 changes: 20 additions & 0 deletions .config/ast-grep/rules/resolved-vc-in-trait.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json

id: resolved-vc-in-trait-arguments
message: Using `ResolvedVc` in a turbo tasks trait is unsound. Consider making `$FN_NAME` take `Vc` instead.
severity: warning # error, warning, info, hint
language: Rust
rule:
pattern:
context: 'let x: ResolvedVc<$A> = 1;'
selector: generic_type
inside:
kind: function_signature_item
stopBy: end
pattern: fn $FN_NAME($_ARGS)
inside:
inside:
kind: trait_item
follows:
pattern: '#[turbo_tasks::value_trait]'
fix: Vc<$A>
30 changes: 30 additions & 0 deletions .config/ast-grep/rules/to-resolved-in-loop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json

id: to-resolved-in-loop
message: 'Calling `$EXPR` in a loop could be a doing a lot of work sequentially. Consider producing an iterator of futures and using `try_join`.'
severity: info # error, warning, info, hint
language: Rust
rule:
kind: for_expression
pattern: for $VAR in $ITER {$$$_ARGS}
has:
kind: try_expression
stopBy: end
all:
- pattern: $ARG.to_resolved().await?
- pattern: $EXPR
# ignore sequential application
- not:
inside:
kind: assignment_expression
pattern: '*$VAR = $_EXPR'
# ignore conditionals via match
- not:
inside:
kind: match_arm
stopBy: end
# ignore conditionals via if
- not:
inside:
kind: if_expression
stopBy: end

0 comments on commit 2e65412

Please sign in to comment.