syntax_search 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb2c76d5b7441b5c04e92205407605d6f71be326bfbf240b11763156fc00e188
4
- data.tar.gz: 728230f8d8694e221149a671d0c44f44d82829407cf5a82b498523015d9df33f
3
+ metadata.gz: 520b6bbfe8e63c7045c02a1f209e73097e732a701e306c65ccc2a29f3e7645d1
4
+ data.tar.gz: 617a4e7177d8d3fb96624149f2afc7cc8698f43e4d1687e8f32ed45c19ec3ad6
5
5
  SHA512:
6
- metadata.gz: 38ecea1a966f361809d3e13440ee16acffd3bbf79d7629a48d65d3a5f5bc4474c51a8b79c1b8f54f9f2737ef4aa86105394e45caae9bd02f125874b8bc9d4496
7
- data.tar.gz: 13520d3877d427ce2423576543688c9d91e0813bd12bf9e1e886dc65874cea527b04a88763867b7767d2cad03bfd5fc0745b22cc69c96dc140477e54b80c7b08
6
+ metadata.gz: 9593ed43b2f33ac12b71630f7ceaa7a971b281c3b476eabad43dd5bd71db76ee37f9b20e303e3a6699ce1e67d467c32237d8c1fbab51d75823b59f63fbe756cc
7
+ data.tar.gz: 64567243a2c7404694b5719eb2c82482da882de1e6592e371d39349c1282e65ad05cb7cbe613e232bf7f7fa9440601ecce47b321063f3c9a0c5f2d5af2909cad
@@ -1,5 +1,9 @@
1
1
  ## HEAD (unreleased)
2
2
 
3
+ ## 0.1.4
4
+
5
+ - Parser gem replaced with Ripper (https://github.com/zombocom/syntax_search/pull/17)
6
+
3
7
  ## 0.1.3
4
8
 
5
9
  - Internal refactor (https://github.com/zombocom/syntax_search/pull/13)
@@ -1,16 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_search (0.1.3)
5
- parser
4
+ syntax_search (0.1.4)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
10
- ast (2.4.1)
11
9
  diff-lcs (1.4.4)
12
- parser (2.7.2.0)
13
- ast (~> 2.4.1)
14
10
  rake (12.3.3)
15
11
  rspec (3.10.0)
16
12
  rspec-core (~> 3.10.0)
data/README.md CHANGED
@@ -10,30 +10,29 @@ What happened? Likely you forgot a `def`, `do`, or maybe you deleted some code a
10
10
 
11
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
12
 
13
- $ syntax_search <path/to/file.rb>
13
+ $ syntax_search path/to/file.rb
14
14
 
15
- SyntaxErrorSearch: A syntax error was detected
15
+ SyntaxSearch: Unmatched `end` detected
16
16
 
17
- This code has an unmatched `end` this is caused by either
18
- missing a syntax keyword (`def`, `do`, etc.) or inclusion
19
- of an extra `end` line
17
+ This code has an unmatched `end`. Ensure that all `end` lines
18
+ in your code have a matching syntax keyword (`def`, `do`, etc.)
19
+ and that you don't have any extra `end` lines.
20
20
 
21
21
  file: path/to/file.rb
22
22
  simplified:
23
23
 
24
- ```
25
- 1 require 'animals'
26
- 2
27
- ❯ 10 defdog
28
- 15 end
29
- 16
30
- 20 def cat
31
- 22 end
32
- ```
24
+ 1 require 'zoo'
25
+ 2
26
+ 3 class Animal
27
+ 4
28
+ 5 defdog
29
+ 7 end
30
+ 8
31
+ 12 end
33
32
 
34
33
  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!
35
34
 
36
- ## Installation
35
+ ## Installation in your codebase
37
36
 
38
37
  To automatically search syntax errors when they happen, add this to your Gemfile:
39
38
 
@@ -51,6 +50,16 @@ If your application is not calling `Bundler.require` then you must manually add
51
50
  require "syntax_search/auto"
52
51
  ```
53
52
 
53
+ If you're using rspec add this to your `.rspec` file:
54
+
55
+ ```
56
+ --require syntax_search/auto
57
+ ```
58
+
59
+ > This is needed because people can execute a single test file via `bundle exec rspec path/to/file_spec.rb` and if that file has a syntax error, it won't load `spec_helper.rb` to trigger any requires.
60
+
61
+ ## Install the CLI
62
+
54
63
  To get the CLI and manually search for syntax errors, install the gem:
55
64
 
56
65
  $ gem install syntax_search
@@ -74,6 +83,10 @@ We know that source code that does not contain a syntax error can be parsed. We
74
83
 
75
84
  Since there can be multiple syntax errors in a document it's not good enough to check individual code blocks, we've got to check multiple at the same time. We will keep creating and adding new blocks to our search until we detect that our "frontier" (which contains all of our blocks) contains the syntax error. After this, we can stop our search and instead focus on filtering to find the smallest subset of blocks that contain the syntax error.
76
85
 
86
+ Here's an example:
87
+
88
+ ![](assets/syntax_search.gif)
89
+
77
90
  ## How is source code broken up into smaller blocks?
78
91
 
79
92
  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.
Binary file
@@ -2,10 +2,10 @@
2
2
 
3
3
  require_relative "syntax_search/version"
4
4
 
5
- require 'parser/current'
6
5
  require 'tmpdir'
7
6
  require 'stringio'
8
7
  require 'pathname'
8
+ require 'ripper'
9
9
 
10
10
  module SyntaxErrorSearch
11
11
  class Error < StandardError; end
@@ -80,6 +80,13 @@ module SyntaxErrorSearch
80
80
  end
81
81
  end
82
82
 
83
+ def self.invalid?(source)
84
+ source = source.join if source.is_a?(Array)
85
+ source = source.to_s
86
+
87
+ Ripper.new(source).tap(&:parse).error?
88
+ end
89
+
83
90
  # Returns truthy if a given input source is valid syntax
84
91
  #
85
92
  # SyntaxErrorSearch.valid?(<<~EOM) # => true
@@ -115,38 +122,12 @@ module SyntaxErrorSearch
115
122
  # so passing a CodeLine in as an object or as an array
116
123
  # will convert it to it's code representation.
117
124
  def self.valid?(source)
118
- source = source.join if source.is_a?(Array)
119
- source = source.to_s
120
-
121
- # Parser writes to stderr even if you catch the error
122
- stderr = $stderr
123
- $stderr = StringIO.new
124
-
125
- Parser::CurrentRuby.parse(source)
126
- true
127
- rescue Parser::SyntaxError => e
128
- yield e if block_given?
129
- false
130
- ensure
131
- $stderr = stderr if stderr
125
+ !invalid?(source)
132
126
  end
133
127
 
134
- def self.invalid_type(source)
135
- message = nil
136
- self.valid?(source) do |error|
137
- message = error.message
138
- end
139
128
 
140
- case message
141
- when nil
142
- :none
143
- when /token kEND/
144
- :unmatched_end
145
- when /token \$end/ #
146
- :missing_end
147
- else
148
- raise "Unexpected message #{message}"
149
- end
129
+ def self.invalid_type(source)
130
+ WhoDisSyntaxError.new(source).call.error_symbol
150
131
  end
151
132
  end
152
133
 
@@ -159,3 +140,4 @@ require_relative "syntax_search/block_expand"
159
140
  require_relative "syntax_search/parse_blocks_from_indent_line"
160
141
 
161
142
  require_relative "syntax_search/code_search"
143
+ require_relative "syntax_search/who_dis_syntax_error"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxErrorSearch
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.4"
5
5
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxErrorSearch
4
+ # Determines what type of syntax error is in the source
5
+ #
6
+ # Example:
7
+ #
8
+ # puts WhoDisSyntaxError.new("def foo;").call.error_symbol
9
+ # # => :missing_end
10
+ class WhoDisSyntaxError < Ripper
11
+ attr_reader :error, :run_once, :error_symbol
12
+
13
+ def call
14
+ @run_once ||= begin
15
+ parse
16
+ true
17
+ end
18
+ self
19
+ end
20
+
21
+ def on_parse_error(msg)
22
+ @error = msg
23
+ if @error.match?(/unexpected end-of-input/)
24
+ @error_symbol = :missing_end
25
+ elsif @error.match?(/unexpected `end'/) || @error.match?(/expecting end-of-input/)
26
+ @error_symbol = :unmatched_end
27
+ else
28
+ @error_symbol = :nope
29
+ end
30
+ end
31
+ end
32
+ end
@@ -25,6 +25,4 @@ Gem::Specification.new do |spec|
25
25
  spec.bindir = "exe"
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
-
29
- spec.add_dependency "parser"
30
28
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - schneems
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-16 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: parser
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
11
+ date: 2020-11-23 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: When you get an "unexpected end" in your syntax this gem helps you find
28
14
  it
29
15
  email:
@@ -45,6 +31,7 @@ files:
45
31
  - LICENSE.txt
46
32
  - README.md
47
33
  - Rakefile
34
+ - assets/syntax_search.gif
48
35
  - bin/console
49
36
  - bin/setup
50
37
  - exe/syntax_search
@@ -60,6 +47,7 @@ files:
60
47
  - lib/syntax_search/fyi.rb
61
48
  - lib/syntax_search/parse_blocks_from_indent_line.rb
62
49
  - lib/syntax_search/version.rb
50
+ - lib/syntax_search/who_dis_syntax_error.rb
63
51
  - syntax_search.gemspec
64
52
  homepage: https://github.com/zombocom/syntax_search.git
65
53
  licenses:
@@ -82,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
70
  - !ruby/object:Gem::Version
83
71
  version: '0'
84
72
  requirements: []
85
- rubygems_version: 3.1.4
73
+ rubygems_version: 3.0.3
86
74
  signing_key:
87
75
  specification_version: 4
88
76
  summary: Find syntax errors in your source in a snap