dead_end 2.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/check_changelog.yml +14 -7
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +2 -2
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -2
- data/README.md +118 -23
- data/exe/dead_end +3 -74
- data/lib/dead_end/auto.rb +1 -21
- data/lib/dead_end/clean_document.rb +21 -30
- data/lib/dead_end/cli.rb +129 -0
- data/lib/dead_end/code_block.rb +18 -2
- data/lib/dead_end/code_frontier.rb +48 -14
- data/lib/dead_end/code_line.rb +27 -21
- data/lib/dead_end/code_search.rb +7 -7
- data/lib/dead_end/display_invalid_blocks.rb +37 -45
- data/lib/dead_end/explain_syntax.rb +103 -0
- data/lib/dead_end/insertion_sort.rb +46 -0
- data/lib/dead_end/left_right_lex_count.rb +157 -0
- data/lib/dead_end/lex_all.rb +12 -8
- data/lib/dead_end/lex_value.rb +2 -0
- data/lib/dead_end/pathname_from_message.rb +47 -0
- data/lib/dead_end/ripper_errors.rb +30 -0
- data/lib/dead_end/version.rb +1 -1
- data/lib/dead_end.rb +161 -1
- metadata +8 -6
- data/lib/dead_end/banner.rb +0 -58
- data/lib/dead_end/fyi.rb +0 -8
- data/lib/dead_end/internals.rb +0 -154
- data/lib/dead_end/who_dis_syntax_error.rb +0 -83
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 802262fe82f2dcb82c0ca5d908852141a6fb7fe533b597f80c4d65b2e7f8b7e6
|
4
|
+
data.tar.gz: 2087ff7725c10acc2eac1278dfe6d64c1e2161ee9d4fe97bb87b33aecc73d468
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25b2a17f9901cbdf5d8a7c3085a931b95f70eda6a51661664c5b859a3f06b21e9fe4aa926e1d93e54f04b05bd4b27e67bfaa6ef84d61fcd0eacbf9929882bc43
|
7
|
+
data.tar.gz: f9aeb83c9d39c05d03be3f0e84c4c2fc86246a61f6b77b6185f2f0a25ec91885ed3d727e7ecf41948a11b5efd3d0b51aef3824103f83800e23734675a33bbed2
|
@@ -1,13 +1,20 @@
|
|
1
1
|
name: Check Changelog
|
2
2
|
|
3
3
|
on:
|
4
|
-
|
5
|
-
|
4
|
+
pull_request:
|
5
|
+
types: [opened, reopened, edited, labeled, unlabeled, synchronize]
|
6
|
+
|
6
7
|
jobs:
|
7
|
-
|
8
|
+
check-changelog:
|
8
9
|
runs-on: ubuntu-latest
|
10
|
+
if: |
|
11
|
+
!contains(github.event.pull_request.body, '[skip changelog]') &&
|
12
|
+
!contains(github.event.pull_request.body, '[changelog skip]') &&
|
13
|
+
!contains(github.event.pull_request.body, '[skip ci]') &&
|
14
|
+
!contains(github.event.pull_request.labels.*.name, 'skip changelog')
|
9
15
|
steps:
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
16
|
+
- uses: actions/checkout@v2.3.5
|
17
|
+
- name: Check that CHANGELOG is touched
|
18
|
+
run: |
|
19
|
+
git fetch origin ${{ github.base_ref }} --depth 1 && \
|
20
|
+
git diff remotes/origin/${{ github.base_ref }} --name-only | grep CHANGELOG.md
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
## HEAD (unreleased)
|
2
2
|
|
3
|
+
## 3.0.2
|
4
|
+
|
5
|
+
- Fix windows filename detection (https://github.com/zombocom/dead_end/pull/114)
|
6
|
+
- Update links on readme and code of conduct (https://github.com/zombocom/dead_end/pull/107)
|
7
|
+
|
8
|
+
## 3.0.1
|
9
|
+
|
10
|
+
- Fix CLI parsing when flags come before filename (https://github.com/zombocom/dead_end/pull/102)
|
11
|
+
|
12
|
+
## 3.0.0
|
13
|
+
|
14
|
+
- [Breaking] CLI now outputs to STDOUT instead of STDERR (https://github.com/zombocom/dead_end/pull/98)
|
15
|
+
- [Breaking] Remove previously deprecated `require "dead_end/fyi"` interface (https://github.com/zombocom/dead_end/pull/94)
|
16
|
+
- Fix double output bug (https://github.com/zombocom/dead_end/pull/99)
|
17
|
+
- Fix bug causing poor results (fix #95, fix #88) (https://github.com/zombocom/dead_end/pull/96)
|
18
|
+
- DeadEnd is now fired on EVERY syntax error (https://github.com/zombocom/dead_end/pull/94)
|
19
|
+
- Output format changes:
|
20
|
+
- Parse errors emitted per-block rather than for the whole document (https://github.com/zombocom/dead_end/pull/94)
|
21
|
+
- The "banner" is now based on lexical analysis rather than parser regex (fix #68, fix #87) (https://github.com/zombocom/dead_end/pull/96)
|
22
|
+
|
23
|
+
## 2.0.2
|
24
|
+
|
25
|
+
- Don't print terminal color codes when output is not tty (https://github.com/zombocom/dead_end/pull/91)
|
26
|
+
|
3
27
|
## 2.0.1
|
4
28
|
|
5
29
|
- Reintroduce Ruby 2.5 support (https://github.com/zombocom/dead_end/pull/90)
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -68,7 +68,7 @@ members of the project's leadership.
|
|
68
68
|
## Attribution
|
69
69
|
|
70
70
|
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
-
available at [https://contributor-covenant.org/version/1/4][version]
|
71
|
+
available at [https://contributor-covenant.org/version/1/4/code-of-conduct/][version]
|
72
72
|
|
73
73
|
[homepage]: https://contributor-covenant.org
|
74
|
-
[version]: https://contributor-covenant.org/version/1/4/
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/code-of-conduct/
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
dead_end (
|
4
|
+
dead_end (3.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -42,6 +42,7 @@ GEM
|
|
42
42
|
rubocop-performance (1.11.5)
|
43
43
|
rubocop (>= 1.7.0, < 2.0)
|
44
44
|
rubocop-ast (>= 0.4.0)
|
45
|
+
ruby-prof (1.4.3)
|
45
46
|
ruby-progressbar (1.11.0)
|
46
47
|
stackprof (0.2.16)
|
47
48
|
standard (1.3.0)
|
@@ -56,8 +57,9 @@ DEPENDENCIES
|
|
56
57
|
dead_end!
|
57
58
|
rake (~> 12.0)
|
58
59
|
rspec (~> 3.0)
|
60
|
+
ruby-prof
|
59
61
|
stackprof
|
60
62
|
standard
|
61
63
|
|
62
64
|
BUNDLED WITH
|
63
|
-
2.2.
|
65
|
+
2.2.30
|
data/README.md
CHANGED
@@ -2,19 +2,14 @@
|
|
2
2
|
|
3
3
|
An error in your code forces you to stop. DeadEnd helps you find those errors to get you back on your way faster.
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
This code has an unmatched `end`. Ensure that all `end` lines
|
8
|
-
in your code have a matching syntax keyword (`def`, `do`, etc.)
|
9
|
-
and that you don't have any extra `end` lines.
|
10
|
-
|
11
|
-
file: path/to/dog.rb
|
12
|
-
simplified:
|
5
|
+
```
|
6
|
+
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
1 class Dog
|
9
|
+
❯ 2 defbark
|
10
|
+
❯ 4 end
|
11
|
+
5 end
|
12
|
+
```
|
18
13
|
|
19
14
|
## Installation in your codebase
|
20
15
|
|
@@ -50,36 +45,108 @@ To get the CLI and manually search for syntax errors (but not automatically anno
|
|
50
45
|
|
51
46
|
This gives you the CLI command `$ dead_end` for more info run `$ dead_end --help`.
|
52
47
|
|
48
|
+
## Editor integration
|
49
|
+
|
50
|
+
An extension is available for VSCode:
|
51
|
+
|
52
|
+
- Extension: https://marketplace.visualstudio.com/items?itemName=Zombocom.dead-end-vscode
|
53
|
+
- GitHub: https://github.com/zombocom/dead_end-vscode
|
54
|
+
|
53
55
|
## What syntax errors does it handle?
|
54
56
|
|
57
|
+
Dead end will fire against all syntax errors and can isolate any syntax error. In addition, dead_end attempts to produce human readable descriptions of what needs to be done to resolve the issue. For example:
|
58
|
+
|
55
59
|
- Missing `end`:
|
56
60
|
|
61
|
+
<!--
|
57
62
|
```ruby
|
58
63
|
class Dog
|
59
64
|
def bark
|
60
65
|
puts "bark"
|
61
|
-
|
62
|
-
def woof
|
63
|
-
puts "woof"
|
64
|
-
end
|
65
66
|
end
|
66
|
-
# => scratch.rb:8: syntax error, unexpected end-of-input, expecting `end'
|
67
67
|
```
|
68
|
+
-->
|
68
69
|
|
69
|
-
|
70
|
+
```
|
71
|
+
Unmatched keyword, missing `end' ?
|
70
72
|
|
73
|
+
❯ 1 class Dog
|
74
|
+
❯ 2 def bark
|
75
|
+
❯ 4 end
|
76
|
+
```
|
77
|
+
|
78
|
+
- Missing keyword
|
79
|
+
<!--
|
71
80
|
```ruby
|
72
81
|
class Dog
|
73
82
|
def speak
|
74
|
-
@sounds.each |sound|
|
83
|
+
@sounds.each |sound|
|
75
84
|
puts sound
|
76
85
|
end
|
77
86
|
end
|
78
87
|
end
|
79
|
-
# => scratch.rb:7: syntax error, unexpected `end', expecting end-of-input
|
80
88
|
```
|
89
|
+
-->
|
81
90
|
|
82
|
-
|
91
|
+
```
|
92
|
+
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
|
93
|
+
|
94
|
+
1 class Dog
|
95
|
+
2 def speak
|
96
|
+
❯ 3 @sounds.each |sound|
|
97
|
+
❯ 5 end
|
98
|
+
6 end
|
99
|
+
7 end
|
100
|
+
```
|
101
|
+
|
102
|
+
- Missing pair characters (like `{}`, `[]`, `()` , or `|<var>|`)
|
103
|
+
<!--
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class Dog
|
107
|
+
def speak(sound
|
108
|
+
puts sound
|
109
|
+
end
|
110
|
+
end
|
111
|
+
```
|
112
|
+
-->
|
113
|
+
|
114
|
+
```
|
115
|
+
Unmatched `(', missing `)' ?
|
116
|
+
|
117
|
+
1 class Dog
|
118
|
+
❯ 2 def speak(sound
|
119
|
+
❯ 4 end
|
120
|
+
5 end
|
121
|
+
```
|
122
|
+
|
123
|
+
- Any ambiguous or unknown errors will be annotated by the original ripper error output:
|
124
|
+
|
125
|
+
<!--
|
126
|
+
class Dog
|
127
|
+
def meals_last_month
|
128
|
+
puts 3 *
|
129
|
+
end
|
130
|
+
end
|
131
|
+
-->
|
132
|
+
|
133
|
+
```
|
134
|
+
syntax error, unexpected end-of-input
|
135
|
+
|
136
|
+
1 class Dog
|
137
|
+
2 def meals_last_month
|
138
|
+
❯ 3 puts 3 *
|
139
|
+
4 end
|
140
|
+
5 end
|
141
|
+
```
|
142
|
+
|
143
|
+
## How is it better than `ruby -wc`?
|
144
|
+
|
145
|
+
Ruby allows you to syntax check a file with warnings using `ruby -wc`. This emits a parser error instead of a human focused error. Ruby's parse errors attempt to narrow down the location and can tell you if there is a glaring indentation error involving `end`.
|
146
|
+
|
147
|
+
The `dead_end` algorithm doesn't just guess at the location of syntax errors, it re-parses the document to prove that it captured them.
|
148
|
+
|
149
|
+
This library focuses on the human side of syntax errors. It cares less about why the document could not be parsed (computer problem) and more on what the programmer needs (human problem) to fix the problem.
|
83
150
|
|
84
151
|
## Sounds cool, but why isn't this baked into Ruby directly?
|
85
152
|
|
@@ -105,9 +172,37 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
105
172
|
|
106
173
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
107
174
|
|
175
|
+
### How to debug changes to output display
|
176
|
+
|
177
|
+
You can see changes to output against a variety of invalid code by running specs and using the `DEBUG_DISPLAY=1` environment variable. For example:
|
178
|
+
|
179
|
+
```
|
180
|
+
$ DEBUG_DISPLAY=1 bundle exec rspec spec/ --format=failures
|
181
|
+
```
|
182
|
+
|
183
|
+
### Run profiler
|
184
|
+
|
185
|
+
You can output profiler data to the `tmp` directory by running:
|
186
|
+
|
187
|
+
```
|
188
|
+
$ DEBUG_PERF=1 bundle exec rspec spec/integration/dead_end_spec.rb
|
189
|
+
```
|
190
|
+
|
191
|
+
Some outputs are in text format, some are html, the raw marshaled data is available in `raw.rb.marshal`. See https://ruby-prof.github.io/#reports for more info. One interesting one, is the "kcachegrind" interface. To view this on mac:
|
192
|
+
|
193
|
+
```
|
194
|
+
$ brew install qcachegrind
|
195
|
+
```
|
196
|
+
|
197
|
+
Open:
|
198
|
+
|
199
|
+
```
|
200
|
+
$ qcachegrind tmp/last/profile.callgrind.out.<numbers>
|
201
|
+
```
|
202
|
+
|
108
203
|
## Contributing
|
109
204
|
|
110
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/zombocom/dead_end. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/zombocom/dead_end/blob/
|
205
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zombocom/dead_end. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/zombocom/dead_end/blob/main/CODE_OF_CONDUCT.md).
|
111
206
|
|
112
207
|
|
113
208
|
## License
|
@@ -116,4 +211,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
116
211
|
|
117
212
|
## Code of Conduct
|
118
213
|
|
119
|
-
Everyone interacting in the DeadEnd project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zombocom/dead_end/blob/
|
214
|
+
Everyone interacting in the DeadEnd project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zombocom/dead_end/blob/main/CODE_OF_CONDUCT.md).
|
data/exe/dead_end
CHANGED
@@ -1,78 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require "pathname"
|
4
|
-
require "optparse"
|
5
3
|
require_relative "../lib/dead_end"
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
parser = OptionParser.new do |opts|
|
12
|
-
opts.banner = <<~EOM
|
13
|
-
Usage: dead_end <file> [options]
|
14
|
-
|
15
|
-
Parses a ruby source file and searches for syntax error(s) such as
|
16
|
-
unexpected `end', expecting end-of-input.
|
17
|
-
|
18
|
-
Example:
|
19
|
-
|
20
|
-
$ dead_end dog.rb
|
21
|
-
|
22
|
-
# ...
|
23
|
-
|
24
|
-
❯ 10 defdog
|
25
|
-
❯ 15 end
|
26
|
-
❯ 16
|
27
|
-
|
28
|
-
Env options:
|
29
|
-
|
30
|
-
DEAD_END_RECORD_DIR=<dir>
|
31
|
-
|
32
|
-
When enabled, records the steps used to search for a syntax error to the
|
33
|
-
given directory
|
34
|
-
|
35
|
-
Options:
|
36
|
-
EOM
|
37
|
-
|
38
|
-
opts.version = DeadEnd::VERSION
|
39
|
-
|
40
|
-
opts.on("--help", "Help - displays this message") do |v|
|
41
|
-
puts opts
|
42
|
-
exit
|
43
|
-
end
|
44
|
-
|
45
|
-
opts.on("--record <dir>", "When enabled, records the steps used to search for a syntax error to the given directory") do |v|
|
46
|
-
options[:record_dir] = v
|
47
|
-
end
|
48
|
-
|
49
|
-
opts.on("--no-terminal", "Disable terminal highlighting") do |v|
|
50
|
-
options[:terminal] = false
|
51
|
-
end
|
52
|
-
end
|
53
|
-
parser.parse!
|
54
|
-
|
55
|
-
file = ARGV[0]
|
56
|
-
|
57
|
-
if file.nil? || file.empty?
|
58
|
-
# Display help if raw command
|
59
|
-
parser.parse! %w[--help]
|
60
|
-
end
|
61
|
-
|
62
|
-
file = Pathname(file)
|
63
|
-
options[:record_dir] = "tmp" if ENV["DEBUG"]
|
64
|
-
|
65
|
-
warn "Record dir: #{options[:record_dir]}" if options[:record_dir]
|
66
|
-
|
67
|
-
display = DeadEnd.call(
|
68
|
-
source: file.read,
|
69
|
-
filename: file.expand_path,
|
70
|
-
terminal: options[:terminal],
|
71
|
-
record_dir: options[:record_dir]
|
72
|
-
)
|
73
|
-
|
74
|
-
if display.document_ok?
|
75
|
-
exit(0)
|
76
|
-
else
|
77
|
-
exit(1)
|
78
|
-
end
|
5
|
+
DeadEnd::Cli.new(
|
6
|
+
argv: ARGV
|
7
|
+
).call
|
data/lib/dead_end/auto.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "../dead_end
|
3
|
+
require_relative "../dead_end"
|
4
4
|
|
5
5
|
# Monkey patch kernel to ensure that all `require` calls call the same
|
6
6
|
# method
|
@@ -33,23 +33,3 @@ module Kernel
|
|
33
33
|
DeadEnd.handle_error(e)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
37
|
-
# I honestly have no idea why this Object delegation is needed
|
38
|
-
# I keep staring at bootsnap and it doesn't have to do this
|
39
|
-
# is there a bug in their implementation they haven't caught or
|
40
|
-
# am I doing something different?
|
41
|
-
class Object
|
42
|
-
private
|
43
|
-
|
44
|
-
def load(path, wrap = false)
|
45
|
-
Kernel.load(path, wrap)
|
46
|
-
rescue SyntaxError => e
|
47
|
-
DeadEnd.handle_error(e)
|
48
|
-
end
|
49
|
-
|
50
|
-
def require(path)
|
51
|
-
Kernel.require(path)
|
52
|
-
rescue SyntaxError => e
|
53
|
-
DeadEnd.handle_error(e)
|
54
|
-
end
|
55
|
-
end
|
@@ -85,17 +85,16 @@ module DeadEnd
|
|
85
85
|
#
|
86
86
|
class CleanDocument
|
87
87
|
def initialize(source:)
|
88
|
-
|
89
|
-
@document = CodeLine.from_source(
|
88
|
+
lines = clean_sweep(source: source)
|
89
|
+
@document = CodeLine.from_source(lines.join, lines: lines)
|
90
90
|
end
|
91
91
|
|
92
92
|
# Call all of the document "cleaners"
|
93
93
|
# and return self
|
94
94
|
def call
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
.join_heredoc!
|
95
|
+
join_trailing_slash!
|
96
|
+
join_consecutive!
|
97
|
+
join_heredoc!
|
99
98
|
|
100
99
|
self
|
101
100
|
end
|
@@ -122,17 +121,15 @@ module DeadEnd
|
|
122
121
|
# puts "world"
|
123
122
|
# EOM
|
124
123
|
#
|
125
|
-
# lines = CleanDocument.new(source: source).
|
124
|
+
# lines = CleanDocument.new(source: source).lines
|
126
125
|
# expect(lines[0].to_s).to eq("\n")
|
127
126
|
# expect(lines[1].to_s).to eq("puts "hello")
|
128
127
|
# expect(lines[2].to_s).to eq("\n")
|
129
128
|
# expect(lines[3].to_s).to eq("puts "world")
|
130
129
|
#
|
131
|
-
#
|
132
|
-
# If you run this after any of the "join" commands, they
|
133
|
-
# will be un-joined.
|
130
|
+
# Important: This must be done before lexing.
|
134
131
|
#
|
135
|
-
# After this change is made, we
|
132
|
+
# After this change is made, we lex the document because
|
136
133
|
# removing comments can change how the doc is parsed.
|
137
134
|
#
|
138
135
|
# For example:
|
@@ -142,7 +139,9 @@ module DeadEnd
|
|
142
139
|
# # comment
|
143
140
|
# where(name: 'schneems')
|
144
141
|
# EOM
|
145
|
-
# expect(
|
142
|
+
# expect(
|
143
|
+
# values.count {|v| v.type == :on_ignored_nl}
|
144
|
+
# ).to eq(1)
|
146
145
|
#
|
147
146
|
# After the comment is removed:
|
148
147
|
#
|
@@ -151,26 +150,18 @@ module DeadEnd
|
|
151
150
|
#
|
152
151
|
# where(name: 'schneems')
|
153
152
|
# EOM
|
154
|
-
# expect(
|
153
|
+
# expect(
|
154
|
+
# values.count {|v| v.type == :on_ignored_nl}
|
155
|
+
# ).to eq(2)
|
155
156
|
#
|
156
|
-
def clean_sweep
|
157
|
-
source
|
158
|
-
#
|
159
|
-
|
160
|
-
|
157
|
+
def clean_sweep(source:)
|
158
|
+
source.lines.map do |line|
|
159
|
+
if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
|
160
|
+
$/
|
161
|
+
else
|
162
|
+
line
|
161
163
|
end
|
162
|
-
|
163
|
-
# Remove comments
|
164
|
-
if code_line.lex.detect { |lex| lex.type != :on_sp }&.type == :on_comment
|
165
|
-
next CodeLine.new(line: "\n", index: code_line.index, lex: [])
|
166
|
-
end
|
167
|
-
|
168
|
-
code_line
|
169
|
-
end.join
|
170
|
-
|
171
|
-
@source = source
|
172
|
-
@document = CodeLine.from_source(source)
|
173
|
-
self
|
164
|
+
end
|
174
165
|
end
|
175
166
|
|
176
167
|
# Smushes all heredoc lines into one line
|
data/lib/dead_end/cli.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pathname"
|
4
|
+
require "optparse"
|
5
|
+
|
6
|
+
module DeadEnd
|
7
|
+
# All the logic of the exe/dead_end CLI in one handy spot
|
8
|
+
#
|
9
|
+
# Cli.new(argv: ["--help"]).call
|
10
|
+
# Cli.new(argv: ["<path/to/file>.rb"]).call
|
11
|
+
# Cli.new(argv: ["<path/to/file>.rb", "--record=tmp"]).call
|
12
|
+
# Cli.new(argv: ["<path/to/file>.rb", "--terminal"]).call
|
13
|
+
#
|
14
|
+
class Cli
|
15
|
+
attr_accessor :options
|
16
|
+
|
17
|
+
# ARGV is Everything passed to the executable, does not include executable name
|
18
|
+
#
|
19
|
+
# All other intputs are dependency injection for testing
|
20
|
+
def initialize(argv:, exit_obj: Kernel, io: $stdout, env: ENV)
|
21
|
+
@options = {}
|
22
|
+
@parser = nil
|
23
|
+
options[:record_dir] = env["DEAD_END_RECORD_DIR"]
|
24
|
+
options[:record_dir] = "tmp" if env["DEBUG"]
|
25
|
+
options[:terminal] = DeadEnd::DEFAULT_VALUE
|
26
|
+
|
27
|
+
@io = io
|
28
|
+
@argv = argv
|
29
|
+
@exit_obj = exit_obj
|
30
|
+
end
|
31
|
+
|
32
|
+
def call
|
33
|
+
if @argv.empty?
|
34
|
+
# Display help if raw command
|
35
|
+
parser.parse! %w[--help]
|
36
|
+
return
|
37
|
+
else
|
38
|
+
# Mutates @argv
|
39
|
+
parse
|
40
|
+
return if options[:exit]
|
41
|
+
end
|
42
|
+
|
43
|
+
file_name = @argv.first
|
44
|
+
if file_name.nil?
|
45
|
+
@io.puts "No file given"
|
46
|
+
@exit_obj.exit(1)
|
47
|
+
return
|
48
|
+
end
|
49
|
+
|
50
|
+
file = Pathname(file_name)
|
51
|
+
if !file.exist?
|
52
|
+
@io.puts "file not found: #{file.expand_path} "
|
53
|
+
@exit_obj.exit(1)
|
54
|
+
return
|
55
|
+
end
|
56
|
+
|
57
|
+
@io.puts "Record dir: #{options[:record_dir]}" if options[:record_dir]
|
58
|
+
|
59
|
+
display = DeadEnd.call(
|
60
|
+
io: @io,
|
61
|
+
source: file.read,
|
62
|
+
filename: file.expand_path,
|
63
|
+
terminal: options.fetch(:terminal, DeadEnd::DEFAULT_VALUE),
|
64
|
+
record_dir: options[:record_dir]
|
65
|
+
)
|
66
|
+
|
67
|
+
if display.document_ok?
|
68
|
+
@exit_obj.exit(0)
|
69
|
+
else
|
70
|
+
@exit_obj.exit(1)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def parse
|
75
|
+
parser.parse!(@argv)
|
76
|
+
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
def parser
|
81
|
+
@parser ||= OptionParser.new do |opts|
|
82
|
+
opts.banner = <<~EOM
|
83
|
+
Usage: dead_end <file> [options]
|
84
|
+
|
85
|
+
Parses a ruby source file and searches for syntax error(s) such as
|
86
|
+
unexpected `end', expecting end-of-input.
|
87
|
+
|
88
|
+
Example:
|
89
|
+
|
90
|
+
$ dead_end dog.rb
|
91
|
+
|
92
|
+
# ...
|
93
|
+
|
94
|
+
❯ 10 defdog
|
95
|
+
❯ 15 end
|
96
|
+
|
97
|
+
ENV options:
|
98
|
+
|
99
|
+
DEAD_END_RECORD_DIR=<dir>
|
100
|
+
|
101
|
+
Records the steps used to search for a syntax error
|
102
|
+
to the given directory
|
103
|
+
|
104
|
+
Options:
|
105
|
+
EOM
|
106
|
+
|
107
|
+
opts.version = DeadEnd::VERSION
|
108
|
+
|
109
|
+
opts.on("--help", "Help - displays this message") do |v|
|
110
|
+
@io.puts opts
|
111
|
+
options[:exit] = true
|
112
|
+
@exit_obj.exit
|
113
|
+
end
|
114
|
+
|
115
|
+
opts.on("--record <dir>", "Records the steps used to search for a syntax error to the given directory") do |v|
|
116
|
+
options[:record_dir] = v
|
117
|
+
end
|
118
|
+
|
119
|
+
opts.on("--terminal", "Enable terminal highlighting") do |v|
|
120
|
+
options[:terminal] = true
|
121
|
+
end
|
122
|
+
|
123
|
+
opts.on("--no-terminal", "Disable terminal highlighting") do |v|
|
124
|
+
options[:terminal] = false
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/lib/dead_end/code_block.rb
CHANGED
@@ -70,8 +70,24 @@ module DeadEnd
|
|
70
70
|
end
|
71
71
|
|
72
72
|
def valid?
|
73
|
-
|
74
|
-
|
73
|
+
if @valid == UNSET
|
74
|
+
# Performance optimization
|
75
|
+
#
|
76
|
+
# If all the lines were previously hidden
|
77
|
+
# and we expand to capture additional empty
|
78
|
+
# lines then the result cannot be invalid
|
79
|
+
#
|
80
|
+
# That means there's no reason to re-check all
|
81
|
+
# lines with ripper (which is expensive).
|
82
|
+
# Benchmark in commit message
|
83
|
+
@valid = if lines.all? { |l| l.hidden? || l.empty? }
|
84
|
+
true
|
85
|
+
else
|
86
|
+
DeadEnd.valid?(lines.map(&:original).join)
|
87
|
+
end
|
88
|
+
else
|
89
|
+
@valid
|
90
|
+
end
|
75
91
|
end
|
76
92
|
|
77
93
|
def to_s
|