const_stricter 1.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/.rspec +3 -0
- data/.rubocop.yml +31 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +10 -0
- data/docs/lint_dummy.jpg +0 -0
- data/dummy/traceable.rb +9 -0
- data/dummy/user.rb +18 -0
- data/lib/const_stricter/const_map.rb +11 -0
- data/lib/const_stricter/const_name.rb +39 -0
- data/lib/const_stricter/const_name_part.rb +5 -0
- data/lib/const_stricter/const_parser.rb +60 -0
- data/lib/const_stricter/const_resolver.rb +69 -0
- data/lib/const_stricter/const_visitor.rb +70 -0
- data/lib/const_stricter/parsed_const.rb +18 -0
- data/lib/const_stricter/railtie.rb +8 -0
- data/lib/const_stricter/scoped_const_visitor.rb +31 -0
- data/lib/const_stricter/tasks/lint.rake +47 -0
- data/lib/const_stricter/version.rb +3 -0
- data/lib/const_stricter.rb +19 -0
- data/lint_dummy.rb +15 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a0e7ca667eae829cfe5f01b2c3c18c13789715cfc1c42c85abea3573642bedbe
|
4
|
+
data.tar.gz: e41d01e354912a8557f475227f811f6a97150c9e1279895470d9d7e175992b38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2a830d6bec3c7336429c0f7f74f585952775acf5e7720eb9ec87488093d4f6a39a79248ec7512b684fd7ec5094797ebc551fca5d294a815df03331375d41a69
|
7
|
+
data.tar.gz: 4793d318c79352c672eef5cf4377f05962eb790ab20d47968b74eaa70fe45cf0dcf64b96ea71fde200ba1291caa413a1720719bba985cf727098c669e970b6c6
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-gp:
|
3
|
+
- ./config/default.yml
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 3.2
|
7
|
+
|
8
|
+
Style/StringLiterals:
|
9
|
+
EnforcedStyle: double_quotes
|
10
|
+
|
11
|
+
Style/StringLiteralsInInterpolation:
|
12
|
+
EnforcedStyle: double_quotes
|
13
|
+
|
14
|
+
Style/FrozenStringLiteralComment:
|
15
|
+
EnforcedStyle: never
|
16
|
+
|
17
|
+
Lint/ConstantDefinitionInBlock:
|
18
|
+
Exclude:
|
19
|
+
- spec/const_stricter/const_stricter_evaluate_spec.rb
|
20
|
+
|
21
|
+
RSpec/LeakyConstantDeclaration:
|
22
|
+
Exclude:
|
23
|
+
- spec/const_stricter/const_stricter_evaluate_spec.rb
|
24
|
+
|
25
|
+
Gp/SpecConstantAssignment:
|
26
|
+
Exclude:
|
27
|
+
- spec/const_stricter/const_stricter_evaluate_spec.rb
|
28
|
+
|
29
|
+
RSpec/DescribedClass:
|
30
|
+
Exclude:
|
31
|
+
- spec/const_stricter/const_stricter_evaluate_spec.rb
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Sergei Malykh
|
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,61 @@
|
|
1
|
+
# const_stricter
|
2
|
+
|
3
|
+
The `const_stricter` is a Ruby library designed to identify unused code by analyzing constant dependencies in your project. There are two approaches to achieve this:
|
4
|
+
|
5
|
+
- **Runtime Tracking**: This method logs calls to classes and modules during runtime. While this approach provides reliable dependency map, it introduces runtime overhead and cannot detect references to undefined constants until the code is deployed to production.
|
6
|
+
- **Static Code Parsing**: This approach parses source files to identify constant usage. Although less precise in cases involving dynamically generated constant names, it allows you to detect errors and unused code before deployment, improving code quality early in the development cycle.
|
7
|
+
|
8
|
+
Gem `const_stricter` uses **Static Code Parsing** via [prism](https://github.com/ruby/prism) to help developers maintain cleaner codebases by identifying and removing unused constants efficiently.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Install the gem and add to the application's Gemfile by executing:
|
13
|
+
```bash
|
14
|
+
bundle add const_stricter --group "development"
|
15
|
+
```
|
16
|
+
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
18
|
+
```bash
|
19
|
+
gem install const_stricter
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Run rake task from the root folder of your application:
|
25
|
+
```bash
|
26
|
+
rake const_stricter:lint
|
27
|
+
```
|
28
|
+
|
29
|
+
As a result, you'll see something like following (run `bin/lint_dummy` from gem root):
|
30
|
+

|
31
|
+
|
32
|
+
The section `Dynamic constants` shows strings that can only be calculated in runtime.
|
33
|
+
|
34
|
+
The pseudo-code in `Missed constants` means that the constant `HTTP` is missing (can't be evaluated) in context of `Traceable`.
|
35
|
+
|
36
|
+
You can specify which files need to be checked by setting the `glob` parameter in the rake task (`{app,lib}/**/*.rb` by default):
|
37
|
+
```bash
|
38
|
+
rake const_stricter:lint[lib/*.rb]
|
39
|
+
```
|
40
|
+
|
41
|
+
## Limitations
|
42
|
+
|
43
|
+
The `const_stricter` can't find constants whose names are set using meta-programming. Examples [in tests](spec/const_stricter/dynamic_spec.rb)
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
48
|
+
|
49
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/corp-gp/const_stricter. 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/corp-gp/const_stricter/blob/master/CODE_OF_CONDUCT.md).
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
58
|
+
|
59
|
+
## Code of Conduct
|
60
|
+
|
61
|
+
Everyone interacting in the `const_stricter` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/corp-gp/const_stricter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/docs/lint_dummy.jpg
ADDED
Binary file
|
data/dummy/traceable.rb
ADDED
data/dummy/user.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class User
|
2
|
+
include Traceable
|
3
|
+
|
4
|
+
ADMIN_ID = 1
|
5
|
+
TRACEABLE_TYPE = "RetailUser"
|
6
|
+
|
7
|
+
def admin?
|
8
|
+
id == ADMIN_ID
|
9
|
+
end
|
10
|
+
|
11
|
+
def gravatar
|
12
|
+
HTTP.get("https://gravatar.com/#{permalink}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def github
|
16
|
+
HTTP.get("https://github.com/#{permalink}")
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ConstStricter
|
2
|
+
class ConstName
|
3
|
+
attr_reader :dynamic, :name_parts
|
4
|
+
private :name_parts
|
5
|
+
|
6
|
+
def initialize(name_parts = []) # rubocop:disable Gp/OptArgParameters
|
7
|
+
@name_parts = name_parts
|
8
|
+
@dynamic = false
|
9
|
+
end
|
10
|
+
|
11
|
+
def inspect = "#{full_name} :#{line_no}"
|
12
|
+
|
13
|
+
def line_no
|
14
|
+
@line_no ||= name_parts[0].line_no
|
15
|
+
end
|
16
|
+
|
17
|
+
def parent = name_parts[0]
|
18
|
+
|
19
|
+
def unshift(name_part)
|
20
|
+
@dynamic ||= name_part.dynamic
|
21
|
+
name_parts.unshift name_part
|
22
|
+
end
|
23
|
+
|
24
|
+
def pop = name_parts.pop
|
25
|
+
|
26
|
+
MODULE_DELIMITER = "::"
|
27
|
+
private_constant :MODULE_DELIMITER
|
28
|
+
|
29
|
+
def self.split(string)
|
30
|
+
string.split(MODULE_DELIMITER)
|
31
|
+
end
|
32
|
+
|
33
|
+
def full_name
|
34
|
+
name_parts.join(MODULE_DELIMITER) unless empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
def empty? = name_parts.empty?
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "const_stricter/scoped_const_visitor"
|
2
|
+
require "const_stricter/parsed_const"
|
3
|
+
|
4
|
+
module ConstStricter
|
5
|
+
class ConstParser
|
6
|
+
attr_reader :file_path
|
7
|
+
|
8
|
+
def initialize(prism_code, file_path:)
|
9
|
+
@prism_code = prism_code
|
10
|
+
@file_path = file_path
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.in_file(file_path:)
|
14
|
+
new(parse_file(file_path), file_path:).find_constants
|
15
|
+
end
|
16
|
+
|
17
|
+
private_class_method def self.parse_file(file_path) = Prism.parse_lex_file(file_path)
|
18
|
+
|
19
|
+
PATH_TO_MAIN = "main"
|
20
|
+
private_constant :PATH_TO_MAIN
|
21
|
+
|
22
|
+
def self.in_code(code:)
|
23
|
+
new(parse_code(code), file_path: PATH_TO_MAIN).find_constants
|
24
|
+
end
|
25
|
+
|
26
|
+
private_class_method def self.parse_code(code) = Prism.parse_lex(code)
|
27
|
+
|
28
|
+
def find_constants
|
29
|
+
visitor = ScopedConstVisitor.new
|
30
|
+
@prism_code.value[0].accept(visitor)
|
31
|
+
|
32
|
+
find_constants_recursive(visitor.const_map)
|
33
|
+
end
|
34
|
+
|
35
|
+
LINE_NO_SEPARATOR = ":"
|
36
|
+
private_constant :LINE_NO_SEPARATOR
|
37
|
+
|
38
|
+
private def find_constants_recursive(const_map, namespaces: [])
|
39
|
+
constants = []
|
40
|
+
|
41
|
+
const_map.each do |namespace, child_const_map|
|
42
|
+
parsed_const =
|
43
|
+
ParsedConst.new(
|
44
|
+
namespace: ConstName.new(namespaces.map(&:full_name)).full_name,
|
45
|
+
const_name: namespace.full_name,
|
46
|
+
)
|
47
|
+
parsed_const.location = [file_path, namespace.line_no].compact.join(LINE_NO_SEPARATOR)
|
48
|
+
parsed_const.dynamic = namespace.dynamic
|
49
|
+
|
50
|
+
constants << parsed_const
|
51
|
+
|
52
|
+
unless child_const_map.empty?
|
53
|
+
constants.concat find_constants_recursive(child_const_map, namespaces: namespaces + [namespace])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
constants
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
module ConstStricter
|
4
|
+
class ConstResolver
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@cache = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.missed?(namespace:, const_name:)
|
12
|
+
evaluate(namespace:, const_name:) != nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.evaluate(namespace:, const_name:)
|
16
|
+
instance.evaluate(namespace:, const_name:)
|
17
|
+
end
|
18
|
+
|
19
|
+
def evaluate(namespace:, const_name:)
|
20
|
+
lookup = ConstLookup.new
|
21
|
+
lookup.cache = @cache
|
22
|
+
|
23
|
+
evaluated_constant =
|
24
|
+
lookup.find_constant(namespace:, const_name:, inherit: false) ||
|
25
|
+
lookup.find_constant(namespace:, const_name:, inherit: true)
|
26
|
+
|
27
|
+
lookup.resolved_paths.each do |const_hsh|
|
28
|
+
@cache[const_hsh] = evaluated_constant
|
29
|
+
end
|
30
|
+
|
31
|
+
evaluated_constant
|
32
|
+
end
|
33
|
+
|
34
|
+
class ConstLookup
|
35
|
+
attr_accessor :cache, :resolved_paths
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@resolved_paths = []
|
39
|
+
end
|
40
|
+
|
41
|
+
def find_constant(namespace:, const_name:, inherit: false)
|
42
|
+
cache_key = { namespace:, const_name: }
|
43
|
+
return @cache[cache_key] if @cache.key?(cache_key)
|
44
|
+
|
45
|
+
resolved_paths << cache_key
|
46
|
+
|
47
|
+
if namespace
|
48
|
+
Object.const_get(namespace).const_get(const_name, inherit)
|
49
|
+
else
|
50
|
+
Object.const_get(const_name, inherit)
|
51
|
+
end
|
52
|
+
rescue NameError => e
|
53
|
+
missed_const_name = e.message[/uninitialized constant (.+)$/, 1]
|
54
|
+
if missed_const_name != const_name && !const_name.start_with?(missed_const_name) && !missed_const_name.delete_prefix(namespace) == const_name
|
55
|
+
# срабатывание может быть вызвано не тем, что не существует искомая константа,
|
56
|
+
# а тем, что есть несвязанная ошибка в коде вызываемого класса/модуля
|
57
|
+
raise
|
58
|
+
end
|
59
|
+
|
60
|
+
if namespace
|
61
|
+
const_path = ConstName.new(ConstName.split(namespace))
|
62
|
+
const_path.pop
|
63
|
+
|
64
|
+
find_constant(namespace: const_path.full_name, const_name:, inherit:)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "active_support/core_ext/module/redefine_method"
|
2
|
+
|
3
|
+
require "const_stricter/const_name"
|
4
|
+
require "const_stricter/const_name_part"
|
5
|
+
require "const_stricter/const_map"
|
6
|
+
|
7
|
+
module ConstStricter
|
8
|
+
class ConstVisitor < Prism::Visitor
|
9
|
+
attr_reader :const_map
|
10
|
+
|
11
|
+
instance_methods.grep(/visit_/).each do |method_name|
|
12
|
+
redefine_method(method_name) { |node| visit_child_nodes(node) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@const_path = []
|
17
|
+
@current_const = ConstName.new
|
18
|
+
@const_map = ConstMap.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def visit_constant_path_node(node)
|
22
|
+
if node.compact_child_nodes.empty?
|
23
|
+
# include ::ComponentViewPath не вызывает visit_constant_read_node
|
24
|
+
visit_constant_read_node(node, force_global_scope: true)
|
25
|
+
else
|
26
|
+
@current_const.unshift(
|
27
|
+
ConstNamePart.new(node.name.to_s).tap do |name_part|
|
28
|
+
name_part.line_no = node.location.start_line
|
29
|
+
end,
|
30
|
+
)
|
31
|
+
visit_child_nodes(node)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
EMPTY_ARRAY = [].freeze
|
36
|
+
private_constant :EMPTY_ARRAY
|
37
|
+
|
38
|
+
def visit_constant_read_node(node, force_global_scope: false)
|
39
|
+
@current_const.unshift(
|
40
|
+
ConstNamePart.new(node.name.to_s).tap do |name_part|
|
41
|
+
name_part.line_no = node.location.start_line
|
42
|
+
end,
|
43
|
+
)
|
44
|
+
@const_map.push(const_path: force_global_scope ? EMPTY_ARRAY : @const_path, const_name: @current_const)
|
45
|
+
@current_const = ConstName.new
|
46
|
+
end
|
47
|
+
|
48
|
+
def visit_child_nodes(node)
|
49
|
+
if !@current_const.empty? && !node.is_a?(Prism::ConstantPathNode)
|
50
|
+
unless @current_const.parent.dynamic
|
51
|
+
# slice возвращает код ноды, включая все дочерние
|
52
|
+
# в родительскую цепочку добавляется только самый верхний уровень
|
53
|
+
# connection.module::Jobs::ImportProductsJob
|
54
|
+
@current_const.unshift(
|
55
|
+
ConstNamePart.new(node.slice).tap do |name_part|
|
56
|
+
name_part.line_no = node.location.start_line
|
57
|
+
name_part.dynamic = true
|
58
|
+
end,
|
59
|
+
)
|
60
|
+
end
|
61
|
+
if node.compact_child_nodes.empty?
|
62
|
+
# Values()::USER_ID
|
63
|
+
@const_map.push(const_path: @const_path, const_name: @current_const)
|
64
|
+
@current_const = ConstName.new
|
65
|
+
end
|
66
|
+
end
|
67
|
+
super
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ConstStricter
|
2
|
+
ParsedConst = Struct.new(:namespace, :const_name, keyword_init: true)
|
3
|
+
|
4
|
+
class ParsedConst
|
5
|
+
attr_accessor :dynamic, :location
|
6
|
+
|
7
|
+
DEFAULT_NAMESPACE = "Object"
|
8
|
+
private_constant :DEFAULT_NAMESPACE
|
9
|
+
|
10
|
+
def initialize(namespace:, const_name:)
|
11
|
+
super
|
12
|
+
|
13
|
+
self.namespace ||= DEFAULT_NAMESPACE
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect = "#{namespace} { #{const_name} } → #{location}"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "const_stricter/const_visitor"
|
2
|
+
|
3
|
+
module ConstStricter
|
4
|
+
class ScopedConstVisitor < ConstVisitor
|
5
|
+
def visit_module_node(node)
|
6
|
+
visit_scoped_node(node)
|
7
|
+
end
|
8
|
+
|
9
|
+
def visit_class_node(node)
|
10
|
+
visit_scoped_node(node)
|
11
|
+
end
|
12
|
+
|
13
|
+
def visit_scoped_node(node)
|
14
|
+
const_name_visitor = ConstVisitor.new
|
15
|
+
node.constant_path.accept(const_name_visitor)
|
16
|
+
|
17
|
+
const_name = const_name_visitor.const_map.keys[0]
|
18
|
+
|
19
|
+
@const_map.push(const_path: @const_path, const_name:)
|
20
|
+
|
21
|
+
@const_path << const_name
|
22
|
+
|
23
|
+
# первый дочерний элемент - это название модуля/класса (constant_path)
|
24
|
+
node.compact_child_nodes[1..].each do |child_node|
|
25
|
+
child_node.accept(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
@const_path.pop if const_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "colorized_string"
|
2
|
+
|
3
|
+
namespace :const_stricter do
|
4
|
+
desc "Lint constants in project const_stricter:lint[{app,lib}/**/*.rb]"
|
5
|
+
task :lint, [:glob] => :environment do |_t, args|
|
6
|
+
glob = args[:glob] || "{app,lib}/**/*.rb"
|
7
|
+
|
8
|
+
constants =
|
9
|
+
Dir.glob(glob).flat_map do |file_path|
|
10
|
+
ConstStricter.constants_in_file(file_path:)
|
11
|
+
end
|
12
|
+
|
13
|
+
missed_constants = Hash.new { |hsh, key| hsh[key] = Set.new }
|
14
|
+
dynamic_constants = Hash.new { |hsh, key| hsh[key] = Set.new }
|
15
|
+
|
16
|
+
constants.each do |parsed_const|
|
17
|
+
if parsed_const.dynamic
|
18
|
+
dynamic_constants[parsed_const] << parsed_const.location
|
19
|
+
elsif !ConstStricter.constant_missed?(namespace: parsed_const.namespace, const_name: parsed_const.const_name)
|
20
|
+
missed_constants[parsed_const] << parsed_const.location
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
unless dynamic_constants.empty?
|
25
|
+
puts ColorizedString["Dynamic constants"].colorize(:light_blue)
|
26
|
+
dynamic_constants.each_key.with_index do |parsed_const, idx|
|
27
|
+
pretty_print(parsed_const, locations: dynamic_constants[parsed_const], number: idx + 1)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
unless missed_constants.empty?
|
32
|
+
puts ColorizedString["Missed constants"].colorize(:yellow)
|
33
|
+
missed_constants.each_key.with_index do |parsed_const, idx|
|
34
|
+
pretty_print(parsed_const, locations: missed_constants[parsed_const], number: idx + 1)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
LOCATION_SEPARATOR = "\n ↳ "
|
41
|
+
|
42
|
+
def pretty_print(parsed_const, locations:, number:)
|
43
|
+
puts <<~TEXT
|
44
|
+
#{number}. #{ColorizedString[parsed_const.const_name].colorize(:light_magenta)} in #{parsed_const.namespace}
|
45
|
+
↳ #{locations.join(LOCATION_SEPARATOR)}
|
46
|
+
TEXT
|
47
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "prism"
|
2
|
+
require "active_support/core_ext/array/wrap"
|
3
|
+
require "active_support/core_ext/object/blank"
|
4
|
+
|
5
|
+
require "const_stricter/version"
|
6
|
+
require "const_stricter/railtie" if defined?(Rails::Railtie)
|
7
|
+
|
8
|
+
module ConstStricter
|
9
|
+
autoload :ConstName, "const_stricter/const_name"
|
10
|
+
autoload :ConstParser, "const_stricter/const_parser"
|
11
|
+
autoload :ConstResolver, "const_stricter/const_resolver"
|
12
|
+
|
13
|
+
module_function
|
14
|
+
|
15
|
+
def constants_in_file(file_path:) = ConstParser.in_file(file_path:)
|
16
|
+
def constants_in_code(code:) = ConstParser.in_code(code:)
|
17
|
+
|
18
|
+
def constant_missed?(namespace:, const_name:) = ConstResolver.missed?(namespace:, const_name:)
|
19
|
+
end
|
data/lint_dummy.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.require :default
|
3
|
+
|
4
|
+
load "const_stricter/tasks/lint.rake"
|
5
|
+
|
6
|
+
APP_GLOB = "dummy/*.rb"
|
7
|
+
|
8
|
+
# load app
|
9
|
+
path = File.expand_path(__dir__)
|
10
|
+
Dir.glob("#{path}/#{APP_GLOB}").each { |f| require f }
|
11
|
+
|
12
|
+
rake_task = Rake::Task["const_stricter:lint"]
|
13
|
+
|
14
|
+
rake_task.clear_prerequisites # reset requirement for environment
|
15
|
+
rake_task.invoke(APP_GLOB)
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: const_stricter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergei Malykh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colorize
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: prism
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- xronos.i.am@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".rspec"
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- CHANGELOG.md
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- docs/lint_dummy.jpg
|
70
|
+
- dummy/traceable.rb
|
71
|
+
- dummy/user.rb
|
72
|
+
- lib/const_stricter.rb
|
73
|
+
- lib/const_stricter/const_map.rb
|
74
|
+
- lib/const_stricter/const_name.rb
|
75
|
+
- lib/const_stricter/const_name_part.rb
|
76
|
+
- lib/const_stricter/const_parser.rb
|
77
|
+
- lib/const_stricter/const_resolver.rb
|
78
|
+
- lib/const_stricter/const_visitor.rb
|
79
|
+
- lib/const_stricter/parsed_const.rb
|
80
|
+
- lib/const_stricter/railtie.rb
|
81
|
+
- lib/const_stricter/scoped_const_visitor.rb
|
82
|
+
- lib/const_stricter/tasks/lint.rake
|
83
|
+
- lib/const_stricter/version.rb
|
84
|
+
- lint_dummy.rb
|
85
|
+
homepage: https://github.com/corp-gp/const_stricter
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata:
|
89
|
+
allowed_push_host: https://rubygems.org
|
90
|
+
homepage_uri: https://github.com/corp-gp/const_stricter
|
91
|
+
source_code_uri: https://github.com/corp-gp/const_stricter
|
92
|
+
changelog_uri: https://github.com/corp-gp/const_stricter/CHANGELOG.md
|
93
|
+
rubygems_mfa_required: 'true'
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 3.2.0
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubygems_version: 3.4.19
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Check project's files for nonexistent constants
|
113
|
+
test_files: []
|