-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
rubocop_ext.rb
106 lines (89 loc) · 3.08 KB
/
rubocop_ext.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true
module RuboCop
module Markdown # :nodoc:
# According to Linguist. mdx was dropped but is being kept for backwards compatibility.
# See https://github.com/github-linguist/linguist/blob/8c380f360ce00b95fa08d14ce0ebccd481af1b33/lib/linguist/languages.yml#L4088-L4098
# Keep in sync with config/default.yml
MARKDOWN_EXTENSIONS = %w[
.md
.livemd
.markdown
.mdown
.mdwn
.mdx
.mkd
.mkdn
.mkdown
.ronn
.scd
.workbook
].freeze
# A list of cops that could produce offenses in commented lines
MARKDOWN_OFFENSE_COPS = %w[Lint/Syntax].freeze
class << self
attr_accessor :config_store
# Merge markdown config into default configuration
# See https://github.com/backus/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
def inject!
path = CONFIG_DEFAULT.to_s
hash = ConfigLoader.send(:load_yaml_configuration, path)
config = Config.new(hash, path)
puts "configuration from #{path}" if ConfigLoader.debug?
config = ConfigLoader.merge_with_default(config, path)
ConfigLoader.instance_variable_set(:@default_configuration, config)
end
def markdown_file?(file)
MARKDOWN_EXTENSIONS.include?(File.extname(file))
end
end
end
end
RuboCop::Markdown.inject!
RuboCop::Runner.prepend(Module.new do
# Set config store for Markdown
def get_processed_source(*args)
RuboCop::Markdown.config_store = @config_store unless RuboCop::Markdown.config_store
super
end
# Do not cache markdown files, 'cause cache doesn't know about processing.
# NOTE: we should involve preprocessing in RuboCop::CachedData#deserialize_offenses
def file_offense_cache(file)
return yield if RuboCop::Markdown.markdown_file?(file)
super
end
def file_finished(file, offenses)
return super unless RuboCop::Markdown.markdown_file?(file)
# Run Preprocess.restore if file has been autocorrected
if @options[:auto_correct] || @options[:autocorrect]
RuboCop::Markdown::Preprocess.restore_and_save!(file)
end
super(file, offenses)
end
end)
RuboCop::Cop::Commissioner::InvestigationReport.prepend(Module.new do
# Skip offenses reported for ignored MD source (trailing whitespaces, etc.)
def offenses
@offenses ||= begin
marker_comment = "##{RuboCop::Markdown::Preprocess::MARKER}"
offenses_per_cop.flatten(1).reject do |offense|
next if RuboCop::Markdown::MARKDOWN_OFFENSE_COPS.include?(offense.cop_name)
offense.location.source_line.start_with?(marker_comment)
end
end
end
end)
# Allow Rubocop to analyze markdown files
RuboCop::TargetFinder.prepend(Module.new do
def ruby_file?(file)
super || RuboCop::Markdown.markdown_file?(file)
end
end)
# Extend ProcessedSource#parse with pre-processing
RuboCop::ProcessedSource.prepend(Module.new do
def parse(src, *args)
# only process Markdown files
src = RuboCop::Markdown::Preprocess.new(path).call(src) if
path && RuboCop::Markdown.markdown_file?(path)
super(src, *args)
end
end)