harri 0.1.0
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 +7 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +12 -0
- data/exe/harri +61 -0
- data/harri.gemspec +34 -0
- data/lib/harri/collector.rb +95 -0
- data/lib/harri/parser.rb +34 -0
- data/lib/harri/processor.rb +27 -0
- data/lib/harri/regexes.rb +60 -0
- data/lib/harri/version.rb +7 -0
- data/lib/harri.rb +35 -0
- data/sig/harri.rbs +4 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a94855b52e7d80f6cee4012850ab58e20b9a9432c8c8cd6f86885606ab0696d8
|
4
|
+
data.tar.gz: f963fb0f1e3ffa283bf7fb8721ce6aa4da5783040c6c1772c2326e42d4562ec1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eea4d8878af6d4c0e661208909eec2450a7aace2c77bc1b32d2e9e6e81e8fd450d4ab1ec09bb478cd16ab3df9ab152397b093de8c2aa2f65ef0a542ce774869a
|
7
|
+
data.tar.gz: 91fac66571b31e73139e8235af2a30d9bbf51269371e8bad9220099d9d5649122a73f9a40955c6b9ab4b41c3d264de22f5e6af6e3608c4e11e2f579d48201b3d
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
harri (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.16.3)
|
10
|
+
rake (13.0.6)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
x86_64-linux
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
harri!
|
17
|
+
minitest (~> 5.0)
|
18
|
+
rake (~> 13.0)
|
19
|
+
|
20
|
+
BUNDLED WITH
|
21
|
+
2.3.26
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 rhizomic
|
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.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Harri: HAskell Remove Redundant Imports
|
2
|
+
|
3
|
+
Do you ever wish GHC would stop harrying you about unused imports and remove
|
4
|
+
them for you?
|
5
|
+
|
6
|
+
This tool does just that.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
$ gem install harri
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Simply pass a log containing the `STDERR` output from `GHC`, and `harri` will
|
15
|
+
auto-strip the unused imports from the affected files:
|
16
|
+
|
17
|
+
$ harri -f ghcid.txt
|
18
|
+
|
19
|
+
Note that the files mentioned in the log are considered _relative_ to the
|
20
|
+
location of the log itself.
|
21
|
+
|
22
|
+
## Development
|
23
|
+
|
24
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
25
|
+
`rake test` to run the tests. You can also run `bin/console` for an interactive
|
26
|
+
prompt that will allow you to experiment.
|
27
|
+
|
28
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
29
|
+
release a new version, update the version number in `version.rb`, and then run
|
30
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
31
|
+
git commits and the created tag, and push the `.gem` file to
|
32
|
+
[rubygems.org](https://rubygems.org).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at
|
37
|
+
https://github.com/rhizomic/harri. This project is intended to be a safe,
|
38
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
39
|
+
the [code of conduct](https://github.com/rhizomic/harri/blob/master/CODE_OF_CONDUCT.md).
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the
|
44
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
45
|
+
|
46
|
+
## Code of Conduct
|
47
|
+
|
48
|
+
Everyone interacting in the Harri project's codebases, issue trackers, chat
|
49
|
+
rooms and mailing lists is expected to follow the
|
50
|
+
[code of conduct](https://github.com/rhizomic/harri/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/exe/harri
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(__dir__ + "/../lib")
|
4
|
+
|
5
|
+
require "harri"
|
6
|
+
require "harri/version"
|
7
|
+
|
8
|
+
require "filewatcher"
|
9
|
+
require "optparse"
|
10
|
+
|
11
|
+
class CLI
|
12
|
+
def self.parse_options(args)
|
13
|
+
options = {}
|
14
|
+
|
15
|
+
option_parser = OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: harri -f FILE"
|
17
|
+
|
18
|
+
opts.separator ""
|
19
|
+
|
20
|
+
opts.separator "Example:"
|
21
|
+
opts.separator " $ harri -f ghcid.txt"
|
22
|
+
|
23
|
+
opts.separator ""
|
24
|
+
|
25
|
+
opts.separator "Options:"
|
26
|
+
|
27
|
+
opts.on("-v", "--version", "Print the version") do |v|
|
28
|
+
puts "Harri v#{Harri::Version::VERSION}"
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
33
|
+
puts opts
|
34
|
+
exit
|
35
|
+
end
|
36
|
+
|
37
|
+
opts.on("-f", "--file FILE", "The file containing GHC errors") do |file|
|
38
|
+
options[:file] = file
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
option_parser.parse!(args)
|
43
|
+
|
44
|
+
options
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
ARGV << "-h" if ARGV.empty? && $stdin.tty?
|
49
|
+
cli_options = CLI.parse_options ARGV
|
50
|
+
|
51
|
+
error_dir = File.expand_path(File.dirname(cli_options[:file]))
|
52
|
+
error_log = File.read cli_options[:file]
|
53
|
+
unused_import_errors = Harri.parse_unused_import_errors_from_log error_log
|
54
|
+
|
55
|
+
unused_import_errors.each do |import_info|
|
56
|
+
source_location = File.join error_dir, import_info[:file]
|
57
|
+
|
58
|
+
file_contents = File.read source_location
|
59
|
+
replaced = Harri.remove_unused_imports file_contents, import_info
|
60
|
+
File.write source_location, replaced
|
61
|
+
end
|
data/harri.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/harri/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "harri"
|
7
|
+
spec.version = Harri::Version::VERSION
|
8
|
+
spec.authors = ["rhizomic"]
|
9
|
+
|
10
|
+
spec.summary = "Removes redundant Haskell imports."
|
11
|
+
spec.description = "Harri: HAskell Remove Redundant Imports. A way to get GHC to stop harrying you about them."
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = ">= 2.6.0"
|
14
|
+
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/rhizomic/harri"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/rhizomic/harri/blob/master/CHANGELOG.md"
|
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(__dir__) do
|
21
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
22
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
# Uncomment to register a new dependency of your gem
|
30
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
31
|
+
|
32
|
+
# For more information and examples about making a new gem, check out our
|
33
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./regexes"
|
4
|
+
|
5
|
+
module Harri
|
6
|
+
module Collector
|
7
|
+
# This function gathers all of the lines describing an error that involves an
|
8
|
+
# unused import.
|
9
|
+
#
|
10
|
+
# An error involving unused imports could look something like this:
|
11
|
+
#
|
12
|
+
# src/Module/TypeGenerator/Generate.hs:31:1: error: [-Wunused-imports, -Werror=unused-imports]
|
13
|
+
# The import of ‘Alpha.Beta.Status’ is redundant
|
14
|
+
# except perhaps to import instances from ‘Alpha.Beta.Status’
|
15
|
+
# To import instances alone, use: import Alpha.Beta.Status()
|
16
|
+
# |
|
17
|
+
# 31 | import Alpha.Beta.Status
|
18
|
+
# | ^^^^^^^^^^^^^^^^^^^^^^^^
|
19
|
+
#
|
20
|
+
# That will occur when an entire module is redundant. If specific imports
|
21
|
+
# from within a module are redundant, the error could look something like
|
22
|
+
# this:
|
23
|
+
#
|
24
|
+
# src/Module/TypeGenerator/OtherGenerator.hs:31:1: error: [-Wunused-imports, -Werror=unused-imports]
|
25
|
+
# The import of ‘RedundantImport,
|
26
|
+
# AnotherRedundantImport, unusedFunction’
|
27
|
+
# from module ‘Alpha.Beta.Status.Types’ is redundant
|
28
|
+
# |
|
29
|
+
# 31 | import Alpha.Beta.Status.Types ( ImportantImport, RedundantImport, UsedImport, AnotherRedundantImport, unusedFunction,)
|
30
|
+
# | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
31
|
+
#
|
32
|
+
# Note that the first line always contains the name of the file, the line
|
33
|
+
# number (and column number) where it occurs, and an optional error marker.
|
34
|
+
# What follows is a textual description of the error, followed by a preview
|
35
|
+
# of the source where it occurs. This function will capture all of these
|
36
|
+
# lines.
|
37
|
+
#
|
38
|
+
# For completion's sake, here's an example of an unrelated error:
|
39
|
+
#
|
40
|
+
# src/Module/TypeGenerator/Generate.hs:712:14: error:
|
41
|
+
# Not in scope: type constructor or class ‘Foo’
|
42
|
+
# |
|
43
|
+
# 712 | doSomething @Foo
|
44
|
+
# |
|
45
|
+
#
|
46
|
+
# This function will filter out any errors that are not related to unused
|
47
|
+
# imports.
|
48
|
+
def self.gather_unused_import_errors(text)
|
49
|
+
dealing_with_unused_import = false
|
50
|
+
unused_import_errors = []
|
51
|
+
current_error = []
|
52
|
+
|
53
|
+
text.each_line do |line|
|
54
|
+
if is_start_of_an_error? line
|
55
|
+
# We've encountered an error, so add the prior error lines (if there were
|
56
|
+
# any) to our collection.
|
57
|
+
if !current_error.empty?
|
58
|
+
unused_import_errors << current_error
|
59
|
+
current_error = []
|
60
|
+
end
|
61
|
+
|
62
|
+
# Keep track of whether or not we're dealing with an unused import.
|
63
|
+
# This is useful to know if we need to capture this and subsequent lines.
|
64
|
+
dealing_with_unused_import = is_unused_import_error? line
|
65
|
+
end
|
66
|
+
|
67
|
+
# If we're _not_ dealing with an unused import, continue to the next
|
68
|
+
# iteration.
|
69
|
+
next if !dealing_with_unused_import
|
70
|
+
|
71
|
+
# Otherwise, add the line to our current list of error lines.
|
72
|
+
current_error << line
|
73
|
+
end
|
74
|
+
|
75
|
+
# We've processed all of the lines; make sure to add the prior error lines
|
76
|
+
# (if there were any) to our collection.
|
77
|
+
if !current_error.empty?
|
78
|
+
unused_import_errors << current_error
|
79
|
+
end
|
80
|
+
|
81
|
+
unused_import_errors
|
82
|
+
end
|
83
|
+
|
84
|
+
# Determines if the provided line is the beginning of a new error.
|
85
|
+
def self.is_start_of_an_error?(line)
|
86
|
+
line =~ Harri::Regexes::START_ERROR_REGEX
|
87
|
+
end
|
88
|
+
|
89
|
+
# Determines if the provided line is the beginning of an "unused import"
|
90
|
+
# error.
|
91
|
+
def self.is_unused_import_error?(line)
|
92
|
+
line =~ Harri::Regexes::UNUSED_IMPORT_ERROR_REGEX
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/harri/parser.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./regexes"
|
4
|
+
|
5
|
+
module Harri
|
6
|
+
module Parser
|
7
|
+
# Given an "unused import" error stanza, parses the involved filename,
|
8
|
+
# module, and unused imports.
|
9
|
+
def self.parse_unused_import_error(lines)
|
10
|
+
first_line = lines[0]
|
11
|
+
remaining_lines = lines.drop 1
|
12
|
+
|
13
|
+
file = first_line.match(Harri::Regexes::START_ERROR_REGEX)[1]
|
14
|
+
|
15
|
+
error = remaining_lines.map(&:strip).join " "
|
16
|
+
|
17
|
+
redundant_module_match = error.match Harri::Regexes::ENTIRE_MODULE_REDUNDANT_REGEX
|
18
|
+
if redundant_module_match
|
19
|
+
{
|
20
|
+
file: file,
|
21
|
+
module: redundant_module_match[1],
|
22
|
+
imports: []
|
23
|
+
}
|
24
|
+
else
|
25
|
+
redundant_imports_match = error.match Harri::Regexes::REDUNDANT_IMPORTS_WITHIN_MODULE_REGEX
|
26
|
+
{
|
27
|
+
file: file,
|
28
|
+
module: redundant_imports_match[2],
|
29
|
+
imports: redundant_imports_match[1].split(",").map(&:strip)
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./regexes"
|
4
|
+
|
5
|
+
module Harri
|
6
|
+
module Processor
|
7
|
+
# Removes the redundant imports from a string.
|
8
|
+
def self.remove_imports(text, import_info)
|
9
|
+
import_regex = Harri::Regexes.import_declaration_regex import_info[:module]
|
10
|
+
match = text.match import_regex
|
11
|
+
|
12
|
+
return text if !match
|
13
|
+
|
14
|
+
if import_info[:imports].empty?
|
15
|
+
# There are no imports, so the whole module is redundant.
|
16
|
+
text.sub(/^#{Regexp.quote(match[0])}/, "")
|
17
|
+
else
|
18
|
+
# Filter out specific imports within the module.
|
19
|
+
filtered_imports = import_info[:imports].reduce(match[0]) do |result, import|
|
20
|
+
reference_regex = Harri::Regexes.named_reference_regex import
|
21
|
+
result.sub reference_regex, ""
|
22
|
+
end
|
23
|
+
text.sub match[0], filtered_imports
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Harri
|
2
|
+
module Regexes
|
3
|
+
# Intended to capture the first line of an error within GHC's output.
|
4
|
+
START_ERROR_REGEX = %r{
|
5
|
+
(.*\.hs) # filename
|
6
|
+
: # literal colon
|
7
|
+
\d+ # line number
|
8
|
+
: # literal colon
|
9
|
+
\d+ # column number
|
10
|
+
: # literal colon
|
11
|
+
\s # space
|
12
|
+
error:.* # literal "error:" with possible marker
|
13
|
+
}x
|
14
|
+
|
15
|
+
# Intended to capture an unused import error within GHC's output.
|
16
|
+
UNUSED_IMPORT_ERROR_REGEX = %r{
|
17
|
+
.*\.hs # filename
|
18
|
+
: # literal colon
|
19
|
+
\d+ # line number
|
20
|
+
: # literal colon
|
21
|
+
\d+ # column number
|
22
|
+
: # literal colon
|
23
|
+
\s # space
|
24
|
+
error: # literal "error:"
|
25
|
+
\s # space
|
26
|
+
\[-Wunused-imports,\s-Werror=unused-imports\] # literal "[-Wunused-imports, -Werror=unused-imports]"
|
27
|
+
}x
|
28
|
+
|
29
|
+
# Intended to capture the scenario when an entire module is redundant.
|
30
|
+
ENTIRE_MODULE_REDUNDANT_REGEX = /The import of ‘(.+)’ is redundant except.*/
|
31
|
+
|
32
|
+
# Intended to capture the scenario when specific imports within a module are redundant.
|
33
|
+
REDUNDANT_IMPORTS_WITHIN_MODULE_REGEX = /The import of ‘(.+)’ from module ‘(.+)’ is redundant.*/
|
34
|
+
|
35
|
+
# Intended to capture a full import declaration within a Haskell module.
|
36
|
+
def self.import_declaration_regex(module_name)
|
37
|
+
%r{
|
38
|
+
^import # literal "import"
|
39
|
+
\s+ # one or more spaces
|
40
|
+
(?:qualified\s*)? # zero or one literal "qualified" with optional space afterwards
|
41
|
+
#{Regexp.quote module_name} # match "import `module_name`" at start of line
|
42
|
+
\s* # optional space
|
43
|
+
(?:qualified\s*)? # zero or one literal "qualified" with optional space afterwards
|
44
|
+
(?:as\s+[A-Z]+[a-zA-Z0-9.]*\s*)? # zero or one literal "as `module_name`" with optional space afterwards
|
45
|
+
(?:hiding\s*)? # zero or one literal "hiding" with optional space afterwards
|
46
|
+
(?:(\((?>[^)(]+|\g<1>)*\)))? # zero or one list of imports (see https://stackoverflow.com/a/35271017)
|
47
|
+
\n? # zero or one linebreak
|
48
|
+
}x
|
49
|
+
end
|
50
|
+
|
51
|
+
# Intended to capture a named reference within an import declaration.
|
52
|
+
def self.named_reference_regex(reference)
|
53
|
+
%r{
|
54
|
+
\b#{Regexp.quote reference}\b # match "`reference`" exactly as a single word
|
55
|
+
(?:\s*,?)? # zero or one instances of optional space followed by zero or one comma
|
56
|
+
\s* # optional space
|
57
|
+
}x
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/harri.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "harri/collector"
|
4
|
+
require_relative "harri/parser"
|
5
|
+
require_relative "harri/processor"
|
6
|
+
require_relative "harri/version"
|
7
|
+
|
8
|
+
module Harri
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
# Parses the unused import errors from a log. This will return something
|
12
|
+
# that looks like this:
|
13
|
+
#
|
14
|
+
# ```
|
15
|
+
# [
|
16
|
+
# {
|
17
|
+
# file: # the filename where the error is occurring
|
18
|
+
# module: # the involved module name
|
19
|
+
# imports: # the list of unused imports (if empty, the entire module is
|
20
|
+
# redundant)
|
21
|
+
# }
|
22
|
+
# ]
|
23
|
+
# ```
|
24
|
+
def self.parse_unused_import_errors_from_log(log_contents)
|
25
|
+
errors = Harri::Collector.gather_unused_import_errors log_contents
|
26
|
+
errors.map do |error|
|
27
|
+
Harri::Parser.parse_unused_import_error error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Removes the redundant imports from a string.
|
32
|
+
def self.remove_unused_imports(text, import_info)
|
33
|
+
Harri::Processor.remove_imports text, import_info
|
34
|
+
end
|
35
|
+
end
|
data/sig/harri.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: harri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rhizomic
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: 'Harri: HAskell Remove Redundant Imports. A way to get GHC to stop harrying
|
14
|
+
you about them.'
|
15
|
+
email:
|
16
|
+
executables:
|
17
|
+
- harri
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".standard.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- exe/harri
|
30
|
+
- harri.gemspec
|
31
|
+
- lib/harri.rb
|
32
|
+
- lib/harri/collector.rb
|
33
|
+
- lib/harri/parser.rb
|
34
|
+
- lib/harri/processor.rb
|
35
|
+
- lib/harri/regexes.rb
|
36
|
+
- lib/harri/version.rb
|
37
|
+
- sig/harri.rbs
|
38
|
+
homepage:
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata:
|
42
|
+
source_code_uri: https://github.com/rhizomic/harri
|
43
|
+
changelog_uri: https://github.com/rhizomic/harri/blob/master/CHANGELOG.md
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.6.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.3.26
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Removes redundant Haskell imports.
|
63
|
+
test_files: []
|