You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-4Lines changed: 42 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,20 @@
1
1
# SyntaxErrorSearch
2
2
3
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/syntax_error_search`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+
Imagine you're programming, everything is awesome. You write code, it runs. Still awesome. You write more code, you save it, you run it and then:
4
4
5
-
TODO: Delete this and the text above, and describe your gem
What happened? Likely you forgot a `def`, `do`, or maybe you deleted some code and missed an `end`. Either way it's an extremely unhelpful error and a very time consuming mistake to track down.
10
+
11
+
What if I told you, that there was a library that helped find your missing `def`s and missing `do`s. What if instead of searching through hundreds of lines of source for the cause of your syntax error, there was a way to highlight just code in the file that contained syntax errors.
12
+
13
+
```
14
+
# TODO example here
15
+
```
16
+
17
+
How much would you pay for such a library? A million, a billion, a trillion? Well friends, today is your lucky day because you can use this library today for free!
6
18
7
19
## Installation
8
20
@@ -20,9 +32,35 @@ Or install it yourself as:
20
32
21
33
$ gem install syntax_error_search
22
34
23
-
## Usage
35
+
## What does it do?
36
+
37
+
When your code triggers a SyntaxError due to an "expecting end-of-input" in a file, this library fires to narrow down your search to the most likely offending locations.
38
+
39
+
## Sounds cool, but why isn't this baked into Ruby directly?
40
+
41
+
I would love to get something like this directly in Ruby, but I first need to prove it's useful. The `did_you_mean` functionality started as a gem that was eventually adopted by a bunch of people and then Ruby core liked it enough that they included it in the source. The goal of this gem is to:
42
+
43
+
1. Get real world useage and feedback. If we gave you an awful suggestion, let us know! We try to handle lots of cases well, but maybe we could be better.
44
+
2. Prove out demand. If you like this idea, then vote for it by putting it in your Gemfile.
45
+
46
+
## How does it detect syntax error locations?
47
+
48
+
Source code with a syntax error in it can be thought of valid code with one or more invalid chunks in it. With this in mind we can "search" for both invalid and valid chunks of code. This library uses a parser to tell if a given chunk of code is valid in which case it's certainly not the cause of our problem. If it's invalid, then we can test to see if removing that chunk from our file would make the whole thing valid. When that happens, we've narrowed down our search. But...things aren't always so easy.
49
+
50
+
By definition source code with a syntax error in it cannot be parsed, so we have to guess how to chunk up the file into smaller pieces. Once we've split up the file we can safely rule out or zoom into a specific piece of code to determine the location of the syntax error. This libary uses indentation and empty lines to make guesses about what might be a "block" of code. Once we've got a chunk of code, we can test it.
51
+
52
+
- If the code parses, it cannot be the cause of our syntax error. We can remove it from our search
53
+
- If the code does not parse, it may be the cause of the error, but we also might have made a bad guess in splitting up the source
54
+
- If we remove that chunk of code from the document and that allows the whole thing to parse, it means the syntax error was for sure in that location.
55
+
- Otherwise, it could mean that either there are multiple syntax errors or that we have a bad guess and need to expand our search.
56
+
57
+
At the end of the day we can't say where the syntax error is FOR SURE, but we can get pretty close. It sounds simple when spelled out like this, but it's a very complicated problem.
58
+
59
+
This one person on twitter told me it's "not possible".
60
+
61
+
## How does this gem know when a syntax error occured?
24
62
25
-
TODO: Write usage instructions here
63
+
While I wish you hadn't asked: If you must know, we're monkey-patching require. It sounds scary, but bootsnap does essentially the same thing and we're way less invasive.
0 commit comments