Skip to content

Commit

Permalink
Add VersionAdded to config/default.yml when running rake new_cop
Browse files Browse the repository at this point in the history
#6293 extended cop metadata.

This PR adds `VersionAdded` to config/default.yml when running `rake new_cop`.

The following is an example.

```console
% rubocop -V
0.59.2 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin17)
% bunle exec rake new_cop[Lint/TheNewCop]
```

In the current RuboCop, a new cop is added with a minor version.

```diff
diff --git a/config/default.yml b/config/default.yml
index e784ea6b7..522f6aa4e 100644
--- a/config/default.yml
+++ b/config/default.yml
@@ -1507,6 +1507,11 @@ Lint/Syntax:
   VersionAdded: 0.9

+Lint/TheNewCop:
+  Description: 'TODO: Write a description of the cop.'
+  Enabled: true
+  VersionAdded: 0.60
+
 Lint/UnderscorePrefixedVariableName:
   Description: 'Do not use prefix `_` for a variable that is used.'
   Enabled: true
```
  • Loading branch information
koic authored and bbatsov committed Oct 15, 2018
1 parent e5ebd56 commit c190f22
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#5980](https://github.com/rubocop-hq/rubocop/issues/5980): Add --safe and --safe-auto-correct options. ([@Darhazer][])
* [#4156](https://github.com/rubocop-hq/rubocop/issues/4156): Add command line option `--auto-gen-only-exclude`. ([@Ana06][], [@jonas054][])
* [#6386](https://github.com/rubocop-hq/rubocop/pull/6386): Add `VersionAdded` meta data to config/default.yml when running `rake new_cop`. ([@koic][])

### Bug fixes

Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/cop/generator.rb
Expand Up @@ -128,6 +128,7 @@ def inject_config(config_file_path: 'config/default.yml')
#{badge}:
Description: 'TODO: Write a description of the cop.'
Enabled: true
VersionAdded: #{bump_minor_version}
YAML
target_line = config.find.with_index(1) do |line, index|
Expand Down Expand Up @@ -209,6 +210,12 @@ def snake_case(camel_case_string)
.gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2')
.downcase
end

def bump_minor_version
versions = RuboCop::Version::STRING.split('.')

"#{versions[0]}.#{versions[1].succ}"
end
end
end
end
3 changes: 3 additions & 0 deletions spec/rubocop/cop/generator_spec.rb
Expand Up @@ -193,13 +193,16 @@ def on_send(node)
end

it 'inserts the cop in alphabetical' do
stub_const('RuboCop::Version::STRING', '0.58.2')

expect(File).to receive(:write).with(path, <<-YAML.strip_indent)
Style/Alias:
Enabled: true
Style/FakeCop:
Description: 'TODO: Write a description of the cop.'
Enabled: true
VersionAdded: 0.59
Style/Lambda:
Enabled: true
Expand Down

0 comments on commit c190f22

Please sign in to comment.