Skip to content

Commit ba77b7b

Browse files
authored
Merge pull request #41 from koic/pluginfy_with_lint_roller
Pluginfy rubocop-md
2 parents cea6619 + 7811d48 commit ba77b7b

File tree

10 files changed

+58
-31
lines changed

10 files changed

+58
-31
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
ruby: ["2.6", "2.7", "3.0", "3.1", "3.2", "3.3", ruby-head, jruby]
20+
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4", ruby-head, jruby]
2121
rubocop_version: [""]
2222
include:
23-
- ruby: "2.6"
24-
rubocop_version: "'1.45.0'"
25-
- ruby: "3.3"
23+
- ruby: "2.7"
24+
rubocop_version: "'1.72.1'"
25+
- ruby: "3.4"
2626
rubocop_version: "github: 'rubocop/rubocop'"
2727
steps:
2828
- uses: actions/checkout@v4

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ Or install it yourself as:
3939

4040
### Command line
4141

42-
Just require `rubocop-md` in your command:
42+
Just require `rubocop-md` plugin in your command:
4343

4444
```sh
45-
rubocop -r "rubocop-md" ./lib
45+
rubocop --plugin "rubocop-md" ./lib
4646
```
4747

4848
Autocorrect works too:
4949

5050
```sh
51-
rubocop -r "rubocop-md" -a ./lib
51+
rubocop --plugin "rubocop-md" -a ./lib
5252
```
5353

5454
### Configuration
@@ -64,7 +64,7 @@ At first, add `rubocop-md` to your main `.rubocop.yml`:
6464
```yml
6565
# .rubocop.yml
6666

67-
require:
67+
plugins:
6868
- "rubocop-md"
6969
```
7070

lib/rubocop/markdown.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ module Markdown
1111

1212
require_relative "markdown/preprocess"
1313
require_relative "markdown/rubocop_ext" if defined?(::RuboCop::ProcessedSource)
14+
require_relative "markdown/plugin"
1415
end
1516
end

lib/rubocop/markdown/plugin.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "lint_roller"
4+
5+
module RuboCop
6+
module Markdown
7+
# A plugin that integrates rubocop-md with RuboCop's plugin system.
8+
class Plugin < LintRoller::Plugin
9+
def about
10+
LintRoller::About.new(
11+
name: "rubocop-md",
12+
version: VERSION,
13+
homepage: "https://github.com/rubocop/rubocop-md",
14+
description: "Run RuboCop against your Markdown files to make sure " \
15+
"that code examples follow style guidelines."
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

lib/rubocop/markdown/rubocop_ext.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,13 @@ module Markdown # :nodoc:
2424
class << self
2525
attr_accessor :config_store
2626

27-
# Merge markdown config into default configuration
28-
# See https://github.com/backus/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
29-
def inject!
30-
path = CONFIG_DEFAULT.to_s
31-
hash = ConfigLoader.send(:load_yaml_configuration, path)
32-
config = Config.new(hash, path)
33-
puts "configuration from #{path}" if ConfigLoader.debug?
34-
config = ConfigLoader.merge_with_default(config, path)
35-
ConfigLoader.instance_variable_set(:@default_configuration, config)
36-
end
37-
3827
def markdown_file?(file)
3928
MARKDOWN_EXTENSIONS.include?(File.extname(file))
4029
end
4130
end
4231
end
4332
end
4433

45-
RuboCop::Markdown.inject!
46-
4734
RuboCop::Runner.prepend(Module.new do
4835
# Set config store for Markdown
4936
def get_processed_source(*args)

rubocop-md.gemspec

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
88
spec.authors = ["Vladimir Dementyev"]
99
spec.email = ["[email protected]"]
1010

11-
spec.summary = %q{Run Rubocop against your Markdown files to make sure that code examples follow style guidelines.}
12-
spec.description = %q{Run Rubocop against your Markdown files to make sure that code examples follow style guidelines.}
11+
spec.summary = %q{Run RuboCop against your Markdown files to make sure that code examples follow style guidelines.}
12+
spec.description = %q{Run RuboCop against your Markdown files to make sure that code examples follow style guidelines.}
1313
spec.homepage = "https://github.com/rubocop/rubocop-md"
1414
spec.license = "MIT"
1515

@@ -23,11 +23,14 @@ Gem::Specification.new do |spec|
2323
"source_code_uri" => "http://github.com/rubocop/rubocop-md"
2424
}
2525

26-
spec.required_ruby_version = ">= 2.6.0"
26+
spec.metadata['default_lint_roller_plugin'] = 'RuboCop::Markdown::Plugin'
27+
28+
spec.required_ruby_version = ">= 2.7.0"
2729

2830
spec.require_paths = ["lib"]
2931

30-
spec.add_runtime_dependency "rubocop", ">= 1.45"
32+
spec.add_runtime_dependency 'lint_roller', '~> 1.1'
33+
spec.add_runtime_dependency "rubocop", ">= 1.72.1"
3134

3235
spec.add_development_dependency "bundler", ">= 1.15"
3336
spec.add_development_dependency "rake", ">= 13.0"

test/fixtures/.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
inherit_from:
22
- "../../.rubocop.yml"
33

4-
require:
4+
plugins:
55
- "rubocop-md"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
inherit_from: "../../../.rubocop.yml"
22

3-
require:
3+
plugins:
44
- "rubocop-md"

test/integration_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
module RuboCopRunner
88
def run_rubocop(path, options: "", config: nil)
9-
md_path = File.expand_path("../lib/rubocop-md.rb", __dir__)
109
md_config_path = File.expand_path("./fixtures/.rubocop.yml", __dir__)
1110

12-
options = "#{options} -r #{md_path}" if ENV["MD_LOAD_MODE"] == "options"
11+
options = "#{options} --plugin rubocop-md" if ENV["MD_LOAD_MODE"] == "options"
1312

1413
if ENV["MD_LOAD_MODE"] == "config"
1514
# Add "_with_require" suffix

test/test_helper.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
44

55
if ENV["MD_LOAD_MODE"] == "options"
6-
$stdout.puts "⚙️ Run rubocop with '-r rubocop-md' options"
6+
$stdout.puts "⚙️ Run rubocop with '--plugin rubocop-md' options"
77
elsif ENV["MD_LOAD_MODE"] == "config"
8-
$stdout.puts "⚙️ Run rubocop with 'require: - rubocop-md' in .rubocop.yml"
8+
$stdout.puts "⚙️ Run rubocop with 'plugins: - rubocop-md' in .rubocop.yml"
99
end
1010

1111
require "minitest/autorun"
@@ -15,4 +15,9 @@
1515
require "markdown_assertions"
1616
require "rubocop-md"
1717

18+
# NOTE: Since a custom testing framework is used, the following abstraction
19+
# for plugin callbacks during testing is not implemented yet.
20+
# https://github.com/rubocop/rubocop/pull/13840
21+
RuboCop::Plugin.integrate_plugins(RuboCop::Config.new, ["rubocop-md"])
22+
1823
RuboCop::Markdown.config_store = RuboCop::ConfigStore.new

0 commit comments

Comments
 (0)