@@ -15,97 +15,101 @@ local math_min = _G.math.min
1515local math_max = _G .math .max
1616local math_huge = _G .math .huge
1717
18- function coverage .Preprocess (code , key )
19- local expressions = {}
20-
21- local function inject_call_expression (parser , node , start , stop )
22- if node .environment == " typesystem" then return node end
18+ local function inject_call_expression (parser , node , start , stop )
19+ local expressions = parser .config .coverage_expressions
2320
24- if node .Type == " expression_postfix_call " and node . type_call then return node end
21+ if node .environment == " typesystem " then return node end
2522
26- if node .Type == " expression_function" then
27- -- don't mark the funciton body as being called
28- start , stop = node .tokens [" function" ].start , node .tokens [" function" ].stop
29- end
23+ if node .Type == " expression_postfix_call" and node .type_call then return node end
3024
31- if node .Type == " expression_table " then
32- -- don't mark the funciton body as being called
33- start , stop = node .tokens [" { " ].start , node .tokens [" { " ].stop
34- end
25+ if node .Type == " expression_function " then
26+ -- don't mark the funciton body as being called
27+ start , stop = node .tokens [" function " ].start , node .tokens [" function " ].stop
28+ end
3529
36- local call_expression = parser :ParseString (" " .. FUNC_NAME .. " (" .. start .. " ," .. stop .. " ,x)" ).statements [1 ].value
30+ if node .Type == " expression_table" then
31+ -- don't mark the funciton body as being called
32+ start , stop = node .tokens [" {" ].start , node .tokens [" {" ].stop
33+ end
3734
38- if node .Type == " expression_postfix_call" and not node .tokens [" call(" ] then
39- node .tokens [" call(" ] = parser :NewToken (" symbol" , " (" )
40- node .tokens [" call)" ] = parser :NewToken (" symbol" , " )" )
41- end
35+ local call_expression = parser :ParseString (" " .. FUNC_NAME .. " (" .. start .. " ," .. stop .. " ,x)" ).statements [1 ].value
4236
43- call_expression .expressions [3 ] = node
37+ if node .Type == " expression_postfix_call" and not node .tokens [" call(" ] then
38+ node .tokens [" call(" ] = parser :NewToken (" symbol" , " (" )
39+ node .tokens [" call)" ] = parser :NewToken (" symbol" , " )" )
40+ end
4441
45- if node .Type == " expression_binary_operator" and node .right then
46- call_expression .right = node .right
47- end
42+ call_expression .expressions [3 ] = node
4843
49- table_insert (expressions , node )
50- -- to prevent start stop messing up from previous injections
51- call_expression .code_start = node .code_start
52- call_expression .code_stop = node .code_stop
53- return call_expression
44+ if node .Type == " expression_binary_operator" and node .right then
45+ call_expression .right = node .right
5446 end
5547
56- local function inject_token (token )
57- token :ReplaceValue (
58- " " .. FUNC_NAME .. " (" .. token .start .. " ," .. token .stop .. " ,x) " .. token :GetValueString ()
59- )
48+ table_insert (expressions , node )
49+ -- to prevent start stop messing up from previous injections
50+ call_expression .code_start = node .code_start
51+ call_expression .code_stop = node .code_stop
52+ return call_expression
53+ end
54+
55+ local function inject_token (token )
56+ token :ReplaceValue (
57+ " " .. FUNC_NAME .. " (" .. token .start .. " ," .. token .stop .. " ,x) " .. token :GetValueString ()
58+ )
59+ end
60+
61+ local function on_parsed_coverage_node (parser , node )
62+ if node .is_statement then
63+ -- inject a call right before the token itself which uses the token for range
64+ if node .Type == " statement_return" then
65+ inject_token (node .tokens [" return" ])
66+ elseif node .Type == " statement_break" then
67+ inject_token (node .tokens [" break" ])
68+ elseif node .Type == " statement_continue" then
69+ inject_token (node .tokens [" continue" ])
70+ elseif node .Type == " statement_call_expression" then
71+ local start , stop = node :GetStartStop ()
72+ node .value = inject_call_expression (parser , node .value , start , stop )
73+ end
74+ elseif node .is_expression then
75+ if
76+ node .is_left_assignment or
77+ node .is_identifier or
78+ (
79+ node :GetStatement ().Type == " statement_function" or
80+ node :GetStatement ().Type == " statement_type_function"
81+ )
82+ or
83+ (
84+ node .Type == " expression_binary_operator" and
85+ node .value .sub_type == " :"
86+ )
87+ or
88+ (
89+ node .parent and
90+ node .parent .Type == " expression_binary_operator" and
91+ (
92+ node .parent .value .sub_type == " ." or
93+ node .parent .value .sub_type == " :"
94+ )
95+ )
96+ then
97+ return
98+ end
99+
100+ return inject_call_expression (parser , node , node :GetStartStop ())
60101 end
102+ end
61103
104+ function coverage .Preprocess (code , key )
105+ local expressions = {}
62106 local compiler = nl .Compiler (
63107 code ,
64108 key ,
65109 {
66110 parser = {
67- on_parsed_node = function (parser , node )
68- if node .is_statement then
69- -- inject a call right before the token itself which uses the token for range
70- if node .Type == " statement_return" then
71- inject_token (node .tokens [" return" ])
72- elseif node .Type == " statement_break" then
73- inject_token (node .tokens [" break" ])
74- elseif node .Type == " statement_continue" then
75- inject_token (node .tokens [" continue" ])
76- elseif node .Type == " statement_call_expression" then
77- local start , stop = node :GetStartStop ()
78- node .value = inject_call_expression (parser , node .value , start , stop )
79- end
80- elseif node .is_expression then
81- if
82- node .is_left_assignment or
83- node .is_identifier or
84- (
85- node :GetStatement ().Type == " statement_function" or
86- node :GetStatement ().Type == " statement_type_function"
87- )
88- or
89- (
90- node .Type == " expression_binary_operator" and
91- node .value .sub_type == " :"
92- )
93- or
94- (
95- node .parent and
96- node .parent .Type == " expression_binary_operator" and
97- (
98- node .parent .value .sub_type == " ." or
99- node .parent .value .sub_type == " :"
100- )
101- )
102- then
103- return
104- end
105-
106- return inject_call_expression (parser , node , node :GetStartStop ())
107- end
108- end ,
111+ coverage_expressions = expressions ,
112+ on_parsed_node = on_parsed_coverage_node ,
109113 skip_import = true ,
110114 },
111115 }
0 commit comments