rubocop-md 1.2.1 → 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: 8e4b62aa5ac22f5dd82b132acd7439582c1d7e4a925d8a44cfeb48f250d579aa
4
- data.tar.gz: 5acca30ab4a740b8bb9fdd25bd1b769f96504a63c8f6e4d3460ddfe61c66dbcf
3
+ metadata.gz: 2e1a8d57e542393892dbdf2f9ffd7229d82e1996e900dd33edec55c0b94371ef
4
+ data.tar.gz: 5503ee8519df1ead15a2ae9602ffc4e960a6d6ddd2d36f32e0dea39c6ac87eef
5
5
  SHA512:
6
- metadata.gz: eb68e5947593be1fce3b2c7131d7a445615b3a9a47c86db9e5b5e31ba966e0c4f7030c20578e140cfe1f55b824e1224a6a2ef3206317db51fa7bd89b60d93e52
7
- data.tar.gz: '0138f08797108912f2fdcc4b68fbff18e1def69a35a133d94a4cdc409f1a12f695ca8d155df4f3349fa99da0a551b9863a47e61b9984609ef2d815e553969a20'
6
+ metadata.gz: 3e0c6ff91ac90e928919e12774f4b1013e67b2957e1b3dfc958ef177f1c74ec3f2eb494ffe58c1a98946a69bc1eace0f79dd5664055f448388fedd02cbe7e824
7
+ data.tar.gz: d24bc990a119f44ddb136047bcf3423934d8252ee340cdc09b9500cf42da09aa352f0a58e8f22d249f9449cad2fb71681f5689c65af760697b493222899e7814
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 1.2.2 (2023-12-01) ❄️
6
+
7
+ - Fix analyzing code blocks with backticks.
8
+
5
9
  ## 1.2.1 (2023-10-20)
6
10
 
7
11
  - Fix incompatibility when loading the plugin from YAML and using other RuboCop options.
@@ -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
 
@@ -61,6 +64,8 @@ RuboCop::Runner.prepend(Module.new do
61
64
  # Skip offenses reported for ignored MD source (trailing whitespaces, etc.)
62
65
  marker_comment = "##{RuboCop::Markdown::Preprocess::MARKER}"
63
66
  offenses.reject! do |offense|
67
+ next if RuboCop::Markdown::MARKDOWN_OFFENSE_COPS.include?(offense.cop_name)
68
+
64
69
  offense.location.source_line.start_with?(marker_comment)
65
70
  end
66
71
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Markdown
5
- VERSION = "1.2.1"
5
+ VERSION = "1.2.2"
6
6
  end
7
7
  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.1
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-10-21 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