-
Notifications
You must be signed in to change notification settings - Fork 168
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 translated Prism AST to run RuboCop #1849
Draft
vinistock
wants to merge
2
commits into
main
Choose a base branch
from
vs/use_prism_translator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
begin | ||
gem("rubocop", ">= 1.63.0") | ||
rescue LoadError | ||
$stderr.puts("AST translation turned off because RuboCop >= 1.63.0 is required") | ||
return | ||
end | ||
|
||
require "prism/translation/parser/rubocop" | ||
|
||
# Processed Source patch so that we can pass the existing AST to RuboCop without having to re-parse files a second time | ||
module ProcessedSourcePatch | ||
extend T::Sig | ||
|
||
sig do | ||
params( | ||
source: String, | ||
ruby_version: Float, | ||
path: T.nilable(String), | ||
parser_engine: Symbol, | ||
prism_result: T.nilable(Prism::ParseLexResult), | ||
).void | ||
end | ||
def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark, prism_result: nil) | ||
@prism_result = prism_result | ||
|
||
# Invoking super will end up invoking our patched version of tokenize, which avoids re-parsing the file | ||
super(source, ruby_version, path, parser_engine: parser_engine) | ||
end | ||
|
||
sig { params(parser: T.untyped).returns(T::Array[T.untyped]) } | ||
def tokenize(parser) | ||
begin | ||
# This is where we need to pass the existing result to prevent a re-parse | ||
ast, comments, tokens = parser.tokenize(@buffer, parse_result: @prism_result) | ||
|
||
ast ||= nil | ||
rescue Parser::SyntaxError | ||
comments = [] | ||
tokens = [] | ||
end | ||
|
||
ast&.complete! | ||
tokens.map! { |t| RuboCop::AST::Token.from_parser_token(t) } | ||
|
||
[ast, comments, tokens] | ||
end | ||
|
||
RuboCop::AST::ProcessedSource.prepend(self) | ||
end | ||
|
||
# This patch allows Prism's translation parser to accept an existing AST in `tokenize`. This doesn't match the original | ||
# signature of RuboCop itself, but there's no other way to allow reusing the AST | ||
module TranslatorPatch | ||
extend T::Sig | ||
extend T::Helpers | ||
|
||
requires_ancestor { Prism::Translation::Parser } | ||
|
||
sig do | ||
params( | ||
source_buffer: ::Parser::Source::Buffer, | ||
recover: T::Boolean, | ||
parse_result: T.nilable(Prism::ParseLexResult), | ||
).returns(T::Array[T.untyped]) | ||
end | ||
def tokenize(source_buffer, recover = false, parse_result: nil) | ||
@source_buffer = source_buffer | ||
source = source_buffer.source | ||
|
||
offset_cache = build_offset_cache(source) | ||
result = if @prism_result | ||
@prism_result | ||
else | ||
begin | ||
unwrap( | ||
Prism.parse_lex(source, filepath: source_buffer.name, version: convert_for_prism(version)), | ||
offset_cache, | ||
) | ||
rescue ::Parser::SyntaxError | ||
raise unless recover | ||
end | ||
end | ||
|
||
program, tokens = result.value | ||
ast = build_ast(program, offset_cache) if result.success? | ||
|
||
[ | ||
ast, | ||
build_comments(result.comments, offset_cache), | ||
build_tokens(tokens, offset_cache), | ||
] | ||
ensure | ||
@source_buffer = nil | ||
end | ||
|
||
Prism::Translation::Parser.prepend(self) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# typed: true | ||
|
||
class RuboCop::Runner | ||
def initialize(options, config_store) | ||
@config_store = T.let(T.unsafe(nil), RuboCop::ConfigStore) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should pull the ast out of this result here, so that
tree
can just be anattr_reader