-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
287 additions
and
1 deletion.
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
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,87 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for spaces between -> and opening parameter | ||
# brace in lambda literals. | ||
# | ||
# @example | ||
# | ||
# EnforcedStyle: require_no_space (default) | ||
# | ||
# @bad | ||
# a = -> (x, y) { x + y } | ||
# | ||
# @good | ||
# a = ->(x, y) { x + y } | ||
# | ||
# @example | ||
# | ||
# EnforcedStyle: require_space | ||
# | ||
# @bad | ||
# a = ->(x, y) { x + y } | ||
# | ||
# @good | ||
# a = -> (x, y) { x + y } | ||
class SpaceInLambdaLiteral < Cop | ||
include ConfigurableEnforcedStyle | ||
|
||
ARROW = '->'.freeze | ||
MSG_REQUIRE_SPACE = 'Use a space between `->` and opening brace ' \ | ||
'in lambda literals'.freeze | ||
MSG_REQUIRE_NO_SPACE = 'Do not use spaces between `->` and opening ' \ | ||
'brace in lambda literals'.freeze | ||
|
||
def on_send(node) | ||
return unless arrow_lambda_with_args?(node) | ||
if style == :require_space && !space_after_arrow?(node) | ||
add_offense(node, node.parent.loc.expression, MSG_REQUIRE_SPACE) | ||
elsif style == :require_no_space && space_after_arrow?(node) | ||
add_offense(node, node.parent.loc.expression, MSG_REQUIRE_NO_SPACE) | ||
end | ||
end | ||
|
||
def autocorrect(lambda_node) | ||
children = lambda_node.parent.children | ||
lambda do |corrector| | ||
if style == :require_space | ||
corrector.insert_before(children[1].source_range, ' ') | ||
else | ||
space_range = range_between(children[0].source_range.end_pos, | ||
children[1].source_range.begin_pos) | ||
corrector.remove(space_range) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def arrow_lambda_with_args?(node) | ||
lambda_node?(node) && arrow_form?(node) && args?(node) | ||
end | ||
|
||
def lambda_node?(node) | ||
receiver, call = *node | ||
receiver.nil? && call == :lambda | ||
end | ||
|
||
def arrow_form?(lambda_node) | ||
lambda_node.loc.selector.source == ARROW | ||
end | ||
|
||
def args?(lambda_node) | ||
_call, args, _body = *lambda_node.parent | ||
!args.children.empty? | ||
end | ||
|
||
def space_after_arrow?(lambda_node) | ||
arrow = lambda_node.parent.children[0] | ||
parentheses = lambda_node.parent.children[1] | ||
parentheses.source_range.begin_pos - arrow.source_range.end_pos > 0 | ||
end | ||
end | ||
end | ||
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
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,150 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Style::SpaceInLambdaLiteral, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'when configured to enforce spaces' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'require_space' } } | ||
|
||
it 'registers an offense for no space between -> and (' do | ||
inspect_source(cop, 'a = ->(b, c) { b + c }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'does not register an offense for a space between -> and (' do | ||
inspect_source(cop, 'a = -> (b, c) { b + c }') | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'does not register an offense for multi-line lambdas' do | ||
inspect_source(cop, ['l = lambda do |a, b|', | ||
' tmp = a * 7', | ||
' tmp * b / 50', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'does not register an offense for no space between -> and {' do | ||
inspect_source(cop, 'a = ->{ b + c }') | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'registers an offense for no space in the inner nested lambda' do | ||
inspect_source(cop, 'a = -> (b = ->(c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'registers an offense for no space in the outer nested lambda' do | ||
inspect_source(cop, 'a = ->(b = -> (c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'registers an offense for no space in both lambdas when nested' do | ||
inspect_source(cop, 'a = ->(b = ->(c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(2) | ||
end | ||
|
||
it 'autocorrects an offense for no space between -> and (' do | ||
code = 'a = ->(b, c) { b + c }' | ||
expected = 'a = -> (b, c) { b + c }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for no space in the inner nested lambda' do | ||
code = 'a = -> (b = ->(c) {}, d) { b + d }' | ||
expected = 'a = -> (b = -> (c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for no space in the outer nested lambda' do | ||
code = 'a = ->(b = -> (c) {}, d) { b + d }' | ||
expected = 'a = -> (b = -> (c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for no space in both lambdas when nested' do | ||
code = 'a = ->(b = ->(c) {}, d) { b + d }' | ||
expected = 'a = -> (b = -> (c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
end | ||
|
||
context 'when configured to enforce no space' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'require_no_space' } } | ||
|
||
it 'registers an offense for a space between -> and (' do | ||
inspect_source(cop, 'a = -> (b, c) { b + c }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'does not register an offense for no space between -> and (' do | ||
inspect_source(cop, 'a = ->(b, c) { b + c }') | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'does not register an offense for multi-line lambdas' do | ||
inspect_source(cop, ['l = lambda do |a, b|', | ||
' tmp = a * 7', | ||
' tmp * b / 50', | ||
'end']) | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'does not register an offense for a space between -> and {' do | ||
inspect_source(cop, 'a = -> { b + c }') | ||
expect(cop.offenses).to be_empty | ||
end | ||
|
||
it 'registers an offense for spaces between -> and (' do | ||
inspect_source(cop, 'a = -> (b, c) { b + c }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'registers an offense for a space in the inner nested lambda' do | ||
inspect_source(cop, 'a = ->(b = -> (c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'registers an offense for a space in the outer nested lambda' do | ||
inspect_source(cop, 'a = -> (b = ->(c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(1) | ||
end | ||
|
||
it 'registers two offenses for a space in both lambdas when nested' do | ||
inspect_source(cop, 'a = -> (b = -> (c) {}, d) { b + d }') | ||
expect(cop.offenses.size).to eq(2) | ||
end | ||
|
||
it 'autocorrects an offense for a space between -> and (' do | ||
code = 'a = -> (b, c) { b + c }' | ||
expected = 'a = ->(b, c) { b + c }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for spaces between -> and (' do | ||
code = 'a = -> (b, c) { b + c }' | ||
expected = 'a = ->(b, c) { b + c }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for a space in the inner nested lambda' do | ||
code = 'a = ->(b = -> (c) {}, d) { b + d }' | ||
expected = 'a = ->(b = ->(c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects an offense for a space in the outer nested lambda' do | ||
code = 'a = -> (b = ->(c) {}, d) { b + d }' | ||
expected = 'a = ->(b = ->(c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
|
||
it 'autocorrects two offenses for a space in both lambdas when nested' do | ||
code = 'a = -> (b = -> (c) {}, d) { b + d }' | ||
expected = 'a = ->(b = ->(c) {}, d) { b + d }' | ||
expect(autocorrect_source(cop, code)).to eq(expected) | ||
end | ||
end | ||
end |