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

Update rubocop to 1.71, rubocop-rspec to 3.4 #325

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ jobs:
run: bundle exec rake test
- name: RuboCop
run: bundle exec rake lint
if: matrix.ruby == '3.0'
51 changes: 36 additions & 15 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require: rubocop-rspec

AllCops:
NewCops: enable
Exclude:
- ext/pg_query/extconf.rb
- vendor/**/*
Expand All @@ -9,46 +10,59 @@ AllCops:
- lib/pg_query/json_field_names.rb
- Rakefile

Layout/AlignHash:
Layout/HashAlignment:
Exclude:
- spec/**/*

Style/WordArray:
Layout/SpaceAroundOperators:
Exclude:
- spec/**/*

Style/StringLiterals:
Layout/SpaceInsideHashLiteralBraces:
Exclude:
- spec/**/*

Layout/SpaceAroundOperators:
Style/WordArray:
Exclude:
- spec/**/*

Layout/SpaceInsideHashLiteralBraces:
Style/StringLiterals:
Exclude:
- spec/**/*

Performance/RegexpMatch:
Exclude:
- ext/pg_query/extconf.rb
Layout/LineLength:
Enabled: false

Layout/EmptyLineAfterGuardClause:
Enabled: false

Style/FormatStringToken:
Enabled: false

Style/StringConcatenation:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/SafeNavigation:
Enabled: false

Documentation:
Style/Documentation:
Enabled: false

LineLength:
# extend self preserves private methods,
# module_function buggily removes them.
Style/ModuleFunction:
Enabled: false

MethodLength:
Metrics/MethodLength:
Enabled: false

ClassLength:
Metrics/ClassLength:
Enabled: false

Metrics/AbcSize:
Expand All @@ -75,9 +89,16 @@ RSpec/NestedGroups:
RSpec/DescribedClass:
Enabled: false

# extend self preserves private methods,
# module_function buggily removes them.
Style/ModuleFunction:
RSpec/ContextWording:
Enabled: false

RSpec/MatchArray:
Enabled: false

RSpec/NamedSubject:
Enabled: false

RSpec/SpecFilePathFormat:
Enabled: false

Lint/ImplicitStringConcatenation:
Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
source 'https://rubygems.org'

gemspec

gem 'rake-compiler', '~> 0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.71'
gem 'rubocop-rspec', '~> 3.4'
gem 'simplecov', '~> 0'
38 changes: 24 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ GEM
google-protobuf (4.29.3)
bigdecimal
rake (>= 13)
json (2.9.1)
language_server-protocol (3.17.0.4)
parallel (1.26.3)
parser (2.7.2.0)
parser (3.3.7.0)
ast (~> 2.4.1)
powerpack (0.1.3)
rainbow (2.2.2)
rake
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
rake-compiler (0.9.9)
rake
regexp_parser (2.10.0)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
Expand All @@ -36,23 +39,30 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
rubocop (0.49.1)
rubocop (1.71.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0)
powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-rspec (1.15.1)
rubocop (>= 0.42.0)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.38.0)
parser (>= 3.3.1.0)
rubocop-rspec (3.4.0)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.13.1)
simplecov_json_formatter (0.1.4)
unicode-display_width (1.8.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)

PLATFORMS
ruby
Expand All @@ -61,8 +71,8 @@ DEPENDENCIES
pg_query!
rake-compiler (~> 0)
rspec (~> 3.0)
rubocop (= 0.49.1)
rubocop-rspec (= 1.15.1)
rubocop (~> 1.71)
rubocop-rspec (~> 3.4)
simplecov (~> 0)

BUNDLED WITH
Expand Down
8 changes: 3 additions & 5 deletions lib/pg_query/fingerprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def fingerprint_value(val, hash, parent_node_name, parent_field_name, need_to_wr

def ignored_node_type?(node)
[A_Const, Alias, ParamRef, SetToDefault, IntList, OidList].include?(node.class) ||
node.is_a?(TypeCast) && (node.arg.node == :a_const || node.arg.node == :param_ref)
(node.is_a?(TypeCast) && %i[a_const param_ref].include?(node.arg.node))
end

def node_protobuf_field_name_to_json_name(node_class, field)
Expand Down Expand Up @@ -112,12 +112,10 @@ def fingerprint_node(node, hash, parent_node_name = nil, parent_field_name = nil
fingerprint_value(val.gsub(/\d{2,}/, ''), hash, postgres_node_name, postgres_field_name, true)
next
end
when 'stmt_len'
next if node.is_a?(RawStmt)
when 'stmt_location'
when 'stmt_len', 'stmt_location'
next if node.is_a?(RawStmt)
when 'kind'
if node.is_a?(A_Expr) && (val == :AEXPR_OP_ANY || val == :AEXPR_IN)
if node.is_a?(A_Expr) && %i[AEXPR_OP_ANY AEXPR_IN].include?(val)
fingerprint_value(:AEXPR_OP, hash, postgres_node_name, postgres_field_name, true)
next
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_query/param_refs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def param_refs # rubocop:disable Metrics/CyclomaticComplexity
case node
when PgQuery::ParamRef
# Ignore param refs inside type casts, as these are already handled
next if location[-3..-1] == %i[type_cast arg param_ref]
next if location[-3..] == %i[type_cast arg param_ref]

results << { 'location' => node.location,
'length' => param_ref_length(node) }
Expand Down
12 changes: 5 additions & 7 deletions lib/pg_query/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def self.parse(query)
end

class ParserResult
attr_reader :query
attr_reader :tree
attr_reader :warnings
attr_reader :query, :tree, :warnings

def initialize(query, tree, warnings = [])
@query = query
Expand Down Expand Up @@ -176,7 +174,7 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
# The following statement types are DDL (changing table structure)
when :alter_table_stmt
case statement.alter_table_stmt.objtype
when :OBJECT_INDEX # Index # rubocop:disable Lint/EmptyWhen
when :OBJECT_INDEX # Index
# ignore `ALTER INDEX index_name`
else
from_clause_items << { item: PgQuery::Node.new(range_var: statement.alter_table_stmt.relation), type: :ddl }
Expand Down Expand Up @@ -230,11 +228,11 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
when :grant_stmt
objects = statement.grant_stmt.objects
case statement.grant_stmt.objtype
when :OBJECT_COLUMN # Column # rubocop:disable Lint/EmptyWhen
when :OBJECT_COLUMN # Column
# FIXME
when :OBJECT_TABLE # Table
from_clause_items += objects.map { |o| { item: o, type: :ddl } }
when :OBJECT_SEQUENCE # Sequence # rubocop:disable Lint/EmptyWhen
when :OBJECT_SEQUENCE # Sequence
# FIXME
end
when :lock_stmt
Expand Down Expand Up @@ -339,7 +337,7 @@ def load_objects! # rubocop:disable Metrics/CyclomaticComplexity
@cte_names.uniq!
end

def statements_and_cte_names_for_with_clause(with_clause) # FIXME
def statements_and_cte_names_for_with_clause(with_clause)
statements = []
cte_names = []

Expand Down
1 change: 1 addition & 0 deletions lib/pg_query/parse_error.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module PgQuery
class ParseError < ArgumentError
attr_reader :location

def initialize(message, source_file, source_line, location)
super("#{message} (#{source_file}:#{source_line})")
@location = location
Expand Down
1 change: 1 addition & 0 deletions lib/pg_query/scan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module PgQuery
class ScanError < ArgumentError
attr_reader :location

def initialize(message, source_file, source_line, location)
super("#{message} (#{source_file}:#{source_line})")
@location = location
Expand Down
8 changes: 2 additions & 6 deletions lib/pg_query/treewalker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ class ParserResult
# multiple parser runs, assuming the same pg_query release and no modifications to the parse tree.
def walk!(&block)
if block.arity == 1
treewalker!(@tree) do |node|
yield(node)
end
treewalker!(@tree, &block)
else
treewalker_with_location!(@tree) do |parent_node, parent_field, node, location|
yield(parent_node, parent_field, node, location)
end
treewalker_with_location!(@tree, &block)
end
end

Expand Down
36 changes: 17 additions & 19 deletions lib/pg_query/truncate.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

module PgQuery
class ParserResult
PossibleTruncation = Struct.new(:location, :node_type, :length, :is_array)
PossibleTruncation = Struct.new(:location, :node_type, :len, :is_array)

# Truncates the query string to be below the specified length, first trying to
# omit less important parts of the query, and only then cutting off the end.
Expand All @@ -14,11 +13,11 @@ def truncate(max_length) # rubocop:disable Metrics/CyclomaticComplexity
truncations = find_possible_truncations

# Truncate the deepest possible truncation that is the longest first
truncations.sort_by! { |t| [-t.location.size, -t.length] }
truncations.sort_by! { |t| [-t.location.size, -t.len] }

tree = dup_tree
truncations.each do |truncation|
next if truncation.length < 3
next if truncation.len < 3

find_tree_location(tree, truncation.location) do |node, _k|
dummy_column_ref = PgQuery::Node.new(column_ref: PgQuery::ColumnRef.new(fields: [PgQuery::Node.new(string: PgQuery::String.new(sval: '…'))]))
Expand Down Expand Up @@ -64,30 +63,29 @@ def find_possible_truncations # rubocop:disable Metrics/CyclomaticComplexity
case k
when :target_list
next unless node.is_a?(PgQuery::SelectStmt) || node.is_a?(PgQuery::UpdateStmt) || node.is_a?(PgQuery::OnConflictClause)
length = if node.is_a?(PgQuery::SelectStmt)
select_target_list_len(v)
else # UpdateStmt / OnConflictClause
update_target_list_len(v)
end
truncations << PossibleTruncation.new(location, :target_list, length, true)
len = if node.is_a?(PgQuery::SelectStmt)
select_target_list_len(v)
else # UpdateStmt / OnConflictClause
update_target_list_len(v)
end
truncations << PossibleTruncation.new(location, :target_list, len, true)
when :where_clause
next unless node.is_a?(PgQuery::SelectStmt) || node.is_a?(PgQuery::UpdateStmt) || node.is_a?(PgQuery::DeleteStmt) ||
node.is_a?(PgQuery::CopyStmt) || node.is_a?(PgQuery::IndexStmt) || node.is_a?(PgQuery::RuleStmt) ||
node.is_a?(PgQuery::InferClause) || node.is_a?(PgQuery::OnConflictClause)

length = PgQuery.deparse_expr(v).size
truncations << PossibleTruncation.new(location, :where_clause, length, false)
len = PgQuery.deparse_expr(v).size
truncations << PossibleTruncation.new(location, :where_clause, len, false)
when :values_lists
length = select_values_lists_len(v)
truncations << PossibleTruncation.new(location, :values_lists, length, false)
len = select_values_lists_len(v)
truncations << PossibleTruncation.new(location, :values_lists, len, false)
when :ctequery
next unless node.is_a?(PgQuery::CommonTableExpr)
length = PgQuery.deparse_stmt(v[v.node.to_s]).size
truncations << PossibleTruncation.new(location, :ctequery, length, false)
len = PgQuery.deparse_stmt(v[v.node.to_s]).size
truncations << PossibleTruncation.new(location, :ctequery, len, false)
when :cols
next unless node.is_a?(PgQuery::InsertStmt)
length = cols_len(v)
truncations << PossibleTruncation.new(location, :cols, length, true)
len = cols_len(v)
truncations << PossibleTruncation.new(location, :cols, len, true)
end
end

Expand Down
9 changes: 3 additions & 6 deletions pg_query.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'pg_query/version'

Gem::Specification.new do |s|
Expand All @@ -12,6 +12,8 @@ Gem::Specification.new do |s|
s.license = 'BSD-3-Clause'
s.homepage = 'https://github.com/pganalyze/pg_query'

s.metadata['rubygems_mfa_required'] = 'true'

s.required_ruby_version = '>= 3.0'

s.extensions = %w[ext/pg_query/extconf.rb]
Expand All @@ -23,10 +25,5 @@ Gem::Specification.new do |s|
s.rdoc_options = %w[--main README.md --exclude ext/]
s.extra_rdoc_files = %w[CHANGELOG.md README.md]

s.add_development_dependency 'rake-compiler', '~> 0'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rubocop', '0.49.1'
s.add_development_dependency 'rubocop-rspec', '1.15.1'
s.add_development_dependency 'simplecov', '~> 0'
s.add_dependency 'google-protobuf', '>= 3.25.3'
end
Loading