rubocop-md 0.0.1 → 0.1.0.pre

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
  SHA1:
3
- metadata.gz: 7c7b9c73c1b57e18aa8503ec32612a418f0c2116
4
- data.tar.gz: 41079271c626e418ceb341c681545fe557012825
3
+ metadata.gz: 1e95074f390ef2a3e4dbe7fe57399cfd4066d661
4
+ data.tar.gz: 2717015145fb478f8dbeb7778e1f1423d34b1195
5
5
  SHA512:
6
- metadata.gz: 6dee3f3a08b43d79485d4344ca5980183bca18d0b7704782442a7c46c5ad3650daf6c24fa1b772fda78640ae13bfb29207ea731472401cb38f533ad7b2f674d8
7
- data.tar.gz: d97dbadbf2742fca963c28a55db3af9006f11e9de6b194784ced04297af0148cf59b7fb9e34901a59f89595e9a078a85c1ab9e4781dee88bc7d97431ba1aa993
6
+ metadata.gz: 6f7dd5441cc1b408114e524be0bf7eae2a9415331801a8157e4d484a610a0661986e9bfc0b462454982fc2e35b0a2d72975e1b464d2b0b65a49c800f87860d21
7
+ data.tar.gz: c61d81e351f7be36dac4fbd6ebbcb010f3d8fdd9f33439ff736b379692a89f9b26c50b38935fc93b3d3fd27a03c5c35363497cac7df29a7e4241d8d3e9c4fa33
data/.gem_release.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  bump:
2
- file: lib/rubocop/md/version.rb
2
+ file: lib/rubocop/markdown/version.rb
data/.rubocop.yml CHANGED
@@ -38,3 +38,11 @@ Metrics/LineLength:
38
38
  Max: 100
39
39
  Exclude:
40
40
  - 'test/**/*.rb'
41
+
42
+ Metrics/MethodLength:
43
+ Exclude:
44
+ - 'test/**/*.rb'
45
+
46
+ Metrics/ClassLength:
47
+ Exclude:
48
+ - 'test/**/*.rb'
data/Rakefile CHANGED
@@ -1,10 +1,14 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
+ require "rubocop/rake_task"
3
4
 
4
5
  Rake::TestTask.new(:test) do |t|
5
6
  t.libs << "test"
6
7
  t.libs << "lib"
8
+ t.warning = false
7
9
  t.test_files = FileList["test/**/*_test.rb"]
8
10
  end
9
11
 
10
- task default: :test
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: [:rubocop, :test]
@@ -0,0 +1,38 @@
1
+ Layout/CommentIndentation:
2
+ Exclude:
3
+ - '**/*.md'
4
+ - '**/*.markdown'
5
+
6
+ Layout/LeadingCommentSpace:
7
+ Exclude:
8
+ - '**/*.md'
9
+ - '**/*.markdown'
10
+
11
+ Layout/TrailingBlankLines:
12
+ Exclude:
13
+ - '**/*.md'
14
+ - '**/*.markdown'
15
+
16
+ Style/AsciiComments:
17
+ Exclude:
18
+ - '**/*.md'
19
+ - '**/*.markdown'
20
+
21
+ Style/Documentation:
22
+ Exclude:
23
+ - '**/*.md'
24
+ - '**/*.markdown'
25
+
26
+ Style/CommentAnnotation:
27
+ Exclude:
28
+ - '**/*.md'
29
+ - '**/*.markdown'
30
+
31
+ Style/FrozenStringLiteralComment:
32
+ Exclude:
33
+ - '**/*.md'
34
+ - '**/*.markdown'
35
+
36
+ Metrics/LineLength:
37
+ IgnoredPatterns:
38
+ - !ruby/regexp /^\#/
@@ -0,0 +1,106 @@
1
+ require "ripper"
2
+
3
+ module RuboCop
4
+ module Markdown
5
+ # Transform source Markdown file into valid Ruby file
6
+ # by commenting out all non-code lines
7
+ module Preprocess
8
+ # This is a regexp to extract code blocks from .md files.
9
+ #
10
+ # Only recognizes backticks-style code blocks.
11
+ #
12
+ # Try it: http://rubular.com/r/iJaKBkSrrT
13
+ MD_REGEXP = /^([ \t]*`{3,4})([\w[[:blank:]]]*\n)([\s\S]+?)(^[ \t]*\1[[:blank:]]*\n)/m
14
+
15
+ MARKER = "<--rubocop/md-->".freeze
16
+
17
+ # See https://github.com/github/linguist/blob/v5.3.3/lib/linguist/languages.yml#L3925
18
+ RUBY_TYPES = %w[
19
+ ruby
20
+ jruby
21
+ macruby
22
+ rake
23
+ rb
24
+ rbx
25
+ ].freeze
26
+
27
+ class Walker # :nodoc:
28
+ STEPS = %i[text code_start code_attr code_body code_end].freeze
29
+
30
+ STEPS.each do |step|
31
+ define_method("#{step}?") do
32
+ STEPS[current_step] == step
33
+ end
34
+ end
35
+
36
+ attr_accessor :current_step
37
+
38
+ def initialize
39
+ @current_step = 0
40
+ end
41
+
42
+ def next!
43
+ self.current_step = current_step == (STEPS.size - 1) ? 0 : current_step + 1
44
+ end
45
+ end
46
+
47
+ class << self
48
+ # rubocop:disable Metrics/MethodLength
49
+ def call(src)
50
+ parts = src.split(MD_REGEXP)
51
+
52
+ walker = Walker.new
53
+
54
+ parts.each do |part|
55
+ if walker.code_body?
56
+ next walker.next! if maybe_ruby?(@syntax) && valid_syntax?(part)
57
+ end
58
+
59
+ if walker.code_attr?
60
+ @syntax = part.gsub(/(^\s+|\s+$)/, "")
61
+ next walker.next!
62
+ end
63
+
64
+ comment_lines! part
65
+
66
+ walker.next!
67
+ end
68
+
69
+ parts.join
70
+ end
71
+ # rubocop:enable Metrics/MethodLength
72
+
73
+ # Revert preprocess changes.
74
+ #
75
+ # When autocorrect is applied, RuboCop re-writes the file
76
+ # using preproccessed source buffer.
77
+ #
78
+ # We have to restore it.
79
+ def restore!(file)
80
+ contents = File.read(file)
81
+ contents.gsub!(/^##{MARKER}/m, "")
82
+ File.write(file, contents)
83
+ end
84
+
85
+ private
86
+
87
+ # Check codeblock attribute to prevent from parsing
88
+ # non-Ruby snippets and avoid false positives
89
+ def maybe_ruby?(syntax)
90
+ syntax.empty? || RUBY_TYPES.include?(syntax)
91
+ end
92
+
93
+ # Try to parse with Ripper.
94
+ # Invalid Ruby (non-Ruby) code returns `nil`.
95
+ def valid_syntax?(src)
96
+ !Ripper.sexp(src).nil?
97
+ end
98
+
99
+ def comment_lines!(src)
100
+ return if src =~ /\A\n\z/
101
+ src.gsub!(/^(.)/m, "##{MARKER}\\1")
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,58 @@
1
+ module RuboCop
2
+ module Markdown # :nodoc:
3
+ MARKDOWN_EXTENSIONS = %w[.md .markdown].freeze
4
+
5
+ # Merge markdown config into default configuration
6
+ # See https://github.com/backus/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
7
+ def self.inject!
8
+ path = CONFIG_DEFAULT.to_s
9
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
10
+ config = Config.new(hash, path)
11
+ puts "configuration from #{path}" if ConfigLoader.debug?
12
+ config = ConfigLoader.merge_with_default(config, path)
13
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
14
+ end
15
+
16
+ def self.markdown_file?(file)
17
+ MARKDOWN_EXTENSIONS.include?(File.extname(file))
18
+ end
19
+ end
20
+ end
21
+
22
+ RuboCop::Markdown.inject!
23
+
24
+ RuboCop::Runner.prepend(Module.new do
25
+ # Do not cache markdown files, 'cause cache doesn't know about processing.
26
+ # NOTE: we should involve preprocessing in RuboCop::CachedData#deserialize_offenses
27
+ def file_offense_cache(file)
28
+ return yield if RuboCop::Markdown.markdown_file?(file)
29
+ super
30
+ end
31
+
32
+ # Run Preprocess.restore if file has been autocorrected
33
+ def process_file(file)
34
+ return super unless @options[:auto_correct] && RuboCop::Markdown.markdown_file?(file)
35
+
36
+ offenses = super
37
+ RuboCop::Markdown::Preprocess.restore!(file)
38
+
39
+ offenses
40
+ end
41
+ end)
42
+
43
+ # Allow Rubocop to analyze markdown files
44
+ RuboCop::TargetFinder.prepend(Module.new do
45
+ def ruby_file?(file)
46
+ super || RuboCop::Markdown.markdown_file?(file)
47
+ end
48
+ end)
49
+
50
+ # Extend ProcessedSource#parse with pre-processing
51
+ RuboCop::ProcessedSource.prepend(Module.new do
52
+ def parse(src, *args)
53
+ # only process Markdown files
54
+ src = RuboCop::Markdown::Preprocess.call(src) if
55
+ path.nil? || RuboCop::Markdown.markdown_file?(path)
56
+ super(src, *args)
57
+ end
58
+ end)
@@ -0,0 +1,5 @@
1
+ module RuboCop
2
+ module Markdown
3
+ VERSION = "0.1.0.pre".freeze
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require "rubocop/markdown/version"
2
+ require "pathname"
3
+
4
+ module RuboCop
5
+ # Plugin to run Rubocop against Markdown files
6
+ module Markdown
7
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
8
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "default.yml").freeze
9
+
10
+ require_relative "./markdown/preprocess"
11
+ require_relative "./markdown/rubocop_ext" if defined?(::RuboCop::ProcessedSource)
12
+ end
13
+ end
data/lib/rubocop-md.rb ADDED
@@ -0,0 +1 @@
1
+ require "rubocop/markdown"
data/rubocop-md.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  lib = File.expand_path("../lib", __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "rubocop/md/version"
3
+ require "rubocop/markdown/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "rubocop-md"
7
- spec.version = Rubocop::Md::VERSION
7
+ spec.version = RuboCop::Markdown::VERSION
8
8
  spec.authors = ["Vladimir Dementyev"]
9
9
  spec.email = ["dementiev.vm@gmail.com"]
10
10
 
@@ -19,8 +19,9 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.require_paths = ["lib"]
21
21
 
22
+ spec.add_runtime_dependency "rubocop", "~> 0.50"
23
+
22
24
  spec.add_development_dependency "bundler", "~> 1.15"
23
25
  spec.add_development_dependency "rake", "~> 10.0"
24
26
  spec.add_development_dependency "minitest", "~> 5.0"
25
- spec.add_development_dependency "rubocop", "~> 0.50"
26
27
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-md
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-09 00:00:00.000000000 Z
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rubocop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
20
- type: :development
19
+ version: '0.50'
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.15'
26
+ version: '0.50'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '1.15'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '1.15'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.0'
47
+ version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.0'
54
+ version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rubocop
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.50'
61
+ version: '5.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.50'
68
+ version: '5.0'
69
69
  description: Run Rubocop against your Markdown files to make sure that code examples
70
70
  follow style guidelines.
71
71
  email:
@@ -82,10 +82,13 @@ files:
82
82
  - LICENSE.txt
83
83
  - README.md
84
84
  - Rakefile
85
- - bin/console
86
85
  - bin/setup
87
- - lib/rubocop/md.rb
88
- - lib/rubocop/md/version.rb
86
+ - config/default.yml
87
+ - lib/rubocop-md.rb
88
+ - lib/rubocop/markdown.rb
89
+ - lib/rubocop/markdown/preprocess.rb
90
+ - lib/rubocop/markdown/rubocop_ext.rb
91
+ - lib/rubocop/markdown/version.rb
89
92
  - rubocop-md.gemspec
90
93
  homepage: https://github.com/palkan/rubocop-md
91
94
  licenses:
@@ -102,9 +105,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
102
105
  version: '0'
103
106
  required_rubygems_version: !ruby/object:Gem::Requirement
104
107
  requirements:
105
- - - ">="
108
+ - - ">"
106
109
  - !ruby/object:Gem::Version
107
- version: '0'
110
+ version: 1.3.1
108
111
  requirements: []
109
112
  rubyforge_project:
110
113
  rubygems_version: 2.6.13
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "rubocop/md"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
@@ -1,5 +0,0 @@
1
- module Rubocop
2
- module Md
3
- VERSION = "0.0.1".freeze
4
- end
5
- end
data/lib/rubocop/md.rb DELETED
@@ -1,8 +0,0 @@
1
- require "rubocop/md/version"
2
-
3
- module Rubocop
4
- # Plugin to run Rubocop against Markdown files
5
- module Md
6
- # Your code goes here...
7
- end
8
- end