rubocop-md 1.2.1 → 1.2.2
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rubocop/markdown/preprocess.rb +31 -44
- data/lib/rubocop/markdown/rubocop_ext.rb +5 -0
- data/lib/rubocop/markdown/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e1a8d57e542393892dbdf2f9ffd7229d82e1996e900dd33edec55c0b94371ef
|
4
|
+
data.tar.gz: 5503ee8519df1ead15a2ae9602ffc4e960a6d6ddd2d36f32e0dea39c6ac87eef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e0c6ff91ac90e928919e12774f4b1013e67b2957e1b3dfc958ef177f1c74ec3f2eb494ffe58c1a98946a69bc1eace0f79dd5664055f448388fedd02cbe7e824
|
7
|
+
data.tar.gz: d24bc990a119f44ddb136047bcf3423934d8252ee340cdc09b9500cf42da09aa352f0a58e8f22d249f9449cad2fb71681f5689c65af760697b493222899e7814
|
data/CHANGELOG.md
CHANGED
@@ -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
|
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 =
|
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
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
128
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2023-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|