rstyle 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rstyle ADDED
@@ -0,0 +1 @@
1
+ --all
data/bin/rstyle CHANGED
@@ -36,7 +36,15 @@ require "rstyle"
36
36
 
37
37
  trap("INT") { puts "Exiting..."; exit(0) }
38
38
 
39
- s = Rstyle.new
39
+ RSTYLE_FILE = ".rstyle"
40
+ lines = []
41
+ if File.exist?(RSTYLE_FILE)
42
+ File.open(RSTYLE_FILE) do |f|
43
+ lines = f.readlines.collect {|l| l.chomp}
44
+ end
45
+ end
46
+
47
+ s = Rstyle.new([lines, ARGV].flatten)
40
48
 
41
49
  if ARGV.length == 0
42
50
  files = []
@@ -45,7 +53,7 @@ if ARGV.length == 0
45
53
  $stderr.puts "failed to execute 'git status'"
46
54
  exit 2
47
55
  end
48
- if line =~ /^[MA].\s(.+)\S*$/
56
+ if line =~ /^\s*[MA].*\s(.+\.rb)\S*$/
49
57
  files << $1
50
58
  end
51
59
  end
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module Rstyle
16
- VERSION = "0.0.1"
16
+ VERSION = "0.0.2"
17
17
  end
data/lib/rstyle.rb CHANGED
@@ -12,14 +12,32 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require "trollop"
16
+
15
17
  class Rstyle
16
18
  attr_reader :line_count, :errors, :warnings
17
19
 
18
- def initialize
20
+ def initialize(options)
21
+ @options = parse_options(options)
19
22
  @errors = 0
20
23
  @warnings = 0
21
24
  end
22
25
 
26
+ def parse_options(options)
27
+ Trollop::options(options) do
28
+ opt :all, "enable all options"
29
+ opt :empty_line, "empty lines"
30
+ opt :tabs, "line contains tab(s)"
31
+ opt :ends_with_whitespace, "line ends with whitespace"
32
+ opt :no_space_after_comma, "no space after ','"
33
+ opt :space_after, "space after ( and [ or before ) and ]"
34
+ opt :two_spaces, "use two spaces before statement modifiers"
35
+ opt :snake_case, "methods should be in snake_case"
36
+ opt :no_for, "don't use for unless you know what you are doing"
37
+ opt :line_length, "line length", :default => 80
38
+ end
39
+ end
40
+
23
41
  def source(files)
24
42
  files.each do |file|
25
43
  if File.directory?(file)
@@ -55,28 +73,29 @@ class Rstyle
55
73
  # strip out text from regexps
56
74
  @line = @line.gsub(/\/([^\/]+)\//, "//")
57
75
 
58
- if @oline.length > 80
59
- error "line longer than 80 characters"
76
+ if @oline.length > @options[:line_length]
77
+ error "line longer than #{@options[:line_length]} characters"
60
78
  end
61
79
 
62
- check "empty line contains whitespace", /^\s+$/
80
+ check :empty_line, "empty line contains whitespace", /^\s+$/
63
81
 
64
- check "line contains tab(s)", /\t/
82
+ check :tabs, "line contains tab(s)", /\t/
65
83
 
66
- check "line ends with whitespace", /\S+\s+$/
84
+ check :ends_with_whitespace, "line ends with whitespace", /\S+\s+$/
67
85
 
68
- check "no space after ,", /,\S+/
86
+ check :no_space_after_comma, "no space after ,", /,\S+/
69
87
 
70
- check "space after ( and [ or before ) and ]", /[\(\[]\s+|\s+[\)\]]/
88
+ check :space_after, "space after ( and [ or before ) and ]",
89
+ /[\(\[]\s+|\s+[\)\]]/
71
90
 
72
- check "use two spaces before statement modifiers",
73
- /\S+( | {3,})(if|unless|until|rescue|while)/
91
+ check :two_spaces, "use two spaces before statement modifiers",
92
+ /\S+( | {3,})(if|unless|until|rescue|while)/
74
93
 
75
- check(/\s*def (\S+)/) do |method|
94
+ check(:snake_case, /\s*def (\S+)/) do |method|
76
95
  error "methods should be in snake_case" if method =~ /[A-Z]/
77
96
  end
78
97
 
79
- check(/^\s*for/) do
98
+ check(:no_for, /^\s*for/) do
80
99
  warning "don't use for unless you know what you are doing"
81
100
  end
82
101
  end
@@ -84,12 +103,14 @@ class Rstyle
84
103
 
85
104
  private
86
105
 
87
- def check(*args)
88
- if @line =~ args[args.length - 1]
89
- if block_given?
90
- yield($1)
91
- else
92
- error(args[0])
106
+ def check(name, *args)
107
+ if @options[name] || @options[:all]
108
+ if @line =~ args[args.length - 1]
109
+ if block_given?
110
+ yield($1)
111
+ else
112
+ error(args[0])
113
+ end
93
114
  end
94
115
  end
95
116
  end
data/rstyle.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_runtime_dependency "highline"
23
+ s.add_runtime_dependency "trollop"
23
24
 
24
25
  s.add_development_dependency "rspec"
25
26
  end
@@ -0,0 +1,7 @@
1
+ class Test
2
+ def foo
3
+ bar
4
+
5
+ def FooBar
6
+ end
7
+ end
data/spec/rstyle_spec.rb CHANGED
@@ -18,7 +18,7 @@ require 'rstyle'
18
18
  describe Rstyle do
19
19
 
20
20
  before(:each) do
21
- @s = Rstyle.new
21
+ @s = Rstyle.new(["--all"])
22
22
  @s.stub(:e)
23
23
  end
24
24
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rstyle
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Martin Englund
@@ -25,7 +25,7 @@ dependencies:
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: trollop
29
29
  prerelease: false
30
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
31
  none: false
@@ -33,8 +33,19 @@ dependencies:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: "0"
36
- type: :development
36
+ type: :runtime
37
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
38
49
  description: Style checking for Ruby code
39
50
  email:
40
51
  - martin@englund.nu
@@ -48,6 +59,7 @@ files:
48
59
  - .autotest
49
60
  - .gitignore
50
61
  - .rspec
62
+ - .rstyle
51
63
  - Gemfile
52
64
  - LICENSE
53
65
  - README.md
@@ -56,6 +68,7 @@ files:
56
68
  - lib/rstyle.rb
57
69
  - lib/rstyle/version.rb
58
70
  - rstyle.gemspec
71
+ - spec/assets/test.rb
59
72
  - spec/rstyle_spec.rb
60
73
  - spec/spec_helper.rb
61
74
  has_rdoc: true
@@ -87,5 +100,6 @@ signing_key:
87
100
  specification_version: 3
88
101
  summary: Style checking for Ruby code
89
102
  test_files:
103
+ - spec/assets/test.rb
90
104
  - spec/rstyle_spec.rb
91
105
  - spec/spec_helper.rb