fdlint 0.1.3 → 0.2.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -9
- data/Gemfile.lock +14 -4
- data/Rakefile +9 -85
- data/bin/fdlint +71 -8
- data/lib/fdlint/cli.rb +102 -0
- data/lib/{file_validator.rb → fdlint/file_validator.rb} +0 -0
- data/lib/fdlint/helper/code_type.rb +55 -0
- data/lib/fdlint/helper/file_reader.rb +19 -0
- data/lib/fdlint/helper/logger.rb +35 -0
- data/lib/fdlint/log_entry.rb +51 -0
- data/lib/fdlint/parser/base_parser.rb +151 -0
- data/lib/{css/parser.rb → fdlint/parser/css/css_parser.rb} +31 -28
- data/lib/{css → fdlint/parser/css}/struct.rb +5 -5
- data/lib/{encoding_error.rb → fdlint/parser/encoding_error.rb} +2 -2
- data/lib/{html/parser.rb → fdlint/parser/html/html_parser.rb} +26 -20
- data/lib/{html → fdlint/parser/html}/query.rb +0 -0
- data/lib/{html → fdlint/parser/html}/rule/check_tag_rule.rb +0 -0
- data/lib/{html → fdlint/parser/html}/struct.rb +82 -38
- data/lib/fdlint/parser/js/expr/expr.rb +71 -0
- data/lib/fdlint/parser/js/expr/left_hand.rb +65 -0
- data/lib/fdlint/parser/js/expr/operate.rb +94 -0
- data/lib/fdlint/parser/js/expr/primary.rb +168 -0
- data/lib/{js/parser.rb → fdlint/parser/js/js_parser.rb} +11 -9
- data/lib/fdlint/parser/js/stat/if.rb +27 -0
- data/lib/fdlint/parser/js/stat/iter.rb +88 -0
- data/lib/fdlint/parser/js/stat/stat.rb +120 -0
- data/lib/fdlint/parser/js/stat/switch.rb +67 -0
- data/lib/fdlint/parser/js/stat/try.rb +30 -0
- data/lib/fdlint/parser/js/stat/var.rb +42 -0
- data/lib/fdlint/parser/js/struct.rb +257 -0
- data/lib/fdlint/parser/node.rb +27 -0
- data/lib/fdlint/parser/parse_error.rb +10 -0
- data/lib/fdlint/parser/parser_visitable.rb +134 -0
- data/lib/{position_info.rb → fdlint/parser/position_info.rb} +12 -1
- data/lib/fdlint/parser.rb +3 -0
- data/lib/fdlint/printer/base_printer.rb +20 -0
- data/lib/fdlint/printer/console_printer.rb +80 -0
- data/lib/fdlint/printer/nocolor_printer.rb +29 -0
- data/lib/fdlint/printer/vim_printer.rb +21 -0
- data/lib/fdlint/printer.rb +1 -0
- data/lib/fdlint/rule/dsl.rb +83 -0
- data/lib/fdlint/rule/validation.rb +79 -0
- data/lib/fdlint/rule.rb +81 -0
- data/lib/fdlint/support/core/array.rb +5 -0
- data/lib/fdlint/support/core/file.rb +8 -0
- data/lib/fdlint/support/core/hash.rb +5 -0
- data/lib/fdlint/support/core/nil.rb +7 -0
- data/lib/fdlint/support/core/string.rb +7 -0
- data/lib/fdlint/support/core_ext.rb +5 -0
- data/lib/fdlint/validator.rb +102 -0
- data/lib/fdlint/version.rb +3 -0
- data/lib/fdlint.rb +5 -0
- data/rules.d/css.rule.rb +100 -0
- data/rules.d/filename.rule.rb +49 -0
- data/rules.d/html.rule.rb +169 -0
- data/rules.d/js.jquery.rule.rb +49 -0
- data/rules.d/js.rule.rb +205 -0
- data/test/default_test.rb +14 -0
- data/test/fixtures/js/scope-test.js +15 -2
- data/test/test_helper.rb +9 -0
- metadata +269 -221
- data/lib/base_parser.rb +0 -143
- data/lib/cmd_runner.rb +0 -145
- data/lib/context.rb +0 -31
- data/lib/css/reader.rb +0 -30
- data/lib/css/rule/check_compression_rule.rb +0 -48
- data/lib/css/rule/checklist.rb +0 -45
- data/lib/helper/code_type.rb +0 -50
- data/lib/helper/color_string.rb +0 -44
- data/lib/helper/file_reader.rb +0 -22
- data/lib/helper/strenc.rb +0 -65
- data/lib/js/expr/expr.rb +0 -66
- data/lib/js/expr/left_hand.rb +0 -63
- data/lib/js/expr/operate.rb +0 -92
- data/lib/js/expr/primary.rb +0 -166
- data/lib/js/rule/all.rb +0 -35
- data/lib/js/rule/checklist.rb +0 -41
- data/lib/js/rule/file_checker.rb +0 -42
- data/lib/js/rule/helper.rb +0 -96
- data/lib/js/rule/no_global.rb +0 -87
- data/lib/js/stat/if.rb +0 -25
- data/lib/js/stat/iter.rb +0 -85
- data/lib/js/stat/stat.rb +0 -117
- data/lib/js/stat/switch.rb +0 -65
- data/lib/js/stat/try.rb +0 -28
- data/lib/js/stat/var.rb +0 -40
- data/lib/js/struct.rb +0 -248
- data/lib/log_entry.rb +0 -49
- data/lib/node.rb +0 -28
- data/lib/parse_error.rb +0 -13
- data/lib/parser_visitable.rb +0 -138
- data/lib/printer/base_printer.rb +0 -24
- data/lib/printer/console_printer.rb +0 -66
- data/lib/printer/nocolor_printer.rb +0 -27
- data/lib/printer/vim_printer.rb +0 -19
- data/lib/rule.rb +0 -241
- data/lib/rule_helper.rb +0 -14
- data/lib/runner.rb +0 -225
- data/rules.d/css.rule +0 -127
- data/rules.d/html.dtd.rule +0 -22
- data/rules.d/html.prop.rule +0 -51
- data/rules.d/html.tag.rule +0 -136
- data/rules.d/js.file.rule +0 -13
- data/rules.d/js.jquery.rule +0 -56
- data/rules.d/js.mergefile.rule +0 -71
- data/rules.d/js.rule +0 -84
data/rules.d/js.rule.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# vi: filetype=ruby
|
3
|
+
|
4
|
+
js_rules
|
5
|
+
|
6
|
+
review( 'file' ) {
|
7
|
+
desc "版权或维护信息在头部注释中声明"
|
8
|
+
rule { |file, source|
|
9
|
+
unless source =~ %r{^\s*
|
10
|
+
/\*\*
|
11
|
+
[^*]*
|
12
|
+
\*+
|
13
|
+
(?:[^/*][^*]*\*+)
|
14
|
+
*/}x
|
15
|
+
warn '功能文件头必须有文档注释(以/**开始的多行注释)'
|
16
|
+
end
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
review( 'statement' ) { |stat|
|
21
|
+
ary = %w(empty var continue break return throw expression)
|
22
|
+
if ary.include?(stat.type) && !stat.end_with_semicolon?
|
23
|
+
error '所有语句结束带上分号'
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
review( 'stat_if' ) {
|
28
|
+
|
29
|
+
rule { |stat|
|
30
|
+
# if (condition1) state1; <--- true_part
|
31
|
+
# else if (condition2) state2; <--- false_part of condition1
|
32
|
+
# true_part of condition2
|
33
|
+
# else state3; <--- false_part of condition2
|
34
|
+
if stat.true_part.type != 'block' ||
|
35
|
+
stat.false_part && stat.false_part.type != 'if' && stat.false_part.type != 'block'
|
36
|
+
error '所有条件区域必须用花括号括起来'
|
37
|
+
end
|
38
|
+
}
|
39
|
+
|
40
|
+
rule { |stat|
|
41
|
+
count = 0;
|
42
|
+
# count "else if"
|
43
|
+
while stat.false_part && stat.false_part.type == 'if'
|
44
|
+
count += 1
|
45
|
+
stat = stat.false_part
|
46
|
+
end
|
47
|
+
# count "else"
|
48
|
+
count += 1 if stat.false_part
|
49
|
+
if count >= 3
|
50
|
+
error '3个条件及以上的条件语句用switch代替if else'
|
51
|
+
end
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
review( 'expr_new' ) { |expr|
|
56
|
+
expr = expr.left
|
57
|
+
if expr.type == '('
|
58
|
+
args = expr.right.elements
|
59
|
+
expr = expr.left
|
60
|
+
end
|
61
|
+
|
62
|
+
if args.nil? || args.empty?
|
63
|
+
if expr.text == 'Object'
|
64
|
+
error '使用{}代替new Object()'
|
65
|
+
elsif expr.text == 'Array'
|
66
|
+
error '使用[]代替new Array()'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
}
|
70
|
+
|
71
|
+
review( 'expression' ) {
|
72
|
+
|
73
|
+
rule { |expr|
|
74
|
+
checks = %w(
|
75
|
+
(.,window,eval)
|
76
|
+
eval
|
77
|
+
)
|
78
|
+
if expr && expr.left && checks.include?(expr.left.text)
|
79
|
+
error '不允许使用eval'
|
80
|
+
end
|
81
|
+
}
|
82
|
+
|
83
|
+
rule { |expr|
|
84
|
+
if expr.left && expr.left.text == 'alert'
|
85
|
+
warn '必须去掉临时调试代码。如果一定要使用alert功能,请使用 window.alert'
|
86
|
+
end
|
87
|
+
}
|
88
|
+
|
89
|
+
rule { |expr|
|
90
|
+
if expr.left
|
91
|
+
if expr.left.text =~ /(?<!this|self),_/
|
92
|
+
error '禁止调用对象的私有方法'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
review( 'expr_equal' ) { |expr|
|
100
|
+
if expr.type == '==' || expr.type == '!='
|
101
|
+
warn '避免使用==和!=操作符'
|
102
|
+
end
|
103
|
+
}
|
104
|
+
|
105
|
+
review( 'stat' ) { |stat|
|
106
|
+
if stat.try_part.contains?('try') ||
|
107
|
+
stat.catch_part && (stat.catch_part.contains? 'try') ||
|
108
|
+
stat.finally_part && (stat.finally_part.contains? 'try')
|
109
|
+
warn 'try catch一般不允许嵌套,若嵌套,需要充分的理由'
|
110
|
+
end
|
111
|
+
}
|
112
|
+
|
113
|
+
# 检查是否有全局变量、全局函数
|
114
|
+
group( 'no global variables or functions' ) {
|
115
|
+
|
116
|
+
scope_depth = 0
|
117
|
+
scoped_vars = []
|
118
|
+
|
119
|
+
before( 'program' ) { scope_depth = 0 }
|
120
|
+
before( 'function_declaration' ) { scope_depth += 1 }
|
121
|
+
|
122
|
+
after( 'function_parameters' ) { |params|
|
123
|
+
scoped_vars << params.elements.map { |expr|
|
124
|
+
if expr.type == "id"
|
125
|
+
param_name = expr.left.text
|
126
|
+
end
|
127
|
+
}.compact
|
128
|
+
}
|
129
|
+
|
130
|
+
after( 'function_declaration' ) { |function|
|
131
|
+
scoped_vars.pop
|
132
|
+
scope_depth -= 1
|
133
|
+
}
|
134
|
+
|
135
|
+
review( 'expr_assignment' ) { |expr|
|
136
|
+
if expr.type == "="
|
137
|
+
expr = expr.left
|
138
|
+
if expr.type == "id" && !scoped_vars.flatten.include?( expr.left.text )
|
139
|
+
error '禁止使用未定义的变量(或全局变量)'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
}
|
143
|
+
|
144
|
+
review( 'stat_var_declaration' ) { |stat|
|
145
|
+
error "禁止使用未定义的变量(或全局变量)" if scope_depth <= 0
|
146
|
+
}
|
147
|
+
|
148
|
+
review( 'function_name' ) { |name|
|
149
|
+
error '不允许申明全局函数' if scope_depth <= 1
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
# bad case: x_y, x_Y, X_y, _Xy
|
154
|
+
# good case: X_Y, _xy, _xY
|
155
|
+
group( '变量名的命名规范' ) {
|
156
|
+
review( 'stat_var_declaration' ) { |stat|
|
157
|
+
var_name = stat.left.text.sub(/^_+/, '')
|
158
|
+
if var_name =~ /[a-z]/ && var_name =~ /[A-Z]/ && var_name =~ /[^_]_/
|
159
|
+
error '成员函数,成员对象,局部变量采用首字母小写的驼峰格式'
|
160
|
+
end
|
161
|
+
}
|
162
|
+
review( 'expr_assignment' ) { |expr|
|
163
|
+
if expr.type == "="
|
164
|
+
var_name = expr.left.text.sub(/^_+/, '')
|
165
|
+
if var_name =~ /[a-z]/ && var_name =~ /[A-Z]/ && var_name =~ /[^_]_/
|
166
|
+
error '成员函数,成员对象,局部变量采用首字母小写的驼峰格式'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
|
173
|
+
review( 'expr_literal_string' ) { |string|
|
174
|
+
if string.text.start_with? '"'
|
175
|
+
warn '尽可能使用单引号,而不是双引号'
|
176
|
+
end
|
177
|
+
}
|
178
|
+
|
179
|
+
review( 'statement' ) { |stat, source, file, parser|
|
180
|
+
line = parser.scanned_source.lines.last
|
181
|
+
if line =~ /^\t+/
|
182
|
+
error '不要使用 tab 来表示缩进'
|
183
|
+
elsif line =~ /^[ ]{1,3}\S/
|
184
|
+
error '应该使用连续的4个空白字符表示缩进'
|
185
|
+
end
|
186
|
+
}
|
187
|
+
|
188
|
+
review( 'expr_logical_or' ) { |expr, source|
|
189
|
+
if expr.left && expr.right
|
190
|
+
left = expr.position.pos - 2
|
191
|
+
unless expr.type == "(" || expr.type == "."
|
192
|
+
unless source[left, expr.type.size + 2] == (" " << expr.type << " ")
|
193
|
+
error '操作符(如, +/-/*/% 等)两边留空'
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
}
|
198
|
+
|
199
|
+
before( 'function_parameters' ) { |params, source, file, parser|
|
200
|
+
if parser.check /[^)]*\)\{/
|
201
|
+
warn "函数定义的'{'前应该留一个空格",
|
202
|
+
pos: parser.scanner_pos,
|
203
|
+
column_offset: parser.rest_source.index('){')
|
204
|
+
end
|
205
|
+
}
|
@@ -6,11 +6,22 @@
|
|
6
6
|
var temp;
|
7
7
|
}
|
8
8
|
|
9
|
-
var _createSeperator = function(){
|
9
|
+
var _createSeperator = function( temp4, data, time, scope ){
|
10
10
|
var _self = this,
|
11
11
|
menuEl = _self.get('menuEl');
|
12
|
-
//
|
12
|
+
//tempӦ��Ҫ����ȷ�ؼ��Ϊȫ�ֱ���
|
13
13
|
temp = [a,b,c];
|
14
|
+
|
15
|
+
// $ �ڸ��ռ��ж��壬����ȫ�ֱ���
|
16
|
+
$ = jQuery;
|
17
|
+
|
18
|
+
// temp3 ���·����壬����ȫ�ֱ���
|
19
|
+
temp3 = "foo";
|
20
|
+
var temp3;
|
21
|
+
|
22
|
+
// temp4 �Ǻ�������������ȫ�ֱ���
|
23
|
+
temp4 = "bar";
|
24
|
+
|
14
25
|
return new Node(temp).appendTo(menuEl);
|
15
26
|
|
16
27
|
test = 123;
|
@@ -19,4 +30,6 @@
|
|
19
30
|
temp2 = 234;
|
20
31
|
};
|
21
32
|
|
33
|
+
$.data;
|
34
|
+
|
22
35
|
})(jQuery);
|