Skip to content
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

Use CommentRanges from the parsed output #11591

Merged
merged 2 commits into from
May 29, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 29, 2024

Summary

This PR updates the usages of CommentRanges from the Indexer to the parsed output.

First, the CommentRanges are now built during the parsing step. In the future, depending on the performance numbers, it could be updated to be done after the parsing step. Nevertheless, the struct will be owned by the parsed output. So, all references should now use the struct from the parsed output instead of the indexer.

Now, the motivation to build CommentRanges while parsing is so that it can be shared between the linter and the formatter as both requires it. This also removes the need to have a dedicated method (tokens_and_ranges).

There are two main updates:

  1. The functions which only require CommentRanges are passed in the type directly
  2. If the functions require other fields from Program, then the program is passed instead

@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label May 29, 2024
@dhruvmanila dhruvmanila marked this pull request as ready for review May 29, 2024 06:01
Base automatically changed from dhruv/token-flags to dhruv/parser-phase-2 May 29, 2024 06:13
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevertheless, the struct will be owned by the parsed output. So, all references should now use the struct from the parsed output instead of the indexer.

I'm not sure if this will remain true if we build the comment ranges after parsing. One of the benefits of building it after parsing is that we can make it optional and only run it when the comment ranges are needed.

crates/ruff_linter/src/checkers/tokens.rs Outdated Show resolved Hide resolved
crates/ruff_linter/src/linter.rs Show resolved Hide resolved
Comment on lines 170 to 172
if !checker
.indexer()
.program()
.comment_ranges()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a case where it could make sense to simply expose comment_ranges on checker so that it matters less where the comment ranges are actually stored. I don't think it is necessary to change now but it would e.g. help if we move comment ranges in the future again.

Overall, I think this is a weakness of our checker API. Lint rules need to know too much about individual representations. I think it would be better if there's a single context and the rules could simply ask:

  • Give me the comment ranges
  • give me the indexer
  • give me the line index

etc. without having to do deep drilling.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I completely agree with your sentiment.

@dhruvmanila dhruvmanila merged commit c0054de into dhruv/parser-phase-2 May 29, 2024
6 of 18 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/comment-ranges branch May 29, 2024 08:34
dhruvmanila added a commit that referenced this pull request May 30, 2024
## Summary

This PR updates the usages of `CommentRanges` from the `Indexer` to the
parsed output.

First, the `CommentRanges` are now built during the parsing step. In the
future, depending on the performance numbers, it could be updated to be
done after the parsing step. Nevertheless, the struct will be owned by
the parsed output. So, all references should now use the struct from the
parsed output instead of the indexer.

Now, the motivation to build `CommentRanges` while parsing is so that it
can be shared between the linter and the formatter as both requires it.
This also removes the need to have a dedicated method
(`tokens_and_ranges`).

There are two main updates:
1. The functions which only require `CommentRanges` are passed in the
type directly
2. If the functions require other fields from `Program`, then the
program is passed instead
dhruvmanila added a commit that referenced this pull request May 31, 2024
## Summary

This PR updates the usages of `CommentRanges` from the `Indexer` to the
parsed output.

First, the `CommentRanges` are now built during the parsing step. In the
future, depending on the performance numbers, it could be updated to be
done after the parsing step. Nevertheless, the struct will be owned by
the parsed output. So, all references should now use the struct from the
parsed output instead of the indexer.

Now, the motivation to build `CommentRanges` while parsing is so that it
can be shared between the linter and the formatter as both requires it.
This also removes the need to have a dedicated method
(`tokens_and_ranges`).

There are two main updates:
1. The functions which only require `CommentRanges` are passed in the
type directly
2. If the functions require other fields from `Program`, then the
program is passed instead
dhruvmanila added a commit that referenced this pull request Jun 3, 2024
## Summary

This PR updates the usages of `CommentRanges` from the `Indexer` to the
parsed output.

First, the `CommentRanges` are now built during the parsing step. In the
future, depending on the performance numbers, it could be updated to be
done after the parsing step. Nevertheless, the struct will be owned by
the parsed output. So, all references should now use the struct from the
parsed output instead of the indexer.

Now, the motivation to build `CommentRanges` while parsing is so that it
can be shared between the linter and the formatter as both requires it.
This also removes the need to have a dedicated method
(`tokens_and_ranges`).

There are two main updates:
1. The functions which only require `CommentRanges` are passed in the
type directly
2. If the functions require other fields from `Program`, then the
program is passed instead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants