Skip to content

Commit 9cc2e15

Browse files
committed
Add a rubocop config
1 parent 8e2e0c8 commit 9cc2e15

File tree

11 files changed

+229
-43
lines changed

11 files changed

+229
-43
lines changed

.rubocop.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
inherit_from: config/rubocop.yml
2+
3+
AllCops:
4+
DisplayCopNames: true
5+
DisplayStyleGuide: true
6+
NewCops: enable
7+
SuggestExtensions: false
8+
TargetRubyVersion: 2.7
9+
Exclude:
10+
- '{bin,coverage,pkg,test/fixtures,vendor,tmp}/**/*'
11+
- test.rb
12+
13+
Layout/LineLength:
14+
Max: 80
15+
AllowedPatterns:
16+
- '^\s*in '
17+
- '^\s*location: Location'
18+
19+
Lint/DuplicateBranch:
20+
Enabled: false
21+
22+
Lint/InterpolationCheck:
23+
Enabled: false
24+
25+
Lint/MissingSuper:
26+
Enabled: false
27+
28+
Metrics:
29+
Enabled: false
30+
31+
Naming/MethodName:
32+
Enabled: false
33+
34+
Naming/MethodParameterName:
35+
Enabled: false
36+
37+
Naming/RescuedExceptionsVariableName:
38+
PreferredName: error
39+
40+
Style/ExplicitBlockArgument:
41+
Enabled: false
42+
43+
Style/FormatString:
44+
EnforcedStyle: percent
45+
46+
Style/GuardClause:
47+
Enabled: false
48+
49+
Style/IdenticalConditionalBranches:
50+
Enabled: false
51+
52+
Style/KeywordParametersOrder:
53+
Enabled: false
54+
55+
Style/MutableConstant:
56+
Enabled: false
57+
58+
Style/NegatedIfElseCondition:
59+
Enabled: false
60+
61+
Style/NumericPredicate:
62+
Enabled: false
63+
64+
Style/ParallelAssignment:
65+
Enabled: false
66+
67+
Style/PerlBackrefs:
68+
Enabled: false
69+
70+
Style/SpecialGlobalVars:
71+
Enabled: false
72+
73+
###
74+
75+
Lint/AssignmentInCondition:
76+
Enabled: false
77+
78+
Lint/UnusedMethodArgument:
79+
Enabled: false
80+
81+
Style/Documentation:
82+
Enabled: false
83+
84+
Style/StringLiterals:
85+
Enabled: false

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
source "https://rubygems.org"
44

55
gemspec
6+
7+
gem "rubocop"

Gemfile.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,35 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
ast (2.4.2)
910
docile (1.4.0)
1011
minitest (5.15.0)
12+
parallel (1.22.1)
13+
parser (3.1.2.0)
14+
ast (~> 2.4.1)
15+
rainbow (3.1.1)
1116
rake (13.0.6)
17+
regexp_parser (2.3.1)
18+
rexml (3.2.5)
19+
rubocop (1.28.2)
20+
parallel (~> 1.10)
21+
parser (>= 3.1.0.0)
22+
rainbow (>= 2.2.2, < 4.0)
23+
regexp_parser (>= 1.8, < 3.0)
24+
rexml
25+
rubocop-ast (>= 1.17.0, < 2.0)
26+
ruby-progressbar (~> 1.7)
27+
unicode-display_width (>= 1.4.0, < 3.0)
28+
rubocop-ast (1.17.0)
29+
parser (>= 3.1.1.0)
30+
ruby-progressbar (1.11.0)
1231
simplecov (0.21.2)
1332
docile (~> 1.1)
1433
simplecov-html (~> 0.11)
1534
simplecov_json_formatter (~> 0.1)
1635
simplecov-html (0.12.3)
1736
simplecov_json_formatter (0.1.4)
37+
unicode-display_width (2.1.0)
1838

1939
PLATFORMS
2040
arm64-darwin-21
@@ -27,6 +47,7 @@ DEPENDENCIES
2747
bundler
2848
minitest
2949
rake
50+
rubocop
3051
simplecov
3152
syntax_tree!
3253

config/rubocop.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Disabling all Layout/* rules, as they're unnecessary when the user is using
2+
# Syntax Tree to handle all of the formatting.
3+
Layout:
4+
Enabled: false
5+
6+
# Re-enable Layout/LineLength because certain cops that most projects use
7+
# (e.g. Style/IfUnlessModifier) require Layout/LineLength to be enabled.
8+
# By leaving it disabled, those rules will mis-fire.
9+
#
10+
# Users can always override these defaults in their own rubocop.yml files.
11+
# https://github.com/prettier/plugin-ruby/issues/825
12+
#
13+
# We're also explicitly allowing pattern matching to extend beyond the maximum
14+
# line length, since SyntaxTree prefers putting patterns on a single line.
15+
Layout/LineLength:
16+
Enabled: true
17+
AllowedPatterns:
18+
- '^\s*in '
19+
20+
Style/MultilineIfModifier:
21+
Enabled: false
22+
23+
# Syntax Tree will expand empty methods to put the end keyword on the subsequent
24+
# line to reduce git diff noise.
25+
Style/EmptyMethod:
26+
EnforcedStyle: expanded
27+
28+
# lambdas that are constructed with the lambda method call cannot be safely
29+
# turned into lambda literals without removing a method call.
30+
Style/Lambda:
31+
Enabled: false
32+
33+
# When method chains with multiple blocks are chained together, rubocop will let
34+
# them pass if they're using braces but not if they're using do and end
35+
# keywords. Because we will break individual blocks down to using keywords if
36+
# they are multiline, this conflicts with rubocop.
37+
Style/MultilineBlockChain:
38+
Enabled: false
39+
40+
# Syntax Tree by default uses double quotes, so changing the configuration here
41+
# to match that.
42+
Style/StringLiterals:
43+
EnforcedStyle: double_quotes
44+
45+
Style/StringLiteralsInInterpolation:
46+
EnforcedStyle: double_quotes
47+
48+
Style/QuotedSymbols:
49+
EnforcedStyle: double_quotes
50+
51+
# We let users have a little more freedom with symbol and words arrays. If the
52+
# user only has an individual item like ["value"] then we don't bother
53+
# converting it because it ends up being just noise.
54+
Style/SymbolArray:
55+
Enabled: false
56+
57+
Style/WordArray:
58+
Enabled: false
59+
60+
# We don't support trailing commas in Syntax Tree by default, so just turning
61+
# these off for now.
62+
Style/TrailingCommaInArguments:
63+
Enabled: false
64+
65+
Style/TrailingCommaInArrayLiteral:
66+
Enabled: false
67+
68+
Style/TrailingCommaInHashLiteral:
69+
Enabled: false

lib/syntax_tree/cli.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def run(argv)
242242

243243
# If we're not reading from stdin and the user didn't supply and
244244
# filepaths to be read, then we exit with the usage message.
245-
if STDIN.tty? && arguments.empty?
245+
if $stdin.tty? && arguments.empty?
246246
warn(HELP)
247247
return 1
248248
end
@@ -280,7 +280,7 @@ def run(argv)
280280
errored = true
281281
rescue Check::UnformattedError, Debug::NonIdempotentFormatError
282282
errored = true
283-
rescue => error
283+
rescue StandardError => error
284284
warn(error.message)
285285
warn(error.backtrace)
286286
errored = true
@@ -298,7 +298,7 @@ def run(argv)
298298
private
299299

300300
def each_file(arguments)
301-
if STDIN.tty?
301+
if $stdin.tty?
302302
arguments.each do |pattern|
303303
Dir.glob(pattern).each do |filepath|
304304
next unless File.file?(filepath)
@@ -309,7 +309,7 @@ def each_file(arguments)
309309
end
310310
end
311311
else
312-
yield HANDLERS[".rb"], :stdin, STDIN.read
312+
yield HANDLERS[".rb"], :stdin, $stdin.read
313313
end
314314
end
315315

lib/syntax_tree/language_server.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module SyntaxTree
1010
class LanguageServer
1111
attr_reader :input, :output
1212

13-
def initialize(input: STDIN, output: STDOUT)
13+
def initialize(input: $stdin, output: $stdout)
1414
@input = input.binmode
1515
@output = output.binmode
1616
end
@@ -88,6 +88,8 @@ def inlay_hints(source)
8888
after: inlay_hints.after.map(&serialize)
8989
}
9090
rescue Parser::ParseError
91+
# If there is a parse error, then we're not going to return any inlay
92+
# hints for this source.
9193
end
9294

9395
def write(value)

lib/syntax_tree/node.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def format(q)
761761
if part.is_a?(StringLiteral)
762762
q.format(part.parts.first)
763763
else
764-
q.text(part.value[1..-1])
764+
q.text(part.value[1..])
765765
end
766766
end
767767
end
@@ -1190,7 +1190,7 @@ def deconstruct_keys(keys)
11901190
end
11911191

11921192
def format(q)
1193-
if value&.is_a?(HashLiteral)
1193+
if value.is_a?(HashLiteral)
11941194
format_contents(q)
11951195
else
11961196
q.group { format_contents(q) }
@@ -2301,7 +2301,7 @@ def format(q)
23012301

23022302
# First, walk down the chain until we get to the point where we're not
23032303
# longer at a chainable node.
2304-
while true
2304+
loop do
23052305
case children.last
23062306
in Call[receiver: Call]
23072307
children << children.last.receiver
@@ -3079,7 +3079,7 @@ def trailing?
30793079
end
30803080

30813081
def ignore?
3082-
value[1..-1].strip == "stree-ignore"
3082+
value[1..].strip == "stree-ignore"
30833083
end
30843084

30853085
def comments
@@ -3917,7 +3917,7 @@ def quotes(q)
39173917
end
39183918
elsif Quotes.locked?(self)
39193919
if quote.start_with?(":")
3920-
[hash_key ? quote[1..-1] : quote, quote[1..-1]]
3920+
[hash_key ? quote[1..] : quote, quote[1..]]
39213921
else
39223922
[hash_key ? quote : ":#{quote}", quote]
39233923
end
@@ -4967,7 +4967,7 @@ def format(q)
49674967

49684968
# The list of nodes that represent patterns inside of pattern matching so that
49694969
# when a pattern is being printed it knows if it's nested.
4970-
PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign]
4970+
PATTERNS = [AryPtn, Binary, FndPtn, HshPtn, RAssign].freeze
49714971

49724972
# Ident represents an identifier anywhere in code. It can represent a very
49734973
# large number of things, depending on where it is in the syntax tree.
@@ -5587,7 +5587,7 @@ def format(q)
55875587
# the values, then we're going to insert them every 3 characters
55885588
# starting from the right.
55895589
index = (value.length + 2) % 3
5590-
q.text(" #{value}"[index..-1].scan(/.../).join("_").strip)
5590+
q.text(" #{value}"[index..].scan(/.../).join("_").strip)
55915591
else
55925592
q.text(value)
55935593
end
@@ -6475,7 +6475,7 @@ def skip_indent?
64756475
# This approach maintains the nice conciseness of the inline version, while
64766476
# keeping the correct semantic meaning.
64776477
module Parentheses
6478-
NODES = [Args, Assign, Assoc, Binary, Call, Defined, MAssign, OpAssign]
6478+
NODES = [Args, Assign, Assoc, Binary, Call, Defined, MAssign, OpAssign].freeze
64796479

64806480
def self.flat(q)
64816481
return yield unless NODES.include?(q.parent.class)
@@ -6686,7 +6686,7 @@ def format(q)
66866686

66876687
contents = -> do
66886688
q.seplist(parts) { |part| q.format(part) }
6689-
q.format(rest) if rest && rest.is_a?(ExcessedComma)
6689+
q.format(rest) if rest&.is_a?(ExcessedComma)
66906690
end
66916691

66926692
if ![Def, Defs, DefEndless].include?(q.parent.class) || parts.empty?
@@ -7323,14 +7323,14 @@ def format(q)
73237323
end
73247324

73257325
q.text("}")
7326-
q.text(ending[1..-1])
7326+
q.text(ending[1..])
73277327
end
73287328
else
73297329
q.group do
73307330
q.text("/")
73317331
q.format_each(parts)
73327332
q.text("/")
7333-
q.text(ending[1..-1])
7333+
q.text(ending[1..])
73347334
end
73357335
end
73367336
end

0 commit comments

Comments
 (0)