fdlint 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/Gemfile.lock +14 -0
- data/README.md +68 -0
- data/Rakefile +92 -0
- data/bin/fdlint +17 -0
- data/lib/base_parser.rb +143 -0
- data/lib/cmd_runner.rb +145 -0
- data/lib/context.rb +31 -0
- data/lib/css/parser.rb +186 -0
- data/lib/css/reader.rb +30 -0
- data/lib/css/rule/check_compression_rule.rb +48 -0
- data/lib/css/rule/checklist.rb +45 -0
- data/lib/css/struct.rb +111 -0
- data/lib/encoding_error.rb +6 -0
- data/lib/file_validator.rb +38 -0
- data/lib/helper/code_type.rb +50 -0
- data/lib/helper/color_string.rb +44 -0
- data/lib/helper/file_reader.rb +22 -0
- data/lib/helper/strenc.rb +65 -0
- data/lib/html/parser.rb +212 -0
- data/lib/html/query.rb +96 -0
- data/lib/html/rule/check_tag_rule.rb +80 -0
- data/lib/html/struct.rb +291 -0
- data/lib/js/expr/expr.rb +66 -0
- data/lib/js/expr/left_hand.rb +63 -0
- data/lib/js/expr/operate.rb +92 -0
- data/lib/js/expr/primary.rb +166 -0
- data/lib/js/parser.rb +116 -0
- data/lib/js/rule/all.rb +35 -0
- data/lib/js/rule/checklist.rb +41 -0
- data/lib/js/rule/file_checker.rb +42 -0
- data/lib/js/rule/helper.rb +96 -0
- data/lib/js/rule/no_global.rb +87 -0
- data/lib/js/stat/if.rb +25 -0
- data/lib/js/stat/iter.rb +85 -0
- data/lib/js/stat/stat.rb +117 -0
- data/lib/js/stat/switch.rb +65 -0
- data/lib/js/stat/try.rb +28 -0
- data/lib/js/stat/var.rb +40 -0
- data/lib/js/struct.rb +248 -0
- data/lib/log_entry.rb +49 -0
- data/lib/node.rb +28 -0
- data/lib/parse_error.rb +13 -0
- data/lib/parser_visitable.rb +138 -0
- data/lib/position_info.rb +46 -0
- data/lib/printer/base_printer.rb +24 -0
- data/lib/printer/console_printer.rb +66 -0
- data/lib/printer/nocolor_printer.rb +27 -0
- data/lib/printer/vim_printer.rb +19 -0
- data/lib/rule.rb +241 -0
- data/lib/rule_helper.rb +14 -0
- data/lib/runner.rb +225 -0
- data/rules.d/css.rule +127 -0
- data/rules.d/html.dtd.rule +22 -0
- data/rules.d/html.prop.rule +51 -0
- data/rules.d/html.tag.rule +136 -0
- data/rules.d/js.file.rule +13 -0
- data/rules.d/js.jquery.rule +56 -0
- data/rules.d/js.mergefile.rule +71 -0
- data/rules.d/js.rule +84 -0
- data/test/all_tests.rb +84 -0
- data/test/cli/cli_test.rb +70 -0
- data/test/cli/log_level_test.rb +51 -0
- data/test/cli/output_format_test.rb +47 -0
- data/test/cli/type_test.rb +77 -0
- data/test/css/mac_line_end_support_test.rb +38 -0
- data/test/css/parser_test.rb +276 -0
- data/test/css/rule/check_encoding_test.rb +66 -0
- data/test/css/rule/check_list_rule_test.rb +167 -0
- data/test/css/rule/compression_test.rb +53 -0
- data/test/css/rule/file_name_test.rb +76 -0
- data/test/fixtures/css/broken.css +4 -0
- data/test/fixtures/css/cbu/36.css +52 -0
- data/test/fixtures/css/cbu/china_top.css +324 -0
- data/test/fixtures/css/cbu/default-merge.css +3 -0
- data/test/fixtures/css/cbu/default.css +13 -0
- data/test/fixtures/css/cbu/diy-merge.css +25 -0
- data/test/fixtures/css/cbu/fns-v1.css +27 -0
- data/test/fixtures/css/cbu/index_v0.1.css +12 -0
- data/test/fixtures/css/cbu/merge.css +11 -0
- data/test/fixtures/css/cbu/min.css +2 -0
- data/test/fixtures/css/cbu/my_home_admin.css +126 -0
- data/test/fixtures/css/cbu/nav.css +95 -0
- data/test/fixtures/css/cbu/pic_list.css +386 -0
- data/test/fixtures/css/cbu/quote-edit.css +18 -0
- data/test/fixtures/css/cbu/selloffer.shopwindow.css +1 -0
- data/test/fixtures/css/cbu/v1.css +9 -0
- data/test/fixtures/css/css3.css +30 -0
- data/test/fixtures/css/empty-min.css +0 -0
- data/test/fixtures/css/empty.css +0 -0
- data/test/fixtures/css/font-family.css +4 -0
- data/test/fixtures/css/gb-good.css +14 -0
- data/test/fixtures/css/gb_using_star.css +4 -0
- data/test/fixtures/css/import.css +18 -0
- data/test/fixtures/css/mac-line-sep-err-min.css +1 -0
- data/test/fixtures/css/mac-line-sep-err.css +1 -0
- data/test/fixtures/css/mac-line-sep-good-min.css +1 -0
- data/test/fixtures/css/mac-line-sep-good.css +1 -0
- data/test/fixtures/css/multi-encoding-in-a-file.css +0 -0
- data/test/fixtures/css/simple.css +1 -0
- data/test/fixtures/css/using_expr.css +8 -0
- data/test/fixtures/css/using_hack.css +21 -0
- data/test/fixtures/css/using_id.css +1 -0
- data/test/fixtures/css/using_star.css +4 -0
- data/test/fixtures/css/utf8_good.css +6 -0
- data/test/fixtures/css/utf8_good_declaring_charset.css +7 -0
- data/test/fixtures/css/utf8_using_star.css +5 -0
- data/test/fixtures/html/1-1.html +120 -0
- data/test/fixtures/html/1-2.html +120 -0
- data/test/fixtures/html/cms.html +373 -0
- data/test/fixtures/html/css_out_of_head.html +9 -0
- data/test/fixtures/html/fdev-template.html +22 -0
- data/test/fixtures/html/google.com.html +33 -0
- data/test/fixtures/html/mixed_log_levels.html +4 -0
- data/test/fixtures/html/mixed_types.html +13 -0
- data/test/fixtures/html/no_dtd.html +6 -0
- data/test/fixtures/html/readme.html +94 -0
- data/test/fixtures/html/review.board.html +163 -0
- data/test/fixtures/html/syntax_err.html +3 -0
- data/test/fixtures/html/train/detail/345/233/276/346/226/207/347/273/223/345/220/210.html +208 -0
- data/test/fixtures/html/train/detail/347/232/204Flash.html +212 -0
- data/test/fixtures/html/train/detail/347/232/204Vedio.html +212 -0
- data/test/fixtures/html/train/index.html +37 -0
- data/test/fixtures/html/train/test.html +1 -0
- data/test/fixtures/html/train//344/277/256/346/224/271/344/270/200/347/272/247/345/210/206/347/261/273.html +112 -0
- data/test/fixtures/html/train//344/277/256/346/224/271/345/255/220/345/210/206/347/261/273.html +108 -0
- data/test/fixtures/html/train//344/277/256/346/224/271/350/257/276/347/250/213.html +195 -0
- data/test/fixtures/html/train//345/215/232/345/256/242/350/256/276/347/275/256.html +142 -0
- data/test/fixtures/html/train//346/265/217/350/247/210/350/256/260/345/275/225.html +191 -0
- data/test/fixtures/html/train//346/267/273/345/212/240/344/270/200/347/272/247/345/210/206/347/261/273.html +113 -0
- data/test/fixtures/html/train//346/267/273/345/212/240/345/255/220/345/210/206/347/261/273.html +112 -0
- data/test/fixtures/html/train//346/267/273/345/212/240/350/257/276/347/250/213.html +195 -0
- data/test/fixtures/html/train//347/231/273/345/275/225.html +20 -0
- data/test/fixtures/html/train//347/256/241/347/220/206/345/210/206/347/261/273.html +210 -0
- data/test/fixtures/html/train//347/256/241/347/220/206/345/217/215/351/246/210.html +222 -0
- data/test/fixtures/html/train//347/256/241/347/220/206/350/257/276/347/250/213.html +284 -0
- data/test/fixtures/html/train//347/256/241/347/220/206/350/264/246/346/210/267.html +107 -0
- data/test/fixtures/html/train//347/275/221/344/270/212/345/237/271/350/256/255home/351/241/265.html +354 -0
- data/test/fixtures/html/train//347/275/221/345/225/206/345/237/271/350/256/255list/351/241/265.html +255 -0
- data/test/fixtures/html/train//350/256/276/347/275/256/351/246/226/351/241/265/346/216/250/350/215/220.html +168 -0
- data/test/fixtures/html/train//350/257/264/346/230/216.txt +3 -0
- data/test/fixtures/html/train//351/246/226/351/241/265/345/271/277/345/221/212/350/256/276/347/275/256.html +297 -0
- data/test/fixtures/html/unescaped.html +2 -0
- data/test/fixtures/html/view.vm +916 -0
- data/test/fixtures/js/jquery-1.7.js +9300 -0
- data/test/fixtures/js/scope-test.js +22 -0
- data/test/helper.rb +41 -0
- data/test/html/mixed_type_test.rb +35 -0
- data/test/html/parser/parse_comment_test.rb +47 -0
- data/test/html/parser/parse_dtd_test.rb +46 -0
- data/test/html/parser/parse_script_tag_test.rb +55 -0
- data/test/html/parser/parse_with_auto_close_tag_test.rb +41 -0
- data/test/html/parser/parse_with_diff_case_test.rb +38 -0
- data/test/html/parser/parse_with_emtpy_test.rb +22 -0
- data/test/html/parser/parse_with_multi_children_test.rb +27 -0
- data/test/html/parser/parse_with_multi_line_test.rb +41 -0
- data/test/html/parser/parse_with_prop_test.rb +88 -0
- data/test/html/parser/parse_with_script_tag_test.rb +26 -0
- data/test/html/parser/parse_with_selfclosing_test.rb +39 -0
- data/test/html/parser/parse_with_simple_tag_test.rb +44 -0
- data/test/html/parser/parse_with_simple_tree_test.rb +40 -0
- data/test/html/parser/parse_with_style_tag_test.rb +22 -0
- data/test/html/parser/parse_with_text_test.rb +45 -0
- data/test/html/parser_test.rb +52 -0
- data/test/html/query_test.rb +52 -0
- data/test/html/rule/check_block_level_element_test.rb +52 -0
- data/test/html/rule/check_button_test.rb +45 -0
- data/test/html/rule/check_class_count_test.rb +36 -0
- data/test/html/rule/check_css_in_head_test.rb +53 -0
- data/test/html/rule/check_dtd_test.rb +46 -0
- data/test/html/rule/check_form_element_name_test.rb +49 -0
- data/test/html/rule/check_head_contain_meta_and_title_test.rb +52 -0
- data/test/html/rule/check_html_template_test.rb +103 -0
- data/test/html/rule/check_hyperlink_with_target_test.rb +40 -0
- data/test/html/rule/check_hyperlink_with_title_test.rb +43 -0
- data/test/html/rule/check_id_n_class_downcase_test.rb +40 -0
- data/test/html/rule/check_img_with_alt_prop_test.rb +33 -0
- data/test/html/rule/check_no_import_css_test.rb +36 -0
- data/test/html/rule/check_prop_have_value_test.rb +32 -0
- data/test/html/rule/check_prop_seperator_test.rb +32 -0
- data/test/html/rule/check_style_prop_test.rb +30 -0
- data/test/html/rule/check_tag_closed_test.rb +59 -0
- data/test/html/rule/check_tag_downcase_test.rb +51 -0
- data/test/html/rule/check_unescape_char_test.rb +35 -0
- data/test/html/rule/check_unique_import_test.rb +56 -0
- data/test/html/rule_test.rb +62 -0
- data/test/js/expr/expr.rb +57 -0
- data/test/js/expr/left_hand.rb +25 -0
- data/test/js/expr/operate.rb +145 -0
- data/test/js/expr/primary.rb +89 -0
- data/test/js/parser_test.rb +98 -0
- data/test/js/rule/alert_check_test.rb +37 -0
- data/test/js/rule/all_test.rb +23 -0
- data/test/js/rule/base_test.rb +34 -0
- data/test/js/rule/file_checker_test.rb +131 -0
- data/test/js/rule/jq_check_test.rb +90 -0
- data/test/js/rule/nest_try_catch_test.rb +71 -0
- data/test/js/rule/new_object_and_new_array_test.rb +38 -0
- data/test/js/rule/no_eval_test.rb +34 -0
- data/test/js/rule/no_global_test.rb +88 -0
- data/test/js/rule/private_method_check_test.rb +58 -0
- data/test/js/rule/semicolon_test.rb +63 -0
- data/test/js/rule/stat_if_with_brace_test.rb +68 -0
- data/test/js/rule/stat_if_with_muti_else_test.rb +68 -0
- data/test/js/rule/use_strict_equal_test.rb +44 -0
- data/test/js/rule_test.rb +47 -0
- data/test/js/stat/if.rb +26 -0
- data/test/js/stat/iter.rb +115 -0
- data/test/js/stat/stat.rb +91 -0
- data/test/js/stat/switch.rb +37 -0
- data/test/js/stat/try.rb +32 -0
- data/test/js/stat/var.rb +38 -0
- data/test/parser_visitable_test.rb +102 -0
- data/test/position_info_test.rb +66 -0
- data/test/rule_dsl/dsl_basic_test.rb +91 -0
- data/test/rule_dsl/importing_test.rb +48 -0
- data/test/runner/log_level_test.rb +58 -0
- metadata +317 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
module XRayTest
|
2
|
+
module JS
|
3
|
+
module Expr
|
4
|
+
|
5
|
+
module Operate
|
6
|
+
|
7
|
+
def test_parse_expr_postfix
|
8
|
+
jses = [
|
9
|
+
'hello++',
|
10
|
+
'i--',
|
11
|
+
'j\n++'
|
12
|
+
]
|
13
|
+
exprs = [
|
14
|
+
'(++,hello,)',
|
15
|
+
'(--,i,)',
|
16
|
+
'j'
|
17
|
+
]
|
18
|
+
add_test :parse_expr_postfix, jses, exprs
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_parse_expr_unary
|
22
|
+
jses = [
|
23
|
+
'++i',
|
24
|
+
'-1',
|
25
|
+
'delete a[hello]',
|
26
|
+
'!a'
|
27
|
+
]
|
28
|
+
exprs = [
|
29
|
+
'(++,,i)',
|
30
|
+
'(-,,1)',
|
31
|
+
'(delete,,([,a,hello))',
|
32
|
+
'(!,,a)'
|
33
|
+
]
|
34
|
+
add_test :parse_expr_unary, jses, exprs
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_parse_expr_mul
|
38
|
+
jses = [
|
39
|
+
'a * b / c % d * e / f'
|
40
|
+
]
|
41
|
+
exprs = [
|
42
|
+
'(/,(*,(%,(/,(*,a,b),c),d),e),f)'
|
43
|
+
]
|
44
|
+
add_test :parse_expr_mul, jses, exprs
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_parse_expr_add
|
48
|
+
jses = [
|
49
|
+
'a * b + c - d / e + f % d + a'
|
50
|
+
]
|
51
|
+
exprs = [
|
52
|
+
'(+,(+,(-,(+,(*,a,b),c),(/,d,e)),(%,f,d)),a)'
|
53
|
+
]
|
54
|
+
add_test :parse_expr_add, jses, exprs
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_parse_expr_shift
|
58
|
+
jses = [
|
59
|
+
'a << 123',
|
60
|
+
'a + 1 >> 1 + 2',
|
61
|
+
'1 * 100 >> 2 >>> 1 + 2 * 3 - 4'
|
62
|
+
]
|
63
|
+
exprs = [
|
64
|
+
'(<<,a,123)',
|
65
|
+
'(>>,(+,a,1),(+,1,2))',
|
66
|
+
'(>>>,(>>,(*,1,100),2),(-,(+,1,(*,2,3)),4))'
|
67
|
+
]
|
68
|
+
add_test :parse_expr_shift, jses, exprs
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_parse_expr_relation
|
72
|
+
jses = [
|
73
|
+
'a + 1 < b',
|
74
|
+
'a + 2 * 3 >= a++',
|
75
|
+
'++a-- > --b + c',
|
76
|
+
'instanceofA instanceof instanceofB',
|
77
|
+
'innameA in innameC'
|
78
|
+
]
|
79
|
+
|
80
|
+
exprs = [
|
81
|
+
'(<,(+,a,1),b)',
|
82
|
+
'(>=,(+,a,(*,2,3)),(++,a,))',
|
83
|
+
'(>,(++,,(--,a,)),(+,(--,,b),c))',
|
84
|
+
'(instanceof,instanceofA,instanceofB)',
|
85
|
+
'(in,innameA,innameC)'
|
86
|
+
]
|
87
|
+
|
88
|
+
add_test :parse_expr_relation, jses, exprs
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_parse_expr_equal
|
92
|
+
jses = [
|
93
|
+
'a + 1 === c',
|
94
|
+
'a + 1 * c !== 11 + 22 < 3',
|
95
|
+
'a + 1 < 3 == 1 << 3',
|
96
|
+
'a << 4 - 5 == 1 << 3 === 5'
|
97
|
+
]
|
98
|
+
|
99
|
+
exprs = [
|
100
|
+
'(===,(+,a,1),c)',
|
101
|
+
'(!==,(+,a,(*,1,c)),(<,(+,11,22),3))',
|
102
|
+
'(==,(<,(+,a,1),3),(<<,1,3))',
|
103
|
+
'(===,(==,(<<,a,(-,4,5)),(<<,1,3)),5)'
|
104
|
+
]
|
105
|
+
|
106
|
+
add_test :parse_expr_equal, jses, exprs
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_parse_expr_bit
|
111
|
+
jses = [
|
112
|
+
'1 & b + 1',
|
113
|
+
'1 & 2 ^ 3 & 4 ^ 5 + 6 & 7',
|
114
|
+
'1 + 2 | 3 + 4 & 5 ^ 6 | 7 - 8'
|
115
|
+
]
|
116
|
+
|
117
|
+
exprs = [
|
118
|
+
'(&,1,(+,b,1))',
|
119
|
+
'(^,(^,(&,1,2),(&,3,4)),(&,(+,5,6),7))',
|
120
|
+
'(|,(|,(+,1,2),(^,(&,(+,3,4),5),6)),(-,7,8))'
|
121
|
+
]
|
122
|
+
|
123
|
+
add_test :parse_expr_bit_or, jses, exprs
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def test_parse_expr_logical
|
128
|
+
jses = [
|
129
|
+
'a + 1 == 0 && b - 1 > 3',
|
130
|
+
'a + 1 == 0 && b === c || a - b == c - d && c > 0'
|
131
|
+
]
|
132
|
+
|
133
|
+
exprs = [
|
134
|
+
'(&&,(==,(+,a,1),0),(>,(-,b,1),3))',
|
135
|
+
'(||,(&&,(==,(+,a,1),0),(===,b,c)),(&&,(==,(-,a,b),(-,c,d)),(>,c,0)))'
|
136
|
+
]
|
137
|
+
|
138
|
+
add_test :parse_expr_logical_or, jses, exprs
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module XRayTest
|
2
|
+
module JS
|
3
|
+
module Expr
|
4
|
+
|
5
|
+
module Primary
|
6
|
+
|
7
|
+
def test_parse_expr_parentheses
|
8
|
+
expr = parse_js :parse_expr_parentheses, '(12.56)'
|
9
|
+
|
10
|
+
assert_equal 'parentheses', expr.type
|
11
|
+
assert_equal '12.56', expr.node.text
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_parse_expr_array
|
15
|
+
js = '[123, 456, "hello world", /this is re/]'
|
16
|
+
expr = parse_js :parse_expr_array, js
|
17
|
+
|
18
|
+
assert_equal 'array', expr.type
|
19
|
+
assert_equal ['123', '456', '"hello world"', '/this is re/'],
|
20
|
+
expr.node.elements.collect(&:text)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_parse_expr_object
|
24
|
+
js = '{
|
25
|
+
name: "hello 1234",
|
26
|
+
"key 2": 123.567e12,
|
27
|
+
12.59: 12.58,
|
28
|
+
18: /hello/
|
29
|
+
}'
|
30
|
+
|
31
|
+
expr = parse_js :parse_expr_object, js
|
32
|
+
|
33
|
+
assert_equal 'object', expr.type
|
34
|
+
|
35
|
+
assert_equal ['name', '"key 2"', '12.59', '18'],
|
36
|
+
expr.node.elements.collect(&:left).collect(&:text)
|
37
|
+
|
38
|
+
assert_equal ['"hello 1234"', '123.567e12', '12.58', '/hello/'],
|
39
|
+
expr.node.elements.collect(&:right).collect(&:text)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_parse_expr_literal
|
43
|
+
jses = [
|
44
|
+
'this.a = 123',
|
45
|
+
'null === a',
|
46
|
+
'true === false',
|
47
|
+
'false === true',
|
48
|
+
|
49
|
+
'123 == a',
|
50
|
+
'.12 = b',
|
51
|
+
'0.123 = c',
|
52
|
+
'+123e+1 = d',
|
53
|
+
'123.123e-1 = e',
|
54
|
+
'12345.123e123',
|
55
|
+
|
56
|
+
'/\\\\/g,a = "bcd"'
|
57
|
+
]
|
58
|
+
|
59
|
+
str1 = %q("hello this is a single line str\\"ing")
|
60
|
+
str2 = %q('hello this is a \'single line string')
|
61
|
+
|
62
|
+
str3 = '"hello this is an mutiline string\\
|
63
|
+
hello this is an mutiline string\\
|
64
|
+
hello this is an mutiline string"'
|
65
|
+
|
66
|
+
re1 = %q{/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g}
|
67
|
+
|
68
|
+
re2 = %q{/\/\*[^*]*\*+([^/][^*]*\*+)*\//gm}
|
69
|
+
|
70
|
+
jses << str1 << str2 << str3 << re1 << re2
|
71
|
+
|
72
|
+
exprs = jses.collect do |js|
|
73
|
+
parse_js :parse_expr_primary, js
|
74
|
+
end
|
75
|
+
|
76
|
+
eqs = %w(
|
77
|
+
this null true false
|
78
|
+
123 .12 0.123 +123e+1 123.123e-1 12345.123e123
|
79
|
+
/\\\\/g
|
80
|
+
)
|
81
|
+
eqs << str1 << str2 << str3 << re1 << re2
|
82
|
+
assert_equal eqs, exprs.collect(&:node).collect(&:text)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../helper', File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'js/parser'
|
5
|
+
|
6
|
+
require_relative 'expr/expr'
|
7
|
+
require_relative 'stat/stat'
|
8
|
+
|
9
|
+
|
10
|
+
module XRayTest
|
11
|
+
module JS
|
12
|
+
class ParserTest < Test::Unit::TestCase
|
13
|
+
include XRay::JS
|
14
|
+
|
15
|
+
include Expr::Expr
|
16
|
+
include Stat::Stat
|
17
|
+
|
18
|
+
|
19
|
+
def test_parse_program()
|
20
|
+
js = '
|
21
|
+
/**
|
22
|
+
* this is mutiline comment
|
23
|
+
*/
|
24
|
+
function say(name1, name2/*inline comment*/, name3) {
|
25
|
+
alert(name + " hello world");
|
26
|
+
console.debug(name2);
|
27
|
+
name1 + name2 + // line comment
|
28
|
+
name3;
|
29
|
+
}
|
30
|
+
; // hello this is comment
|
31
|
+
var a = 1;
|
32
|
+
a++;
|
33
|
+
'
|
34
|
+
parser = create_parser(js)
|
35
|
+
program = parser.parse_program
|
36
|
+
elms = program.elements
|
37
|
+
|
38
|
+
assert_equal 4, elms.length
|
39
|
+
|
40
|
+
s_comments = parser.singleline_comments
|
41
|
+
assert_equal 2, s_comments.length
|
42
|
+
|
43
|
+
m_comments = parser.mutiline_comments
|
44
|
+
assert_equal 2, m_comments.length
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_parse_function_declaration
|
48
|
+
js = '
|
49
|
+
function sum(a1, a2, a3) {
|
50
|
+
a1 = a2 + a3;
|
51
|
+
a2 = a2 * a3;
|
52
|
+
a3++;
|
53
|
+
}
|
54
|
+
'
|
55
|
+
|
56
|
+
parser = create_parser(js)
|
57
|
+
func = parser.parse_function_declaration
|
58
|
+
|
59
|
+
assert_equal 'sum', func.name.text
|
60
|
+
assert_equal '[a1,a2,a3]', func.parameters.text
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_parse_singleline_comment
|
64
|
+
js = '
|
65
|
+
// this is simple comment
|
66
|
+
a = 123;
|
67
|
+
'
|
68
|
+
|
69
|
+
comment = parse_js :parse_singleline_comment, js
|
70
|
+
assert_equal '// this is simple comment', comment.text
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
def create_parser(js)
|
75
|
+
Parser.new js, XRayTest::Logger.new
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def parse_js(action, js)
|
80
|
+
parser = create_parser js
|
81
|
+
parser.send(action)
|
82
|
+
end
|
83
|
+
|
84
|
+
def add_test(action, jses, exprs)
|
85
|
+
if jses.class == String
|
86
|
+
jses = [jses]
|
87
|
+
exprs = [exprs]
|
88
|
+
end
|
89
|
+
jses.each_with_index do |js, index|
|
90
|
+
expr = parse_js action, js
|
91
|
+
assert_equal exprs[index], expr.text
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative 'base_test'
|
3
|
+
|
4
|
+
require 'js/rule/checklist'
|
5
|
+
|
6
|
+
module XRayTest
|
7
|
+
module JS
|
8
|
+
module Rule
|
9
|
+
|
10
|
+
class AlertCheckTest < BaseTest
|
11
|
+
|
12
|
+
def test_visit_alert
|
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
|
23
|
+
|
24
|
+
private
|
25
|
+
|
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
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative '../../helper'
|
2
|
+
|
3
|
+
require 'js/rule/all'
|
4
|
+
|
5
|
+
module XRayTest
|
6
|
+
module JS
|
7
|
+
module Rule
|
8
|
+
|
9
|
+
class AllTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_initialize
|
12
|
+
all = XRay::JS::Rule::All.new
|
13
|
+
|
14
|
+
assert all.respond_to?(:each)
|
15
|
+
assert all.size > 0
|
16
|
+
assert all.size == all.rules.size
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../../helper'
|
2
|
+
|
3
|
+
require 'js/parser'
|
4
|
+
require 'parser_visitable'
|
5
|
+
|
6
|
+
module XRayTest
|
7
|
+
|
8
|
+
module JS
|
9
|
+
module Rule
|
10
|
+
|
11
|
+
class VisitableParser < XRay::JS::Parser
|
12
|
+
include XRay::ParserVisitable
|
13
|
+
end
|
14
|
+
|
15
|
+
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
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'base_test'
|
4
|
+
|
5
|
+
require 'js/rule/file_checker'
|
6
|
+
|
7
|
+
module XRayTest
|
8
|
+
module JS
|
9
|
+
module Rule
|
10
|
+
|
11
|
+
class FileCheckerTest < BaseTest
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@checker = XRay::JS::Rule::FileChecker.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_merge_file?
|
18
|
+
assert @checker.merge_file?('abc-merge.js')
|
19
|
+
assert @checker.merge_file?('abc-merge-12345.js')
|
20
|
+
assert @checker.merge_file?('abc-merge12345.js')
|
21
|
+
|
22
|
+
assert !@checker.merge_file?('abc-merge.css')
|
23
|
+
assert !@checker.merge_file?('abc-merge.jss')
|
24
|
+
assert !@checker.merge_file?('-merge.js')
|
25
|
+
assert !@checker.merge_file?('abc-mergeabc.js')
|
26
|
+
assert !@checker.merge_file?('abc-merge-.js')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_min_file?
|
30
|
+
assert @checker.min_file?('abc-min.js')
|
31
|
+
|
32
|
+
assert !@checker.min_file?('abc-min.jss')
|
33
|
+
assert !@checker.min_file?('-min.js')
|
34
|
+
assert !@checker.min_file?('abc-min1.js')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_grep_import_js_path
|
38
|
+
pathes = @checker.grep_import_js_path fixture_01
|
39
|
+
assert_equal 6, pathes.length
|
40
|
+
assert_equal %w(
|
41
|
+
http://style.china.alibaba.com/js/common/aliclick.js
|
42
|
+
http://style.china.alibaba.com/js/detail/xpclick.js
|
43
|
+
http://style.china.alibaba.com/js/lib/fdev-v4/widget/util/debug-min.js
|
44
|
+
http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/alitalk-min.js
|
45
|
+
http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js
|
46
|
+
http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js
|
47
|
+
), pathes.collect { |path| path[:url] }
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_check_one_line_one_import
|
51
|
+
pathes = @checker.grep_import_js_path fixture_01
|
52
|
+
ret = []
|
53
|
+
pathes.each { |import|
|
54
|
+
r = @checker.check_js_merge_importing import, pathes, ""
|
55
|
+
ret.concat r
|
56
|
+
}
|
57
|
+
assert_equal [
|
58
|
+
["merge文件需要引用压缩版的js, 如a-min.js", :warn, 12, 63],
|
59
|
+
["merge文件需要引用压缩版的js, 如a-min.js", :warn, 13, 62],
|
60
|
+
["一行只能有一个import文件", :error, 18, 96],
|
61
|
+
["merge文件格式不正确", :error, 19, 1],
|
62
|
+
["merge文件格式不正确", :error, 20, 1]
|
63
|
+
], ret
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_has_doc_comment
|
67
|
+
js = '
|
68
|
+
/**
|
69
|
+
* 功能文件头必须要有文档注释
|
70
|
+
* 现在没有对doc格式进行规范
|
71
|
+
* 所以暂时不做文档注释内容检查
|
72
|
+
* 只检则有注释
|
73
|
+
*/
|
74
|
+
|
75
|
+
i++;
|
76
|
+
'
|
77
|
+
assert @checker.has_doc_comment?(js)
|
78
|
+
|
79
|
+
js = '
|
80
|
+
i++;
|
81
|
+
'
|
82
|
+
assert !@checker.has_doc_comment?(js)
|
83
|
+
|
84
|
+
js = '
|
85
|
+
/*
|
86
|
+
* 多行注释不属于文档注释
|
87
|
+
* 文档注释需要以/*开头
|
88
|
+
*/
|
89
|
+
'
|
90
|
+
assert !@checker.has_doc_comment?(js)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_check_import_js_scope
|
94
|
+
#not implements
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_check_import_js_exist_and_min
|
98
|
+
# not implements
|
99
|
+
end
|
100
|
+
|
101
|
+
def fixture_01
|
102
|
+
<<END
|
103
|
+
(function(){
|
104
|
+
ImportJavscript = {
|
105
|
+
url:function(url){
|
106
|
+
document.write('<script type="text/javascript" src="'+url+'"></scr'+'ipt>');
|
107
|
+
}
|
108
|
+
};
|
109
|
+
})();
|
110
|
+
|
111
|
+
/**
|
112
|
+
* 打点依赖库
|
113
|
+
*/
|
114
|
+
ImportJavscript.url('http://style.china.alibaba.com/js/common/aliclick.js');
|
115
|
+
ImportJavscript.url(http://style.china.alibaba.com/js/detail/xpclick.js);
|
116
|
+
|
117
|
+
/**
|
118
|
+
* 旺铺基础JS
|
119
|
+
*/
|
120
|
+
ImportJavscript.url("http://style.china.alibaba.com/js/lib/fdev-v4/widget/util/debug-min.js"); ImportJavscript.url("http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/alitalk-min.js");
|
121
|
+
ImportJavscript.url( 'http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js' ) ;
|
122
|
+
ImportJavscript.url('http://style.china.alibaba.com/js/lib/fdev-v4/widget/web/sweet-min.js')
|
123
|
+
END
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require_relative 'base_test'
|
3
|
+
|
4
|
+
require 'js/rule/checklist'
|
5
|
+
|
6
|
+
module XRayTest
|
7
|
+
module JS
|
8
|
+
module Rule
|
9
|
+
|
10
|
+
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
|
+
|
35
|
+
def test_check_forbit_method_call
|
36
|
+
jses = %w(
|
37
|
+
$.sub
|
38
|
+
$.noConflict
|
39
|
+
jQuery.sub
|
40
|
+
jQuery.noConflict
|
41
|
+
)
|
42
|
+
|
43
|
+
jses.each do |js|
|
44
|
+
r = visit js
|
45
|
+
assert r.include?( ['禁止使用jQuery.sub()和jQuery.noConflict方法', :error] )
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_check_data_call_param
|
50
|
+
jses = %w(
|
51
|
+
$.data("doc-config")
|
52
|
+
jQuery.data('doc-config')
|
53
|
+
)
|
54
|
+
jses.each do |js|
|
55
|
+
r = visit js
|
56
|
+
assert r.include? ['使用".data()"读写自定义属性时需要转化成驼峰形式', :error]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_check_ctor_selector
|
61
|
+
jses = [
|
62
|
+
"$('.myclass', div)",
|
63
|
+
"$('.myclass')",
|
64
|
+
"$('[name=123]')",
|
65
|
+
"$(':first')"
|
66
|
+
]
|
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
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|