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

Add forwarded_kwrestarg node to AST::Builder #259

Merged
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
1 change: 1 addition & 0 deletions changelog/new_add_forwarded_kwrestarg_to_ast_builder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#259](https://github.com/rubocop/rubocop-ast/pull/259): Add `forwarded_kwrestarg` node to `AST::Builder`. ([@koic][])
141 changes: 71 additions & 70 deletions lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,76 +19,77 @@ class Builder < Parser::Builders::Default

# @api private
NODE_MAP = {
and: AndNode,
and_asgn: AndAsgnNode,
alias: AliasNode,
arg: ArgNode,
blockarg: ArgNode,
forward_arg: ArgNode,
kwarg: ArgNode,
kwoptarg: ArgNode,
kwrestarg: ArgNode,
optarg: ArgNode,
restarg: ArgNode,
shadowarg: ArgNode,
args: ArgsNode,
array: ArrayNode,
lvasgn: AsgnNode,
ivasgn: AsgnNode,
cvasgn: AsgnNode,
gvasgn: AsgnNode,
block: BlockNode,
numblock: BlockNode,
break: BreakNode,
case_match: CaseMatchNode,
casgn: CasgnNode,
case: CaseNode,
class: ClassNode,
const: ConstNode,
def: DefNode,
defined?: DefinedNode,
defs: DefNode,
dstr: DstrNode,
ensure: EnsureNode,
for: ForNode,
forward_args: ForwardArgsNode,
float: FloatNode,
hash: HashNode,
if: IfNode,
in_pattern: InPatternNode,
int: IntNode,
index: IndexNode,
indexasgn: IndexasgnNode,
irange: RangeNode,
erange: RangeNode,
kwargs: HashNode,
kwsplat: KeywordSplatNode,
lambda: LambdaNode,
module: ModuleNode,
next: NextNode,
op_asgn: OpAsgnNode,
or_asgn: OrAsgnNode,
or: OrNode,
pair: PairNode,
procarg0: Procarg0Node,
regexp: RegexpNode,
rescue: RescueNode,
resbody: ResbodyNode,
return: ReturnNode,
csend: CsendNode,
send: SendNode,
str: StrNode,
xstr: StrNode,
sclass: SelfClassNode,
super: SuperNode,
zsuper: SuperNode,
sym: SymbolNode,
until: UntilNode,
until_post: UntilNode,
when: WhenNode,
while: WhileNode,
while_post: WhileNode,
yield: YieldNode
and: AndNode,
and_asgn: AndAsgnNode,
alias: AliasNode,
arg: ArgNode,
blockarg: ArgNode,
forward_arg: ArgNode,
kwarg: ArgNode,
kwoptarg: ArgNode,
kwrestarg: ArgNode,
optarg: ArgNode,
restarg: ArgNode,
shadowarg: ArgNode,
args: ArgsNode,
array: ArrayNode,
lvasgn: AsgnNode,
ivasgn: AsgnNode,
cvasgn: AsgnNode,
gvasgn: AsgnNode,
block: BlockNode,
numblock: BlockNode,
break: BreakNode,
case_match: CaseMatchNode,
casgn: CasgnNode,
case: CaseNode,
class: ClassNode,
const: ConstNode,
def: DefNode,
defined?: DefinedNode,
defs: DefNode,
dstr: DstrNode,
ensure: EnsureNode,
for: ForNode,
forward_args: ForwardArgsNode,
forwarded_kwrestarg: KeywordSplatNode,
Copy link
Member Author

Choose a reason for hiding this comment

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

This line is the only one added in this map.

float: FloatNode,
hash: HashNode,
if: IfNode,
in_pattern: InPatternNode,
int: IntNode,
index: IndexNode,
indexasgn: IndexasgnNode,
irange: RangeNode,
erange: RangeNode,
kwargs: HashNode,
kwsplat: KeywordSplatNode,
lambda: LambdaNode,
module: ModuleNode,
next: NextNode,
op_asgn: OpAsgnNode,
or_asgn: OrAsgnNode,
or: OrNode,
pair: PairNode,
procarg0: Procarg0Node,
regexp: RegexpNode,
rescue: RescueNode,
resbody: ResbodyNode,
return: ReturnNode,
csend: CsendNode,
send: SendNode,
str: StrNode,
xstr: StrNode,
sclass: SelfClassNode,
super: SuperNode,
zsuper: SuperNode,
sym: SymbolNode,
until: UntilNode,
until_post: UntilNode,
when: WhenNode,
while: WhileNode,
while_post: WhileNode,
yield: YieldNode
}.freeze

# Generates {Node} from the given information.
Expand Down
13 changes: 10 additions & 3 deletions lib/rubocop/ast/node/keyword_splat_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module RuboCop
module AST
# A node extension for `kwsplat` nodes. This will be used in place of a
# plain node when the builder constructs the AST, making its methods
# available to all `kwsplat` nodes within RuboCop.
# A node extension for `kwsplat` and `forwarded_kwrestarg` nodes. This will be used in
# place of a plain node when the builder constructs the AST, making its methods available to
# all `kwsplat` and `forwarded_kwrestarg` nodes within RuboCop.
class KeywordSplatNode < Node
include HashElementNode

Expand Down Expand Up @@ -41,6 +41,13 @@ def operator
def node_parts
[self, self]
end

# This provides `forwarded_kwrestarg` node to return true to be compatible with `kwsplat` node.
#
# @return [true]
def kwsplat_type?
true
end
end
end
end
64 changes: 52 additions & 12 deletions spec/rubocop/ast/keyword_splat_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,83 @@

RSpec.describe RuboCop::AST::KeywordSplatNode do
let(:kwsplat_node) { parse_source(source).ast.children.last }
let(:source) { '{ a: 1, **foo }' }

describe '.new' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node).to be_a(described_class) }
end

describe '#hash_rocket?' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node).not_to be_hash_rocket }
end

describe '#colon?' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node).not_to be_colon }
end

describe '#key' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node.key).to eq(kwsplat_node) }
end

describe '#value' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node.value).to eq(kwsplat_node) }
end

describe '#operator' do
let(:source) { '{ a: 1, **foo }' }

it { expect(kwsplat_node.operator).to eq('**') }
end

describe '#kwsplat_type?' do
it { expect(kwsplat_node).to be_kwsplat_type }
end

describe '#forwarded_kwrestarg_type?' do
it { expect(kwsplat_node).not_to be_forwarded_kwrestarg_type }
end

context 'when forwarded keyword rest arguments', :ruby32 do
let(:kwsplat_node) { parse_source(source).ast.children.last.children.last }
let(:source) do
<<~RUBY
def foo(**)
{ a: 1, ** }
end
RUBY
end

describe '.new' do
it { expect(kwsplat_node).to be_a(described_class) }
end

describe '#hash_rocket?' do
it { expect(kwsplat_node).not_to be_hash_rocket }
end

describe '#colon?' do
it { expect(kwsplat_node).not_to be_colon }
end

describe '#key' do
it { expect(kwsplat_node.key).to eq(kwsplat_node) }
end

describe '#value' do
it { expect(kwsplat_node.value).to eq(kwsplat_node) }
end

describe '#operator' do
it { expect(kwsplat_node.operator).to eq('**') }
end

describe '#kwsplat_type?' do
it { expect(kwsplat_node).to be_kwsplat_type }
end
marcandre marked this conversation as resolved.
Show resolved Hide resolved

describe '#forwarded_kwrestarg_type?' do
it { expect(kwsplat_node).to be_forwarded_kwrestarg_type }
end
end

describe '#same_line?' do
let(:first_pair) { parse_source(source).ast.children[0] }
let(:second_pair) { parse_source(source).ast.children[1] }
Expand Down