rubocop-hanami 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9d0cdb245fcc1d369d11881b9ebc6064cc09dfe3acf0267fd2329fe3cd0f662c
4
+ data.tar.gz: 9649a91707ea8b596f96edb4e6c636a3ed6fe1b323a1533c1395c032178b4116
5
+ SHA512:
6
+ metadata.gz: b0c2d75c9f70db90f82d4bb707b315a178972c6a761f681264f3b6739cffe155c80e76c74e253dcd8bd5ccd6e35e878410eb1b3989c641e5c9125493506da7b1
7
+ data.tar.gz: 38031749617b42962b73ef90165d04ca176b5c2160265c3207a2a847f1314d70dc3886bda2e4feb865d9d9fed473635601e9b96279041c761345b029c5023770
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [v0.1.0] - 2026-10-07
11
+
12
+ ### Added
13
+
14
+ - `Hanami/SliceExports`, which flags a slice that never calls `export`, an `export` that takes anything but a
15
+ literal array of strings, and a key that matches `ForbiddenExports` or misses every `AllowedExports` pattern.
16
+ `ParentClasses` names the superclasses that mark a class as a slice, `Hanami::Slice` by default.
17
+ `RequireExport: false` lets a slice leave out `export`.
18
+
19
+ [Unreleased]: https://github.com/aaronmallen/rubocop-hanami/compare/0.1.0...HEAD
20
+ [v0.1.0]: https://github.com/aaronmallen/rubocop-hanami/releases/tag/0.1.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aaron Allen
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,11 @@
1
+ Hanami/SliceExports:
2
+ Description: Checks that each slice exports a literal list of allowed keys.
3
+ Enabled: true
4
+ VersionAdded: "0.1.0"
5
+ Include:
6
+ - "**/config/slices/*.rb"
7
+ - "**/slices/*/config/slice.rb"
8
+ ForbiddenExports: []
9
+ ParentClasses:
10
+ - Hanami::Slice
11
+ RequireExport: true
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Hanami
6
+ # Checks that every slice calls `export` with a literal array of keys, and that each key
7
+ # passes `AllowedExports` and `ForbiddenExports`. A slice that never calls `export` lets
8
+ # every other slice import any key from its container.
9
+ #
10
+ # Both settings take strings, matched exactly, and regular expressions. `ParentClasses` names
11
+ # the superclasses that mark a class as a slice, for an app whose slices inherit from a base
12
+ # class of its own. Set `RequireExport` to false to let a slice leave out `export` and check
13
+ # only the keys of the slices that call it.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # class Admin::Slice < Hanami::Slice
18
+ # end
19
+ #
20
+ # # bad
21
+ # class Admin::Slice < Hanami::Slice
22
+ # export KEYS
23
+ # end
24
+ #
25
+ # # good
26
+ # class Admin::Slice < Hanami::Slice
27
+ # export ["repos.queries"]
28
+ # end
29
+ #
30
+ # @example AllowedExports: ['repos.queries', !ruby/regexp /\Aoperations\./]
31
+ # # bad
32
+ # export ["repos.mutations"]
33
+ #
34
+ # # good
35
+ # export ["repos.queries", "operations.create"]
36
+ #
37
+ # @example ParentClasses: ['MyApp::Slice']
38
+ # # bad
39
+ # class Admin::Slice < MyApp::Slice
40
+ # end
41
+ #
42
+ # @example RequireExport: false
43
+ # # good
44
+ # class Admin::Slice < Hanami::Slice
45
+ # end
46
+ #
47
+ # @example ForbiddenExports: ['repos.mutations']
48
+ # # bad
49
+ # export ["repos.mutations"]
50
+ class SliceExports < Base
51
+ MSG_FORBIDDEN = "`%<key>s` matches ForbiddenExports."
52
+ MSG_LITERAL = "Export a literal array of strings."
53
+ MSG_MISSING = "Call `export`; a slice without it lets other slices import every key."
54
+ MSG_NOT_ALLOWED = "`%<key>s` matches none of AllowedExports."
55
+
56
+ def_node_matcher :export_argument, "(send nil? :export $_)"
57
+
58
+ def on_class(node)
59
+ return unless slice_class?(node)
60
+
61
+ arguments = node.each_descendant(:send).filter_map { |send| export_argument(send) }
62
+ return add_offense(node.identifier, message: MSG_MISSING) if arguments.empty? && cop_config["RequireExport"]
63
+
64
+ arguments.each { |argument| check(argument) }
65
+ end
66
+
67
+ private
68
+
69
+ def check(argument)
70
+ return add_offense(argument, message: MSG_LITERAL) unless argument.array_type?
71
+
72
+ argument.each_value do |value|
73
+ next add_offense(value, message: MSG_LITERAL) unless value.str_type?
74
+
75
+ message = message_for(value.value)
76
+ add_offense(value, message: format(message, key: value.value)) if message
77
+ end
78
+ end
79
+
80
+ def matches?(setting, key)
81
+ Array(cop_config[setting]).any? { |pattern| pattern.is_a?(Regexp) ? pattern.match?(key) : pattern == key }
82
+ end
83
+
84
+ def message_for(key)
85
+ return MSG_FORBIDDEN if matches?("ForbiddenExports", key)
86
+
87
+ MSG_NOT_ALLOWED if cop_config.key?("AllowedExports") && !matches?("AllowedExports", key)
88
+ end
89
+
90
+ def slice_class?(node)
91
+ parent = node.parent_class
92
+ parent&.const_type? && Array(cop_config["ParentClasses"]).include?(parent.const_name)
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lint_roller"
4
+
5
+ module RuboCop
6
+ module Hanami
7
+ # Hands RuboCop the default configuration for every cop in the Hanami department.
8
+ class Plugin < LintRoller::Plugin
9
+ def about
10
+ LintRoller::About.new(
11
+ name: "rubocop-hanami",
12
+ version: Gem.loaded_specs["rubocop-hanami"].version.to_s,
13
+ homepage: "https://github.com/aaronmallen/rubocop-hanami",
14
+ description: "A collection of RuboCop cops for Hanami.",
15
+ )
16
+ end
17
+
18
+ def rules(_context)
19
+ LintRoller::Rules.new(
20
+ type: :path,
21
+ config_format: :rubocop,
22
+ value: Pathname.new(__dir__).join("../../../config/default.yml"),
23
+ )
24
+ end
25
+
26
+ def supported?(context)
27
+ context.engine == :rubocop
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ require_relative "rubocop/hanami/plugin"
6
+ require_relative "rubocop/cop/hanami/slice_exports"
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-hanami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Allen
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: lint_roller
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rubocop
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.72'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.72'
40
+ description: A collection of RuboCop cops that check Hanami apps and slices.
41
+ email:
42
+ - hello@aaronmallen.me
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - LICENSE
49
+ - config/default.yml
50
+ - lib/rubocop-hanami.rb
51
+ - lib/rubocop/cop/hanami/slice_exports.rb
52
+ - lib/rubocop/hanami/plugin.rb
53
+ homepage: https://github.com/aaronmallen/rubocop-hanami
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ bug_tracker_uri: https://github.com/aaronmallen/rubocop-hanami/issues
58
+ changelog_uri: https://github.com/aaronmallen/rubocop-hanami/blob/main/CHANGELOG.md
59
+ default_lint_roller_plugin: RuboCop::Hanami::Plugin
60
+ homepage_uri: https://github.com/aaronmallen/rubocop-hanami
61
+ rubygems_mfa_required: 'true'
62
+ source_code_uri: https://github.com/aaronmallen/rubocop-hanami
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '3.3'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 4.0.20
78
+ specification_version: 4
79
+ summary: RuboCop cops for Hanami
80
+ test_files: []