fdlint 0.2.0.pre → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,28 +1,24 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckPropSeperatorTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
include
|
11
|
+
include FdlintTest::HTML
|
13
12
|
|
14
|
-
|
15
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
16
|
-
end
|
13
|
+
check_rule [:error, '属性值必须使用双引号'] do
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
15
|
+
should_with_result do
|
16
|
+
%Q{<input type='radio' />}
|
17
|
+
end
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
should_without_result do
|
20
|
+
%Q{<input type="radio" />}
|
21
|
+
end
|
26
22
|
end
|
27
23
|
|
28
24
|
end
|
@@ -1,26 +1,21 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckStylePropTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
|
13
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
14
|
-
end
|
11
|
+
include FdlintTest::HTML
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
check_rule [:error, '不能定义内嵌样式style'] do
|
14
|
+
|
15
|
+
should_with_result do
|
16
|
+
%Q{<div style='background:transparent;' />}
|
17
|
+
end
|
20
18
|
|
21
|
-
def test_check_tag_with_style_prop
|
22
|
-
prop = XRay::HTML::Property.new('style', 'nogo')
|
23
|
-
assert_equal [["不能定义内嵌样式style", :error]], @rule.check_html_property(prop)
|
24
19
|
end
|
25
20
|
|
26
21
|
end
|
@@ -1,56 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckTagClosedTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
|
13
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_check_normal_tag
|
17
|
-
tag = XRay::HTML::Element.new('div', {:class=>'footer'})
|
18
|
-
assert_equal [], @rule.check_html_tag(tag)
|
19
|
-
end
|
11
|
+
include FdlintTest::HTML
|
20
12
|
|
21
|
-
|
22
|
-
tag = XRay::HTML::Element.new('div', {:class=>'footer'})
|
23
|
-
tag.close_type = :none
|
24
|
-
assert_equal [["标签必须正确闭合", :error]], @rule.check_html_tag(tag)
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_check_normal_tag_self_closed
|
28
|
-
tag = XRay::HTML::Element.new('div', {:class=>'footer'})
|
29
|
-
tag.close_type = :self
|
30
|
-
assert_equal [], @rule.check_html_tag(tag)
|
31
|
-
end
|
13
|
+
check_rule [:error, '标签必须正确闭合'] do
|
32
14
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
tag = XRay::HTML::TextElement.new('hello world')
|
46
|
-
assert_equal [], @rule.check_html_tag(tag)
|
15
|
+
should_without_result do
|
16
|
+
[
|
17
|
+
%Q{<br>},
|
18
|
+
%Q{<br/>},
|
19
|
+
%Q{<p></p>},
|
20
|
+
%Q{<p/>},
|
21
|
+
%Q{<img src="" alt="" >},
|
22
|
+
%Q{<img src="" alt="" />},
|
23
|
+
%Q{text},
|
24
|
+
%Q{<!--comment-->}
|
25
|
+
]
|
26
|
+
end
|
47
27
|
end
|
48
28
|
|
49
|
-
def test_check_comment
|
50
|
-
tag = XRay::HTML::CommentElement.new('hello world')
|
51
|
-
assert_equal [], @rule.check_html_tag(tag)
|
52
|
-
end
|
53
|
-
|
54
29
|
end
|
55
30
|
|
56
31
|
end
|
@@ -1,50 +1,53 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckTagDowncaseTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
|
13
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
14
|
-
end
|
11
|
+
include FdlintTest::HTML
|
15
12
|
|
16
|
-
|
17
|
-
prop = XRay::HTML::Property.new('href', '#nogo')
|
18
|
-
assert_equal [], @rule.check_html_property(prop)
|
19
|
-
end
|
13
|
+
check_rule [:error, '标签名必须小写'] do
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_check_tag_with_upcase_name
|
27
|
-
tag = XRay::HTML::Element.new('DIV', {:class=>'footer'})
|
28
|
-
assert_equal [["标签名必须小写", :error]], @rule.check_html_tag(tag)
|
29
|
-
end
|
15
|
+
should_with_result do
|
16
|
+
%Q{<INPUT type="radio" />}
|
17
|
+
end
|
30
18
|
|
31
|
-
|
32
|
-
|
33
|
-
|
19
|
+
should_without_result do
|
20
|
+
%Q{<input type="radio" />}
|
21
|
+
end
|
34
22
|
end
|
35
23
|
|
36
|
-
|
37
|
-
|
38
|
-
|
24
|
+
check_rule [:error, '属性名连字符用中横线'] do
|
25
|
+
should_with_result do
|
26
|
+
[
|
27
|
+
%Q{<a data_id="1"/>}
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
should_without_result do
|
32
|
+
%Q{<a data-id="1"/>}
|
33
|
+
end
|
39
34
|
end
|
40
35
|
|
41
|
-
|
42
|
-
|
43
|
-
|
36
|
+
check_rule [:error, '属性名必须小写'] do
|
37
|
+
should_with_result do
|
38
|
+
[
|
39
|
+
%Q{<a dataId="1"/>},
|
40
|
+
%Q{<a HREF=""/>},
|
41
|
+
%Q{<a Href=""/>}
|
42
|
+
]
|
43
|
+
end
|
44
|
+
|
45
|
+
should_without_result do
|
46
|
+
%Q{<a href=""/>}
|
47
|
+
end
|
44
48
|
end
|
45
49
|
|
46
50
|
end
|
47
|
-
|
48
51
|
end
|
49
52
|
end
|
50
53
|
end
|
@@ -1,31 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckUnescapedCharTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
|
13
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
14
|
-
end
|
11
|
+
include FdlintTest::HTML
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
13
|
+
check_rule [:error, '特殊HTML符号(>和<)必须转义'] do
|
14
|
+
|
15
|
+
should_with_result do
|
16
|
+
[
|
17
|
+
%Q{<test>>>></test>},
|
18
|
+
%Q{<a><<<</a>},
|
19
|
+
%Q{<a><<<>>></a>}
|
20
|
+
]
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
should_without_result do
|
24
|
+
[
|
25
|
+
%Q{<p/>},
|
26
|
+
%Q{<script> 1>5 </script>}
|
27
|
+
]
|
28
|
+
end
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -1,49 +1,31 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
require_relative '../../helper'
|
4
|
-
require 'html/rule/check_tag_rule'
|
5
4
|
|
6
|
-
module
|
5
|
+
module FdlintTest
|
7
6
|
module HTML
|
8
7
|
module Rule
|
9
8
|
|
10
9
|
class CheckUniqueImportTest < Test::Unit::TestCase
|
11
10
|
|
12
|
-
|
13
|
-
@rule = XRay::HTML::Rule::CheckTagRule.new
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_check_normal_script
|
17
|
-
tag = XRay::HTML::Element.new('script', {:src=>'http://style.china.alibaba.com/lib/fdev-v4/core/fdev-min.js'})
|
18
|
-
assert_equal [], @rule.visit_tag(tag)
|
19
|
-
end
|
11
|
+
include FdlintTest::HTML
|
20
12
|
|
21
|
-
|
22
|
-
tag = XRay::HTML::Element.new('script', {:src=>'http://style.china.alibaba.com/lib/fdev-v4/core/fdev-min.js'})
|
23
|
-
assert_equal [], @rule.visit_tag(tag)
|
24
|
-
(1..10).each do
|
25
|
-
assert_equal [["避免重复引用同一或相同功能文件", :error]], @rule.visit_tag(tag)
|
26
|
-
end
|
27
|
-
end
|
13
|
+
check_rule [:error, '避免重复引用同一文件'] do
|
28
14
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
15
|
+
should_with_result do
|
16
|
+
[
|
17
|
+
%Q{<script src="fdev-min.js"></script>} * 2,
|
18
|
+
%Q{<script src="fdev-min.js"></script>} * 5,
|
19
|
+
%Q{<link rel="stylesheet" href="test.css"/>} * 2,
|
20
|
+
%Q{<link rel="stylesheet" href="test.css"/>} * 5
|
21
|
+
]
|
34
22
|
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_check_normal_style
|
38
|
-
tag = XRay::HTML::Element.new('link', {:rel => 'stylesheet', :href=>'http://style.china.alibaba.com/css/lib/fdev-v4/core/fdev-min.css'}, [], :self)
|
39
|
-
assert_equal [], @rule.visit_tag(tag)
|
40
|
-
end
|
41
23
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
24
|
+
should_without_result do
|
25
|
+
[
|
26
|
+
%Q{<script src="fdev-min.js"></script>},
|
27
|
+
%Q{<link rel="stylesheet" href="test.css"/>}
|
28
|
+
]
|
47
29
|
end
|
48
30
|
end
|
49
31
|
|
data/test/html/rule_test.rb
CHANGED
@@ -18,13 +18,11 @@ require_relative 'rule/check_head_contain_meta_and_title_test'
|
|
18
18
|
require_relative 'rule/check_block_level_element_test'
|
19
19
|
require_relative 'rule/check_form_element_name_test'
|
20
20
|
require_relative 'rule/check_button_test'
|
21
|
-
require_relative 'rule/check_class_count_test'
|
22
21
|
require_relative 'rule/check_tag_closed_test'
|
23
|
-
require_relative 'rule/check_html_template_test'
|
24
22
|
require_relative 'rule/check_css_in_head_test'
|
25
23
|
require_relative 'rule/check_unescape_char_test'
|
26
24
|
|
27
|
-
module
|
25
|
+
module FdlintTest
|
28
26
|
|
29
27
|
module HTML
|
30
28
|
|
@@ -47,9 +45,7 @@ module XRayTest
|
|
47
45
|
tests << Rule::CheckBlockLevelElementTest.suite
|
48
46
|
tests << Rule::CheckFormElementNameTest.suite
|
49
47
|
tests << Rule::CheckButtonTest.suite
|
50
|
-
tests << Rule::CheckClassCountTest.suite
|
51
48
|
tests << Rule::CheckTagClosedTest.suite
|
52
|
-
tests << Rule::CheckHTMLTemplateTest.suite
|
53
49
|
tests << Rule::CheckCSSInHeadTest.suite
|
54
50
|
tests << Rule::CheckUnescapedCharTest.suite
|
55
51
|
tests
|
@@ -58,5 +54,5 @@ module XRayTest
|
|
58
54
|
end
|
59
55
|
end
|
60
56
|
|
61
|
-
Test::Unit::UI::Console::TestRunner.run(
|
57
|
+
Test::Unit::UI::Console::TestRunner.run( FdlintTest::HTML::RuleTest ) if __FILE__ == $0
|
62
58
|
|
data/test/js/expr/expr.rb
CHANGED
data/test/js/expr/left_hand.rb
CHANGED
data/test/js/expr/operate.rb
CHANGED
data/test/js/expr/primary.rb
CHANGED
data/test/js/parser_test.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require File.expand_path('../helper', File.dirname(__FILE__))
|
3
3
|
|
4
|
-
require 'js/
|
4
|
+
require 'fdlint/parser/js/js_parser'
|
5
5
|
|
6
6
|
require_relative 'expr/expr'
|
7
7
|
require_relative 'stat/stat'
|
8
8
|
|
9
9
|
|
10
|
-
module
|
10
|
+
module FdlintTest
|
11
11
|
module JS
|
12
12
|
class ParserTest < Test::Unit::TestCase
|
13
|
-
include XRay::JS
|
14
13
|
|
15
14
|
include Expr::Expr
|
16
15
|
include Stat::Stat
|
17
|
-
|
18
16
|
|
19
17
|
def test_parse_program()
|
20
18
|
js = '
|
@@ -72,8 +70,7 @@ module XRayTest
|
|
72
70
|
|
73
71
|
|
74
72
|
def create_parser(js)
|
75
|
-
Parser.new js
|
76
|
-
|
73
|
+
::Fdlint::Parser::JS::JsParser.new js
|
77
74
|
end
|
78
75
|
|
79
76
|
def parse_js(action, js)
|
@@ -1,32 +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 AlertCheckTest < BaseTest
|
11
9
|
|
12
|
-
|
13
|
-
js = 'alert'
|
14
|
-
ret = visit js
|
15
|
-
assert_equal 1, ret.length
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_vist_window_alert
|
19
|
-
js = "window.alert"
|
20
|
-
ret = visit js
|
21
|
-
assert_equal [], ret
|
22
|
-
end
|
10
|
+
check_rule [:warn, '必须去掉临时调试代码。如果一定要使用alert功能,请使用 window.alert'] do
|
23
11
|
|
24
|
-
|
12
|
+
should_with_result do
|
13
|
+
'alert'
|
14
|
+
end
|
25
15
|
|
26
|
-
def visit(js)
|
27
|
-
expr = parse js, 'expr_member'
|
28
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
29
|
-
rule.send :visit_expr_member, expr
|
30
16
|
end
|
31
17
|
|
32
18
|
end
|
data/test/js/rule/base_test.rb
CHANGED
@@ -1,32 +1,18 @@
|
|
1
1
|
require_relative '../../helper'
|
2
2
|
|
3
|
-
require 'js/
|
4
|
-
require 'parser_visitable'
|
3
|
+
require 'fdlint/parser/js/js_parser'
|
4
|
+
require 'fdlint/parser/parser_visitable'
|
5
5
|
|
6
|
-
module
|
6
|
+
module FdlintTest
|
7
7
|
|
8
8
|
module JS
|
9
9
|
module Rule
|
10
10
|
|
11
|
-
class VisitableParser <
|
12
|
-
include XRay::ParserVisitable
|
11
|
+
class VisitableParser < Fdlint::Parser::JS::JsParser
|
13
12
|
end
|
14
13
|
|
15
14
|
class BaseTest < Test::Unit::TestCase
|
16
|
-
|
17
|
-
def parse(js, action = 'parse_program')
|
18
|
-
parser = XRay::JS::Parser.new js, XRayTest::Logger.new
|
19
|
-
parser.send "parse_#{action}"
|
20
|
-
end
|
21
|
-
|
22
|
-
def parse_with_rule(js, rule, options = {})
|
23
|
-
parser = VisitableParser.new js, XRayTest::Logger.new
|
24
|
-
rule = rule.new if rule.is_a? Class
|
25
|
-
parser.add_visitor rule
|
26
|
-
parser.parse_program
|
27
|
-
parser.results
|
28
|
-
end
|
29
|
-
|
15
|
+
include FdlintTest::JS
|
30
16
|
end
|
31
17
|
|
32
18
|
end
|
@@ -1,86 +1,54 @@
|
|
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 JqCheckTest < BaseTest
|
11
|
-
def test_visit_expr_member
|
12
|
-
js = 'jQuery.noConflict'
|
13
|
-
ret = visit js
|
14
|
-
assert_equal 2, ret.length
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_check_direct_jquery_call
|
18
|
-
js = "jQuery.namespace"
|
19
|
-
ret = visit js
|
20
|
-
assert_equal [], ret
|
21
|
-
|
22
|
-
jses = [
|
23
|
-
'jQuery.bind',
|
24
|
-
'jQuery[method]',
|
25
|
-
'jQuery(function($) {})',
|
26
|
-
'jQuery.extend'
|
27
|
-
]
|
28
|
-
|
29
|
-
jses.each do |js|
|
30
|
-
r = visit js
|
31
|
-
assert r.include? ['禁止直接使用jQuery变量,使用全局闭包写法"(function($, NS){....})(jQuery,Namespace);",jQuery.namespace例外', :error]
|
32
|
-
end
|
33
|
-
end
|
34
9
|
|
35
|
-
|
36
|
-
|
10
|
+
|
11
|
+
should_with_result [:error, '禁止使用jQuery.sub()和jQuery.noConflict方法'] do
|
12
|
+
%w{
|
13
|
+
jQ.noConflict
|
37
14
|
$.sub
|
38
15
|
$.noConflict
|
39
16
|
jQuery.sub
|
40
17
|
jQuery.noConflict
|
41
|
-
|
18
|
+
}
|
19
|
+
end
|
42
20
|
|
43
|
-
|
44
|
-
|
45
|
-
|
21
|
+
check_rule [:error, '禁止直接使用jQuery变量,使用全局闭包写法,jQuery.namespace例外'] do
|
22
|
+
should_with_result do
|
23
|
+
%w{
|
24
|
+
jQuery.bind
|
25
|
+
jQuery[method]
|
26
|
+
jQuery(function($)\ {})
|
27
|
+
jQuery.extend
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
next
|
32
|
+
|
33
|
+
should_without_result do
|
34
|
+
"jQuery.namespace"
|
46
35
|
end
|
47
36
|
end
|
48
37
|
|
49
|
-
|
50
|
-
|
38
|
+
should_with_result [:error, '使用".data()"读写自定义属性时需要转化成驼峰形式'] do
|
39
|
+
%w{
|
51
40
|
$.data("doc-config")
|
52
41
|
jQuery.data('doc-config')
|
53
|
-
|
54
|
-
jses.each do |js|
|
55
|
-
r = visit js
|
56
|
-
assert r.include? ['使用".data()"读写自定义属性时需要转化成驼峰形式', :error]
|
57
|
-
end
|
42
|
+
}
|
58
43
|
end
|
59
44
|
|
60
|
-
|
61
|
-
|
45
|
+
should_with_result [:warn, '使用选择器时,能确定tagName的,必须加上tagName'] do
|
46
|
+
[
|
62
47
|
"$('.myclass', div)",
|
63
48
|
"$('.myclass')",
|
64
49
|
"$('[name=123]')",
|
65
50
|
"$(':first')"
|
66
51
|
]
|
67
|
-
|
68
|
-
jses.each do |js|
|
69
|
-
r = visit js
|
70
|
-
assert r.include? ['使用选择器时,能确定tagName的,必须加上tagName', :warn]
|
71
|
-
end
|
72
|
-
|
73
|
-
js = '$()'
|
74
|
-
ret = visit js
|
75
|
-
assert_equal [], ret
|
76
|
-
end
|
77
|
-
|
78
|
-
private
|
79
|
-
|
80
|
-
def visit(js)
|
81
|
-
expr = parse js, 'expr_member'
|
82
|
-
rule = XRay::JS::Rule::ChecklistRule.new
|
83
|
-
rule.send :visit_expr_member, expr
|
84
52
|
end
|
85
53
|
|
86
54
|
end
|