2
2
// See end of file for extended copyright information.
3
3
4
4
#include < cstring>
5
- #include < gmock/gmock.h>
6
5
#include < gtest/gtest.h>
7
6
#include < quick-lint-js/container/padded-string.h>
8
7
#include < quick-lint-js/diag-collector.h>
14
13
#include < quick-lint-js/port/char8.h>
15
14
#include < quick-lint-js/variable-analyzer-support.h>
16
15
17
- using ::testing::ElementsAreArray;
18
- using ::testing::IsEmpty;
19
-
20
16
namespace quick_lint_js {
21
17
namespace {
22
18
TEST (Test_Variable_Analyzer_Parse,
23
19
let_variable_use_before_declaration_with_parsing) {
24
- Padded_String input (u8" let x = y, y = x;" _sv);
25
- Diag_Collector v;
26
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
27
- Parser p (&input, &v, javascript_options);
28
- EXPECT_TRUE (p.parse_and_visit_statement (l));
29
- l.visit_end_of_module ();
30
-
31
- EXPECT_THAT (
32
- v.errors ,
33
- ElementsAreArray ({
34
- DIAG_TYPE_2_OFFSETS (&input, Diag_Variable_Used_Before_Declaration, //
35
- use, 8 , u8" y" _sv, declaration, 11 , u8" y" _sv),
36
- }));
20
+ test_parse_and_analyze (
21
+ u8" let x = y, y = x;" _sv, //
22
+ u8" ^ Diag_Variable_Used_Before_Declaration.declaration\n " _diag //
23
+ u8" ^ .use" _diag,
24
+ javascript_analyze_options, default_globals);
37
25
}
38
26
39
27
TEST (Test_Variable_Analyzer_Parse, generic_parameter_use_before_declaration) {
40
- Padded_String input (u8" function f<T extends T>() {}" _sv);
41
- Diag_Collector v;
42
- Variable_Analyzer l (&v, &default_globals, typescript_var_options);
43
- Parser p (&input, &v, typescript_options);
44
- EXPECT_TRUE (p.parse_and_visit_statement (l));
45
- l.visit_end_of_module ();
46
-
47
- EXPECT_THAT (v.errors ,
48
- ElementsAreArray ({
49
- DIAG_TYPE_2_OFFSETS (
50
- &input, Diag_Cyclic_TypeScript_Type_Definition, //
51
- use, u8" function f<T extends " _sv.size (), u8" T" _sv, //
52
- declaration, u8" function f<" _sv.size (), u8" T" _sv),
53
- }));
28
+ test_parse_and_analyze (
29
+ u8" function f<T extends T>() {}" _sv,
30
+ u8" ^ Diag_Cyclic_TypeScript_Type_Definition.use\n " _diag
31
+ u8" ^ .declaration" _diag,
32
+ typescript_analyze_options.with_allow_parse_errors (), default_globals);
54
33
}
55
34
56
35
TEST (
57
36
Test_Variable_Analyzer_Parse,
58
37
variables_with_different_escape_sequences_are_equivalent_after_normalization) {
59
- Padded_String input (u8" let \\ u{69} = 0; i += 1; \\ u0069;" _sv);
60
- Diag_Collector v;
61
-
62
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
63
- Parser p (&input, &v, javascript_options);
64
- p.parse_and_visit_module (l);
65
-
66
- EXPECT_THAT (v.errors , IsEmpty ());
38
+ test_parse_and_analyze (u8" let \\ u{69} = 0; i += 1; \\ u0069;" _sv, no_diags,
39
+ javascript_analyze_options, default_globals);
67
40
}
68
41
69
42
TEST (Test_Variable_Analyzer_Parse,
@@ -77,38 +50,19 @@ TEST(Test_Variable_Analyzer_Parse,
77
50
78
51
TEST (Test_Variable_Analyzer_Parse,
79
52
escape_sequences_are_allowed_for_arguments_variable) {
80
- Padded_String input (u8R"( function f() { return \u{61}rgument\u{73}; })" _sv);
81
- Diag_Collector v;
82
-
83
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
84
- Parser p (&input, &v, javascript_options);
85
- p.parse_and_visit_module (l);
86
-
87
- EXPECT_THAT (v.errors , IsEmpty ());
53
+ test_parse_and_analyze (u8R"( function f() { return \u{61}rgument\u{73}; })" _sv,
54
+ no_diags, javascript_analyze_options, default_globals);
88
55
}
89
56
90
57
TEST (Test_Variable_Analyzer_Parse,
91
58
function_statement_inside_if_does_not_conflict_with_let_variable) {
92
- Padded_String input (u8" let f;\n if (true)\n function f() {}" _sv);
93
-
94
- Diag_Collector v;
95
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
96
- Parser p (&input, &v, javascript_options);
97
- p.parse_and_visit_module (l);
98
-
99
- EXPECT_THAT (v.errors , IsEmpty ());
59
+ test_parse_and_analyze (u8" let f;\n if (true)\n function f() {}" _sv, no_diags,
60
+ javascript_analyze_options, default_globals);
100
61
}
101
62
102
63
TEST (Test_Variable_Analyzer_Parse, typeof_with_conditional_operator) {
103
- {
104
- Padded_String input (u8" typeof x ? 10 : 20;" _sv);
105
- Diag_Collector v;
106
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
107
- Parser p (&input, &v, javascript_options);
108
- p.parse_and_visit_module (l);
109
-
110
- EXPECT_THAT (v.errors , IsEmpty ());
111
- }
64
+ test_parse_and_analyze (u8" typeof x ? 10 : 20;" _sv, no_diags,
65
+ javascript_analyze_options, default_globals);
112
66
}
113
67
114
68
TEST (Test_Variable_Analyzer_Parse, prefix_plusplus_on_const_variable) {
@@ -118,27 +72,13 @@ TEST(Test_Variable_Analyzer_Parse, prefix_plusplus_on_const_variable) {
118
72
u8" ^ .declaration" _diag,
119
73
javascript_analyze_options, default_globals);
120
74
121
- {
122
- Padded_String input (u8" const x = {y : 10};\n ++x.y;" _sv);
123
- Diag_Collector v;
124
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
125
- Parser p (&input, &v, javascript_options);
126
- p.parse_and_visit_module (l);
127
-
128
- EXPECT_THAT (v.errors , IsEmpty ());
129
- }
75
+ test_parse_and_analyze (u8" const x = {y : 10};\n ++x.y;" _sv, no_diags,
76
+ javascript_analyze_options, default_globals);
130
77
}
131
78
132
79
TEST (Test_Variable_Analyzer_Parse, prefix_plusplus_plus_operand) {
133
- {
134
- Padded_String input (u8" const x = [42]; ++x[0];" _sv);
135
- Diag_Collector v;
136
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
137
- Parser p (&input, &v, javascript_options);
138
- p.parse_and_visit_module (l);
139
-
140
- EXPECT_THAT (v.errors , IsEmpty ());
141
- }
80
+ test_parse_and_analyze (u8" const x = [42]; ++x[0];" _sv, no_diags,
81
+ javascript_analyze_options, default_globals);
142
82
143
83
test_parse_and_analyze (
144
84
u8" const x = 42;\n const y =10;\n ++x + y;" _sv, //
@@ -148,124 +88,62 @@ TEST(Test_Variable_Analyzer_Parse, prefix_plusplus_plus_operand) {
148
88
}
149
89
150
90
TEST (Test_Variable_Analyzer_Parse, use_await_label_in_non_async_function) {
151
- Padded_String input (u8" function f() {await: for(;;){break await;}}" _sv);
152
- Diag_Collector v;
153
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
154
- Parser p (&input, &v, javascript_options);
155
- p.parse_and_visit_module (l);
156
-
157
- EXPECT_THAT (v.errors , IsEmpty ());
91
+ test_parse_and_analyze (u8" function f() {await: for(;;){break await;}}" _sv,
92
+ no_diags, javascript_analyze_options, default_globals);
158
93
}
159
94
160
95
TEST (Test_Variable_Analyzer_Parse, use_yield_label_in_non_generator_function) {
161
- Padded_String input (u8" function f() {yield: for(;;){break yield;}}" _sv);
162
- Diag_Collector v;
163
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
164
- Parser p (&input, &v, javascript_options);
165
- p.parse_and_visit_module (l);
166
-
167
- EXPECT_THAT (v.errors , IsEmpty ());
96
+ test_parse_and_analyze (u8" function f() {yield: for(;;){break yield;}}" _sv,
97
+ no_diags, javascript_analyze_options, default_globals);
168
98
}
169
99
170
100
TEST (Test_Variable_Analyzer_Parse, escape_sequence_in_keyword_identifier) {
171
101
// The parser should not report a stray 'finally' keyword.
172
102
// The linter should not report that 'finally' is undeclared.
173
- Padded_String input (u8" let which = \\ u{66}inally;" _sv);
174
- Diag_Collector v;
175
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
176
- Parser p (&input, &v, javascript_options);
177
- p.parse_and_visit_module (l);
178
-
179
- EXPECT_THAT (v.errors ,
180
- ElementsAreArray ({
181
- DIAG_TYPE (Diag_Keywords_Cannot_Contain_Escape_Sequences),
182
- }));
103
+ test_parse_and_analyze (u8" let which = \\ u{66}inally;" _sv,
104
+ u8" Diag_Keywords_Cannot_Contain_Escape_Sequences" _diag,
105
+ javascript_analyze_options.with_allow_parse_errors (),
106
+ default_globals);
183
107
}
184
108
185
109
TEST (Test_Variable_Analyzer_Parse, delete_local_variable) {
186
- Padded_String input (
187
- u8" function f(param) { let v; delete v; delete param; }" _sv);
188
- Diag_Collector v;
189
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
190
- Parser p (&input, &v, javascript_options);
191
- p.parse_and_visit_module (l);
192
-
193
- EXPECT_THAT (v.errors ,
194
- ElementsAreArray ({
195
- DIAG_TYPE (Diag_Redundant_Delete_Statement_On_Variable),
196
- DIAG_TYPE (Diag_Redundant_Delete_Statement_On_Variable),
197
- }));
110
+ test_parse_and_analyze (
111
+ u8" function f(param) { let v; delete v; delete param; }" _sv,
112
+ u8" Diag_Redundant_Delete_Statement_On_Variable" _diag,
113
+ u8" Diag_Redundant_Delete_Statement_On_Variable" _diag,
114
+ javascript_analyze_options, default_globals);
198
115
}
199
116
200
117
TEST (Test_Variable_Analyzer_Parse, extends_self) {
201
- {
202
- Padded_String input (
203
- u8" function C() {}\n " _sv
204
- u8" {\n " _sv
205
- u8" class C extends C {}" _sv
206
- u8" }" _sv);
207
- Diag_Collector v;
208
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
209
- Parser p (&input, &v, javascript_options);
210
- p.parse_and_visit_module (l);
211
-
212
- EXPECT_THAT (v.errors , ElementsAreArray ({
213
- DIAG_TYPE (Diag_Variable_Used_Before_Declaration),
214
- }));
215
- }
216
-
217
- {
218
- Padded_String input (
219
- u8" function C() {}\n " _sv
220
- u8" {\n " _sv
221
- u8" class C extends (null, [C][0], Object) {}" _sv
222
- u8" }" _sv);
223
- Diag_Collector v;
224
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
225
- Parser p (&input, &v, javascript_options);
226
- p.parse_and_visit_module (l);
227
-
228
- EXPECT_THAT (v.errors , ElementsAreArray ({
229
- DIAG_TYPE (Diag_Variable_Used_Before_Declaration),
230
- }));
231
- }
232
-
233
- {
234
- Padded_String input (
235
- u8" function C() {}\n " _sv
236
- u8" {\n " _sv
237
- u8" (class C extends C {})" _sv
238
- u8" }" _sv);
239
- Diag_Collector v;
240
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
241
- Parser p (&input, &v, javascript_options);
242
- p.parse_and_visit_module (l);
243
-
244
- EXPECT_THAT (v.errors , ElementsAreArray ({
245
- DIAG_TYPE (Diag_Variable_Used_Before_Declaration),
246
- }));
247
- }
118
+ test_parse_and_analyze (
119
+ u8" function C() {}\n " _sv
120
+ u8" {\n " _sv
121
+ u8" class C extends C {}" _sv
122
+ u8" }" _sv,
123
+ u8" Diag_Variable_Used_Before_Declaration" _diag,
124
+ javascript_analyze_options, default_globals);
125
+ test_parse_and_analyze (
126
+ u8" function C() {}\n " _sv
127
+ u8" {\n " _sv
128
+ u8" class C extends (null, [C][0], Object) {}" _sv
129
+ u8" }" _sv,
130
+ u8" Diag_Variable_Used_Before_Declaration" _diag,
131
+ javascript_analyze_options, default_globals);
132
+ test_parse_and_analyze (
133
+ u8" function C() {}\n " _sv
134
+ u8" {\n " _sv
135
+ u8" (class C extends C {})" _sv
136
+ u8" }" _sv,
137
+ u8" Diag_Variable_Used_Before_Declaration" _diag,
138
+ javascript_analyze_options, default_globals);
248
139
}
249
140
250
141
TEST (Test_Variable_Analyzer_Parse,
251
142
typescript_static_block_can_reference_class) {
252
- {
253
- Padded_String input (u8" class C { static { C; } }" _sv);
254
- Diag_Collector v;
255
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
256
- Parser p (&input, &v, typescript_options);
257
- p.parse_and_visit_module (l);
258
- EXPECT_THAT (v.errors , IsEmpty ());
259
- }
260
-
261
- {
262
- Padded_String input (u8" (class C { static { C; } });" _sv);
263
- Diag_Collector v;
264
- Variable_Analyzer l (&v, &default_globals, javascript_var_options);
265
- Parser p (&input, &v, typescript_options);
266
- p.parse_and_visit_module (l);
267
- EXPECT_THAT (v.errors , IsEmpty ());
268
- }
143
+ test_parse_and_analyze (u8" class C { static { C; } }" _sv, no_diags,
144
+ javascript_analyze_options, default_globals);
145
+ test_parse_and_analyze (u8" (class C { static { C; } });" _sv, no_diags,
146
+ javascript_analyze_options, default_globals);
269
147
}
270
148
}
271
149
}
0 commit comments