rubocop-md 1.2.1 → 1.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e4b62aa5ac22f5dd82b132acd7439582c1d7e4a925d8a44cfeb48f250d579aa
4
- data.tar.gz: 5acca30ab4a740b8bb9fdd25bd1b769f96504a63c8f6e4d3460ddfe61c66dbcf
3
+ metadata.gz: c7c6dbb228497df06f33f4c2c5f91b9fc719aa1026428b8ca86e596f4658ffcb
4
+ data.tar.gz: c42e2dea6fe018ec5e0f23dd39caeaff927408729ee69c911f7220446017d1c0
5
5
  SHA512:
6
- metadata.gz: eb68e5947593be1fce3b2c7131d7a445615b3a9a47c86db9e5b5e31ba966e0c4f7030c20578e140cfe1f55b824e1224a6a2ef3206317db51fa7bd89b60d93e52
7
- data.tar.gz: '0138f08797108912f2fdcc4b68fbff18e1def69a35a133d94a4cdc409f1a12f695ca8d155df4f3349fa99da0a551b9863a47e61b9984609ef2d815e553969a20'
6
+ metadata.gz: d3a4dd5d3e3eb894db690a40f699303f4350664516cd83475127786e8348086b0e23d8209fb2187aa0df33e287f4a6f72f840efe7b9dc7214b0113180299e59f
7
+ data.tar.gz: 5cd2b8c3675fd5fa84676c00d75ce2872dbc193ab4f55fc01283450c5bc3ad3774c80156b712332db467585baa8e3913064f5c14095102eb2bb979b572e1e51e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 1.2.3 (2024-09-12)
6
+
7
+ - Do no try linting `.mdx` files by default.
8
+
9
+ ## 1.2.2 (2023-12-01) ❄️
10
+
11
+ - Fix analyzing code blocks with backticks.
12
+
5
13
  ## 1.2.1 (2023-10-20)
6
14
 
7
15
  - Fix incompatibility when loading the plugin from YAML and using other RuboCop options.
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/rubocop-md.svg)](http://badge.fury.io/rb/rubocop-md)
2
- ![Test](https://github.com/rubocop-hq/rubocop-md/workflows/Test/badge.svg)
2
+ ![Test](https://github.com/rubocop/rubocop-md/workflows/Test/badge.svg)
3
3
 
4
4
  # Rubocop Markdown
5
5
 
@@ -158,7 +158,7 @@ To install this gem onto your local machine, run `bundle exec rake install`.
158
158
 
159
159
  ## Contributing
160
160
 
161
- Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop-hq/rubocop-md.
161
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop/rubocop-md.
162
162
 
163
163
  ## License
164
164
 
data/config/default.yml CHANGED
@@ -1,111 +1,86 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Include
4
+ - Exclude
5
+
1
6
  Markdown:
2
7
  WarnInvalid: true
3
8
  Autodetect: true
4
9
 
5
- Layout/CommentIndentation:
6
- Exclude:
7
- - '**/*.md'
8
- - '**/*.markdown'
10
+ # Keep in sync with MARKDOWN_EXTENSIONS
11
+ Layout/CommentIndentation: &markdown_excludes
12
+ Exclude:
13
+ - "**/*.md"
14
+ - "**/*.livemd"
15
+ - "**/*.markdown"
16
+ - "**/*.mdown"
17
+ - "**/*.mdwn"
18
+ - "**/*.mkd"
19
+ - "**/*.mkdn"
20
+ - "**/*.mkdown"
21
+ - "**/*.ronn"
22
+ - "**/*.scd"
23
+ - "**/*.workbook"
9
24
 
10
25
  Layout/LeadingCommentSpace:
11
- Exclude:
12
- - '**/*.md'
13
- - '**/*.markdown'
26
+ <<: *markdown_excludes
14
27
 
15
28
  Layout/TrailingBlankLines:
16
- Exclude:
17
- - '**/*.md'
18
- - '**/*.markdown'
29
+ <<: *markdown_excludes
19
30
 
20
31
  Layout/TrailingEmptyLines:
21
- Exclude:
22
- - '**/*.md'
23
- - '**/*.markdown'
32
+ <<: *markdown_excludes
24
33
 
25
34
  Layout/EmptyLineBetweenDefs:
26
- Exclude:
27
- - '**/*.md'
28
- - '**/*.markdown'
35
+ <<: *markdown_excludes
29
36
 
30
37
  Lint/DuplicateRequire:
31
- Exclude:
32
- - '**/*.md'
33
- - '**/*.markdown'
38
+ <<: *markdown_excludes
34
39
 
35
40
  Lint/DuplicateMethods:
36
- Exclude:
37
- - '**/*.md'
38
- - '**/*.markdown'
41
+ <<: *markdown_excludes
39
42
 
40
43
  Lint/RedundantCopDisableDirective:
41
- Exclude:
42
- - '**/*.md'
43
- - '**/*.markdown'
44
+ <<: *markdown_excludes
44
45
 
45
46
  Lint/RedundantCopEnableDirective:
46
- Exclude:
47
- - '**/*.md'
48
- - '**/*.markdown'
47
+ <<: *markdown_excludes
49
48
 
50
49
  Lint/RedundantRequireStatement:
51
- Exclude:
52
- - '**/*.md'
53
- - '**/*.markdown'
50
+ <<: *markdown_excludes
54
51
 
55
52
  Lint/UnusedMethodArgument:
56
- Exclude:
57
- - '**/*.md'
58
- - '**/*.markdown'
53
+ <<: *markdown_excludes
59
54
 
60
55
  Lint/UnusedBlockArgument:
61
- Exclude:
62
- - '**/*.md'
63
- - '**/*.markdown'
56
+ <<: *markdown_excludes
64
57
 
65
58
  Lint/UselessAssignment:
66
- Exclude:
67
- - '**/*.md'
68
- - '**/*.markdown'
59
+ <<: *markdown_excludes
69
60
 
70
61
  Lint/UselessMethodDefinition:
71
- Exclude:
72
- - '**/*.md'
73
- - '**/*.markdown'
62
+ <<: *markdown_excludes
74
63
 
75
64
  Lint/UselessAccessModifier:
76
- Exclude:
77
- - '**/*.md'
78
- - '**/*.markdown'
65
+ <<: *markdown_excludes
79
66
 
80
67
  Lint/Void:
81
- Exclude:
82
- - '**/*.md'
83
- - '**/*.markdown'
68
+ <<: *markdown_excludes
84
69
 
85
70
  Naming/FileName:
86
- Exclude:
87
- - '**/*.md'
88
- - '**/*.markdown'
71
+ <<: *markdown_excludes
89
72
 
90
73
  Style/AsciiComments:
91
- Exclude:
92
- - '**/*.md'
93
- - '**/*.markdown'
74
+ <<: *markdown_excludes
94
75
 
95
76
  Style/Documentation:
96
- Exclude:
97
- - '**/*.md'
98
- - '**/*.markdown'
77
+ <<: *markdown_excludes
99
78
 
100
79
  Style/CommentAnnotation:
101
- Exclude:
102
- - '**/*.md'
103
- - '**/*.markdown'
80
+ <<: *markdown_excludes
104
81
 
105
82
  Style/FrozenStringLiteralComment:
106
- Exclude:
107
- - '**/*.md'
108
- - '**/*.markdown'
83
+ <<: *markdown_excludes
109
84
 
110
85
  Layout/LineLength:
111
86
  IgnoredPatterns:
@@ -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
  #
@@ -53,11 +39,15 @@ module RuboCop
53
39
  # using preproccessed source buffer.
54
40
  #
55
41
  # We have to restore it.
56
- def restore!(file)
42
+ def restore_and_save!(file)
57
43
  contents = File.read(file)
58
- contents.gsub!(/^##{MARKER}/m, "")
44
+ restore!(contents)
59
45
  File.write(file, contents)
60
46
  end
47
+
48
+ def restore!(src)
49
+ src.gsub!(/^##{MARKER}/m, "")
50
+ end
61
51
  end
62
52
 
63
53
  attr_reader :config
@@ -68,31 +58,34 @@ module RuboCop
68
58
 
69
59
  # rubocop:disable Metrics/MethodLength
70
60
  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!
61
+ src.gsub(MD_REGEXP) do |full_match|
62
+ m = Regexp.last_match
63
+ open_backticks = m[1]
64
+ syntax = m[2]
65
+ code = m[3]
66
+ close_backticks = m[4]
67
+ markdown = m[5]
68
+
69
+ if markdown
70
+ # We got markdown outside of a codeblock
71
+ comment_lines(markdown)
72
+ elsif ruby_codeblock?(syntax, code)
73
+ # The codeblock we parsed is assumed ruby, keep as is and append markers to backticks
74
+ "#{comment_lines(open_backticks + syntax)}\n#{code}#{comment_lines(close_backticks)}"
75
+ else
76
+ # The codeblock is not relevant, comment it out
77
+ comment_lines(full_match)
78
78
  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
79
  end
89
-
90
- parts.join
91
80
  end
92
81
  # rubocop:enable Metrics/MethodLength
93
82
 
94
83
  private
95
84
 
85
+ def ruby_codeblock?(syntax, src)
86
+ maybe_ruby?(syntax) && valid_syntax?(syntax, src)
87
+ end
88
+
96
89
  # Check codeblock attribute to prevent from parsing
97
90
  # non-Ruby snippets and avoid false positives
98
91
  def maybe_ruby?(syntax)
@@ -124,10 +117,8 @@ module RuboCop
124
117
  config["Markdown"]&.fetch("Autodetect", true)
125
118
  end
126
119
 
127
- def comment_lines!(src)
128
- return if src =~ /\A\n\z/
129
-
130
- src.gsub!(/^(.)/m, "##{MARKER}\\1")
120
+ def comment_lines(src)
121
+ src.gsub(/^/, "##{MARKER}")
131
122
  end
132
123
  end
133
124
  end
@@ -2,21 +2,25 @@
2
2
 
3
3
  module RuboCop
4
4
  module Markdown # :nodoc:
5
- # According to Linguist.
6
- # See https://github.com/github/linguist/blob/96ca71ab99c2f9928d5d69f4c08fd2a51440d045/lib/linguist/languages.yml#L3065-L3083
5
+ # See https://github.com/github-linguist/linguist/blob/8c380f360ce00b95fa08d14ce0ebccd481af1b33/lib/linguist/languages.yml#L4088-L4098
6
+ # Keep in sync with config/default.yml
7
7
  MARKDOWN_EXTENSIONS = %w[
8
8
  .md
9
+ .livemd
9
10
  .markdown
10
11
  .mdown
11
12
  .mdwn
12
- .mdx
13
13
  .mkd
14
14
  .mkdn
15
15
  .mkdown
16
16
  .ronn
17
+ .scd
17
18
  .workbook
18
19
  ].freeze
19
20
 
21
+ # A list of cops that could produce offenses in commented lines
22
+ MARKDOWN_OFFENSE_COPS = %w[Lint/Syntax].freeze
23
+
20
24
  class << self
21
25
  attr_accessor :config_store
22
26
 
@@ -56,25 +60,29 @@ RuboCop::Runner.prepend(Module.new do
56
60
  super
57
61
  end
58
62
 
59
- def inspect_file(*args)
60
- super.tap do |(offenses, *)|
61
- # Skip offenses reported for ignored MD source (trailing whitespaces, etc.)
62
- marker_comment = "##{RuboCop::Markdown::Preprocess::MARKER}"
63
- offenses.reject! do |offense|
64
- offense.location.source_line.start_with?(marker_comment)
65
- end
66
- end
67
- end
68
-
69
63
  def file_finished(file, offenses)
70
64
  return super unless RuboCop::Markdown.markdown_file?(file)
71
65
 
72
66
  # Run Preprocess.restore if file has been autocorrected
73
67
  if @options[:auto_correct] || @options[:autocorrect]
74
- RuboCop::Markdown::Preprocess.restore!(file)
68
+ RuboCop::Markdown::Preprocess.restore_and_save!(file)
75
69
  end
76
70
 
77
- super(file, offenses)
71
+ super
72
+ end
73
+ end)
74
+
75
+ RuboCop::Cop::Commissioner::InvestigationReport.prepend(Module.new do
76
+ # Skip offenses reported for ignored MD source (trailing whitespaces, etc.)
77
+ def offenses
78
+ @offenses ||= begin
79
+ marker_comment = "##{RuboCop::Markdown::Preprocess::MARKER}"
80
+ offenses_per_cop.flatten(1).reject do |offense|
81
+ next if RuboCop::Markdown::MARKDOWN_OFFENSE_COPS.include?(offense.cop_name)
82
+
83
+ offense.location.source_line.start_with?(marker_comment)
84
+ end
85
+ end
78
86
  end
79
87
  end)
80
88
 
@@ -91,6 +99,6 @@ RuboCop::ProcessedSource.prepend(Module.new do
91
99
  # only process Markdown files
92
100
  src = RuboCop::Markdown::Preprocess.new(path).call(src) if
93
101
  path && RuboCop::Markdown.markdown_file?(path)
94
- super(src, *args)
102
+ super
95
103
  end
96
104
  end)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Markdown
5
- VERSION = "1.2.1"
5
+ VERSION = "1.2.3"
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-21 00:00:00.000000000 Z
11
+ date: 2024-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '1.45'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '1.45'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -83,16 +83,16 @@ files:
83
83
  - lib/rubocop/markdown/preprocess.rb
84
84
  - lib/rubocop/markdown/rubocop_ext.rb
85
85
  - lib/rubocop/markdown/version.rb
86
- homepage: https://github.com/rubocop-hq/rubocop-md
86
+ homepage: https://github.com/rubocop/rubocop-md
87
87
  licenses:
88
88
  - MIT
89
89
  metadata:
90
- bug_tracker_uri: http://github.com/rubocop-hq/rubocop-md/issues
91
- changelog_uri: https://github.com/rubocop-hq/rubocop-md/blob/master/CHANGELOG.md
92
- documentation_uri: https://github.com/rubocop-hq/rubocop-md/blob/master/README.md
93
- homepage_uri: https://github.com/rubocop-hq/rubocop-md
94
- source_code_uri: http://github.com/rubocop-hq/rubocop-md
95
- post_install_message:
90
+ bug_tracker_uri: http://github.com/rubocop/rubocop-md/issues
91
+ changelog_uri: https://github.com/rubocop/rubocop-md/blob/master/CHANGELOG.md
92
+ documentation_uri: https://github.com/rubocop/rubocop-md/blob/master/README.md
93
+ homepage_uri: https://github.com/rubocop/rubocop-md
94
+ source_code_uri: http://github.com/rubocop/rubocop-md
95
+ post_install_message:
96
96
  rdoc_options: []
97
97
  require_paths:
98
98
  - lib
@@ -107,8 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubygems_version: 3.4.20
111
- signing_key:
110
+ rubygems_version: 3.4.19
111
+ signing_key:
112
112
  specification_version: 4
113
113
  summary: Run Rubocop against your Markdown files to make sure that code examples follow
114
114
  style guidelines.