lintrunner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ module Lintrunner
2
+ module Reporter
3
+ class File < Base
4
+
5
+ def finish(messages)
6
+ puts messages.collect(&:filename).uniq.join("\n") if messages.any?
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module Lintrunner
2
+ module Reporter
3
+ class Text < Base
4
+
5
+ def start(name)
6
+ puts "Running #{name} linters".underline
7
+ end
8
+
9
+ def report(message)
10
+ puts "#{location(message)} #{message.description} #{message_name(message)}"
11
+ end
12
+
13
+ def finish(messages)
14
+ puts "No messages found".color(:green) if messages.empty?
15
+ puts
16
+ end
17
+
18
+ private
19
+
20
+ def message_name(message)
21
+ "(#{message.name})".color(:yellow)
22
+ end
23
+
24
+ def location(message)
25
+ "#{message.filename.to_s.color(:cyan)}:#{message.line.to_s.color(:green)}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module Lintrunner
2
+ module Runner
3
+ class Base
4
+ include Lintrunner::GitHelpers
5
+
6
+ attr_accessor :path, :match, :executor, :git
7
+
8
+ def initialize(path:, match:, executor:)
9
+ self.path = path
10
+ self.executor = executor
11
+ self.match = Regexp.new(Regexp.escape(match))
12
+ self.git = Rugged::Repository.new(path)
13
+ end
14
+
15
+ def run(reporter = nil)
16
+ raise "Runner must implement #run method"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ module Lintrunner
2
+ module Runner
3
+ class ChangedFile < Base
4
+
5
+ def run(reporter)
6
+ warnings = []
7
+ git_changeset.each do |patch|
8
+ filename = patch.delta.new_file[:path]
9
+ next if patch.delta.binary?
10
+ next unless filename =~ match
11
+ full_path = File.join(path, filename)
12
+
13
+ messages = executor.execute(full_path, filename: filename)
14
+ warnings.concat messages
15
+ output = messages.collect do |message|
16
+ reporter.report(message)
17
+ end
18
+ end
19
+ warnings
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,89 @@
1
+ module Lintrunner
2
+ module Runner
3
+ class Diff < Base
4
+
5
+ def run(reporter)
6
+ warnings = []
7
+ git_changeset.each do |patch|
8
+ filename = patch.delta.new_file[:path]
9
+ next if patch.delta.binary?
10
+ next if filename.end_with?('app/styles/pattern-guide/pattern-guide.scss')
11
+ next unless filename =~ match
12
+
13
+ before_contents = contents_for(patch.delta, :src)
14
+ after_contents = contents_for(patch.delta, :dst)
15
+
16
+ if before_contents && after_contents
17
+ before_file = tempfile(before_contents)
18
+ after_file = tempfile(after_contents)
19
+
20
+ before_messages = executor.execute(before_file, filename: filename)
21
+ after_messages = executor.execute(after_file, filename: filename)
22
+
23
+ line_map = line_map_for(patch)
24
+ after_messages.each do |lint|
25
+ same_lint = before_messages.find do |b_lint|
26
+ b_lint.line == line_map[lint.line] &&
27
+ b_lint.name == lint.name && b_lint.description == lint.description
28
+ end
29
+ warnings << lint unless same_lint
30
+ reporter.report(lint) unless same_lint
31
+ end
32
+ elsif after_contents
33
+ after_file = tempfile(after_contents)
34
+ after_messages = executor.execute(after_file, filename: filename)
35
+ warnings.concat after_messages
36
+ output = after_messages.collect do |message|
37
+ reporter.report(message)
38
+ end
39
+ end
40
+ end
41
+ warnings
42
+ end
43
+
44
+ private
45
+
46
+ # Get the file contents of a delta
47
+ # if type=:src, return original file
48
+ # if type=:dst, return modified file
49
+ def contents_for(delta, type=:dst)
50
+ ref = if type == :src
51
+ delta.old_file[:oid]
52
+ elsif type == :dst
53
+ delta.new_file[:oid]
54
+ end
55
+ @git.lookup(ref).content if ref != "0000000000000000000000000000000000000000"
56
+ end
57
+
58
+ def line_map_for(patch)
59
+ line_map = []
60
+ patch.hunks.first.lines.each do |line|
61
+ if line.new_lineno != -1
62
+ line_map[line.new_lineno] = line.old_lineno == -1 ? nil : line.old_lineno
63
+ end
64
+ end
65
+ line_map
66
+ end
67
+
68
+ def tempfile(string)
69
+ t = Tempfile.new('diff_file')
70
+ # ensure tempfiles aren't unlinked when GC runs by maintaining a reference to them.
71
+ @tempfiles ||= []
72
+ @tempfiles.push(t)
73
+ t.print(string)
74
+ t.flush
75
+ t.close
76
+ t.path
77
+ end
78
+
79
+ def invert(array)
80
+ result = []
81
+ array.each_with_index do |el, i|
82
+ result[el] = i if el
83
+ end
84
+ result
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,26 @@
1
+ module Lintrunner
2
+ module Runner
3
+ class NewFile < Base
4
+
5
+ def run(reporter)
6
+ warnings = []
7
+ git_changeset.each do |patch|
8
+ filename = patch.delta.new_file[:path]
9
+ next if patch.delta.binary?
10
+ next unless patch.delta.added?
11
+ next unless filename =~ match
12
+
13
+ full_path = File.join(path, filename)
14
+
15
+ messages = executor.execute(full_path, filename: filename)
16
+ warnings.concat messages
17
+ output = messages.collect do |message|
18
+ reporter.report(message)
19
+ end
20
+ end
21
+ warnings
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module Lintrunner
2
+ module Runner
3
+ class Repo < Base
4
+
5
+ def run(reporter = nil)
6
+ warnings = []
7
+ files.each do |filename|
8
+ # next if patch.delta.binary?
9
+ full_path = File.join(path, filename)
10
+ next unless filename =~ match
11
+
12
+ messages = executor.execute(full_path, filename: filename)
13
+ warnings.concat messages
14
+ output = messages.collect do |message|
15
+ reporter.report(message)
16
+ end
17
+ end
18
+ warnings
19
+ end
20
+
21
+ private
22
+
23
+ def files
24
+ files = []
25
+ git.head.target.tree.walk_blobs { |root, entry| files << "#{root}#{entry[:name]}" }
26
+ files
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Lintrunner
2
+ VERSION = "0.0.1"
3
+ end
data/lib/lintrunner.rb ADDED
@@ -0,0 +1,34 @@
1
+ # Dependencies
2
+ require "json"
3
+ require "rugged"
4
+ require "tempfile"
5
+ require "stringio"
6
+ require "pathname"
7
+ require "rainbow/ext/string"
8
+
9
+ require "lintrunner/version"
10
+ require "lintrunner/git_helpers"
11
+ require "lintrunner/message"
12
+ require "lintrunner/plugin"
13
+ require "lintrunner/executor"
14
+
15
+ # Parsers
16
+ require "lintrunner/parser/base"
17
+ require "lintrunner/parser/eslint"
18
+ require "lintrunner/parser/scss_lint"
19
+ require "lintrunner/parser/rubocop"
20
+
21
+ # Runners
22
+ require "lintrunner/runner/base"
23
+ require "lintrunner/runner/diff"
24
+ require "lintrunner/runner/new_file"
25
+ require "lintrunner/runner/changed_file"
26
+ require "lintrunner/runner/repo"
27
+
28
+ # Reporters
29
+ require "lintrunner/reporter/base"
30
+ require "lintrunner/reporter/text"
31
+ require "lintrunner/reporter/context"
32
+ require "lintrunner/reporter/file"
33
+
34
+ module Lintrunner; end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lintrunner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lintrunner"
8
+ spec.version = Lintrunner::VERSION
9
+ spec.authors = ["Ivan Tse"]
10
+ spec.email = ["ivan.tse1@gmail.com"]
11
+ spec.summary = %q{Run multiple lints with various runners}
12
+ spec.description = %q{lintrunner}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rugged", "~> 0.21"
22
+ spec.add_dependency "rainbow", " ~> 2.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
data/test.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Lintrunner
2
+ module Plugin
3
+ class BindingPryCheck
4
+
5
+ attr_accessor :filename
6
+
7
+ def initialize(filename)
8
+ self.filename = filename
9
+ @binding_pry_matcher = /binding\.pry/
10
+ end
11
+
12
+ def run(options = {})
13
+ lineno = 1
14
+ warnings = []
15
+ return warnings if binary?(filename)
16
+ File.read(filename).each_line do |line|
17
+ warnings << create_message(options[:filename] || filename, lineno) if line[@binding_pry_matcher]
18
+ lineno += 1
19
+ end
20
+ warnings
21
+ end
22
+
23
+ def create_message(filename, lineno)
24
+ Lintrunner::Message.new(
25
+ filename: filename,
26
+ line: lineno,
27
+ name: "BindingPryCheck",
28
+ description: "binding.pry detected"
29
+ )
30
+ end
31
+
32
+ def binary?(file)
33
+ # return false if image?(file)
34
+ bytes = File.stat(file).blksize
35
+ bytes = 4096 if bytes > 4096
36
+ s = (File.read(file, bytes) || "")
37
+ s = s.encode('US-ASCII', :undef => :replace).split(//)
38
+ ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30
39
+ end
40
+
41
+ end
42
+ end
43
+ end
data/test2.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Test2
2
+ <<<<<<< HEAD:file.txt
3
+ Hello world
4
+ =======
5
+ Goodbye
6
+ >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
7
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lintrunner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Tse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rugged
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.21'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.21'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rainbow
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: lintrunner
70
+ email:
71
+ - ivan.tse1@gmail.com
72
+ executables:
73
+ - lintrunner
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".lintrunner_config"
79
+ - ".rubocop.yml"
80
+ - ".scss-lint.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/lintrunner
86
+ - lib/lintrunner.rb
87
+ - lib/lintrunner/cli.rb
88
+ - lib/lintrunner/executor.rb
89
+ - lib/lintrunner/git_helpers.rb
90
+ - lib/lintrunner/message.rb
91
+ - lib/lintrunner/options.rb
92
+ - lib/lintrunner/parser/base.rb
93
+ - lib/lintrunner/parser/eslint.rb
94
+ - lib/lintrunner/parser/rubocop.rb
95
+ - lib/lintrunner/parser/scss_lint.rb
96
+ - lib/lintrunner/plugin.rb
97
+ - lib/lintrunner/reporter/base.rb
98
+ - lib/lintrunner/reporter/context.rb
99
+ - lib/lintrunner/reporter/file.rb
100
+ - lib/lintrunner/reporter/text.rb
101
+ - lib/lintrunner/runner/base.rb
102
+ - lib/lintrunner/runner/changed_file.rb
103
+ - lib/lintrunner/runner/diff.rb
104
+ - lib/lintrunner/runner/new_file.rb
105
+ - lib/lintrunner/runner/repo.rb
106
+ - lib/lintrunner/version.rb
107
+ - lintrunner.gemspec
108
+ - test.rb
109
+ - test2.rb
110
+ homepage: ''
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.2.2
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Run multiple lints with various runners
134
+ test_files: []