rubocop-hash_inspect 0.2.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/CHANGELOG.md +22 -0
- data/LICENSE +197 -0
- data/README.md +109 -0
- data/config/default.yml +14 -0
- data/lib/rubocop/cop/hash_inspect/legacy_hash_inspect_format.rb +115 -0
- data/lib/rubocop/cop/hash_inspect_cops.rb +3 -0
- data/lib/rubocop/hash_inspect/plugin.rb +32 -0
- data/lib/rubocop/hash_inspect/version.rb +7 -0
- data/lib/rubocop/hash_inspect.rb +9 -0
- data/lib/rubocop-hash_inspect.rb +9 -0
- metadata +118 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 2d13c20e6dd17a7804619a5bbe3947d261df3eb9c5e154d085e33aa9190d6c7a
|
|
4
|
+
data.tar.gz: 9aea51916a39c43a902aa084d38b35b5276c6bf9d61ab14edf17f6c86246d6f4
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ea2ac279ad489da0e9b3917bcf403da2e8641d16b5ac6db5eeb0731c85178934b095a5f9da80749088c4b15754229df1618d609bd6fae4999537b35ef37833c0
|
|
7
|
+
data.tar.gz: d266ae065bd001d8b54dca42684cb54b40ac34550551248b01d1d0915822f6462b39a414bee015e656ba0ce69505912c6532b49f3fc6ee3f69d966fbbbd7346c
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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.2.0](https://github.com/puppetlabs/rubocop-hash_inspect/tree/v0.2.0) - 2026-06-17
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- HashInspect/LegacyHashInspectFormat cop: flags string, interpolated-string, and regexp literals containing the legacy Hash#inspect output format ({:sym=>val}, Ruby <= 3.3) that breaks on Ruby 3.4 / Puppet 9, which changed Hash#inspect to {sym: val} ([Ruby Bug #20433](https://bugs.ruby-lang.org/issues/20433)).
|
|
15
|
+
|
|
16
|
+
## [v0.1.0](https://github.com/puppetlabs/rubocop-hash_inspect/tree/v0.1.0) - 2026-06-15
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- Gem skeleton with plugin registration via `LintRoller::Plugin`
|
|
21
|
+
- Support for loading via both `plugins:` (RuboCop >= 1.72) and `require:` directives
|
|
22
|
+
- `config/default.yml` scaffold (zero cops — `LegacyHashInspectFormat` cop lands in 0.2.0)
|
data/LICENSE
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of developing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent contributions
|
|
76
|
+
Licensable by such Contributor that are necessarily infringed by
|
|
77
|
+
their Contribution(s) alone or by the combination of their
|
|
78
|
+
Contribution(s) with the Work to which such Contribution(s) were
|
|
79
|
+
submitted. If You institute patent litigation against any entity
|
|
80
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
81
|
+
the Work or any direct or indirect patent contribution embodied in
|
|
82
|
+
the Work constitutes patent or contributory patent infringement, then
|
|
83
|
+
any patent rights granted to You under this License for that Work
|
|
84
|
+
shall terminate as of the date such litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the
|
|
117
|
+
License.
|
|
118
|
+
|
|
119
|
+
You may add Your own license statement for Your modifications and
|
|
120
|
+
may provide additional terms and conditions for use, reproduction,
|
|
121
|
+
or modification of Your modifications, or for such other Derivative
|
|
122
|
+
Works as a whole, provided Your use, reproduction, and distribution
|
|
123
|
+
of the Work otherwise complies with the conditions stated in this
|
|
124
|
+
License.
|
|
125
|
+
|
|
126
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
127
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
128
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
129
|
+
this License, without any additional terms and conditions.
|
|
130
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
131
|
+
the terms of any separate license agreement you may have executed
|
|
132
|
+
with Licensor regarding such Contributions.
|
|
133
|
+
|
|
134
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
135
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
136
|
+
except as required for reasonable and customary use in describing the
|
|
137
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
138
|
+
|
|
139
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
140
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
141
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
142
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
143
|
+
implied, including, without limitation, any conditions of TITLE,
|
|
144
|
+
NONINFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
145
|
+
PURPOSE. You are solely responsible for determining the
|
|
146
|
+
appropriateness of using or reproducing the Work and assume any
|
|
147
|
+
risks associated with Your exercise of permissions under this License.
|
|
148
|
+
|
|
149
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
150
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
151
|
+
unless required by applicable law (such as deliberate and grossly
|
|
152
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
153
|
+
liable to You for damages, including any direct, indirect, special,
|
|
154
|
+
incidental, or exemplary damages of any character arising as a
|
|
155
|
+
result of this License or out of the use or inability to use the
|
|
156
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
157
|
+
work stoppage, computer failure or malfunction, or all other
|
|
158
|
+
commercial damages or losses), even if such Contributor has been
|
|
159
|
+
advised of the possibility of such damages.
|
|
160
|
+
|
|
161
|
+
9. Accepting Warranty or Liability. While redistributing the Work or
|
|
162
|
+
Derivative Works thereof, You may choose to offer, and charge a fee
|
|
163
|
+
for, acceptance of support, warranty, indemnity, or other liability
|
|
164
|
+
obligations and/or rights consistent with this License. However, in
|
|
165
|
+
accepting such obligations, You may offer such liability only on Your
|
|
166
|
+
own behalf and on behalf of Your sole responsibility to whom You
|
|
167
|
+
accept such liability obligations, not on behalf of any other
|
|
168
|
+
Contributor, and only if You agree to indemnify, defend, and hold
|
|
169
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
170
|
+
asserted against, such Contributor by reason of your accepting any
|
|
171
|
+
such warranty or additional liability.
|
|
172
|
+
|
|
173
|
+
END OF TERMS AND CONDITIONS
|
|
174
|
+
|
|
175
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
176
|
+
|
|
177
|
+
To apply the Apache License to your work, attach the following
|
|
178
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
179
|
+
replaced with your own identifying information. (Don't include
|
|
180
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
181
|
+
comment syntax for the format in question; please specify a
|
|
182
|
+
applicable license, inclusion of the notice in the third-party
|
|
183
|
+
notices file.
|
|
184
|
+
|
|
185
|
+
Copyright [yyyy] [name of copyright owner]
|
|
186
|
+
|
|
187
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
188
|
+
you may not use this file except in compliance with the License.
|
|
189
|
+
You may obtain a copy of the License at
|
|
190
|
+
|
|
191
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
192
|
+
|
|
193
|
+
Unless required by applicable law or agreed to in writing, software
|
|
194
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
195
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
196
|
+
See the License for the specific language governing permissions and
|
|
197
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# rubocop-hash_inspect
|
|
2
|
+
|
|
3
|
+
A RuboCop extension gem that flags Ruby code relying on the **legacy (Ruby <= 3.3)
|
|
4
|
+
`Hash#inspect` output format**. Ruby 3.4 changed `Hash#inspect` from `{:x=>1}` to
|
|
5
|
+
`{x: 1}`, breaking tests that hardcode the old rocket-syntax form. This cop catches
|
|
6
|
+
those literals statically during `pdk validate` so Puppet module authors can fix them
|
|
7
|
+
before upgrading to Puppet 9 (Ruby 4).
|
|
8
|
+
|
|
9
|
+
## Why This Matters — Ruby 3.4 Hash#inspect Change
|
|
10
|
+
|
|
11
|
+
Ruby 3.4 (December 2024) changed the output of `Hash#inspect`:
|
|
12
|
+
|
|
13
|
+
- **Ruby <= 3.3 (Puppet 8):** `{:x=>1, "baz"=>3}`
|
|
14
|
+
- **Ruby >= 3.4 (Puppet 9 / Ruby 4):** `{x: 1, "baz" => 3}`
|
|
15
|
+
|
|
16
|
+
Code that hardcodes the old format in test assertions silently breaks on Puppet 9.
|
|
17
|
+
This cop detects the legacy rocket-syntax format inside string and regexp literals.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
Add to your `Gemfile` (development group):
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem 'rubocop-hash_inspect', '~> 0.2', require: false
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then add to your `.rubocop.yml` (preferred — requires RuboCop >= 1.72):
|
|
28
|
+
|
|
29
|
+
```yaml
|
|
30
|
+
plugins:
|
|
31
|
+
- rubocop-hash_inspect
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or using the legacy `require:` directive (compatible with all RuboCop versions):
|
|
35
|
+
|
|
36
|
+
```yaml
|
|
37
|
+
require:
|
|
38
|
+
- rubocop-hash_inspect
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
Once loaded, the cop `HashInspect/LegacyHashInspectFormat` runs automatically as part
|
|
44
|
+
of `rubocop` (or `pdk validate` when wired via `pdk-templates`).
|
|
45
|
+
|
|
46
|
+
### Cop: `HashInspect/LegacyHashInspectFormat`
|
|
47
|
+
|
|
48
|
+
Flags string, regexp, and interpolated-string literals that contain the legacy
|
|
49
|
+
`Hash#inspect` rocket-syntax format (`{:sym=>val}`), which breaks on Ruby 3.4 / Puppet 9.
|
|
50
|
+
|
|
51
|
+
#### Example — flagged (legacy format)
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
# Legacy format — breaks on Ruby 3.4 / Puppet 9
|
|
55
|
+
expect(result.inspect).to eq("{:a=>1}")
|
|
56
|
+
expect(msg).to match(/\{:name=>"foo"\}/)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
#### Example — correct
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
# New format — correct on Ruby >= 3.4
|
|
63
|
+
expect(result.inspect).to eq("{a: 1}")
|
|
64
|
+
expect(msg).to match(/\{name: "foo"\}/)
|
|
65
|
+
# Or better: use a matcher that doesn't depend on inspect output:
|
|
66
|
+
expect(result).to eq({ a: 1 })
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Offense message
|
|
70
|
+
|
|
71
|
+
> `Legacy \`Hash#inspect\` format (\`{:sym=>...}\`). Ruby 3.4+ renders hashes as \`{sym: ...}\`, so this hardcoded value breaks on Ruby 3.4 / Puppet 9. Update it to the new format.`
|
|
72
|
+
|
|
73
|
+
#### Disable or configure
|
|
74
|
+
|
|
75
|
+
```yaml
|
|
76
|
+
# Disable for a specific file (inline):
|
|
77
|
+
# rubocop:disable HashInspect/LegacyHashInspectFormat
|
|
78
|
+
|
|
79
|
+
# Disable globally in .rubocop.yml:
|
|
80
|
+
HashInspect/LegacyHashInspectFormat:
|
|
81
|
+
Enabled: false
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then run
|
|
87
|
+
`bundle exec rake` to run the spec suite and self-lint together.
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
bin/setup
|
|
91
|
+
bundle exec rake # spec + self-lint (default task)
|
|
92
|
+
bundle exec rake spec # specs only
|
|
93
|
+
bundle exec rake rubocop # self-lint only
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To generate a new cop skeleton:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
bundle exec rake 'new_cop[HashInspect/CopName]'
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Contributing
|
|
103
|
+
|
|
104
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
105
|
+
https://github.com/puppetlabs/rubocop-hash_inspect.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
This gem is available as open source under the terms of the [Apache License 2.0](LICENSE).
|
data/config/default.yml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
HashInspect/LegacyHashInspectFormat:
|
|
2
|
+
Description: >-
|
|
3
|
+
Detects string or regexp literals containing the legacy Hash#inspect output
|
|
4
|
+
format (Ruby <= 3.3 brace-anchored symbol rocket: `{:sym=>val}`). This format
|
|
5
|
+
changed in Ruby 3.4 (Bug #20433). Hardcoded strings using the old format
|
|
6
|
+
break on Ruby 3.4 / Puppet 9. Update to the new format or use a dynamic
|
|
7
|
+
assertion that does not depend on inspect output.
|
|
8
|
+
Enabled: true
|
|
9
|
+
Severity: convention
|
|
10
|
+
Safe: true
|
|
11
|
+
SafeAutoCorrect: false
|
|
12
|
+
VersionAdded: '0.2'
|
|
13
|
+
References:
|
|
14
|
+
- https://bugs.ruby-lang.org/issues/20433
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module HashInspect
|
|
6
|
+
# Detects string, interpolated-string, and regexp literals that hardcode
|
|
7
|
+
# the legacy `Hash#inspect` output format from Ruby <= 3.3. Ruby 3.4
|
|
8
|
+
# changed `Hash#inspect` from `{:x=>1}` to `{x: 1}` (Bug #20433). Code
|
|
9
|
+
# that compares against or embeds the old format breaks silently on
|
|
10
|
+
# Ruby 3.4 / Puppet 9.
|
|
11
|
+
#
|
|
12
|
+
# The detection signal is the brace-anchored, no-space symbol-rocket
|
|
13
|
+
# pattern `{:sym=>` (plain or quoted symbol key immediately followed by
|
|
14
|
+
# `=>` with no surrounding spaces). This is the discriminator between the
|
|
15
|
+
# legacy and Ruby 3.4+ formats.
|
|
16
|
+
#
|
|
17
|
+
# Note: string-keyed legacy form `{"baz"=>3}` is explicitly out of scope
|
|
18
|
+
# for v1 (kept to protect the clean-module baseline — FAM-03, v2).
|
|
19
|
+
# Comments and real Ruby hash literal nodes are not scanned by construction.
|
|
20
|
+
#
|
|
21
|
+
# Known heuristic limitation (WR-02): an interpolated string whose symbol
|
|
22
|
+
# KEY name is dynamic, e.g. `"{:#{key}=>1}"`, is not detected. The key
|
|
23
|
+
# name is unknown at static-analysis time, and the interpolation-gap
|
|
24
|
+
# sentinel (WR-01 fix) also prevents fabricating a signature across the
|
|
25
|
+
# `#{}` boundary. This is an intentional, documented non-detection; it is
|
|
26
|
+
# not an accidental gap.
|
|
27
|
+
#
|
|
28
|
+
# @example
|
|
29
|
+
# # bad - hardcoded legacy Hash#inspect output
|
|
30
|
+
# expect(result).to eq("{:a=>1}")
|
|
31
|
+
# expect(result).to eq("{:\"foo-bar\"=>2}")
|
|
32
|
+
# expect(result).to match(/\{:a=>1\}/)
|
|
33
|
+
# expect(result).to eq("{:a=>#{value}}")
|
|
34
|
+
#
|
|
35
|
+
# # good - use the new format or a dynamic matcher
|
|
36
|
+
# expect(result).to eq("{a: 1}")
|
|
37
|
+
# expect(result).to match(/\{a: 1\}/)
|
|
38
|
+
# expect(result).to include("a:")
|
|
39
|
+
#
|
|
40
|
+
class LegacyHashInspectFormat < Base
|
|
41
|
+
# Offense message (D-12 exact wording).
|
|
42
|
+
MSG = 'Legacy `Hash#inspect` format (`{:sym=>...}`). Ruby 3.4+ renders ' \
|
|
43
|
+
'hashes as `{sym: ...}`, so this hardcoded value breaks on Ruby 3.4 ' \
|
|
44
|
+
'/ Puppet 9. Update it to the new format.'
|
|
45
|
+
|
|
46
|
+
# Detection regex (D-11): brace-anchored, no-space symbol-rocket.
|
|
47
|
+
# Matches `{` followed by any non-`}` content, then a symbol key
|
|
48
|
+
# (plain `\w+` or double-quoted `"[^"]*"`) immediately followed by
|
|
49
|
+
# `=>` with no surrounding spaces. This discriminates against:
|
|
50
|
+
# - Ruby 3.4+ new format (uses `: ` not `=>`)
|
|
51
|
+
# - spaced rockets (`{:a => 1}` — has spaces around `=>`)
|
|
52
|
+
# - string-keyed form (`{"baz"=>3}` — no leading `:`)
|
|
53
|
+
# - bare `:a=>1` without an enclosing brace
|
|
54
|
+
# Uses negated character class and non-overlapping alternation only —
|
|
55
|
+
# no nested quantifiers — ensuring linear-time matching (T-02-01).
|
|
56
|
+
LEGACY_SIGNATURE = /\{[^}]*:(?:\w+|"[^"]*")=>/.freeze
|
|
57
|
+
|
|
58
|
+
# Called on every `str` (plain string literal) node. Reads the node's
|
|
59
|
+
# unescaped String value (never `node.source` — D-10) and fires an
|
|
60
|
+
# offense on the outer node when it matches LEGACY_SIGNATURE (D-07).
|
|
61
|
+
# Skips `str` nodes that are literal segments inside a `dstr` or
|
|
62
|
+
# `regexp` — those are handled by `on_dstr`/`on_regexp` which report
|
|
63
|
+
# on the outer node (D-07). Prevents duplicate offenses.
|
|
64
|
+
def on_str(node)
|
|
65
|
+
return if node.parent&.type?(:dstr, :regexp)
|
|
66
|
+
|
|
67
|
+
value = node.children.first
|
|
68
|
+
add_offense(node) if value.is_a?(String) && LEGACY_SIGNATURE.match?(value)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Called on every `dstr` (interpolated string) node. Maps over ALL
|
|
72
|
+
# children: literal `str_type?` segments contribute their text value;
|
|
73
|
+
# each interpolation (`begin`/`send`/etc.) node is replaced by the
|
|
74
|
+
# sentinel `' } '` (space + closing-brace + space). This prevents the
|
|
75
|
+
# regex from matching a signature fabricated across a `#{}` boundary
|
|
76
|
+
# (WR-01 fix): the `}` in the sentinel terminates `[^}]*`, and the
|
|
77
|
+
# spaces break any `:sym=>`/`\w+` run.
|
|
78
|
+
# E.g. `"{:a=>#{v}}"` produces `"{:a=> } "` which still matches because
|
|
79
|
+
# the full `{:a=>` is in the static segment before the sentinel.
|
|
80
|
+
# E.g. `"{#{prefix}:role=>admin}"` produces `"{ } :role=>admin}"` which
|
|
81
|
+
# does NOT match because `{` and `:role=>` are separated by the sentinel.
|
|
82
|
+
def on_dstr(node)
|
|
83
|
+
static_text = static_text_with_sentinels(node)
|
|
84
|
+
add_offense(node) if LEGACY_SIGNATURE.match?(static_text)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Called on every `regexp` node. Applies the same sentinel-aware
|
|
88
|
+
# concatenation as `on_dstr` via `static_text_with_sentinels`, then
|
|
89
|
+
# normalizes escaped braces (`\{`->`{`, `\}`->`}`) so that
|
|
90
|
+
# `/\{:a=>1\}/` matches the same brace-anchored signature as the string
|
|
91
|
+
# form (D-06, D-07). The gsub unescape step runs on the joined text
|
|
92
|
+
# after sentinel insertion, exactly preserving WR-03's load-bearing
|
|
93
|
+
# behaviour for `%r{\{...\}}` patterns.
|
|
94
|
+
def on_regexp(node)
|
|
95
|
+
static_text = static_text_with_sentinels(node)
|
|
96
|
+
unescaped = static_text.gsub('\\{', '{').gsub('\\}', '}')
|
|
97
|
+
add_offense(node) if LEGACY_SIGNATURE.match?(unescaped)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
# Returns a string formed by joining each child of `node`: literal
|
|
103
|
+
# `str_type?` children contribute their text; all other children
|
|
104
|
+
# (interpolations) contribute the sentinel `' } '`. The `}` in the
|
|
105
|
+
# sentinel terminates the regex's `[^}]*` run so a LEGACY_SIGNATURE
|
|
106
|
+
# cannot be fabricated across an interpolation boundary (WR-01).
|
|
107
|
+
def static_text_with_sentinels(node)
|
|
108
|
+
node.children.map do |child|
|
|
109
|
+
child.str_type? ? child.children.first : ' } '
|
|
110
|
+
end.join
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'lint_roller'
|
|
4
|
+
require_relative 'version'
|
|
5
|
+
|
|
6
|
+
module RuboCop
|
|
7
|
+
module HashInspect
|
|
8
|
+
# A plugin that integrates rubocop-hash_inspect with RuboCop's plugin system.
|
|
9
|
+
class Plugin < LintRoller::Plugin
|
|
10
|
+
def about
|
|
11
|
+
LintRoller::About.new(
|
|
12
|
+
name: 'rubocop-hash_inspect',
|
|
13
|
+
version: VERSION,
|
|
14
|
+
homepage: 'https://github.com/puppetlabs/rubocop-hash_inspect',
|
|
15
|
+
description: 'A RuboCop extension that flags reliance on legacy Hash#inspect output format.'
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def supported?(context)
|
|
20
|
+
context.engine == :rubocop
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def rules(_context)
|
|
24
|
+
LintRoller::Rules.new(
|
|
25
|
+
type: :path,
|
|
26
|
+
config_format: :rubocop,
|
|
27
|
+
value: Pathname.new(__dir__).join('../../../config/default.yml')
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rubocop-hash_inspect
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Puppet
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-06-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: lint_roller
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rubocop
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 1.72.2
|
|
34
|
+
- - "<"
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '2.0'
|
|
37
|
+
type: :runtime
|
|
38
|
+
prerelease: false
|
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 1.72.2
|
|
44
|
+
- - "<"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rubocop-ast
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 1.45.1
|
|
54
|
+
- - "<"
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '2.0'
|
|
57
|
+
type: :runtime
|
|
58
|
+
prerelease: false
|
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: 1.45.1
|
|
64
|
+
- - "<"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '2.0'
|
|
67
|
+
description: |
|
|
68
|
+
A RuboCop cop that statically detects Ruby code relying on the pre-Ruby 3.4
|
|
69
|
+
Hash#inspect output format (e.g. {:sym=>1}). Ruby 3.4 changed Hash#inspect to
|
|
70
|
+
produce {sym: 1, "str" => 2}, breaking tests that hardcode the old format.
|
|
71
|
+
Run as part of `pdk validate` to catch incompatibilities before upgrading to
|
|
72
|
+
Puppet 9 (Ruby 4).
|
|
73
|
+
email:
|
|
74
|
+
- puppet@puppet.com
|
|
75
|
+
executables: []
|
|
76
|
+
extensions: []
|
|
77
|
+
extra_rdoc_files: []
|
|
78
|
+
files:
|
|
79
|
+
- CHANGELOG.md
|
|
80
|
+
- LICENSE
|
|
81
|
+
- README.md
|
|
82
|
+
- config/default.yml
|
|
83
|
+
- lib/rubocop-hash_inspect.rb
|
|
84
|
+
- lib/rubocop/cop/hash_inspect/legacy_hash_inspect_format.rb
|
|
85
|
+
- lib/rubocop/cop/hash_inspect_cops.rb
|
|
86
|
+
- lib/rubocop/hash_inspect.rb
|
|
87
|
+
- lib/rubocop/hash_inspect/plugin.rb
|
|
88
|
+
- lib/rubocop/hash_inspect/version.rb
|
|
89
|
+
homepage: https://github.com/puppetlabs/rubocop-hash_inspect
|
|
90
|
+
licenses:
|
|
91
|
+
- Apache-2.0
|
|
92
|
+
metadata:
|
|
93
|
+
homepage_uri: https://github.com/puppetlabs/rubocop-hash_inspect
|
|
94
|
+
source_code_uri: https://github.com/puppetlabs/rubocop-hash_inspect
|
|
95
|
+
changelog_uri: https://github.com/puppetlabs/rubocop-hash_inspect/blob/main/CHANGELOG.md
|
|
96
|
+
bug_tracker_uri: https://github.com/puppetlabs/rubocop-hash_inspect/issues
|
|
97
|
+
rubygems_mfa_required: 'true'
|
|
98
|
+
default_lint_roller_plugin: RuboCop::HashInspect::Plugin
|
|
99
|
+
post_install_message:
|
|
100
|
+
rdoc_options: []
|
|
101
|
+
require_paths:
|
|
102
|
+
- lib
|
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: 2.7.0
|
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: '0'
|
|
113
|
+
requirements: []
|
|
114
|
+
rubygems_version: 3.4.19
|
|
115
|
+
signing_key:
|
|
116
|
+
specification_version: 4
|
|
117
|
+
summary: RuboCop extension that flags reliance on legacy Hash#inspect output format.
|
|
118
|
+
test_files: []
|