fdlint 0.2.0.pre → 0.2.0
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +36 -15
- data/Rakefile +1 -1
- data/bin/fdlint +30 -9
- data/lib/fdlint/cli.rb +62 -10
- data/lib/fdlint/helper/logger.rb +1 -1
- data/lib/fdlint/log_entry.rb +14 -0
- data/lib/fdlint/parser/html/html_parser.rb +10 -12
- data/lib/fdlint/parser/html/query.rb +64 -50
- data/lib/fdlint/parser/html/struct.rb +20 -3
- data/lib/fdlint/parser/parser_visitable.rb +11 -5
- data/lib/fdlint/printer/base_printer.rb +6 -0
- data/lib/fdlint/printer/console_printer.rb +15 -23
- data/lib/fdlint/rule.rb +4 -4
- data/lib/fdlint/support/core/object.rb +6 -0
- data/lib/fdlint/support/core_ext.rb +1 -0
- data/lib/fdlint/validator.rb +60 -8
- data/lib/fdlint/version.rb +1 -1
- data/lib/hook/git_pre_commit +18 -0
- data/rules.d/css.rule.rb +1 -1
- data/rules.d/filename.rule.rb +3 -1
- data/rules.d/html.rule.rb +45 -32
- data/rules.d/js.rule.rb +2 -2
- data/test/all_tests.rb +28 -42
- data/test/css/mac_line_end_support_test.rb +10 -12
- data/test/css/parser_test.rb +4 -5
- data/test/css/rule/check_encoding_test.rb +7 -8
- data/test/css/rule/check_list_rule_test.rb +85 -102
- data/test/css/rule/file_name_test.rb +18 -39
- data/test/default_test.rb +1 -14
- data/test/fixtures/css/ad.css +0 -0
- data/test/fixtures/css/adver/test.css +0 -0
- data/test/fixtures/css/camelCase.css +0 -0
- data/test/fixtures/css/min/ad.css +0 -0
- data/test/fixtures/css/min/ad.min.css +0 -0
- data/test/fixtures/html/mixed_log_levels.html +1 -0
- data/test/fixtures/html/mixed_types.html +3 -0
- data/test/helper.rb +116 -16
- data/test/html/mixed_type_test.rb +14 -20
- data/test/html/parser/parse_comment_test.rb +4 -4
- data/test/html/parser/parse_dtd_test.rb +7 -7
- data/test/html/parser/parse_script_tag_test.rb +21 -14
- data/test/html/parser/parse_with_auto_close_tag_test.rb +12 -10
- data/test/html/parser/parse_with_diff_case_test.rb +8 -9
- data/test/html/parser/parse_with_emtpy_test.rb +5 -3
- data/test/html/parser/parse_with_multi_children_test.rb +4 -4
- data/test/html/parser/parse_with_multi_line_test.rb +5 -5
- data/test/html/parser/parse_with_prop_test.rb +10 -10
- data/test/html/parser/parse_with_script_tag_test.rb +4 -4
- data/test/html/parser/parse_with_selfclosing_test.rb +11 -11
- data/test/html/parser/parse_with_simple_tag_test.rb +7 -7
- data/test/html/parser/parse_with_simple_tree_test.rb +6 -6
- data/test/html/parser/parse_with_style_tag_test.rb +4 -4
- data/test/html/parser/parse_with_text_test.rb +7 -7
- data/test/html/parser_test.rb +19 -19
- data/test/html/query_test.rb +18 -18
- data/test/html/rule/check_block_level_element_test.rb +22 -24
- data/test/html/rule/check_button_test.rb +9 -9
- data/test/html/rule/check_css_in_head_test.rb +11 -32
- data/test/html/rule/check_dtd_test.rb +15 -11
- data/test/html/rule/check_form_element_name_test.rb +10 -9
- data/test/html/rule/check_head_contain_meta_and_title_test.rb +28 -34
- data/test/html/rule/check_hyperlink_with_target_test.rb +23 -17
- data/test/html/rule/check_hyperlink_with_title_test.rb +13 -20
- data/test/html/rule/check_id_n_class_downcase_test.rb +8 -19
- data/test/html/rule/check_img_with_alt_prop_test.rb +11 -12
- data/test/html/rule/check_no_import_css_test.rb +20 -20
- data/test/html/rule/check_prop_have_value_test.rb +9 -13
- data/test/html/rule/check_prop_seperator_test.rb +9 -13
- data/test/html/rule/check_style_prop_test.rb +7 -12
- data/test/html/rule/check_tag_closed_test.rb +15 -40
- data/test/html/rule/check_tag_downcase_test.rb +31 -28
- data/test/html/rule/check_unescape_char_test.rb +17 -17
- data/test/html/rule/check_unique_import_test.rb +15 -33
- data/test/html/rule_test.rb +2 -6
- data/test/js/expr/expr.rb +1 -1
- data/test/js/expr/left_hand.rb +1 -1
- data/test/js/expr/operate.rb +1 -1
- data/test/js/expr/primary.rb +1 -1
- data/test/js/parser_test.rb +3 -6
- data/test/js/rule/alert_check_test.rb +5 -19
- data/test/js/rule/base_test.rb +5 -19
- data/test/js/rule/jq_check_test.rb +26 -58
- data/test/js/rule/nest_try_catch_test.rb +26 -43
- data/test/js/rule/new_object_and_new_array_test.rb +5 -21
- data/test/js/rule/no_eval_test.rb +3 -19
- data/test/js/rule/no_global_test.rb +34 -73
- data/test/js/rule/private_method_check_test.rb +14 -43
- data/test/js/rule/semicolon_test.rb +18 -47
- data/test/js/rule/stat_if_with_brace_test.rb +30 -53
- data/test/js/rule/stat_if_with_muti_else_test.rb +36 -53
- data/test/js/rule/use_strict_equal_test.rb +16 -29
- data/test/js/rule_test.rb +2 -4
- data/test/js/stat/if.rb +1 -1
- data/test/js/stat/iter.rb +1 -1
- data/test/js/stat/stat.rb +1 -1
- data/test/js/stat/switch.rb +1 -1
- data/test/js/stat/try.rb +1 -1
- data/test/js/stat/var.rb +1 -1
- data/test/parser_visitable_test.rb +14 -36
- data/test/position_info_test.rb +4 -5
- data/test/runner/log_level_test.rb +30 -25
- metadata +11 -14
- data/lib/fdlint/file_validator.rb +0 -38
- data/lib/fdlint/parser/html/rule/check_tag_rule.rb +0 -80
- data/test/css/rule/compression_test.rb +0 -53
- data/test/html/rule/check_class_count_test.rb +0 -36
- data/test/html/rule/check_html_template_test.rb +0 -103
- data/test/js/rule/all_test.rb +0 -23
- data/test/js/rule/file_checker_test.rb +0 -131
- data/test/rule_dsl/dsl_basic_test.rb +0 -91
- data/test/rule_dsl/importing_test.rb +0 -48
- data/test/test_helper.rb +0 -9
@@ -1,29 +1,28 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class NestTryCatchTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
10
|
+
check_rule [:warn, 'try catch一般不允许嵌套,若嵌套,需要充分的理由'] do
|
11
|
+
should_with_result do
|
12
|
+
[
|
13
|
+
'try {
|
14
|
+
if (a > 0) {
|
15
|
+
}
|
16
|
+
} finally {
|
17
|
+
try {
|
18
|
+
|
19
|
+
} catch (e) {
|
20
|
+
|
21
|
+
}
|
22
|
+
}
|
23
|
+
',
|
23
24
|
|
24
|
-
|
25
|
-
js = '
|
26
|
-
try {
|
25
|
+
'try {
|
27
26
|
if (a > 0) {
|
28
27
|
try {
|
29
28
|
|
@@ -33,35 +32,19 @@ module XRayTest
|
|
33
32
|
}
|
34
33
|
} finally {
|
35
34
|
|
36
|
-
}
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def test_nest_in_finnally_part
|
43
|
-
js = '
|
44
|
-
try {
|
45
|
-
if (a > 0) {
|
46
|
-
}
|
47
|
-
} finally {
|
35
|
+
}'
|
36
|
+
]
|
37
|
+
end
|
38
|
+
|
39
|
+
should_without_result do
|
40
|
+
'
|
48
41
|
try {
|
49
|
-
|
42
|
+
|
50
43
|
} catch (e) {
|
51
44
|
|
52
|
-
}
|
53
|
-
|
54
|
-
|
55
|
-
ret = visit js
|
56
|
-
assert_equal [['try catch一般不允许嵌套,若嵌套,需要充分的理由', :warn]], ret
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def visit(js)
|
62
|
-
stat = parse js, 'stat_try'
|
63
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
64
|
-
rule.visit_stat_try stat
|
45
|
+
}
|
46
|
+
'
|
47
|
+
end
|
65
48
|
end
|
66
49
|
|
67
50
|
end
|
@@ -1,34 +1,18 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class NewObjectAndNewArrayTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
new Object();
|
15
|
-
'
|
16
|
-
ret = visit js
|
17
|
-
assert_equal [['使用{}代替new Object()', :error]], ret
|
18
|
-
|
19
|
-
js = '
|
20
|
-
new Array();
|
21
|
-
'
|
22
|
-
ret = visit js
|
23
|
-
assert_equal [['使用[]代替new Array()', :error]], ret
|
10
|
+
should_with_result [:error, '使用{}代替new Object()'] do
|
11
|
+
['new Object()', 'new Object']
|
24
12
|
end
|
25
|
-
|
26
|
-
private
|
27
13
|
|
28
|
-
|
29
|
-
|
30
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
31
|
-
rule.visit_expr_new expr
|
14
|
+
should_with_result [:error, '使用[]代替new Array()'] do
|
15
|
+
['new Array()', 'new Array']
|
32
16
|
end
|
33
17
|
|
34
18
|
end
|
@@ -1,30 +1,14 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class NoEvalTest < BaseTest
|
11
|
-
|
12
|
-
def test_no_eval
|
13
|
-
js = 'eval("a = 1 + 2 + 3")'
|
14
|
-
ret = visit js
|
15
|
-
assert_equal [['不允许使用eval', :error]], ret
|
16
|
-
|
17
|
-
js = 'window.eval("1 + 2 + 3")'
|
18
|
-
ret = visit js
|
19
|
-
assert_equal [['不允许使用eval', :error]], ret
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
9
|
|
24
|
-
|
25
|
-
|
26
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
27
|
-
rule.visit_expr_member expr
|
10
|
+
should_with_result [:error, '不允许使用eval'] do
|
11
|
+
['eval("a = 1 + 2 + 3")', 'window.eval("1 + 2 + 3")']
|
28
12
|
end
|
29
13
|
|
30
14
|
end
|
@@ -1,84 +1,45 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class NoGlobalTest < BaseTest
|
11
|
-
|
12
|
-
class Parser < XRay::JS::Parser
|
13
|
-
include XRay::ParserVisitable
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_main
|
17
|
-
js = '
|
18
|
-
var a = 1;
|
19
|
-
a++;
|
20
|
-
function hello() {
|
21
|
-
}
|
22
|
-
if (a < 100) {
|
23
|
-
var b;
|
24
|
-
(function(k) {
|
25
|
-
var c = 123;
|
26
|
-
|
27
|
-
function d(j) {
|
28
|
-
a = 1000;
|
29
|
-
z = 200;
|
30
|
-
k = 300;
|
31
|
-
j = 400
|
32
|
-
}
|
33
|
-
})();
|
34
|
-
}
|
35
|
-
'
|
36
|
-
|
37
|
-
ret = parse js
|
38
|
-
assert_equal 4, ret.length
|
39
|
-
assert_equal '(var,[(var=,a,1)],)', ret[0].node.text
|
40
|
-
assert_equal 'hello', ret[1].node.text
|
41
|
-
assert_equal '(var,[(var=,b,)],)', ret[2].node.text
|
42
|
-
assert_equal '(=,z,200)', ret[3].node.text
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_scope_in_other_function
|
46
|
-
js = <<-JS
|
47
|
-
/**
|
48
|
-
* scope test fixture
|
49
|
-
*/
|
50
|
-
(function($){
|
51
|
-
function test(){
|
52
|
-
var temp;
|
53
|
-
}
|
54
9
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
10
|
+
should_with_result [:error, '禁止使用未定义的变量(或全局变量)'] do
|
11
|
+
[
|
12
|
+
%Q{ var a = 1; },
|
13
|
+
%Q{ (function(k) {
|
14
|
+
var c = 123;
|
15
|
+
|
16
|
+
function d(j) {
|
17
|
+
a = 1000;
|
18
|
+
z = 200;
|
19
|
+
k = 300;
|
20
|
+
j = 400
|
21
|
+
}
|
22
|
+
})()
|
23
|
+
}, <<-SRC
|
24
|
+
/**
|
25
|
+
* scope test fixture
|
26
|
+
*/
|
27
|
+
(function($){
|
28
|
+
function test(){
|
29
|
+
var temp;
|
30
|
+
}
|
31
|
+
|
32
|
+
var _createSeperator = function(){
|
33
|
+
var _self = this,
|
34
|
+
menuEl = _self.get('menuEl');
|
35
|
+
|
36
|
+
//global var
|
37
|
+
temp = [a,b,c];
|
38
|
+
return new Node(temp).appendTo(menuEl);
|
39
|
+
};
|
40
|
+
})(jQuery);
|
41
|
+
SRC
|
42
|
+
]
|
82
43
|
end
|
83
44
|
|
84
45
|
end
|
@@ -1,54 +1,25 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class PrivateMethodCheckTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
js = "a._check.call(a, 'good')"
|
26
|
-
ret = visit js
|
27
|
-
assert_equal 1, ret.length
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_private_called
|
31
|
-
js = "a.__check('good')"
|
32
|
-
ret = visit js
|
33
|
-
assert_equal 1, ret.length
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_good_case
|
37
|
-
js = "this.__check('good')"
|
38
|
-
ret = visit js
|
39
|
-
assert_equal [], ret
|
40
|
-
|
41
|
-
js = "self._check('good')"
|
42
|
-
ret = visit js
|
43
|
-
assert_equal [], ret
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def visit(js)
|
49
|
-
expr = parse js, 'expr_member'
|
50
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
51
|
-
rule.send :visit_expr_member, expr
|
10
|
+
check_rule [:error, '禁止调用对象的私有方法'] do
|
11
|
+
|
12
|
+
should_with_result do
|
13
|
+
%w[a._check('good')
|
14
|
+
a._check.call(a,\ 'good')
|
15
|
+
a.__check('good')]
|
16
|
+
end
|
17
|
+
|
18
|
+
should_without_result do
|
19
|
+
%w[a.check("good")
|
20
|
+
this.__check('good')
|
21
|
+
self._check('good')]
|
22
|
+
end
|
52
23
|
end
|
53
24
|
|
54
25
|
end
|
@@ -1,59 +1,30 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class SemicolonTest < BaseTest
|
11
|
-
include XRay::JS::Rule
|
12
9
|
|
13
|
-
|
14
|
-
@msg = '所有语句结束带上分号'
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_block
|
18
|
-
js = '{
|
19
|
-
i = i++;
|
20
|
-
}'
|
10
|
+
check_rule [:error, '所有语句结束带上分号'] do
|
21
11
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_expression
|
41
|
-
js = '
|
42
|
-
a = i + 1 + 2 + 3 * 9
|
43
|
-
i++
|
44
|
-
'
|
45
|
-
ret = visit js, 'expression'
|
46
|
-
assert_equal [[@msg, :error]], ret
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def visit(js, type)
|
52
|
-
stat = parse js, :statement
|
53
|
-
assert_equal type, stat.type
|
54
|
-
|
55
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
56
|
-
rule.visit_statement stat
|
12
|
+
should_with_result do
|
13
|
+
[
|
14
|
+
%Q{var a = 1, b = 2
|
15
|
+
a++;},
|
16
|
+
|
17
|
+
%Q{a = i + 1 + 2 + 3 * 9
|
18
|
+
i++}
|
19
|
+
]
|
20
|
+
end
|
21
|
+
|
22
|
+
should_without_result do
|
23
|
+
[
|
24
|
+
%Q{ i = i++; },
|
25
|
+
';'
|
26
|
+
]
|
27
|
+
end
|
57
28
|
end
|
58
29
|
|
59
30
|
end
|
@@ -1,64 +1,41 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class StatIfWithBraceTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
i--;
|
42
|
-
} else i++;
|
43
|
-
|
44
|
-
'
|
45
|
-
ret = visit js
|
46
|
-
assert_equal [[@msg, :error]], ret
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_fail_3
|
50
|
-
js = 'if (i) i--; else i++;'
|
51
|
-
|
52
|
-
ret = visit js
|
53
|
-
assert_equal [[@msg, :error]], ret
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def visit(js)
|
59
|
-
stat = parse js, 'stat_if'
|
60
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
61
|
-
rule.visit_stat_if stat
|
10
|
+
check_rule [:error, '所有条件区域必须用花括号括起来'] do
|
11
|
+
|
12
|
+
should_with_result do
|
13
|
+
[
|
14
|
+
%Q{
|
15
|
+
if (hello()) {
|
16
|
+
i--;
|
17
|
+
} else i++;
|
18
|
+
},
|
19
|
+
%Q{
|
20
|
+
if (i > 0)
|
21
|
+
i--
|
22
|
+
hello();
|
23
|
+
}
|
24
|
+
]
|
25
|
+
end
|
26
|
+
|
27
|
+
should_without_result do
|
28
|
+
[
|
29
|
+
%Q{
|
30
|
+
if (i == 0) {
|
31
|
+
i++;
|
32
|
+
} else {
|
33
|
+
i--
|
34
|
+
}
|
35
|
+
},
|
36
|
+
%Q{ 'if (i) i--; else i++;'}
|
37
|
+
]
|
38
|
+
end
|
62
39
|
end
|
63
40
|
|
64
41
|
end
|
@@ -1,64 +1,47 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class StatIfWithMutiElseTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
} else if (a > 60) {
|
48
|
-
}
|
49
|
-
'
|
50
|
-
ret = visit js
|
51
|
-
assert_equal [[@msg, :error]], ret
|
52
|
-
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def visit(js)
|
59
|
-
stat = parse js, 'stat_if'
|
60
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
61
|
-
rule.visit_stat_if stat
|
10
|
+
check_rule [:error, '3个条件及以上的条件语句用switch代替if else'] do
|
11
|
+
|
12
|
+
should_with_result do
|
13
|
+
[
|
14
|
+
%Q{
|
15
|
+
if (a > 1) {
|
16
|
+
} else if (a > 20) {
|
17
|
+
} else if (a > 30) {
|
18
|
+
} else {
|
19
|
+
}
|
20
|
+
},
|
21
|
+
|
22
|
+
%Q{
|
23
|
+
if (a > 2) {
|
24
|
+
} else if (a > 40) {
|
25
|
+
} else if (a > 50) {
|
26
|
+
} else if (a > 60) {
|
27
|
+
}
|
28
|
+
}
|
29
|
+
]
|
30
|
+
end
|
31
|
+
|
32
|
+
should_without_result do
|
33
|
+
[
|
34
|
+
%Q{
|
35
|
+
if (a > 0) {
|
36
|
+
|
37
|
+
} else if (a > 10) {
|
38
|
+
|
39
|
+
} else {
|
40
|
+
|
41
|
+
}
|
42
|
+
},
|
43
|
+
].last
|
44
|
+
end
|
62
45
|
end
|
63
46
|
|
64
47
|
end
|
@@ -1,40 +1,27 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require_relative 'base_test'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
module XRayTest
|
4
|
+
module FdlintTest
|
7
5
|
module JS
|
8
6
|
module Rule
|
9
7
|
|
10
8
|
class UseStrictEqualTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
js = 'i != hello()'
|
28
|
-
ret = visit js
|
29
|
-
assert_equal [['避免使用==和!=操作符', :warn]], ret
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def visit(js)
|
35
|
-
expr = parse js, 'expr_equal'
|
36
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
37
|
-
rule.visit_expr_equal expr
|
10
|
+
check_rule [:warn, '避免使用==和!=操作符'] do
|
11
|
+
|
12
|
+
should_with_result do
|
13
|
+
[
|
14
|
+
'i == 1 + 1 / 2',
|
15
|
+
'i != hello()'
|
16
|
+
]
|
17
|
+
end
|
18
|
+
|
19
|
+
should_without_result do
|
20
|
+
[
|
21
|
+
'i === 1 + 1 / 2',
|
22
|
+
'i !== hello()'
|
23
|
+
]
|
24
|
+
end
|
38
25
|
end
|
39
26
|
|
40
27
|
end
|