wegolint 0.0.3 → 0.0.4

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wegolint (0.0.3)
4
+ wegolint (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  ## What is it?
2
- This is a tool that validates a code file for a set of style guide rules. These
3
- rules are run along with the standard parser for the given language. The main
4
- use for this tool is to pass errors and line numbers to the [Syntastic Vim
5
- plugin](https://github.com/scrooloose/syntastic).
2
+ This is a tool that validates a code file against a set of style guide rules
3
+ defined using regexes. These rules are run along with the standard parser for
4
+ the given language. The main use for this tool is to pass errors and line
5
+ numbers to the [Syntastic Vim plugin](https://github.com/scrooloose/syntastic).
6
6
 
7
7
  ## Installation Steps
8
8
  1. Install syntastic fork into `~/.vim/bundle/`:
@@ -21,3 +21,17 @@ let g:syntastic_javascript_checker='wegolint'
21
21
 
22
22
  ## Screenshot with Syntastic
23
23
  ![Screenshot](http://dl.dropbox.com/u/48928587/Screenshots/h.png)
24
+
25
+ ## Rule Definition Syntax
26
+ Wegolint uses a simple DSL for adding new rules:
27
+
28
+ ```ruby
29
+ # only run this rule against Ruby files, default is to run against all files
30
+ language :ruby do
31
+ rule "Don't use foo as a var name" do
32
+ pattern /foo =/
33
+ end
34
+ end
35
+ ```
36
+
37
+ Rules can be added by dropping files into the `~/.wegolint` folder.
data/lib/wegolint/dsl.rb CHANGED
@@ -2,6 +2,7 @@ module WegoLint
2
2
  module DSL
3
3
  module RuleDirectives
4
4
  @@collapse_quotes = false
5
+ @@collapse_hashes = false
5
6
 
6
7
  def included(klass)
7
8
  @@pattern = nil
@@ -19,6 +20,15 @@ module WegoLint
19
20
  def collapse_quotes?
20
21
  @@collapse_quotes
21
22
  end
23
+
24
+ def collapse_hashes
25
+ @@collapse_hashes = true
26
+ end
27
+
28
+ def collapse_hashes?
29
+ @@collapse_hashes
30
+ end
31
+
22
32
  end
23
33
 
24
34
  def language(name, &block)
data/lib/wegolint/rule.rb CHANGED
@@ -16,6 +16,7 @@ class WegoLint::Rule
16
16
  def run(text)
17
17
  text.split("\n").each_with_index do |line, index|
18
18
  line = collapse_quotes(line) if self.class.collapse_quotes?
19
+ line = collapse_hashes(line) if self.class.collapse_hashes?
19
20
  if line =~ self.class.pattern
20
21
  @errors << {message: description, line_number: index + 1 }
21
22
  end
@@ -32,6 +33,10 @@ class WegoLint::Rule
32
33
  /(#{no_space_beginning_block}|#{no_space_end_block})/
33
34
  end
34
35
 
36
+ def collapse_hashes(line)
37
+ line.gsub(/\{.*?(\w+\:|\=\>).*?}/, "{ }")
38
+ end
39
+
35
40
  def collapse_quotes(line)
36
41
  newline = line
37
42
  ['"', "'"].each do |char|
@@ -24,25 +24,33 @@ class WegoLint::RuleRegistry
24
24
  end
25
25
 
26
26
  def find_by_description(description)
27
- rules.keys.each do |key|
28
- match = rules[key].find {|rule| rule.description == description }
29
- return match if match
27
+ rules.keys.each do |ruleset|
28
+ match = rules[ruleset].keys.find { |key| key == description }
29
+ return rules[ruleset][match] if match
30
30
  end
31
31
  nil
32
32
  end
33
33
 
34
34
  def load_rules
35
- Dir[File.dirname(__FILE__) + '/rules/*'].each { |rule| require rule }
35
+ local_rules_dir = File.dirname(__FILE__) + '/../../rules/'
36
+ Dir[local_rules_dir + '*'].each { |rule| require rule }
37
+
38
+ rules_directory = ENV["HOME"] + '/.wegolint'
39
+ standard_rules_directory = rules_directory + '/standard'
40
+ FileUtils.mkdir_p rules_directory
41
+ FileUtils.mkdir_p standard_rules_directory
42
+ Dir[rules_directory + '/**/*.rb'].each { |rule| require rule }
36
43
  end
37
44
 
38
45
  # Pattern stolen from rspec
39
46
  def add_rule(description, block)
40
47
  WegoLint::RuleRegistry::rule_count += 1
41
- klass = WegoLint::RuleRegistry.const_set("Rule_#{WegoLint::RuleRegistry::rule_count}",
42
- subclass(WegoLint::Rule, block))
43
- @rules[@language_context] = [] unless @rules[@language_context]
48
+ klass = WegoLint::RuleRegistry.
49
+ const_set("Rule_#{WegoLint::RuleRegistry::rule_count}",
50
+ subclass(WegoLint::Rule, block))
51
+ @rules[@language_context] = {} unless @rules[@language_context]
44
52
  rule = klass.new(description, @language_context)
45
- @rules[@language_context] << rule
53
+ @rules[@language_context][description] = rule
46
54
  rule
47
55
  end
48
56
 
@@ -0,0 +1,3 @@
1
+ module WegoLint
2
+ VERSION = '0.0.4'
3
+ end
@@ -47,19 +47,9 @@ class WegoLint::WegoLinter
47
47
  end
48
48
  end
49
49
 
50
- # The real algorithm for this has to find the first instance of any pair,
51
- # strip it, start over, find the second instance, strip it, start over, etc.
52
- def collapse_quotes(line)
53
- newline = line
54
- ['"', "'"].each do |char|
55
- newline.gsub!(/#{char}.+?#{char}/, "#{char}TEST#{char}")
56
- end
57
- newline
58
- end
59
-
60
50
  def apply_rules(text, ruleset = :general)
61
51
  return unless rules[ruleset]
62
- rules[ruleset].each do |rule|
52
+ rules[ruleset].each do |key, rule|
63
53
  rule.run(text)
64
54
  @output += format_errors(rule.errors) + "\n" unless rule.errors.empty?
65
55
  end
File without changes
@@ -2,6 +2,7 @@ language :ruby do
2
2
  rule 'Space should be around block { |param| foo }' do
3
3
  pattern /[\.\s]#{block_not_padded}/
4
4
 
5
+ collapse_hashes
5
6
  collapse_quotes
6
7
  end
7
8
 
@@ -23,7 +23,7 @@ describe RuleRegistry do
23
23
 
24
24
  context "when there are more than two rules" do
25
25
  before do
26
- rule_registry.add_rule('Another rule', proc { pattern /bar/ } )
26
+ rule_registry.add_rule('Another rule', proc { pattern /bar/ })
27
27
  end
28
28
 
29
29
  it "creates a rule with the specified pattern" do
@@ -35,15 +35,24 @@ describe RuleRegistry do
35
35
  describe ".rules" do
36
36
  it "rule is added to the general rules list when language is not set" do
37
37
  rule_registry.add_rule(description, block)
38
- rule = rule_registry.rules[:general].first
38
+ rule = rule_registry.rules[:general]['A new rule']
39
39
  rule.description.should == 'A new rule'
40
40
  end
41
41
 
42
42
  it "rule is added to the language rules list when language is set" do
43
43
  rule_registry.language_context = :ruby
44
44
  rule_registry.add_rule('Another rule', block)
45
- rule = rule_registry.rules[:ruby].first
45
+ rule = rule_registry.rules[:ruby]['Another rule']
46
46
  rule.description.should == 'Another rule'
47
47
  end
48
+
49
+ it "rule overrides previously set rule with same description" do
50
+ rule_registry.language_context = :ruby
51
+ rule1 = rule_registry.add_rule('A rule', block)
52
+ rule2 = rule_registry.add_rule('A rule', block)
53
+ rule = rule_registry.rules[:ruby]['A rule']
54
+ rule1.should_not === rule
55
+ rule2.should === rule
56
+ end
48
57
  end
49
58
  end
data/spec/rule_spec.rb CHANGED
@@ -66,11 +66,17 @@ a = 1
66
66
  ['a','b','c'].each {|item| puts item }
67
67
  ['a','b','c'].each { |item| puts item}
68
68
  b = 2
69
+ def foo(array)
70
+ end
71
+ foo {key: 'value'}
72
+ foo {:key => 'value'}
69
73
  EOF
70
74
  end
71
75
 
72
76
  it { should_have_error description, 2, :ruby }
73
77
  it { should_have_error description, 3, :ruby }
78
+ it { should_not_have_error description, 7, :ruby }
79
+ it { should_not_have_error description, 8, :ruby }
74
80
  end
75
81
 
76
82
  context 'No spaces after (, [ or before ], )' do
@@ -18,9 +18,10 @@ describe WegoLinter do
18
18
  end
19
19
 
20
20
  it "sorts rules into language groups" do
21
- subject[:general].should_not be_empty
22
- subject[:ruby].should_not be_empty
23
- subject[:general].first.should be_a Rule
21
+ subject[:general].should_not be_nil
22
+ subject[:ruby].should_not be_nil
23
+ subject[:general].first[0].should be_a String
24
+ subject[:general].first[1].should be_a Rule
24
25
  end
25
26
  end
26
27
 
data/wegolint.gemspec CHANGED
@@ -1,6 +1,8 @@
1
+ require File.expand_path('../lib/wegolint/version', __FILE__)
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = 'wegolint'
3
- s.version = '0.0.3'
5
+ s.version = WegoLint::VERSION
4
6
  s.date = '2012-09-30'
5
7
  s.summary = 'Parses code for style guide violations'
6
8
  s.description = <<-EOF
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wegolint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -34,9 +34,10 @@ files:
34
34
  - lib/wegolint/dsl.rb
35
35
  - lib/wegolint/rule.rb
36
36
  - lib/wegolint/rule_registry.rb
37
- - lib/wegolint/rules/general.rb
38
- - lib/wegolint/rules/ruby.rb
37
+ - lib/wegolint/version.rb
39
38
  - lib/wegolint/wegolinter.rb
39
+ - rules/general.rb
40
+ - rules/ruby.rb
40
41
  - spec/rule_registry_spec.rb
41
42
  - spec/rule_spec.rb
42
43
  - spec/spec_helper.rb