From 9de6fff05472032774baab3cb95bac1a2af041e9 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 10 Aug 2020 23:20:19 -0400 Subject: [PATCH] In Ruby 2.4, `Set#===` is harmonized with Ruby 2.5+ to call `include?` --- CHANGELOG.md | 4 ++++ lib/rubocop/ast.rb | 1 + lib/rubocop/ast/ext/set.rb | 12 ++++++++++++ spec/rubocop/ast/ext/set_spec.rb | 9 +++++++++ 4 files changed, 26 insertions(+) create mode 100644 lib/rubocop/ast/ext/set.rb create mode 100644 spec/rubocop/ast/ext/set_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index ede2be76e..cdfc2d989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ * [#89](https://github.com/rubocop-hq/rubocop-ast/pull/89): Support right hand assignment for Ruby 2.8 (3.0) parser. ([@koic][]) * [#93](https://github.com/rubocop-hq/rubocop-ast/pull/93): Add `Node#{left|right}_sibling{s}` ([@marcandre][]) +### Changes + +* [#94](https://github.com/rubocop-hq/rubocop-ast/pull/94): In Ruby 2.4, `Set#===` is harmonized with Ruby 2.5+ to call `include?`. ([@marcandre][]) + ## 0.3.0 (2020-08-01) ### New features diff --git a/lib/rubocop/ast.rb b/lib/rubocop/ast.rb index 253e80a3f..153ada376 100644 --- a/lib/rubocop/ast.rb +++ b/lib/rubocop/ast.rb @@ -5,6 +5,7 @@ require 'set' require_relative 'ast/ext/range' +require_relative 'ast/ext/set' require_relative 'ast/node_pattern' require_relative 'ast/sexp' require_relative 'ast/node' diff --git a/lib/rubocop/ast/ext/set.rb b/lib/rubocop/ast/ext/set.rb new file mode 100644 index 000000000..fb41b9748 --- /dev/null +++ b/lib/rubocop/ast/ext/set.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +test = :foo +case test +when Set[:foo] + # ok, RUBY_VERSION > 2.4 +else + # Harmonize `Set#===` + class Set + alias === include? + end +end diff --git a/spec/rubocop/ast/ext/set_spec.rb b/spec/rubocop/ast/ext/set_spec.rb new file mode 100644 index 000000000..370f1da8c --- /dev/null +++ b/spec/rubocop/ast/ext/set_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +# rubocop:disable RSpec/DescribeClass, Style/CaseEquality +RSpec.describe 'Set#===' do + it 'tests for inclusion' do + expect(Set[1, 2, 3] === 2).to eq true + end +end +# rubocop:enable RSpec/DescribeClass, Style/CaseEquality