Skip to content

Commit 81f0c33

Browse files
committed
Handle shorthand syntax in explanations
When performing a performance test on https://github.com/kddnewton/syntax_tree/blob/50f9fc1b815156bdde53f13d4f250d9522f15e64/test/syntax_tree_test.rb I noticed a strange output. The explain syntax indicated that it was missing a bracket when I had only deleted an `end`: ``` $ time DEAD_END_TIMEOUT=10 ./exe/dead_end spec/fixtures/syntax_tree.rb.txt --> /Users/rschneeman/Documents/projects/dead_end/spec/fixtures/syntax_tree.rb.txt Unmatched `]', missing `[' ? Unmatched keyword, missing `end' ? 6 class SyntaxTree < Ripper 170 def self.parse(source) 174 end 176 private ❯ 754 def on_args_add(arguments, argument) ❯ 776 class ArgsAddBlock ❯ 810 end 9233 end DEAD_END_TIMEOUT=10 ./exe/dead_end spec/fixtures/syntax_tree.rb.txt 1.63s user 0.07s system 99% cpu 1.703 total ``` The cause was this line: ``` node.is_a?(Op) && %w[| ||].include?(node.value) && ``` With the `%w[]` syntax the lexer tokenizes `%w[` which (since it doesn't match `[` is not counted. This commit corrects that logic by explicitly looking for the last character of these events.
1 parent da3596a commit 81f0c33

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (unreleased)
22

3+
- Fix explanation involving shorthand syntax for literals like `%w[]` and `%Q{}` (https://github.com/zombocom/dead_end/pull/116)
4+
35
## 3.0.2
46

57
- Fix windows filename detection (https://github.com/zombocom/dead_end/pull/114)

lib/dead_end/left_right_lex_count.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ def count_lex(lex)
6161
# ^^^
6262
# Means it's a string or a symbol `"{"` rather than being
6363
# part of a data structure (like a hash) `{ a: b }`
64+
# ignore it.
65+
when :on_words_beg, :on_symbos_beg, :on_qwords_beg,
66+
:on_qsymbols_beg, :on_regexp_beg, :on_tstring_beg
67+
# ^^^
68+
# Handle shorthand syntaxes like `%Q{ i am a string }`
69+
#
70+
# The start token will be the full thing `%Q{` but we
71+
# need to count it as if it's a `{`. Any token
72+
# can be used
73+
char = lex.token[-1]
74+
@count_for_char[char] += 1 if @count_for_char.key?(char)
6475
when :on_embexpr_beg
6576
# ^^^
6677
# Embedded string expressions like `"#{foo} <-embed"`

spec/unit/explain_syntax_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
module DeadEnd
66
RSpec.describe "ExplainSyntax" do
7+
it "handles %w[]" do
8+
source = <<~EOM
9+
node.is_a?(Op) && %w[| ||].include?(node.value) &&
10+
EOM
11+
12+
explain = ExplainSyntax.new(
13+
code_lines: CodeLine.from_source(source)
14+
).call
15+
16+
expect(explain.missing).to eq([])
17+
end
18+
719
it "doesn't falsely identify strings or symbols as critical chars" do
820
source = <<~EOM
921
a = ['(', '{', '[', '|']

0 commit comments

Comments
 (0)