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

+ builder.rb: warn it in a block with no ordinary params. #980

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@ def accessible(node)
end

unless @parser.static_env.declared?(name)
if @parser.version == 33 &&
name == :it &&
@parser.context.in_block &&
[email protected]_numparam_stack.has_ordinary_params?
diagnostic :warning, :ambiguous_it_call, nil, node.loc.expression
end

return n(:send, [ nil, name ],
var_send_map(node))
end
Expand Down
1 change: 1 addition & 0 deletions lib/parser/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module Parser
# Parser warnings
:useless_else => 'else without rescue is useless',
:duplicate_hash_key => 'key is duplicated and overwritten',
:ambiguous_it_call => '`it` calls without arguments refers to the first block param',

# Parser errors that are not Ruby errors
:invalid_encoding => 'literal contains escape sequences incompatible with UTF-8',
Expand Down
31 changes: 31 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11409,4 +11409,35 @@ def test_ungettable_gvar
' ^^^^^^ location',
ALL_VERSIONS)
end

def test_it_warning_in_33
refute_diagnoses(
'if false; it; end',
ALL_VERSIONS)
refute_diagnoses(
'def foo; it; end',
ALL_VERSIONS)
assert_diagnoses(
[:warning, :ambiguous_it_call, {}],
'0.times { it }',
' ^^ location',
['3.3'])
refute_diagnoses(
'0.times { || it }',
ALL_VERSIONS)
refute_diagnoses(
'0.times { |_n| it }',
ALL_VERSIONS)
assert_diagnoses(
[:warning, :ambiguous_it_call, {}],
'0.times { it; it = 1; it }',
' ^^ location',
['3.3'])
refute_diagnoses(
'0.times { it = 1; it }',
ALL_VERSIONS)
refute_diagnoses(
'it = 1; 0.times { it }',
ALL_VERSIONS)
end
end