rubocop-md 1.2.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 481459568a982c49d109a7514bc891f22acf485a22954372eae24fb94e7543bc
4
- data.tar.gz: 9af60abc5f0fc512eeeba617cf52b12ce471db0ff0b39e452f2e5ac246dad51a
3
+ metadata.gz: 2e1a8d57e542393892dbdf2f9ffd7229d82e1996e900dd33edec55c0b94371ef
4
+ data.tar.gz: 5503ee8519df1ead15a2ae9602ffc4e960a6d6ddd2d36f32e0dea39c6ac87eef
5
5
  SHA512:
6
- metadata.gz: 8f7b69a5a9bb3aec08882635b3746ed6aececc5c485b3d228564c66bc41fbb06935b1574b69027fb18c7fdd1c6515a17e8f7f1a99e83b8b9ad2b1daa4de06bd2
7
- data.tar.gz: 2917282ce852086f06facda93acdd037a5782f217376b709c00ab05a0805bf72c4e7ac67520e2a99ed60a17dbd7339eb74b57e9564fad65d96956a5326724574
6
+ metadata.gz: 3e0c6ff91ac90e928919e12774f4b1013e67b2957e1b3dfc958ef177f1c74ec3f2eb494ffe58c1a98946a69bc1eace0f79dd5664055f448388fedd02cbe7e824
7
+ data.tar.gz: d24bc990a119f44ddb136047bcf3423934d8252ee340cdc09b9500cf42da09aa352f0a58e8f22d249f9449cad2fb71681f5689c65af760697b493222899e7814
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 1.2.2 (2023-12-01) ❄️
6
+
7
+ - Fix analyzing code blocks with backticks.
8
+
9
+ ## 1.2.1 (2023-10-20)
10
+
11
+ - Fix incompatibility when loading the plugin from YAML and using other RuboCop options.
12
+
5
13
  ## 1.2.0 (2023-01-31)
6
14
 
7
15
  - Fix parsing compound syntax hints in code snippets.
data/README.md CHANGED
@@ -12,7 +12,12 @@ Run Rubocop against your Markdown files to make sure that code examples follow s
12
12
  - Preserves specified language (i.e., do not try to analyze "\`\`\`sh")
13
13
  - **Supports autocorrect 📝**
14
14
 
15
- This project was developed to keep [test-prof](https://github.com/test-prof/test-prof) guides consistent with Ruby style guide.
15
+ This project was developed to keep [test-prof](https://github.com/test-prof/test-prof) guides consistent with Ruby style guide back in 2017. Since then, many popular Ruby projects adopted it, including:
16
+
17
+ - [Ruby on Rails](https://github.com/rails/rails)
18
+ - [AnyCable](https://github.com/anycable/anycable)
19
+ - [ViewComponent](https://github.com/ViewComponent/view_component)
20
+ - [Action Policy](https://github.com/palkan/action_policy)
16
21
 
17
22
  ## Installation
18
23
 
@@ -111,7 +116,7 @@ Lint/Void:
111
116
 
112
117
  You can use this tricks
113
118
 
114
- ```md
119
+ ````md
115
120
  # my_post.md
116
121
 
117
122
  ... some markdown ...
@@ -129,7 +134,7 @@ end of snippet
129
134
  <span style="display:none;"># rubocop:enable all</span>
130
135
 
131
136
  ... continuation of article ...
132
- ```
137
+ ````
133
138
 
134
139
  ## How it works?
135
140
 
@@ -7,12 +7,18 @@ module RuboCop
7
7
  # Transform source Markdown file into valid Ruby file
8
8
  # by commenting out all non-code lines
9
9
  class Preprocess
10
- # This is a regexp to extract code blocks from .md files.
10
+ # This is a regexp to parse code blocks from .md files.
11
11
  #
12
12
  # Only recognizes backticks-style code blocks.
13
13
  #
14
14
  # Try it: https://rubular.com/r/YMqSWiBuh2TKIJ
15
- MD_REGEXP = /^([ \t]*`{3,4})([\w[[:blank:]]+]*\n)([\s\S]+?)(^[ \t]*\1[[:blank:]]*\n?)/m.freeze
15
+ MD_REGEXP = /
16
+ ^([[:blank:]]*`{3,4}) # Match opening backticks
17
+ ([\w[[:blank:]]+]*)?\n # Match the code block syntax
18
+ ([\s\S]+?) # Match everything inside the code block
19
+ (^[[:blank:]]*\1[[:blank:]]*\n?) # Match closing backticks
20
+ |(^.*$) # If we are not in a codeblock, match the whole line
21
+ /x.freeze
16
22
 
17
23
  MARKER = "<--rubocop/md-->"
18
24
 
@@ -26,26 +32,6 @@ module RuboCop
26
32
  rbx
27
33
  ].freeze
28
34
 
29
- class Walker # :nodoc:
30
- STEPS = %i[text code_start code_attr code_body code_end].freeze
31
-
32
- STEPS.each do |step|
33
- define_method("#{step}?") do
34
- STEPS[current_step] == step
35
- end
36
- end
37
-
38
- attr_accessor :current_step
39
-
40
- def initialize
41
- @current_step = 0
42
- end
43
-
44
- def next!
45
- self.current_step = current_step == (STEPS.size - 1) ? 0 : current_step + 1
46
- end
47
- end
48
-
49
35
  class << self
50
36
  # Revert preprocess changes.
51
37
  #
@@ -68,31 +54,34 @@ module RuboCop
68
54
 
69
55
  # rubocop:disable Metrics/MethodLength
70
56
  def call(src)
71
- parts = src.split(MD_REGEXP)
72
-
73
- walker = Walker.new
74
-
75
- parts.each do |part|
76
- if walker.code_body? && maybe_ruby?(@syntax) && valid_syntax?(@syntax, part)
77
- next walker.next!
57
+ src.gsub(MD_REGEXP) do |full_match|
58
+ m = Regexp.last_match
59
+ open_backticks = m[1]
60
+ syntax = m[2]
61
+ code = m[3]
62
+ close_backticks = m[4]
63
+ markdown = m[5]
64
+
65
+ if markdown
66
+ # We got markdown outside of a codeblock
67
+ comment_lines(markdown)
68
+ elsif ruby_codeblock?(syntax, code)
69
+ # The codeblock we parsed is assumed ruby, keep as is and append markers to backticks
70
+ "#{comment_lines(open_backticks + syntax)}\n#{code}#{comment_lines(close_backticks)}"
71
+ else
72
+ # The codeblock is not relevant, comment it out
73
+ comment_lines(full_match)
78
74
  end
79
-
80
- if walker.code_attr?
81
- @syntax = part.gsub(/(^\s+|\s+$)/, "")
82
- next walker.next!
83
- end
84
-
85
- comment_lines! part
86
-
87
- walker.next!
88
75
  end
89
-
90
- parts.join
91
76
  end
92
77
  # rubocop:enable Metrics/MethodLength
93
78
 
94
79
  private
95
80
 
81
+ def ruby_codeblock?(syntax, src)
82
+ maybe_ruby?(syntax) && valid_syntax?(syntax, src)
83
+ end
84
+
96
85
  # Check codeblock attribute to prevent from parsing
97
86
  # non-Ruby snippets and avoid false positives
98
87
  def maybe_ruby?(syntax)
@@ -124,10 +113,8 @@ module RuboCop
124
113
  config["Markdown"]&.fetch("Autodetect", true)
125
114
  end
126
115
 
127
- def comment_lines!(src)
128
- return if src =~ /\A\n\z/
129
-
130
- src.gsub!(/^(.)/m, "##{MARKER}\\1")
116
+ def comment_lines(src)
117
+ src.gsub(/^/, "##{MARKER}")
131
118
  end
132
119
  end
133
120
  end
@@ -17,6 +17,9 @@ module RuboCop
17
17
  .workbook
18
18
  ].freeze
19
19
 
20
+ # A list of cops that could produce offenses in commented lines
21
+ MARKDOWN_OFFENSE_COPS = %w[Lint/Syntax].freeze
22
+
20
23
  class << self
21
24
  attr_accessor :config_store
22
25
 
@@ -42,9 +45,10 @@ RuboCop::Markdown.inject!
42
45
 
43
46
  RuboCop::Runner.prepend(Module.new do
44
47
  # Set config store for Markdown
45
- def initialize(*args)
48
+ def get_processed_source(*args)
49
+ RuboCop::Markdown.config_store = @config_store unless RuboCop::Markdown.config_store
50
+
46
51
  super
47
- RuboCop::Markdown.config_store = @config_store
48
52
  end
49
53
 
50
54
  # Do not cache markdown files, 'cause cache doesn't know about processing.
@@ -60,6 +64,8 @@ RuboCop::Runner.prepend(Module.new do
60
64
  # Skip offenses reported for ignored MD source (trailing whitespaces, etc.)
61
65
  marker_comment = "##{RuboCop::Markdown::Preprocess::MARKER}"
62
66
  offenses.reject! do |offense|
67
+ next if RuboCop::Markdown::MARKDOWN_OFFENSE_COPS.include?(offense.cop_name)
68
+
63
69
  offense.location.source_line.start_with?(marker_comment)
64
70
  end
65
71
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Markdown
5
- VERSION = "1.2.0"
5
+ VERSION = "1.2.2"
6
6
  end
7
7
  end
@@ -9,7 +9,7 @@ module RuboCop
9
9
  PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
10
10
  CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
11
11
 
12
- require_relative "./markdown/preprocess"
13
- require_relative "./markdown/rubocop_ext" if defined?(::RuboCop::ProcessedSource)
12
+ require_relative "markdown/preprocess"
13
+ require_relative "markdown/rubocop_ext" if defined?(::RuboCop::ProcessedSource)
14
14
  end
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-md
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-31 00:00:00.000000000 Z
11
+ date: 2023-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -74,28 +74,16 @@ executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - ".gem_release.yml"
78
- - ".github/dependabot.yml"
79
- - ".github/workflows/lint.yml"
80
- - ".github/workflows/test-jruby.yml"
81
- - ".github/workflows/test.yml"
82
- - ".gitignore"
83
- - ".rubocop.yml"
84
- - ".travis.yml"
85
77
  - CHANGELOG.md
86
- - Gemfile
87
78
  - LICENSE.txt
88
79
  - README.md
89
- - Rakefile
90
80
  - config/default.yml
91
- - gemfiles/rubocopmaster.gemfile
92
81
  - lib/rubocop-md.rb
93
82
  - lib/rubocop/markdown.rb
94
83
  - lib/rubocop/markdown/preprocess.rb
95
84
  - lib/rubocop/markdown/rubocop_ext.rb
96
85
  - lib/rubocop/markdown/version.rb
97
- - rubocop-md.gemspec
98
- homepage: https://github.com/palkan/rubocop-md
86
+ homepage: https://github.com/rubocop-hq/rubocop-md
99
87
  licenses:
100
88
  - MIT
101
89
  metadata:
@@ -119,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
107
  - !ruby/object:Gem::Version
120
108
  version: '0'
121
109
  requirements: []
122
- rubygems_version: 3.3.11
110
+ rubygems_version: 3.4.20
123
111
  signing_key:
124
112
  specification_version: 4
125
113
  summary: Run Rubocop against your Markdown files to make sure that code examples follow
data/.gem_release.yml DELETED
@@ -1,3 +0,0 @@
1
- bump:
2
- file: lib/rubocop/markdown/version.rb
3
- skip_ci: true
@@ -1,6 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: 'github-actions'
4
- directory: '/'
5
- schedule:
6
- interval: 'weekly'
@@ -1,41 +0,0 @@
1
- name: Lint
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- pull_request:
8
-
9
- jobs:
10
- rubocop:
11
- runs-on: ubuntu-latest
12
- env:
13
- BUNDLE_JOBS: 4
14
- BUNDLE_RETRY: 3
15
- CI: true
16
- steps:
17
- - uses: actions/checkout@v3
18
- - uses: ruby/setup-ruby@v1
19
- with:
20
- ruby-version: 2.7
21
- bundler-cache: true
22
- - name: Run Rubocop
23
- run: |
24
- bundle exec rubocop
25
- rubocop-md:
26
- runs-on: ubuntu-latest
27
- env:
28
- BUNDLE_JOBS: 4
29
- BUNDLE_RETRY: 3
30
- CI: true
31
- steps:
32
- - uses: actions/checkout@v3
33
- - uses: ruby/setup-ruby@v1
34
- with:
35
- ruby-version: 3.1
36
- bundler-cache: true
37
- - name: Run Rubocop
38
- run: |
39
- bundle exec rubocop -r./lib/rubocop-md.rb README.md CHANGELOG.md
40
- git status
41
- git diff --exit-code
@@ -1,24 +0,0 @@
1
- name: JRuby Test
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- pull_request:
8
-
9
- jobs:
10
- test:
11
- runs-on: ubuntu-latest
12
- env:
13
- BUNDLE_JOBS: 4
14
- BUNDLE_RETRY: 3
15
- CI: true
16
- steps:
17
- - uses: actions/checkout@v3
18
- - uses: ruby/setup-ruby@v1
19
- with:
20
- ruby-version: jruby
21
- bundler-cache: true
22
- - name: Run tests
23
- run: |
24
- bundle exec rake test
@@ -1,38 +0,0 @@
1
- name: Test
2
-
3
- on:
4
- push:
5
- branches:
6
- - master
7
- pull_request:
8
- schedule:
9
- - cron: "10 4 * * */2"
10
-
11
- jobs:
12
- test:
13
- runs-on: ubuntu-latest
14
- env:
15
- BUNDLE_JOBS: 4
16
- BUNDLE_RETRY: 3
17
- CI: true
18
- strategy:
19
- fail-fast: false
20
- matrix:
21
- ruby: ["2.7", "3.0", "3.1", "3.2", ruby-head]
22
- gemfile: [
23
- "Gemfile"
24
- ]
25
- include:
26
- - ruby: "2.6"
27
- gemfile: "Gemfile"
28
- - ruby: "3.1"
29
- gemfile: "gemfiles/rubocopmaster.gemfile"
30
- steps:
31
- - uses: actions/checkout@v3
32
- - uses: ruby/setup-ruby@v1
33
- with:
34
- ruby-version: ${{ matrix.ruby }}
35
- bundler-cache: true
36
- - name: Run tests
37
- run: |
38
- bundle exec rake test
data/.gitignore DELETED
@@ -1,10 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /Gemfile.local
5
- /_yardoc/
6
- /coverage/
7
- /doc/
8
- /pkg/
9
- /spec/reports/
10
- /tmp/
data/.rubocop.yml DELETED
@@ -1,63 +0,0 @@
1
- AllCops:
2
- NewCops: enable
3
- Include:
4
- - 'lib/**/*.rb'
5
- - 'lib/**/*.rake'
6
- - 'test/**/*.rb'
7
- Exclude:
8
- - 'bin/**/*'
9
- - 'vendor/**/*'
10
- - 'tmp/**/*'
11
- - 'Rakefile'
12
- - 'Gemfile'
13
- - '*.gemspec'
14
- DisplayCopNames: true
15
- StyleGuideCopsOnly: false
16
- TargetRubyVersion: 2.5
17
- SuggestExtensions: false
18
-
19
- Style/Documentation:
20
- Exclude:
21
- - 'test/**/*.rb'
22
-
23
- Style/StringLiterals:
24
- EnforcedStyle: double_quotes
25
-
26
- Style/RegexpLiteral:
27
- Enabled: false
28
-
29
- Style/ClassAndModuleChildren:
30
- Exclude:
31
- - 'test/**/*.rb'
32
-
33
- Style/HashEachMethods:
34
- Enabled: true
35
-
36
- Style/HashTransformKeys:
37
- Enabled: true
38
-
39
- Style/HashTransformValues:
40
- Enabled: true
41
-
42
- Layout/EmptyLinesAroundArguments:
43
- Enabled: false
44
-
45
- Layout/SpaceInsideStringInterpolation:
46
- EnforcedStyle: no_space
47
-
48
- Naming/FileName:
49
- Exclude:
50
- - 'lib/rubocop-md.rb'
51
-
52
- Layout/LineLength:
53
- Max: 100
54
- Exclude:
55
- - 'test/**/*.rb'
56
-
57
- Metrics/MethodLength:
58
- Exclude:
59
- - 'test/**/*.rb'
60
-
61
- Metrics/ClassLength:
62
- Exclude:
63
- - 'test/**/*.rb'
data/.travis.yml DELETED
@@ -1,33 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- before_install:
4
- - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
5
- - gem install bundler -v '< 2'
6
-
7
- notifications:
8
- email: false
9
-
10
- matrix:
11
- fast_finish: true
12
- include:
13
- - rvm: ruby-head
14
- gemfile: gemfiles/rubocopmaster.gemfile
15
- - rvm: jruby-9.2.8.0
16
- gemfile: Gemfile
17
- - rvm: 2.7
18
- gemfile: gemfiles/rubocopmaster.gemfile
19
- - rvm: 2.7
20
- gemfile: Gemfile
21
- - rvm: 2.6
22
- gemfile: Gemfile
23
- - rvm: 2.5
24
- gemfile: Gemfile
25
- - rvm: 2.4.2
26
- gemfile: Gemfile
27
- allow_failures:
28
- - rvm: ruby-head
29
- gemfile: gemfiles/rubocopmaster.gemfile
30
- - rvm: jruby-9.2.8.0
31
- gemfile: Gemfile
32
- - rvm: 2.5.0
33
- gemfile: gemfiles/rubocopmaster.gemfile
data/Gemfile DELETED
@@ -1,12 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in rubocop-md.gemspec
6
- gemspec
7
-
8
- local_gemfile = "Gemfile.local"
9
-
10
- if File.exist?(local_gemfile)
11
- eval(File.read(local_gemfile)) # rubocop:disable Security/Eval
12
- end
data/Rakefile DELETED
@@ -1,14 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
- require "rubocop/rake_task"
4
-
5
- Rake::TestTask.new(:test) do |t|
6
- t.libs << "test"
7
- t.libs << "lib"
8
- t.warning = false
9
- t.test_files = FileList["test/**/*_test.rb"]
10
- end
11
-
12
- RuboCop::RakeTask.new
13
-
14
- task default: [:rubocop, :test]
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "rubocop", github: "bbatsov/rubocop"
4
-
5
- gemspec path: ".."
data/rubocop-md.gemspec DELETED
@@ -1,37 +0,0 @@
1
- lib = File.expand_path("../lib", __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "rubocop/markdown/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "rubocop-md"
7
- spec.version = RuboCop::Markdown::VERSION
8
- spec.authors = ["Vladimir Dementyev"]
9
- spec.email = ["dementiev.vm@gmail.com"]
10
-
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.}
13
- spec.homepage = "https://github.com/palkan/rubocop-md"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
- f.match(%r{^(test|spec|features)/})
18
- end
19
-
20
- spec.metadata = {
21
- "bug_tracker_uri" => "http://github.com/rubocop-hq/rubocop-md/issues",
22
- "changelog_uri" => "https://github.com/rubocop-hq/rubocop-md/blob/master/CHANGELOG.md",
23
- "documentation_uri" => "https://github.com/rubocop-hq/rubocop-md/blob/master/README.md",
24
- "homepage_uri" => "https://github.com/rubocop-hq/rubocop-md",
25
- "source_code_uri" => "http://github.com/rubocop-hq/rubocop-md"
26
- }
27
-
28
- spec.required_ruby_version = ">= 2.6.0"
29
-
30
- spec.require_paths = ["lib"]
31
-
32
- spec.add_runtime_dependency "rubocop", ">= 1.0"
33
-
34
- spec.add_development_dependency "bundler", ">= 1.15"
35
- spec.add_development_dependency "rake", ">= 13.0"
36
- spec.add_development_dependency "minitest", "~> 5.0"
37
- end