ruby_friendly_error 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a41f6d667be9cadbacddde143ff345d2d145e6d0e2d5c0c42249eceb1918b2d
4
+ data.tar.gz: 1901ec22809369187299906734c171be17ba0e3b4ff2e0a2597b151b1456638c
5
+ SHA512:
6
+ metadata.gz: 8d68f65c5654d7584b6a4231345ad41f2146e2f8f09358b446dafe85fa82627e5ec7e3ab150b0c3a4c3b8496b0849b5bde1d3c48cf81baf36bab28b387861722
7
+ data.tar.gz: 565906dd0162aac5eb29094334519600354d527df8bc44d5811342ad5b1157fee14a9fed0a582aee73a4adbc354e388a1a22be4e1f153aa7822c403bee6bb6b7
data/.git_consistent ADDED
@@ -0,0 +1,50 @@
1
+ type:
2
+ type: enum
3
+ required: true
4
+ description: commit type
5
+ values:
6
+ - name: feat
7
+ description: when implementing function
8
+ - name: fix
9
+ description: when fixing a bug
10
+ - name: docs
11
+ description: when writing docs
12
+ - name: refactor
13
+ description: when refactoring
14
+ - name: perf
15
+ description: when improving performance
16
+ - name: test
17
+ description: when adding tests
18
+ - name: ci
19
+ description: when change the CI build
20
+ - name: chore
21
+ description: when change other that don't modify code or test files
22
+ - name: revert
23
+ description: when reverts a previous commit
24
+ - name: remove
25
+ description: when removing function
26
+ - name: sec
27
+ description: when dealing with security
28
+ - name: up
29
+ description: when upgrading dependencies
30
+ - name: down
31
+ description: when downgrading dependencies
32
+ - name: tada
33
+ description: when celebrating
34
+ subject:
35
+ type: string
36
+ required: true
37
+ description: The subject contains succinct description of the change
38
+ rules:
39
+ firstLatter: lower
40
+ dotAtEnd: false
41
+ nonAscii: false
42
+ body:
43
+ type: text
44
+ default: ''
45
+ required: false
46
+ description: The body contains details of the change
47
+ rules:
48
+ firstLatter: upper
49
+ dotAtEnd: true
50
+ nonAscii: false
@@ -0,0 +1,3 @@
1
+ <type>: <subject>
2
+
3
+ <body>
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /coverage/
3
+ /doc/
4
+ /pkg/
5
+ /spec/reports/
6
+ /tmp/
7
+ /vendor/
8
+
9
+ # rspec failure tracking
10
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,132 @@
1
+ # ver: 0.9.3
2
+ # rubocop: v0.58
3
+
4
+ inherit_from:
5
+ - .rubocop_rspec.yml
6
+ - .rubocop_todo.yml
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 2.5
10
+ DisplayCopNames: true
11
+ Exclude:
12
+ - 'vendor/**/*'
13
+ - 'samples/*'
14
+
15
+ Metrics/LineLength:
16
+ Max: 160
17
+
18
+ Metrics/MethodLength:
19
+ Max: 20
20
+
21
+ Metrics/AbcSize:
22
+ Enabled: false
23
+
24
+ Metrics/BlockLength:
25
+ Enabled: false
26
+
27
+ Style/StringLiterals:
28
+ ConsistentQuotesInMultiline: true
29
+
30
+ Style/AsciiComments:
31
+ Enabled: false
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Style/TrailingCommaInArrayLiteral:
37
+ EnforcedStyleForMultiline: comma
38
+
39
+ Style/TrailingCommaInHashLiteral:
40
+ EnforcedStyleForMultiline: comma
41
+
42
+ Style/TrailingCommaInArguments:
43
+ EnforcedStyleForMultiline: comma
44
+
45
+ Style/ClassAndModuleChildren:
46
+ Enabled: false
47
+
48
+ Style/FrozenStringLiteralComment:
49
+ Enabled: true
50
+
51
+ Style/MethodDefParentheses:
52
+ EnforcedStyle: require_no_parentheses
53
+
54
+ Style/BarePercentLiterals:
55
+ EnforcedStyle: percent_q
56
+
57
+ Style/TrailingCommaInArguments:
58
+ EnforcedStyleForMultiline: consistent_comma
59
+
60
+ Style/TrailingCommaInArrayLiteral:
61
+ EnforcedStyleForMultiline: consistent_comma
62
+
63
+ Layout/AlignHash:
64
+ EnforcedHashRocketStyle: table
65
+ EnforcedColonStyle: table
66
+ EnforcedLastArgumentHashStyle: always_inspect
67
+
68
+ Layout/AccessModifierIndentation:
69
+ EnforcedStyle: outdent
70
+
71
+ Layout/AlignParameters:
72
+ EnforcedStyle: with_first_parameter
73
+
74
+ Layout/CaseIndentation:
75
+ EnforcedStyle: end
76
+ IndentOneStep: true
77
+ IndentationWidth: 2
78
+
79
+ Layout/EmptyLinesAroundBlockBody:
80
+ EnforcedStyle: no_empty_lines
81
+
82
+ Layout/EmptyLinesAroundClassBody:
83
+ EnforcedStyle: no_empty_lines
84
+
85
+ Layout/EmptyLinesAroundModuleBody:
86
+ EnforcedStyle: no_empty_lines
87
+
88
+ Layout/IndentArray:
89
+ EnforcedStyle: consistent
90
+
91
+ Layout/IndentHash:
92
+ EnforcedStyle: consistent
93
+
94
+ Layout/ExtraSpacing:
95
+ AllowForAlignment: true
96
+ ForceEqualSignAlignment: true
97
+
98
+ Layout/MultilineMethodCallIndentation:
99
+ EnforcedStyle: indented
100
+
101
+ # for the following case.
102
+ # validates :project, presence: true
103
+ # validates :title , presence: true
104
+ # validates :status , presence: true
105
+ Layout/SpaceBeforeComma:
106
+ Enabled: false
107
+
108
+ # for the following case.
109
+ # expect(json['title']).to eq 'implement hoge'
110
+ # expect(json['status']).to eq 'todo'
111
+ # expect(json['priority']).to eq 1
112
+ # expect(json['point']).to eq 1
113
+ Layout/SpaceBeforeFirstArg:
114
+ Enabled: false
115
+
116
+ # for the following case.
117
+ # db = ENV['db'] || 'apollo'
118
+ # user = ENV['user'] || 'postgres'
119
+ # password = ENV['password'] || ''
120
+ Layout/SpaceAroundOperators:
121
+ Enabled: false
122
+
123
+ Naming/UncommunicativeMethodParamName:
124
+ AllowedNames:
125
+ - io
126
+ - id
127
+ - to
128
+ - by
129
+ - on
130
+ - in
131
+ - at
132
+ - ex
@@ -0,0 +1,108 @@
1
+ require: rubocop-rspec
2
+
3
+ RSpec/AlignLeftLetBrace:
4
+ Enabled: true
5
+ RSpec/AlignRightLetBrace:
6
+ Enabled: false
7
+ RSpec/AnyInstance:
8
+ Enabled: true
9
+ RSpec/AroundBlock:
10
+ Enabled: true
11
+ RSpec/BeEql:
12
+ Enabled: true
13
+ RSpec/BeforeAfterAll:
14
+ Enabled: false
15
+ RSpec/ContextWording:
16
+ Enabled: true
17
+ RSpec/DescribeMethod:
18
+ Enabled: true
19
+ RSpec/DescribeSymbol:
20
+ Enabled: true
21
+ RSpec/DescribedClass:
22
+ EnforcedStyle: described_class
23
+ RSpec/EmptyExampleGroup:
24
+ Enabled: true
25
+ RSpec/EmptyLineAfterFinalLet:
26
+ Enabled: true
27
+ RSpec/EmptyLineAfterSubject:
28
+ Enabled: true
29
+ RSpec/ExampleLength:
30
+ Enabled: false
31
+ RSpec/ExampleWithoutDescription:
32
+ Enabled: true
33
+ RSpec/ExampleWording:
34
+ Enabled: true
35
+ RSpec/ExpectActual:
36
+ Enabled: true
37
+ RSpec/ExpectChange:
38
+ EnforcedStyle: method_call
39
+ RSpec/ExpectInHook:
40
+ Enabled: true
41
+ RSpec/ExpectOutput:
42
+ Enabled: true
43
+ RSpec/FilePath:
44
+ Enabled: true
45
+ RSpec/Focus:
46
+ Enabled: true
47
+ RSpec/HookArgument:
48
+ EnforcedStyle: implicit
49
+ RSpec/InstanceSpy:
50
+ Enabled: true
51
+ RSpec/InstanceVariable:
52
+ Enabled: true
53
+ RSpec/InvalidPredicateMatcher:
54
+ Enabled: true
55
+ RSpec/ItBehavesLike:
56
+ EnforcedStyle: it_behaves_like
57
+ RSpec/IteratedExpectation:
58
+ Enabled: false
59
+ RSpec/LeadingSubject:
60
+ Enabled: true
61
+ RSpec/LetBeforeExamples:
62
+ Enabled: true
63
+ RSpec/LetSetup:
64
+ Enabled: false
65
+ RSpec/MessageChain:
66
+ Enabled: false
67
+ RSpec/MessageExpectation:
68
+ EnforcedStyle: expect
69
+ RSpec/MessageSpies:
70
+ EnforcedStyle: receive
71
+ RSpec/MultipleDescribes:
72
+ Enabled: true
73
+ RSpec/MultipleExpectations:
74
+ Max: 10
75
+ Exclude:
76
+ - 'spec/requests/**/*_spec.rb'
77
+ - 'spec/features/**/*_spec.rb'
78
+ - 'spec/system/**/*_spec.rb'
79
+ RSpec/MultipleSubjects:
80
+ Enabled: true
81
+ RSpec/NamedSubject:
82
+ Enabled: false
83
+ # RSpec/NestedGroups:
84
+ # Enabled: TODO
85
+ RSpec/NotToNot:
86
+ Enabled: true
87
+ RSpec/OverwritingSetup:
88
+ Enabled: true
89
+ RSpec/RepeatedDescription:
90
+ Enabled: true
91
+ RSpec/RepeatedExample:
92
+ Enabled: true
93
+ RSpec/ReturnFromStub:
94
+ Enabled: true
95
+ RSpec/ScatteredLet:
96
+ Enabled: true
97
+ RSpec/ScatteredSetup:
98
+ Enabled: false
99
+ RSpec/SharedContext:
100
+ Enabled: true
101
+ RSpec/SingleArgumentMessageChain:
102
+ Enabled: false
103
+ RSpec/SubjectStub:
104
+ Enabled: true
105
+ RSpec/VerifiedDoubles:
106
+ Enabled: true
107
+ RSpec/VoidExpect:
108
+ Enabled: true
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,3 @@
1
+ Metrics/ModuleLength:
2
+ Exclude:
3
+ - lib/ruby_friendly_error.rb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ ---
2
+ sudo: false
3
+ notifications:
4
+ email: false
5
+ language: ruby
6
+ cache: bundler
7
+ rvm:
8
+ - 2.4.4
9
+ - 2.5.1
10
+ before_install:
11
+ - gem install bundler -v 1.16.3
12
+ script:
13
+ - bundle exec rubocop
14
+ - bundle exec rspec
@@ -0,0 +1,70 @@
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 isuke770@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
+ TODO
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ruby_friendly_error.gemspec
8
+ gemspec
9
+
10
+ group :development, :test do
11
+ gem 'pry-byebug', '~> 3.6'
12
+ gem 'rubocop-rspec', '~> 1.28'
13
+ end
14
+
15
+ group :test do
16
+ gem 'rspec', '~> 3.0'
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_friendly_error (0.0.1)
5
+ colorize (~> 0.8)
6
+ i18n (~> 1.1)
7
+ parser (~> 2.5.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.0)
13
+ byebug (10.0.2)
14
+ coderay (1.1.2)
15
+ colorize (0.8.1)
16
+ concurrent-ruby (1.0.5)
17
+ diff-lcs (1.3)
18
+ i18n (1.1.0)
19
+ concurrent-ruby (~> 1.0)
20
+ jaro_winkler (1.5.1)
21
+ method_source (0.9.0)
22
+ parallel (1.12.1)
23
+ parser (2.5.1.2)
24
+ ast (~> 2.4.0)
25
+ powerpack (0.1.2)
26
+ pry (0.11.3)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ pry-byebug (3.6.0)
30
+ byebug (~> 10.0)
31
+ pry (~> 0.10)
32
+ rainbow (3.0.0)
33
+ rake (10.5.0)
34
+ rspec (3.8.0)
35
+ rspec-core (~> 3.8.0)
36
+ rspec-expectations (~> 3.8.0)
37
+ rspec-mocks (~> 3.8.0)
38
+ rspec-core (3.8.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-expectations (3.8.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-mocks (3.8.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-support (3.8.0)
47
+ rubocop (0.58.2)
48
+ jaro_winkler (~> 1.5.1)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.5, != 2.5.1.1)
51
+ powerpack (~> 0.1)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (~> 1.0, >= 1.0.1)
55
+ rubocop-rspec (1.28.0)
56
+ rubocop (>= 0.58.0)
57
+ ruby-progressbar (1.10.0)
58
+ unicode-display_width (1.4.0)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ bundler (~> 1.16)
65
+ pry-byebug (~> 3.6)
66
+ rake (~> 10.0)
67
+ rspec (~> 3.0)
68
+ rubocop-rspec (~> 1.28)
69
+ ruby_friendly_error!
70
+
71
+ BUNDLED WITH
72
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 isuke
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.
data/README.adoc ADDED
@@ -0,0 +1,43 @@
1
+ :chapter-label:
2
+ :icons: font
3
+ :lang: en
4
+ :sectanchors:
5
+ :sectlinks:
6
+ :sectnums:
7
+ :source-highlighter: highlightjs
8
+ :toc: left
9
+ :toclevels: 1
10
+
11
+ = RubyFriendlyError image:https://img.shields.io/badge/ruby-2.4.4-cc342d.svg["ruby 2.4.4", link="https://www.ruby-lang.org/en/news/2018/03/28/ruby-2-4-4-released/"] image:https://img.shields.io/badge/ruby-2.5.1-cc342d.svg["ruby 2.5.1", link="https://www.ruby-lang.org/en/news/2018/03/28/ruby-2-5-1-released/"]
12
+
13
+ image:https://travis-ci.org/isuke/ruby_friendly_error.svg?branch=master["Build Status", link="https://travis-ci.org/isuke/ruby_friendly_error"]
14
+
15
+ == Installation and Usage
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'ruby_friendly_error'
21
+ ```
22
+
23
+ ```sh
24
+ $ bundle exec ruby_friendly_error your.rb
25
+ ```
26
+
27
+ == Development
28
+
29
+ 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.
30
+
31
+ 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 https://rubygems.org[rubygems.org].
32
+
33
+ == Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/isuke/ruby_friendly_error. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the http://contributor-covenant.org[Contributor Covenant] code of conduct.
36
+
37
+ == License
38
+
39
+ The gem is available as open source under the terms of the https://opensource.org/licenses/MIT[MIT License].
40
+
41
+ == Code of Conduct
42
+
43
+ Everyone interacting in the RubyFriendlyError project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/isuke/ruby_friendly_error/blob/master/CODE_OF_CONDUCT.adoc).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ruby_friendly_error'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'ruby_friendly_error'
5
+
6
+ RubyFriendlyError.load ARGV[0]
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parser/current' unless defined? Parser::AST::Node
4
+
5
+ class Parser::AST::Node
6
+ CLASS_VARIABLE_ASSIGNED_TYPE = :cvasgn
7
+ CLASS_VARIABLE_TYPE = :cvar
8
+ INSTANCE_VARIABLE_ASSIGNED_TYPE = :ivasgn
9
+ INSTANCE_VARIABLE_TYPE = :ivar
10
+ LOCAL_VARIABLE_ASSIGNED_TYPE = :lvasgn
11
+
12
+ VARIABLE_TYPES = [
13
+ CLASS_VARIABLE_ASSIGNED_TYPE,
14
+ CLASS_VARIABLE_TYPE,
15
+ INSTANCE_VARIABLE_ASSIGNED_TYPE,
16
+ INSTANCE_VARIABLE_TYPE,
17
+ LOCAL_VARIABLE_ASSIGNED_TYPE,
18
+ ].freeze
19
+
20
+ ARG_TYPE = :arg
21
+ OPTION_ARG_TYPE = :optarg
22
+ REST_ARG_TYPE = :restarg
23
+ BLOCK_ARG_TYPE = :blockarg
24
+ KEYWORD_ARG_TYPE = :kwarg
25
+ KEYWORD_ARG_OPTION_TYPE = :kwoptarg
26
+ KEYWORD_REST_ARG_TYPE = :kwrestarg
27
+
28
+ ARG_TYPES = [
29
+ ARG_TYPE,
30
+ OPTION_ARG_TYPE,
31
+ REST_ARG_TYPE,
32
+ BLOCK_ARG_TYPE,
33
+ KEYWORD_ARG_TYPE,
34
+ KEYWORD_ARG_OPTION_TYPE,
35
+ KEYWORD_REST_ARG_TYPE,
36
+ ].freeze
37
+
38
+ def find_by_variable_name variable_name
39
+ to_a.each do |node|
40
+ next unless node.is_a? Parser::AST::Node
41
+ if VARIABLE_TYPES.include?(node.type) || ARG_TYPES.include?(node.type)
42
+ return node if node.to_a[0].to_s.sub(/^@*/, '') == variable_name.to_s
43
+ end
44
+
45
+ result = node.find_by_variable_name(variable_name)
46
+ return result if result
47
+ end
48
+ nil
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyFriendlyError
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+ require 'i18n'
5
+ require 'parser/current'
6
+
7
+ require 'ruby_friendly_error/ast'
8
+ require 'ruby_friendly_error/version'
9
+
10
+ Bundler.require(:development)
11
+
12
+ Parser::Builders::Default.emit_lambda = true
13
+ Parser::Builders::Default.emit_procarg0 = true
14
+ Parser::Builders::Default.emit_encoding = true
15
+ Parser::Builders::Default.emit_index = true
16
+
17
+ module RubyFriendlyError
18
+ ROOT_PATH = Pathname.new(__FILE__).dirname.parent.to_s
19
+ WINDOW = 2
20
+
21
+ I18n.load_path = Dir[File.join(ROOT_PATH, 'locales', '*.yml')]
22
+ I18n.backend.load_translations
23
+ I18n.backend.store_translations(:en , YAML.load_file(File.join(ROOT_PATH, 'locales', 'en.yml')))
24
+
25
+ class << self
26
+ def load file_path
27
+ exec File.read(file_path), file_path
28
+ end
29
+
30
+ def exec file_content, file_name = '(eval)'
31
+ eval file_content, nil, file_name # rubocop:disable Security/Eval
32
+ rescue Exception => ex # rubocop:disable Lint/RescueException
33
+ exception_file_name = file_name == '(eval)' ? file_name : ex.backtrace.first.match(/(.+):[0-9]+:/)[1]
34
+ exception_file_content = file_name == '(eval)' ? file_content : File.read(exception_file_name)
35
+ exception_handling ex, exception_file_name, exception_file_content, file_name != '(eval)'
36
+ end
37
+
38
+ private
39
+
40
+ def exception_handling exception, file_name, file_content, displayed_backtrace
41
+ display_backtrace file_name, exception.backtrace if displayed_backtrace
42
+
43
+ case exception
44
+ when SyntaxError
45
+ case exception.message
46
+ when /unnecessary `end`/
47
+ render_unnecessary_end_error file_content, exception
48
+ when /unexpected end-of-input/
49
+ render_missing_end_error file_content, exception
50
+ end
51
+ when NameError
52
+ ast = suppress_error_display { Parser::CurrentRuby.parse file_content }
53
+ render_name_error_with_did_you_mean file_content, exception, ast
54
+ end
55
+
56
+ raise exception
57
+ end
58
+
59
+ def render_missing_end_error file_content, ex
60
+ line = ex.message.match(/:([0-9]+):/)[1].to_i
61
+ display_error_line file_content, line
62
+ STDERR.puts I18n.t('syntax_error.title').colorize(:light_red) + ':'
63
+ STDERR.puts format_lines(I18n.t('syntax_error.missing_end')) { |l, _i| " #{l}" }.colorize(:light_red)
64
+ end
65
+
66
+ def render_unnecessary_end_error file_content, ex
67
+ line = ex.message.match(/:([0-9]+):/)[1].to_i
68
+ display_error_line file_content, line
69
+ STDERR.puts I18n.t('syntax_error.title').colorize(:light_red) + ':'
70
+ STDERR.puts format_lines(I18n.t('syntax_error.unnecessary_end')) { |l, _i| " #{l}" }.colorize(:light_red)
71
+ end
72
+
73
+ def render_name_error_with_did_you_mean file_content, ex, ast
74
+ corrections = ex.spell_checker.corrections
75
+ corrections.each do |var_name|
76
+ node = ast.find_by_variable_name var_name
77
+ display_error_line file_content, node.loc.line
78
+ end
79
+
80
+ STDERR.puts I18n.t('name_error.title').colorize(:light_red) + ':'
81
+ var_name_str = ex.spell_checker.name.underline
82
+ corrections_str = corrections.map { |c| "`#{c.to_s.underline}`" }.join(', ')
83
+ message = I18n.t('name_error.with_did_you_mean', var_name: var_name_str, corrections: corrections_str)
84
+ STDERR.puts format_lines(message) { |l, _i| " #{l}" }.colorize(:light_red)
85
+ end
86
+
87
+ def suppress_error_display
88
+ original_stdout = $stderr
89
+ $stderr = StringIO.new
90
+
91
+ yield
92
+ ensure
93
+ $stderr = original_stdout
94
+ end
95
+
96
+ def display_error_line file_content, error_line, window = WINDOW
97
+ lines = file_content.split("\n", -1)
98
+ line_size = lines.size
99
+ window_start = [error_line - window, 1].max
100
+ window_end = [error_line + window, line_size].min
101
+
102
+ if error_line > 1
103
+ start_line = window_start
104
+ end_line = error_line - 1
105
+ STDERR.puts format_lines(lines, start_line, end_line) { |l, i| "#{start_line + i}: #{l}" }&.colorize(:light_yellow)
106
+ end
107
+ STDERR.puts "#{error_line}: #{lines[error_line - 1]}".rstrip.colorize(:light_red)
108
+ if error_line < line_size
109
+ start_line = error_line + 1
110
+ end_line = window_end
111
+ STDERR.puts format_lines(lines, error_line + 1, end_line) { |l, i| "#{start_line + i}: #{l}" }&.colorize(:light_yellow)
112
+ end
113
+ STDERR.puts
114
+ end
115
+
116
+ def format_lines lines, start_line = 1, end_line = 0
117
+ lines =
118
+ case lines
119
+ when String then lines.split("\n", -1)
120
+ when Array then lines
121
+ end
122
+ lines[(start_line - 1)..(end_line - 1)]
123
+ &.map
124
+ &.with_index { |l, i| block_given? ? yield(l, i).rstrip : l.rstrip }
125
+ &.join("\n")
126
+ end
127
+
128
+ def display_backtrace file_name, backtrace
129
+ fake_backtrace = backtrace
130
+ .dup
131
+ .delete_if { |b| b.match(/ruby_friendly_error/) }
132
+ .delete_if { |b| b.match(/`block in exec'/) }
133
+ .map do |b|
134
+ if b.match? Regexp.new("#{file_name}.*`exec'")
135
+ b.sub("`exec'", "`<main>'")
136
+ else
137
+ b
138
+ end
139
+ end
140
+
141
+ fake_backtrace_size = fake_backtrace.size
142
+ fake_backtrace_digits = fake_backtrace_size.to_s.length
143
+
144
+ fake_backtrace.reverse_each.with_index do |b, i|
145
+ num = fake_backtrace_size - i
146
+ num_digits = num.to_s.length
147
+ STDERR.puts "#{' ' * (fake_backtrace_digits - num_digits)}#{num}:#{b}".colorize(:light_red)
148
+ end
149
+ STDERR.puts
150
+ end
151
+ end
152
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,14 @@
1
+ en:
2
+ syntax_error:
3
+ title: "syntax error"
4
+ missing_end: |
5
+ missing `end`.
6
+ Probably the cause is more before line.
7
+ unnecessary_end: |
8
+ exist unnecessary `end`.
9
+ Please remove unnecessary `end`.
10
+ name_error:
11
+ title: "name error"
12
+ with_did_you_mean: |
13
+ undefined local variable or method `%{var_name}`
14
+ Did you mean? %{corrections}
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ruby_friendly_error/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ruby_friendly_error'
9
+ spec.version = RubyFriendlyError::VERSION
10
+ spec.authors = ['isuke']
11
+ spec.email = ['isuke770@gmail.com']
12
+
13
+ spec.summary = 'make to ruby error messages friendly.'
14
+ spec.description = 'make to ruby error messages friendly.'
15
+ spec.homepage = 'https://github.com/isuke/ruby_friendly_error'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'colorize', '~> 0.8'
28
+ spec.add_dependency 'i18n' , '~> 1.1'
29
+ spec.add_dependency 'parser' , '~> 2.5.1'
30
+ spec.add_development_dependency 'bundler' , '~> 1.16'
31
+ spec.add_development_dependency 'rake' , '~> 10.0'
32
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ def foo(a, b)
4
+ end
5
+
6
+ foo(1)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # 1: prayer_life = 100
4
+ # 2: player_lifee = 200
5
+ # 3:
6
+ # 4: puts 'hoge' if player_life > 0
7
+
8
+ # 1: prayer_life = 100
9
+ # 2: player_lifee = 200
10
+ # 3:
11
+
12
+ # name error:
13
+ # undefined local variable or method `player_life`
14
+ # Did you mean? `player_lifee`, `prayer_life`
15
+
16
+ prayer_life = 100
17
+
18
+ player_lifee = 100
19
+
20
+ puts 'hoge' if player_life > 0
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ def hoge prayer_life = 100 , player_lifee = 100
4
+ puts 'hoge' if player_life > 0
5
+ end
6
+
7
+ hoge
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # 10: # missing end
4
+ # 11: end
5
+ # 12:
6
+
7
+ # syntax error:
8
+ # missing `end`.
9
+ # Probably the cause is more before line.
10
+
11
+ if gets.to_i == 1
12
+ if gets.to_i == 2
13
+ puts 'foobar'
14
+ # missing `end`
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ player_life = 100
4
+
5
+ puts 'hoge' if player_life > 0
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # 7: end
4
+ # 8:
5
+ # 9: end # unnecessary `end`
6
+
7
+ # syntax error:
8
+ # exist unnecessary `end`.
9
+ # Please remove unnecessary `end`.
10
+
11
+ if gets.to_i == 1
12
+ if gets.to_i == 2
13
+ puts 'foobar'
14
+ end
15
+ end
16
+
17
+ end # unnecessary `end`
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_friendly_error
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - isuke
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.5.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: make to ruby error messages friendly.
84
+ email:
85
+ - isuke770@gmail.com
86
+ executables:
87
+ - ruby_friendly_error
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".git_consistent"
92
+ - ".gitcommit_template"
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".rubocop.yml"
96
+ - ".rubocop_rspec.yml"
97
+ - ".rubocop_todo.yml"
98
+ - ".ruby-version"
99
+ - ".travis.yml"
100
+ - CODE_OF_CONDUCT.adoc
101
+ - Gemfile
102
+ - Gemfile.lock
103
+ - LICENSE.txt
104
+ - README.adoc
105
+ - Rakefile
106
+ - bin/console
107
+ - bin/setup
108
+ - exe/ruby_friendly_error
109
+ - lib/ruby_friendly_error.rb
110
+ - lib/ruby_friendly_error/ast.rb
111
+ - lib/ruby_friendly_error/version.rb
112
+ - locales/en.yml
113
+ - ruby_friendly_error.gemspec
114
+ - samples/mismatch_args.rb
115
+ - samples/miss_spell1.rb
116
+ - samples/miss_spell2.rb
117
+ - samples/missing_end.rb
118
+ - samples/not_error.rb
119
+ - samples/unnecessary_end.rb
120
+ homepage: https://github.com/isuke/ruby_friendly_error
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.7.6
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: make to ruby error messages friendly.
144
+ test_files: []