syntax_search 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7bc1e7685829016c797af5b036c68d0ba625a3a195c134486ca2852fda4aa339
4
+ data.tar.gz: be5bb2c996fe5a4f9762257be2ff93d028ac3c2faa8db1e27c239c56ad65a552
5
+ SHA512:
6
+ metadata.gz: ab335dbb7813fca47d939f9e88af8f669eddec5a7e773a846f09fe04082982bc59d79b1f8d7a056c3441e79226536161544aada44fd3eabea72d5363303cc616
7
+ data.tar.gz: 47c0ccd38ec581c3266001db6028d71d3fdec089e51edc603ca4d3518eae042753b1fb885e9c194dc697b7522ec5aca8ac23447ff58f79755d7f1291fdef26d6
@@ -0,0 +1,41 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@1.1.2
4
+ references:
5
+ unit: &unit
6
+ run:
7
+ name: Run test suite
8
+ command: bundle exec rspec spec/
9
+
10
+ jobs:
11
+ "ruby-2-5":
12
+ docker:
13
+ - image: circleci/ruby:2.5
14
+ steps:
15
+ - checkout
16
+ - ruby/install-deps
17
+ - <<: *unit
18
+
19
+ "ruby-2-6":
20
+ docker:
21
+ - image: circleci/ruby:2.6
22
+ steps:
23
+ - checkout
24
+ - ruby/install-deps
25
+ - <<: *unit
26
+
27
+ "ruby-2-7":
28
+ docker:
29
+ - image: circleci/ruby:2.7
30
+ steps:
31
+ - checkout
32
+ - ruby/install-deps
33
+ - <<: *unit
34
+
35
+ workflows:
36
+ version: 2
37
+ build:
38
+ jobs:
39
+ - "ruby-2-5"
40
+ - "ruby-2-6"
41
+ - "ruby-2-7"
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .DS_Store
13
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at richard.schneeman+foo@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
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]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in syntax_search.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 12.0"
9
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ syntax_search (0.1.0)
5
+ parser
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.1)
11
+ diff-lcs (1.4.4)
12
+ parser (2.7.2.0)
13
+ ast (~> 2.4.1)
14
+ rake (12.3.3)
15
+ rspec (3.10.0)
16
+ rspec-core (~> 3.10.0)
17
+ rspec-expectations (~> 3.10.0)
18
+ rspec-mocks (~> 3.10.0)
19
+ rspec-core (3.10.0)
20
+ rspec-support (~> 3.10.0)
21
+ rspec-expectations (3.10.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-mocks (3.10.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-support (3.10.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ rake (~> 12.0)
34
+ rspec (~> 3.0)
35
+ syntax_search!
36
+
37
+ BUNDLED WITH
38
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 schneems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,98 @@
1
+ # SyntaxSearch
2
+
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
+
5
+ ```
6
+ file.rb:333: syntax error, unexpected `end', expecting end-of-input
7
+ ```
8
+
9
+ 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
+ $ syntax_search <path/to/file.rb>
15
+
16
+ SyntaxErrorSearch: A syntax error was detected
17
+
18
+ This code has an unmatched `end` this is caused by either
19
+ missing a syntax keyword (`def`, `do`, etc.) or inclusion
20
+ of an extra `end` line
21
+
22
+ file: path/to/file.rb
23
+ simplified:
24
+
25
+ ```
26
+ 1 require 'animals'
27
+ 2
28
+ ❯ 10 defdog
29
+ ❯ 15 end
30
+ ❯ 16
31
+ 20 def cat
32
+ 22 end
33
+ ```
34
+ ```
35
+
36
+ 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!
37
+
38
+ ## Installation
39
+
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'syntax_search', require: "syntax_search/auto"
44
+ ```
45
+
46
+ And then execute:
47
+
48
+ $ bundle install
49
+
50
+ Or install it yourself as:
51
+
52
+ $ gem install syntax_search
53
+
54
+ ## What does it do?
55
+
56
+ When your code triggers a SyntaxError due to an "unexpected `end'" in a file, this library fires to narrow down your search to the most likely offending locations.
57
+
58
+ ## Sounds cool, but why isn't this baked into Ruby directly?
59
+
60
+ 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:
61
+
62
+ 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.
63
+ 2. Prove out demand. If you like this idea, then vote for it by putting it in your Gemfile.
64
+
65
+ ## How does it detect syntax error locations?
66
+
67
+ We know that source code that does not contain a syntax error can be parsed. We also know that code with a syntax error contains both valid code and invalid code. If you remove the invalid code, then we can programatically determine that the code we removed contained a syntax error. We can do this detection by generating small code blocks and searching for which blocks need to be removed to generate valid source code.
68
+
69
+ 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.
70
+
71
+ ## How is source code broken up into smaller blocks?
72
+
73
+ 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.
74
+
75
+ 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. Even when code is not correctly indented/formatted we can still likely tell you where to start searching even if we can't point at the exact problem line or location.
76
+
77
+ ## How does this gem know when a syntax error occured in my code?
78
+
79
+ Right now the search isn't performed automatically when you get a syntax error. Instead we append a warning message letting you know how to test the file. Eventually we'll enable the seach by default instead of printing a warning message. To do both of these we have to monkeypatch `require` in the same way that bootsnap does.
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ 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).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zombocom/syntax_search. 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/[USERNAME]/syntax_search/blob/master/CODE_OF_CONDUCT.md).
90
+
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
95
+
96
+ ## Code of Conduct
97
+
98
+ Everyone interacting in the SyntaxErrorSearch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zombocom/syntax_search/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "syntax_search"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ require "optparse"
5
+ require_relative "../lib/syntax_search.rb"
6
+
7
+ options = {}
8
+ options[:terminal] = true
9
+ options[:record_dir] = ENV["SYNTAX_SEARCH_RECORD_DIR"]
10
+
11
+ parser = OptionParser.new do |opts|
12
+ opts.banner = <<~EOM
13
+ Usage: syntax_search <file> [options]
14
+
15
+ Parses a ruby source file and searches for syntax error(s) unexpected `end', expecting end-of-input.
16
+
17
+ Example:
18
+
19
+ $ syntax_search dog.rb
20
+
21
+ # ...
22
+
23
+ ```
24
+ 1 require 'animals'
25
+ 2
26
+ ❯ 10 defdog
27
+ ❯ 15 end
28
+ ❯ 16
29
+ 20 def cat
30
+ 22 end
31
+ ```
32
+
33
+ Env options:
34
+
35
+ SYNTAX_SEARCH_RECORD_DIR=<dir>
36
+
37
+ When enabled, records the steps used to search for a syntax error to the given directory
38
+
39
+ Options:
40
+ EOM
41
+
42
+ opts.on("--help", "Help - displays this message") do |v|
43
+ puts opts
44
+ exit
45
+ end
46
+
47
+ opts.on("--record <dir>", "When enabled, records the steps used to search for a syntax error to the given directory") do |v|
48
+ options[:record_dir] = v
49
+ end
50
+
51
+ opts.on("--no-terminal", "Disable terminal highlighting") do |v|
52
+ options[:terminal] = false
53
+ end
54
+ end
55
+ parser.parse!
56
+
57
+ file = ARGV[0]
58
+
59
+ if file.nil? || file.empty?
60
+ # Display help if raw command
61
+ parser.parse! %w[--help]
62
+ end
63
+
64
+ file = Pathname(file)
65
+
66
+ $stderr.puts "Record dir: #{options[:record_dir]}" if options[:record_dir]
67
+
68
+ SyntaxErrorSearch.call(
69
+ source: file.read,
70
+ filename: file.expand_path,
71
+ terminal: options[:terminal],
72
+ record_dir: options[:record_dir]
73
+ )
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "syntax_search/version"
4
+
5
+ require 'parser/current'
6
+ require 'tmpdir'
7
+ require 'stringio'
8
+ require 'pathname'
9
+
10
+ module SyntaxErrorSearch
11
+ class Error < StandardError; end
12
+ SEARCH_SOURCE_ON_ERROR_DEFAULT = true
13
+
14
+ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT)
15
+ raise e if !e.message.include?("expecting end-of-input")
16
+
17
+ filename = e.message.split(":").first
18
+
19
+ $stderr.sync = true
20
+ $stderr.puts "Run `$ syntax_search #{filename}` for more options\n"
21
+
22
+ if search_source_on_error
23
+ self.call(
24
+ source: Pathname(filename).read,
25
+ filename: filename,
26
+ terminal: true
27
+ )
28
+ end
29
+
30
+ $stderr.puts ""
31
+ $stderr.puts ""
32
+ raise e
33
+ end
34
+
35
+ def self.call(source: , filename: , terminal: false, record_dir: nil)
36
+ search = CodeSearch.new(source, record_dir: record_dir).call
37
+
38
+ blocks = search.invalid_blocks
39
+ DisplayInvalidBlocks.new(
40
+ blocks: blocks,
41
+ filename: filename,
42
+ terminal: terminal,
43
+ io: $stderr
44
+ ).call
45
+ end
46
+
47
+ # Used for counting spaces
48
+ module SpaceCount
49
+ def self.indent(string)
50
+ string.split(/\S/).first&.length || 0
51
+ end
52
+ end
53
+
54
+ # This will tell you if the `code_lines` would be valid
55
+ # if you removed the `without_lines`. In short it's a
56
+ # way to detect if we've found the lines with syntax errors
57
+ # in our document yet.
58
+ #
59
+ # code_lines = [
60
+ # CodeLine.new(line: "def foo\n", index: 0)
61
+ # CodeLine.new(line: " def bar\n", index: 1)
62
+ # CodeLine.new(line: "end\n", index: 2)
63
+ # ]
64
+ #
65
+ # SyntaxErrorSearch.valid_without?(
66
+ # without_lines: code_lines[1],
67
+ # code_lines: code_lines
68
+ # ) # => true
69
+ #
70
+ # SyntaxErrorSearch.valid?(code_lines) # => false
71
+ def self.valid_without?(without_lines: , code_lines:)
72
+ lines = code_lines - Array(without_lines).flatten
73
+
74
+ if lines.empty?
75
+ return true
76
+ else
77
+ return valid?(lines)
78
+ end
79
+ end
80
+
81
+ # Returns truthy if a given input source is valid syntax
82
+ #
83
+ # SyntaxErrorSearch.valid?(<<~EOM) # => true
84
+ # def foo
85
+ # end
86
+ # EOM
87
+ #
88
+ # SyntaxErrorSearch.valid?(<<~EOM) # => false
89
+ # def foo
90
+ # def bar # Syntax error here
91
+ # end
92
+ # EOM
93
+ #
94
+ # You can also pass in an array of lines and they'll be
95
+ # joined before evaluating
96
+ #
97
+ # SyntaxErrorSearch.valid?(
98
+ # [
99
+ # "def foo\n",
100
+ # "end\n"
101
+ # ]
102
+ # ) # => true
103
+ #
104
+ # SyntaxErrorSearch.valid?(
105
+ # [
106
+ # "def foo\n",
107
+ # " def bar\n", # Syntax error here
108
+ # "end\n"
109
+ # ]
110
+ # ) # => false
111
+ #
112
+ # As an FYI the CodeLine class instances respond to `to_s`
113
+ # so passing a CodeLine in as an object or as an array
114
+ # will convert it to it's code representation.
115
+ def self.valid?(source)
116
+ source = source.join if source.is_a?(Array)
117
+ source = source.to_s
118
+
119
+ # Parser writes to stderr even if you catch the error
120
+ stderr = $stderr
121
+ $stderr = StringIO.new
122
+
123
+ Parser::CurrentRuby.parse(source)
124
+ true
125
+ rescue Parser::SyntaxError
126
+ false
127
+ ensure
128
+ $stderr = stderr if stderr
129
+ end
130
+ end
131
+
132
+ require_relative "syntax_search/code_line"
133
+ require_relative "syntax_search/code_block"
134
+ require_relative "syntax_search/code_frontier"
135
+ require_relative "syntax_search/code_search"
136
+ require_relative "syntax_search/display_invalid_blocks"