-
Notifications
You must be signed in to change notification settings - Fork 28
Line-matching rewriter #940
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0e3a6f2
Add expected line-matched test outputs
amomchilov a2443d9
Add `Translate::Validator`
amomchilov 73d57eb
Use Validator to validate test case expectations
amomchilov ca58d55
Move total line count validation to Validator
amomchilov 3fbaab2
Add `LineMatchingTranslator`
amomchilov b5ffd2d
Fix indent
amomchilov 7b7ef52
Ensure that rewriting annotations happens once per signature
vinistock 75ff215
Update exported RBIs
vinistock e7964c9
Add RBS_WRITTEN_ANNOTATION prefix to multiline annotations
vinistock 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -100,6 +100,8 @@ def visit_attr(node) | |
| location: "#{@file}:#{node.location.start_line}", | ||
| ) | ||
|
|
||
| known_annotations = nil #: Array[Spoom::RBS::Annotation]? | ||
|
|
||
| signatures.each do |signature| | ||
| attr_type = ::RBS::Parser.parse_type(signature.string) | ||
| sig = RBI::Sig.new | ||
|
|
@@ -118,17 +120,21 @@ def visit_attr(node) | |
|
|
||
| sig.return_type = @type_translator.translate(attr_type) | ||
|
|
||
| apply_member_annotations(comments.method_annotations, sig) | ||
| known_annotations = apply_member_annotations(comments.method_annotations, sig) | ||
|
|
||
| @rewriter << Source::Replace.new( | ||
| signature.location.start_offset, | ||
| signature.location.end_offset, | ||
| sig.string(max_line_length: @max_line_length), | ||
| pad_out_line_count(of: sig.string(max_line_length: @max_line_length), to_height_of: signature), | ||
| ) | ||
| rescue ::RBS::ParsingError, ::RBI::Error | ||
| # Ignore signatures with errors | ||
| next | ||
| end | ||
|
|
||
| if known_annotations | ||
| rewrite_member_annotations(comments.method_annotations, known: known_annotations) | ||
| end | ||
| end | ||
|
|
||
| #: (Prism::DefNode, Spoom::RBS::Comments) -> void | ||
|
|
@@ -146,6 +152,8 @@ def rewrite_def(def_node, comments) | |
| builder.visit(def_node) | ||
| rbi_node = builder.tree.nodes.first #: as RBI::Method | ||
|
|
||
| known_annotations = nil #: Array[Spoom::RBS::Annotation]? | ||
|
|
||
| signatures.each do |signature| | ||
| begin | ||
| method_type = ::RBS::Parser.parse_method_type(signature.string) | ||
|
|
@@ -163,7 +171,7 @@ def rewrite_def(def_node, comments) | |
|
|
||
| sig = translator.result | ||
|
|
||
| apply_member_annotations(comments.method_annotations, sig) | ||
| known_annotations = apply_member_annotations(comments.method_annotations, sig) | ||
|
|
||
| # Sorbet runtime doesn't support `sig` on `method_added` or | ||
| # `singleton_method_added`, so we always use `without_runtime` for them. | ||
|
|
@@ -174,9 +182,13 @@ def rewrite_def(def_node, comments) | |
| @rewriter << Source::Replace.new( | ||
| signature.location.start_offset, | ||
| signature.location.end_offset, | ||
| sig.string(max_line_length: @max_line_length), | ||
| pad_out_line_count(of: sig.string(max_line_length: @max_line_length), to_height_of: signature), | ||
| ) | ||
| end | ||
|
|
||
| if known_annotations | ||
| rewrite_member_annotations(comments.method_annotations, known: known_annotations) | ||
| end | ||
| end | ||
|
|
||
| #: (Array[Spoom::RBS::Signature], method_name: String, location: String) -> Array[Spoom::RBS::Signature] | ||
|
|
@@ -187,27 +199,26 @@ def apply_overloads_strategy(signatures, method_name:, location:) | |
| when :translate_all | ||
| signatures | ||
| when :translate_last | ||
| kept = signatures.last #: as Spoom::RBS::Signature | ||
| others = signatures[0...-1] #: as !nil | ||
| others.each { |signature| rewrite_discarded_overload(signature) } | ||
|
|
||
| # Delete all the signatures we didn't keep | ||
| others.each do |signature| | ||
| from = adjust_to_line_start(signature.location.start_offset) | ||
| to = adjust_to_line_end(signature.location.end_offset) | ||
| @rewriter << Source::Delete.new(from, to) | ||
| end | ||
| kept = signatures.last #: as Spoom::RBS::Signature | ||
| [kept] | ||
| else # :raise | ||
| raise Error, "Method `#{method_name}` at #{location} has multiple overloaded signatures" | ||
| end | ||
| end | ||
|
|
||
| # Called for every overloaded method sig that we discard because it wasn't the last one. | ||
| # @abstract | ||
| #: (Spoom::RBS::Signature) -> void | ||
| def rewrite_discarded_overload(signature) = raise | ||
|
|
||
| #: (PrismTypes::anyScopeNode) -> void | ||
| def apply_class_annotations(node) | ||
| comments = node_rbs_comments(node) | ||
| return if comments.empty? | ||
|
|
||
| indent = " " * (node.location.start_column + 2) | ||
| insert_pos = case node | ||
| when Prism::ClassNode | ||
| (node.superclass || node.constant_path).location.end_offset | ||
|
|
@@ -217,16 +228,14 @@ def apply_class_annotations(node) | |
| node.expression.location.end_offset | ||
| end | ||
|
|
||
| class_annotations = comments.class_annotations | ||
| if class_annotations.any? | ||
| # Only translate (and `extend T::Helpers`) when there's at least one *known* class | ||
| # annotation. A node with only unknown annotations (e.g. `@private`) is left untouched. | ||
| if comments.class_annotations.any? | ||
| unless already_extends?(node, /^(::)?T::Helpers$/) | ||
| @rewriter << Source::Insert.new(insert_pos, "\n#{indent}extend T::Helpers\n") | ||
| extend_with("T::Helpers", into: node, at: insert_pos) | ||
| end | ||
|
|
||
| class_annotations.reverse_each do |annotation| | ||
| from = adjust_to_line_start(annotation.location.start_offset) | ||
| to = adjust_to_line_end(annotation.location.end_offset) | ||
|
|
||
| comments.annotations.reverse_each do |annotation| | ||
| content = case annotation.string | ||
| when "@abstract" | ||
| "abstract!" | ||
|
|
@@ -241,15 +250,13 @@ def apply_class_annotations(node) | |
| rbs_type = @type_translator.translate(srb_type) | ||
| "requires_ancestor { #{rbs_type} }" | ||
| else | ||
| apply_class_annotation(annotation, parent_node: node, insert_pos:, sorbet_replacement: nil) | ||
| next | ||
| end | ||
|
|
||
| @rewriter << Source::Delete.new(from, to) | ||
|
|
||
| newline = node.body.nil? ? "" : "\n" | ||
| @rewriter << Source::Insert.new(insert_pos, "\n#{indent}#{content}#{newline}") | ||
| apply_class_annotation(annotation, parent_node: node, insert_pos:, sorbet_replacement: content) | ||
| rescue ::RBS::ParsingError, ::RBI::Error | ||
| # Ignore annotations with errors | ||
| apply_class_annotation(annotation, parent_node: node, insert_pos:, sorbet_replacement: nil) | ||
| next | ||
| end | ||
| end | ||
|
|
@@ -261,14 +268,11 @@ def apply_class_annotations(node) | |
| next unless signature.string.start_with?("[") | ||
|
|
||
| type_params = ::RBS::Parser.parse_type_params(signature.string) | ||
| rewrite_type_params_signature(signature, type_params:) | ||
| next if type_params.empty? | ||
|
|
||
| from = adjust_to_line_start(signature.location.start_offset) | ||
| to = adjust_to_line_end(signature.location.end_offset) | ||
| @rewriter << Source::Delete.new(from, to) | ||
|
|
||
| unless already_extends?(node, /^(::)?T::Generic$/) | ||
| @rewriter << Source::Insert.new(insert_pos, "\n#{indent}extend T::Generic\n") | ||
| extend_with("T::Generic", into: node, at: insert_pos) | ||
| end | ||
|
|
||
| type_params.each do |type_param| | ||
|
|
@@ -293,8 +297,7 @@ def apply_class_annotations(node) | |
| end | ||
| end | ||
|
|
||
| newline = node.body.nil? ? "" : "\n" | ||
| @rewriter << Source::Insert.new(insert_pos, "\n#{indent}#{type_member}#{newline}") | ||
| insert_type_member(type_member, parent_node: node, insert_pos:) | ||
| rescue ::RBS::ParsingError, ::RBI::Error | ||
| # Ignore signatures with errors | ||
| next | ||
|
|
@@ -303,8 +306,31 @@ def apply_class_annotations(node) | |
| end | ||
| end | ||
|
|
||
| #: (Array[Spoom::RBS::Annotation], RBI::Sig) -> void | ||
| # @param is_known: true if this is an RBS annotation that we recognize | ||
| # false for some other `@`-prefixed thing, like a documentation `@param` tag. | ||
| # @abstract | ||
| #: ( | ||
| #| Spoom::RBS::Annotation, | ||
| #| parent_node: PrismTypes::anyScopeNode, | ||
| #| insert_pos: Integer, | ||
| #| sorbet_replacement: String? | ||
| #| ) -> void | ||
| def apply_class_annotation(annotation, parent_node:, insert_pos:, sorbet_replacement:) = raise | ||
|
|
||
| # Rewrites the `#: [...]` type params comment (e.g. delete it, or mark it as translated). | ||
| # @abstract | ||
| #: (Spoom::RBS::Signature, type_params: Array[::RBS::AST::TypeParam]) -> void | ||
| def rewrite_type_params_signature(signature, type_params:) = raise | ||
|
|
||
| # Inserts a single `type_member` declaration into the class/module body. | ||
| # @abstract | ||
| #: (String type_member, parent_node: PrismTypes::anyScopeNode, insert_pos: Integer) -> void | ||
| def insert_type_member(type_member, parent_node:, insert_pos:) = raise | ||
|
|
||
| #: (Array[Spoom::RBS::Annotation], RBI::Sig) -> Array[Spoom::RBS::Annotation] | ||
| def apply_member_annotations(annotations, sig) | ||
| known = [] #: Array[Spoom::RBS::Annotation] | ||
|
|
||
| annotations.each do |annotation| | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll follow-up to tidy this with |
||
| case annotation.string | ||
| when "@abstract" | ||
|
|
@@ -323,10 +349,37 @@ def apply_member_annotations(annotations, sig) | |
| sig.is_overridable = true | ||
| when "@without_runtime" | ||
| sig.without_runtime = true | ||
| else | ||
| next | ||
| end | ||
|
|
||
| known << annotation | ||
| end | ||
|
|
||
| known | ||
| end | ||
|
|
||
| # Rewrites the member annotation comments in the source. Called once per method, | ||
| # regardless of how many overloaded signatures share the annotations, to avoid | ||
| # emitting duplicate markers. | ||
| # | ||
| #: (Array[Spoom::RBS::Annotation], known: Array[Spoom::RBS::Annotation]) -> void | ||
| def rewrite_member_annotations(annotations, known:) | ||
| annotations.each do |annotation| | ||
| rewrite_annotation(annotation, is_known: known.include?(annotation)) | ||
| end | ||
| end | ||
|
|
||
| # @param is_known: true if this is an RBS annotation that we recognize | ||
| # false for some other `@`-prefixed thing, like a documentation `@param` tag. | ||
| # @overridable | ||
| #: (Spoom::RBS::Annotation, is_known: bool) -> void | ||
| def rewrite_annotation(annotation, is_known:) = nil # no-op | ||
|
|
||
| # @abstract | ||
| #: (String mixin_name, into: PrismTypes::anyScopeNode, at: Integer) -> void | ||
| def extend_with(mixin_name, into:, at:) = raise | ||
|
|
||
| #: (PrismTypes::anyScopeNode, Regexp) -> bool | ||
| def already_extends?(node, constant_regex) | ||
| node.child_nodes.any? do |c| | ||
|
|
@@ -408,12 +461,22 @@ def apply_type_aliases(comments) | |
|
|
||
| @rewriter << Source::Delete.new(from, to) | ||
| content = "#{indent}#{alias_name} = T.type_alias { #{sorbet_type.to_rbi} }\n" | ||
| content = pad_out_line_count(of: content, to_height_of: type_alias) | ||
| @rewriter << Source::Insert.new(insert_pos, content) | ||
| rescue ::RBS::ParsingError, ::RBI::Error | ||
| # Ignore type aliases with errors | ||
| next | ||
| end | ||
| end | ||
|
|
||
| # @overridable | ||
| #: (of: String, to_height_of: Spoom::RBS::Comment) -> String | ||
| def pad_out_line_count(of:, to_height_of:) | ||
| replacement = of | ||
|
|
||
| # no-op implementation | ||
| replacement | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or 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.
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.
These are making this class harder to read but I'm not sure what we can do. One alternative I kinda like it to move these abstract methods to
HumanReadableTranslatorinstead. Call sites in this class would then delegate to the appropriate "mode" that's being set,@translation_mode.apply_class_anotation. It wouldn't get rid of the abstract but make the intent more clear. Wdyt? Maybe as a future improvement to not hold up the PR.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.
@amomchilov I'll leave this one for your assessment. I agree that something along the lines of what Kaan suggested would make the code a bit easier to follow.
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.
Same. Will follow-up.