fashion-police 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/fashion-police +33 -0
- data/lib/fashion-police.rb +196 -0
- data/spec/fashion_police_spec.rb +361 -0
- data/spec/fixtures/bad.js +24 -0
- data/spec/fixtures/good.js +244 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6ad2b24bff44bedebe1b4795721c4e1778aa4c31
|
4
|
+
data.tar.gz: fad809fe3c2e068796c67e2b44901a2a43e9b1c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31d53ece6148dfcfd3e610dcac377956ea9abf2deb1d858455f61b4d62421b32985ae22734438c2da421b425765c8c746809254388571e7772448571b871ccb9
|
7
|
+
data.tar.gz: ea4e644bffc0ca75594eae973bb03855cc15acc00c23ba0aa04f8cfd43eb5041b3af6a40739f6a946759a4ef03efc31793e062d03cc572f6588b7db449f305ed
|
data/bin/fashion-police
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rainbow'
|
5
|
+
require 'fashion-police'
|
6
|
+
|
7
|
+
@fashion_police = FashionPolice.new
|
8
|
+
errors = []
|
9
|
+
|
10
|
+
unstaged = `git diff --name-only`.split("\n")
|
11
|
+
staged = `git diff --name-only --cached`.split("\n")
|
12
|
+
|
13
|
+
files = unstaged + staged
|
14
|
+
|
15
|
+
files.each do |filename|
|
16
|
+
begin
|
17
|
+
@fashion_police.investigate(File.read(filename))
|
18
|
+
rescue FashionPolice::BadCode
|
19
|
+
errors << filename.background(:red)
|
20
|
+
errors << @fashion_police.errors.collect {|element| element.to_s.foreground(:red) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if errors.empty?
|
25
|
+
exit(0)
|
26
|
+
else
|
27
|
+
puts "JavaScript style errors!".foreground(:red)
|
28
|
+
errors.each do |error|
|
29
|
+
puts error
|
30
|
+
end
|
31
|
+
exit(1)
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,196 @@
|
|
1
|
+
class FashionPolice
|
2
|
+
attr_reader :errors
|
3
|
+
attr_accessor :rules
|
4
|
+
|
5
|
+
class SpacesNotTabs
|
6
|
+
def test(string)
|
7
|
+
not string.match(/\t/)
|
8
|
+
end
|
9
|
+
|
10
|
+
def error_message
|
11
|
+
"Use spaces, not tabs"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class FourSpaces
|
16
|
+
def test(string)
|
17
|
+
spaces = string.match(/^(\s+)\S/)
|
18
|
+
return true unless spaces && spaces[1]
|
19
|
+
|
20
|
+
return (spaces[1].length % 4 == 0)
|
21
|
+
end
|
22
|
+
|
23
|
+
def error_message
|
24
|
+
"Four spaces per indentation level"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class SpacesInFunctionDeclarations
|
29
|
+
def test(string)
|
30
|
+
return false if string.match(/function\(\.*\)\{/)
|
31
|
+
return false if string.match(/function\s+\(.*\)\{/)
|
32
|
+
return true if string.match(/function\s+\(.*\)\s+\{/)
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
36
|
+
def error_message
|
37
|
+
"Put spaces in function declarations"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class SpacesAroundElses
|
42
|
+
def test(string)
|
43
|
+
return true if string.match(/} else {/)
|
44
|
+
return false if string.match(/}else{/)
|
45
|
+
return false if string.match(/} +else{/)
|
46
|
+
return false if string.match(/}else +{/)
|
47
|
+
return false if string.match(/} +else ?{/)
|
48
|
+
return false if string.match(/} ?else +{/)
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
|
52
|
+
def error_message
|
53
|
+
"Put spaces around elses"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class SpacesAroundEqualsSigns
|
58
|
+
def test(string)
|
59
|
+
return true if string.match(/ = /)
|
60
|
+
return false if string.match(/\S=[^=]/)
|
61
|
+
return true
|
62
|
+
end
|
63
|
+
|
64
|
+
def error_message
|
65
|
+
"Put spaces around equals signs"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class SpacesAroundArgumentsInForLoops
|
70
|
+
def test(string)
|
71
|
+
return false if string.match(/for\(\S+\s+[^\)]+\)/)
|
72
|
+
return true
|
73
|
+
end
|
74
|
+
|
75
|
+
def error_message
|
76
|
+
"Put spaces around for loop arguments"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class SpacesAroundArgumentsInParens
|
81
|
+
def test(string)
|
82
|
+
return true if string.match(/\( .* \)/)
|
83
|
+
return true if string.match(/\S*\(function\(/)
|
84
|
+
return true if string.match(/\S+\(["'].*["']\)/)
|
85
|
+
return false if string.match(/\(\S+\)/)
|
86
|
+
|
87
|
+
return true
|
88
|
+
end
|
89
|
+
|
90
|
+
def error_message
|
91
|
+
"Put spaces around arguments inside parentheses"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class SpacesBeforeAngleBrackets
|
96
|
+
def test(string)
|
97
|
+
return true if string.match(/(\S+ )?\{/)
|
98
|
+
return false if string.match(/\S\{/)
|
99
|
+
|
100
|
+
return true
|
101
|
+
end
|
102
|
+
|
103
|
+
def error_message
|
104
|
+
"Put spaces in front of angle brackets"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class SpacesAroundArgumentsInAngleBrackets
|
109
|
+
def test(string)
|
110
|
+
return true if string.match(/\{ .* \}/)
|
111
|
+
return false if string.match(/\{[^\}]+\}/)
|
112
|
+
|
113
|
+
return true
|
114
|
+
end
|
115
|
+
|
116
|
+
def error_message
|
117
|
+
"Put spaces around arguments inside angle brackets"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class SpacesAroundArgumentsInSquareBrackets
|
122
|
+
def test(string)
|
123
|
+
return true if string.match(/\[ .* \]/)
|
124
|
+
return false if string.match(/\[[^\]]+\]/)
|
125
|
+
|
126
|
+
return true
|
127
|
+
end
|
128
|
+
|
129
|
+
def error_message
|
130
|
+
"Put spaces around arguments inside square brackets"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
class SpacesAroundArgumentsInFunctionDeclarations
|
135
|
+
def test(string)
|
136
|
+
return true if string.match(/function( .+)?\( [^\)]+ \)/)
|
137
|
+
return false if string.match(/function( .+)?\([^\)]+\)/)
|
138
|
+
return true
|
139
|
+
end
|
140
|
+
|
141
|
+
def error_message
|
142
|
+
"Put spaces around for loop arguments"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
class ColumnWidth
|
147
|
+
def test(string)
|
148
|
+
string.length <= 80
|
149
|
+
end
|
150
|
+
|
151
|
+
def error_message
|
152
|
+
"Maximum text width: 80 characters"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class BadCode < Exception; end
|
157
|
+
|
158
|
+
def initialize
|
159
|
+
@errors = []
|
160
|
+
@rules = [ SpacesNotTabs.new,
|
161
|
+
FourSpaces.new,
|
162
|
+
SpacesInFunctionDeclarations.new,
|
163
|
+
SpacesAroundElses.new,
|
164
|
+
SpacesAroundEqualsSigns.new,
|
165
|
+
SpacesAroundArgumentsInForLoops.new,
|
166
|
+
SpacesAroundArgumentsInParens.new,
|
167
|
+
SpacesBeforeAngleBrackets.new,
|
168
|
+
SpacesAroundArgumentsInAngleBrackets.new,
|
169
|
+
SpacesAroundArgumentsInSquareBrackets.new,
|
170
|
+
SpacesAroundArgumentsInFunctionDeclarations.new,
|
171
|
+
ColumnWidth.new ]
|
172
|
+
end
|
173
|
+
|
174
|
+
def investigate(code)
|
175
|
+
code.split("\n").each_with_index do |line, index|
|
176
|
+
next if comment?(line)
|
177
|
+
line_number = index + 1
|
178
|
+
raise(BadCode) unless permit?(line_number, line)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def comment?(line)
|
183
|
+
line.match(/^ *\/\//)
|
184
|
+
end
|
185
|
+
|
186
|
+
def permit?(line_number, string)
|
187
|
+
@rules.inject(true) do |memo, rule|
|
188
|
+
unless rule.test(string)
|
189
|
+
memo = false
|
190
|
+
@errors << {line_number => rule.error_message}
|
191
|
+
end
|
192
|
+
memo
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
@@ -0,0 +1,361 @@
|
|
1
|
+
require_relative("../lib/fashion-police")
|
2
|
+
|
3
|
+
describe FashionPolice do
|
4
|
+
|
5
|
+
describe "has a very simple set of integration specs which" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@fashion_police = FashionPolice.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "passes good JavaScript through silently" do
|
12
|
+
good = File.dirname(__FILE__) + '/fixtures/good.js'
|
13
|
+
expect(lambda { @fashion_police.investigate(File.read(good)) }).to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "complains about bad JavaScript" do
|
17
|
+
bad = File.dirname(__FILE__) + '/fixtures/bad.js'
|
18
|
+
expect(lambda { @fashion_police.investigate(File.read(bad)) }).to raise_error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "provides a tool which" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
@fashion_police = FashionPolice.new
|
26
|
+
@bad_code = <<-BAD_CODE
|
27
|
+
function (argument) {
|
28
|
+
console.log('wrong indentation style');
|
29
|
+
}
|
30
|
+
BAD_CODE
|
31
|
+
end
|
32
|
+
|
33
|
+
it "detects violations of style rules" do
|
34
|
+
expect( lambda {@fashion_police.investigate(@bad_code)} ).to raise_error(FashionPolice::BadCode)
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "on error" do
|
38
|
+
|
39
|
+
it "keeps track of line number and error message" do
|
40
|
+
@fashion_police.permit?(1, ' function( arg ) {').should be_false
|
41
|
+
@fashion_police.errors.should == [{1 => "Use spaces, not tabs"}]
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "enforces a coding style which" do
|
49
|
+
|
50
|
+
it "uses spaces, not tabs" do
|
51
|
+
FashionPolice::SpacesNotTabs.new.test(" ").should be_false
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "uses four spaces per indent" do
|
55
|
+
|
56
|
+
it "at zero levels in" do
|
57
|
+
FashionPolice::FourSpaces.new.test("function").should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "at 0.5 levels in" do
|
61
|
+
FashionPolice::FourSpaces.new.test(" function").should be_false
|
62
|
+
end
|
63
|
+
|
64
|
+
it "at one level in" do
|
65
|
+
FashionPolice::FourSpaces.new.test(" function").should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "at 1.5 levels in" do
|
69
|
+
FashionPolice::FourSpaces.new.test(" function").should be_false
|
70
|
+
end
|
71
|
+
|
72
|
+
it "at two levels in" do
|
73
|
+
FashionPolice::FourSpaces.new.test(" function").should be_true
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "puts spaces before function definitions" do
|
79
|
+
|
80
|
+
before do
|
81
|
+
@rule = FashionPolice::SpacesInFunctionDeclarations.new
|
82
|
+
end
|
83
|
+
|
84
|
+
it "(positive case)" do
|
85
|
+
@rule.test("function() { return true; }").should be_true
|
86
|
+
end
|
87
|
+
|
88
|
+
it "(negative case)" do
|
89
|
+
@rule.test("function (){ return true; }").should be_false
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
it "allows some leeway for functions invoking anonymous functions" do
|
95
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
96
|
+
@rule.test("foo(function() {});").should be_true
|
97
|
+
end
|
98
|
+
|
99
|
+
it "makes an exception for functions invoking single string literals" do
|
100
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
101
|
+
@rule.test("foo('bar');").should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "puts spaces around arguments in parentheses" do
|
105
|
+
|
106
|
+
before do
|
107
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
108
|
+
end
|
109
|
+
|
110
|
+
it "(positive case)" do
|
111
|
+
@rule.test("function( arg ){ return true; }").should be_true
|
112
|
+
end
|
113
|
+
|
114
|
+
it "(negative case)" do
|
115
|
+
@rule.test("function(arg){ return true; }").should be_false
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "puts spaces around arguments in angle brackets" do
|
121
|
+
|
122
|
+
before do
|
123
|
+
@rule = FashionPolice::SpacesAroundArgumentsInAngleBrackets.new
|
124
|
+
end
|
125
|
+
|
126
|
+
it "(positive case)" do
|
127
|
+
@rule.test("function( arg ){ return true; }").should be_true
|
128
|
+
end
|
129
|
+
|
130
|
+
it "(negative case)" do
|
131
|
+
@rule.test("function( arg ){return true;}").should be_false
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "puts spaces before angle brackets" do
|
137
|
+
|
138
|
+
before do
|
139
|
+
@rule = FashionPolice::SpacesBeforeAngleBrackets.new
|
140
|
+
end
|
141
|
+
|
142
|
+
it "(positive case)" do
|
143
|
+
@rule.test("function( arg ) { return true; }").should be_true
|
144
|
+
end
|
145
|
+
|
146
|
+
it "(negative case)" do
|
147
|
+
@rule.test("function( arg ){ return true; }").should be_true
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
it "allows some leeway when giving a function an object without spaces" do
|
153
|
+
@rule = FashionPolice::SpacesBeforeAngleBrackets.new
|
154
|
+
@rule.test(" foo({").should be_true
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "puts spaces around arguments in square brackets" do
|
158
|
+
|
159
|
+
before do
|
160
|
+
@rule = FashionPolice::SpacesAroundArgumentsInSquareBrackets.new
|
161
|
+
end
|
162
|
+
|
163
|
+
it "(positive case)" do
|
164
|
+
@rule.test("function( arg ){ return [ 0 ]; }").should be_true
|
165
|
+
end
|
166
|
+
|
167
|
+
it "(negative case)" do
|
168
|
+
@rule.test("function( arg ){ return [0]; }").should be_false
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
it "makes an exception for parentheses around square brackets" do
|
174
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
175
|
+
@rule.test("foo([ 'alpha', 'beta' ]);").should be_true
|
176
|
+
end
|
177
|
+
|
178
|
+
describe "puts spaces around args to ifs" do
|
179
|
+
|
180
|
+
before do
|
181
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
182
|
+
end
|
183
|
+
|
184
|
+
it "(positive case)" do
|
185
|
+
@rule.test("if ( condition ) {").should be_true
|
186
|
+
end
|
187
|
+
|
188
|
+
it "(negative case)" do
|
189
|
+
@rule.test("if(condition){").should be_false
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
|
194
|
+
describe "puts spaces around args to ifs" do
|
195
|
+
|
196
|
+
before do
|
197
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
198
|
+
end
|
199
|
+
|
200
|
+
it "(positive case)" do
|
201
|
+
@rule.test("if ( true ) {").should be_true
|
202
|
+
end
|
203
|
+
|
204
|
+
it "(negative case)" do
|
205
|
+
@rule.test("if(true){").should be_false
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "puts spaces around args to whiles" do
|
211
|
+
|
212
|
+
before do
|
213
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
214
|
+
end
|
215
|
+
|
216
|
+
it "(positive case)" do
|
217
|
+
@rule.test("while ( condition ) {").should be_true
|
218
|
+
end
|
219
|
+
|
220
|
+
it "(negative case)" do
|
221
|
+
@rule.test("while(condition){").should be_false
|
222
|
+
end
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "allows hashes, un-preceded by spaces, as arguments to functions" do
|
227
|
+
|
228
|
+
it "(positive case)" do
|
229
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
230
|
+
@rule.test("var fooBar = new FooBar({ a : 'alpha' });").should be_true
|
231
|
+
end
|
232
|
+
|
233
|
+
it "(negative case)" do
|
234
|
+
@rule = FashionPolice::SpacesAroundArgumentsInAngleBrackets.new
|
235
|
+
@rule.test("var fooBar = new FooBar({a : 'alpha'});").should be_false
|
236
|
+
end
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
it "makes an exception for function invocations with parentheses around square brackets" do
|
241
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
242
|
+
@rule.test("foo([ 'alpha', 'beta' ]);").should be_true
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "puts spaces around args to for loops which go through array elements" do
|
246
|
+
|
247
|
+
it "(positive case)" do
|
248
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
249
|
+
@rule.test("for ( prop in object ) {").should be_true
|
250
|
+
end
|
251
|
+
|
252
|
+
it "(negative case)" do
|
253
|
+
@rule = FashionPolice::SpacesAroundArgumentsInForLoops.new
|
254
|
+
@rule.test("for(prop in object){").should be_false
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
describe "puts spaces around args to iterating for loops" do
|
260
|
+
|
261
|
+
it "(positive case)" do
|
262
|
+
@rule = FashionPolice::SpacesAroundArgumentsInParens.new
|
263
|
+
@rule.test("for ( var i = 0; i < 100; i++ ) {").should be_true
|
264
|
+
end
|
265
|
+
|
266
|
+
it "(negative case)" do
|
267
|
+
@rule = FashionPolice::SpacesAroundArgumentsInForLoops.new
|
268
|
+
@rule.test("for(var i=0;i<100;i++) {").should be_false
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
describe "puts spaces around elses" do
|
274
|
+
|
275
|
+
before do
|
276
|
+
@rule = FashionPolice::SpacesAroundElses.new
|
277
|
+
end
|
278
|
+
|
279
|
+
it "(positive case)" do
|
280
|
+
@rule.test("} else {").should be_true
|
281
|
+
end
|
282
|
+
|
283
|
+
it "(negative cases)" do
|
284
|
+
@rule.test("}else{").should be_false
|
285
|
+
@rule.test("} else{").should be_false
|
286
|
+
@rule.test("}else {").should be_false
|
287
|
+
@rule.test("} else {").should be_false
|
288
|
+
@rule.test("} else {").should be_false
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
describe "puts spaces around anonymous function args" do
|
294
|
+
|
295
|
+
before do
|
296
|
+
@rule = FashionPolice::SpacesAroundArgumentsInFunctionDeclarations.new
|
297
|
+
end
|
298
|
+
|
299
|
+
it "(positive case)" do
|
300
|
+
@rule.test("var square = function( number ) {").should be_true
|
301
|
+
end
|
302
|
+
|
303
|
+
it "(negative cases)" do
|
304
|
+
@rule.test("var square = function(number){").should be_false
|
305
|
+
end
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
describe "puts spaces around named function args" do
|
310
|
+
|
311
|
+
before do
|
312
|
+
@rule = FashionPolice::SpacesAroundArgumentsInFunctionDeclarations.new
|
313
|
+
end
|
314
|
+
|
315
|
+
it "(positive cases)" do
|
316
|
+
@rule.test("var factorial = function factorial( number ) {").should be_true
|
317
|
+
@rule.test("function FooBar( options ) {").should be_true
|
318
|
+
end
|
319
|
+
|
320
|
+
it "(negative cases)" do
|
321
|
+
@rule.test("var factorial = function factorial(number){").should be_false
|
322
|
+
end
|
323
|
+
|
324
|
+
end
|
325
|
+
|
326
|
+
describe "puts spaces around equals signs" do
|
327
|
+
|
328
|
+
before do
|
329
|
+
@rule = FashionPolice::SpacesAroundEqualsSigns.new
|
330
|
+
end
|
331
|
+
|
332
|
+
it "(positive case)" do
|
333
|
+
@rule.test("var square = function( number ) {").should be_true
|
334
|
+
end
|
335
|
+
|
336
|
+
it "(negative cases)" do
|
337
|
+
@rule.test("var square=function( number ){").should be_false
|
338
|
+
end
|
339
|
+
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|
343
|
+
|
344
|
+
describe "limits column width to 80 characters" do
|
345
|
+
|
346
|
+
before do
|
347
|
+
@rule = FashionPolice::ColumnWidth.new
|
348
|
+
end
|
349
|
+
|
350
|
+
it "(positive case)" do
|
351
|
+
@rule.test("sdfasdfasdf").should be_true
|
352
|
+
end
|
353
|
+
|
354
|
+
it "(negative case)" do
|
355
|
+
too_big = " "
|
356
|
+
@rule.test(too_big).should be_false
|
357
|
+
end
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
// 2.A.1.1
|
2
|
+
// Examples of really cramped syntax
|
3
|
+
|
4
|
+
if(condition) doSomething();
|
5
|
+
|
6
|
+
while(condition) iterating++;
|
7
|
+
|
8
|
+
for(var i=0;i<100;i++) someIterativeFn();
|
9
|
+
|
10
|
+
// Bad
|
11
|
+
var foo = "";
|
12
|
+
var bar = "";
|
13
|
+
var qux;
|
14
|
+
|
15
|
+
|
16
|
+
// Bad
|
17
|
+
function foo() {
|
18
|
+
|
19
|
+
// some statements here
|
20
|
+
|
21
|
+
var bar = "",
|
22
|
+
qux;
|
23
|
+
}
|
24
|
+
|
@@ -0,0 +1,244 @@
|
|
1
|
+
// 2.A.1.1
|
2
|
+
// Use whitespace to promote readability
|
3
|
+
|
4
|
+
if ( condition ) {
|
5
|
+
// statements
|
6
|
+
}
|
7
|
+
|
8
|
+
while ( condition ) {
|
9
|
+
// statements
|
10
|
+
}
|
11
|
+
|
12
|
+
for ( var i = 0; i < 100; i++ ) {
|
13
|
+
// statements
|
14
|
+
}
|
15
|
+
|
16
|
+
// Even better:
|
17
|
+
|
18
|
+
var i,
|
19
|
+
length = 100;
|
20
|
+
|
21
|
+
for ( i = 0; i < length; i++ ) {
|
22
|
+
// statements
|
23
|
+
}
|
24
|
+
|
25
|
+
// Or...
|
26
|
+
|
27
|
+
var i = 0,
|
28
|
+
length = 100;
|
29
|
+
|
30
|
+
for ( ; i < length; i++ ) {
|
31
|
+
// statements
|
32
|
+
}
|
33
|
+
|
34
|
+
var prop;
|
35
|
+
|
36
|
+
for ( prop in object ) {
|
37
|
+
// statements
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
if ( true ) {
|
42
|
+
// statements
|
43
|
+
} else {
|
44
|
+
// statements
|
45
|
+
}
|
46
|
+
|
47
|
+
// 2.B.1.1
|
48
|
+
// Variables
|
49
|
+
var foo = "bar",
|
50
|
+
num = 1,
|
51
|
+
undef;
|
52
|
+
|
53
|
+
// Literal notations:
|
54
|
+
var array = [],
|
55
|
+
object = {};
|
56
|
+
|
57
|
+
|
58
|
+
// 2.B.1.2
|
59
|
+
// Using only one `var` per scope (function) promotes readability
|
60
|
+
// and keeps your declaration list free of clutter (also saves a few keystrokes)
|
61
|
+
|
62
|
+
// Bad
|
63
|
+
var foo = "";
|
64
|
+
var bar = "";
|
65
|
+
var qux;
|
66
|
+
|
67
|
+
// Good
|
68
|
+
var foo = "",
|
69
|
+
bar = "",
|
70
|
+
quux;
|
71
|
+
|
72
|
+
// or..
|
73
|
+
var // Comment on these
|
74
|
+
foo = "",
|
75
|
+
bar = "",
|
76
|
+
quux;
|
77
|
+
|
78
|
+
// 2.B.1.3
|
79
|
+
// var statements should always be in the beginning of their respective scope (function).
|
80
|
+
// Same goes for const and let from ECMAScript 6.
|
81
|
+
|
82
|
+
// Good
|
83
|
+
function foo() {
|
84
|
+
var bar = "",
|
85
|
+
qux;
|
86
|
+
|
87
|
+
// all statements after the variables declarations.
|
88
|
+
}
|
89
|
+
|
90
|
+
// 2.B.2.1
|
91
|
+
// Named Function Declaration
|
92
|
+
function foo( arg1, argN ) {
|
93
|
+
|
94
|
+
}
|
95
|
+
|
96
|
+
// Usage
|
97
|
+
foo( arg1, argN );
|
98
|
+
|
99
|
+
|
100
|
+
// 2.B.2.2
|
101
|
+
// Named Function Declaration
|
102
|
+
function square( number ) {
|
103
|
+
return number * number;
|
104
|
+
}
|
105
|
+
|
106
|
+
// Usage
|
107
|
+
square( 10 );
|
108
|
+
|
109
|
+
// Really contrived continuation passing style
|
110
|
+
function square( number, callback ) {
|
111
|
+
callback( number * number );
|
112
|
+
}
|
113
|
+
|
114
|
+
square( 10, function( square ) {
|
115
|
+
// callback statements
|
116
|
+
});
|
117
|
+
|
118
|
+
|
119
|
+
// 2.B.2.3
|
120
|
+
// Function Expression
|
121
|
+
var square = function( number ) {
|
122
|
+
// Return something valuable and relevant
|
123
|
+
return number * number;
|
124
|
+
};
|
125
|
+
|
126
|
+
// Function Expression with Identifier
|
127
|
+
// This preferred form has the added value of being
|
128
|
+
// able to call itself and have an identity in stack traces:
|
129
|
+
var factorial = function factorial( number ) {
|
130
|
+
if ( number < 2 ) {
|
131
|
+
return 1;
|
132
|
+
}
|
133
|
+
|
134
|
+
return number * factorial( number - 1 );
|
135
|
+
};
|
136
|
+
|
137
|
+
|
138
|
+
// 2.B.2.4
|
139
|
+
// Constructor Declaration
|
140
|
+
function FooBar( options ) {
|
141
|
+
|
142
|
+
this.options = options;
|
143
|
+
}
|
144
|
+
|
145
|
+
// Usage
|
146
|
+
var fooBar = new FooBar({ a: "alpha" });
|
147
|
+
|
148
|
+
fooBar.options;
|
149
|
+
// { a: "alpha" }
|
150
|
+
|
151
|
+
// 2.C.1.1
|
152
|
+
// Functions with callbacks
|
153
|
+
foo(function() {
|
154
|
+
// Note there is no extra space between the first paren
|
155
|
+
// of the executing function call and the word "function"
|
156
|
+
});
|
157
|
+
|
158
|
+
// Function accepting an array, no space
|
159
|
+
foo([ "alpha", "beta" ]);
|
160
|
+
|
161
|
+
// 2.C.1.2
|
162
|
+
// Function accepting an object, no space
|
163
|
+
foo({
|
164
|
+
a: "alpha",
|
165
|
+
b: "beta"
|
166
|
+
});
|
167
|
+
|
168
|
+
// Single argument string literal, no space
|
169
|
+
foo("bar");
|
170
|
+
|
171
|
+
// Inner grouping parens, no space
|
172
|
+
if ( !("foo" in obj) ) {
|
173
|
+
|
174
|
+
}
|
175
|
+
|
176
|
+
// 5.1.1
|
177
|
+
// A Practical Module
|
178
|
+
|
179
|
+
(function( global ) {
|
180
|
+
var Module = (function() {
|
181
|
+
|
182
|
+
var data = "secret";
|
183
|
+
|
184
|
+
return {
|
185
|
+
// This is some boolean property
|
186
|
+
bool: true,
|
187
|
+
// Some string value
|
188
|
+
string: "a string",
|
189
|
+
// An array property
|
190
|
+
array: [ 1, 2, 3, 4 ],
|
191
|
+
// An object property
|
192
|
+
object: {
|
193
|
+
lang: "en-Us"
|
194
|
+
},
|
195
|
+
getData: function() {
|
196
|
+
// get the current value of `data`
|
197
|
+
return data;
|
198
|
+
},
|
199
|
+
setData: function( value ) {
|
200
|
+
// set the value of `data` and return it
|
201
|
+
return ( data = value );
|
202
|
+
}
|
203
|
+
};
|
204
|
+
})();
|
205
|
+
|
206
|
+
// Other things might happen here
|
207
|
+
|
208
|
+
// expose our module to the global object
|
209
|
+
global.Module = Module;
|
210
|
+
|
211
|
+
})( this );
|
212
|
+
|
213
|
+
// 5.2.1
|
214
|
+
// A Practical Constructor
|
215
|
+
|
216
|
+
(function( global ) {
|
217
|
+
|
218
|
+
function Ctor( foo ) {
|
219
|
+
|
220
|
+
this.foo = foo;
|
221
|
+
|
222
|
+
return this;
|
223
|
+
}
|
224
|
+
|
225
|
+
Ctor.prototype.getFoo = function() {
|
226
|
+
return this.foo;
|
227
|
+
};
|
228
|
+
|
229
|
+
Ctor.prototype.setFoo = function( val ) {
|
230
|
+
return ( this.foo = val );
|
231
|
+
};
|
232
|
+
|
233
|
+
|
234
|
+
// To call constructor's without `new`, you might do this:
|
235
|
+
var ctor = function( foo ) {
|
236
|
+
return new Ctor( foo );
|
237
|
+
};
|
238
|
+
|
239
|
+
|
240
|
+
// expose our constructor to the global object
|
241
|
+
global.ctor = ctor;
|
242
|
+
|
243
|
+
})( this );
|
244
|
+
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fashion-police
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Giles Bowkett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rainbow
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Style guide all the things.
|
42
|
+
email:
|
43
|
+
- gilesb@gmail.com
|
44
|
+
executables:
|
45
|
+
- fashion-police
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- lib/fashion-police.rb
|
50
|
+
- bin/fashion-police
|
51
|
+
- spec/fashion_police_spec.rb
|
52
|
+
- spec/fixtures/bad.js
|
53
|
+
- spec/fixtures/good.js
|
54
|
+
homepage: ''
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message: |2+
|
59
|
+
|
60
|
+
=====================================================================
|
61
|
+
To enforce JS code style:
|
62
|
+
rm $(git rev-parse --git-dir)/hooks/pre-commit.sample
|
63
|
+
echo 'fashion-police' > $(git rev-parse --git-dir)/hooks/pre-commit
|
64
|
+
chmod 0755 $(git rev-parse --git-dir)/hooks/pre-commit
|
65
|
+
=====================================================================
|
66
|
+
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project: fashion-police
|
82
|
+
rubygems_version: 2.0.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Enforce Idiomatic.js via pre-commit hook.
|
86
|
+
test_files:
|
87
|
+
- spec/fashion_police_spec.rb
|
88
|
+
- spec/fixtures/bad.js
|
89
|
+
- spec/fixtures/good.js
|