1use std::fmt;
2
3use full_moon::{ast::Ast, LuaVersion};
4
5use crate::{
6 ast_converter::{AstConverter, ConvertError},
7 nodes::*,
8 utils::Timer,
9};
10
11#[derive(Clone, Debug, Default, PartialEq, Eq)]
13pub struct Parser {
14 hold_token_data: bool,
15}
16
17impl Parser {
18 pub fn parse(&self, code: &str) -> Result<Block, ParserError> {
20 let full_moon_parse_timer = Timer::now();
21 let parse_result = full_moon::parse_fallible(code, LuaVersion::luau()).into_result();
22 log::trace!(
23 "full-moon parsing done in {}",
24 full_moon_parse_timer.duration_label()
25 );
26 parse_result.map_err(ParserError::parsing).and_then(|ast| {
27 log::trace!("start converting full-moon AST");
28 let conversion_timer = Timer::now();
29 let block = self.convert_ast(ast).map_err(ParserError::converting);
30 log::trace!(
31 " ⨽ completed AST conversion in {}",
32 conversion_timer.duration_label()
33 );
34 block
35 })
36 }
37
38 pub fn preserve_tokens(mut self) -> Self {
40 self.hold_token_data = true;
41 self
42 }
43
44 pub(crate) fn is_preserving_tokens(&self) -> bool {
45 self.hold_token_data
46 }
47
48 #[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", skip_all))]
49 fn convert_ast(&self, ast: Ast) -> Result<Block, ConvertError> {
50 AstConverter::new(self.hold_token_data).convert(&ast)
51 }
52}
53
54#[derive(Clone, Debug)]
55enum ParserErrorKind {
56 Parsing(Vec<full_moon::Error>),
57 Converting(ConvertError),
58}
59
60#[derive(Clone, Debug)]
62pub struct ParserError {
63 kind: Box<ParserErrorKind>,
64}
65
66impl ParserError {
67 fn parsing(err: Vec<full_moon::Error>) -> Self {
68 Self {
69 kind: ParserErrorKind::Parsing(err).into(),
70 }
71 }
72
73 fn converting(err: ConvertError) -> Self {
74 Self {
75 kind: ParserErrorKind::Converting(err).into(),
76 }
77 }
78}
79
80impl fmt::Display for ParserError {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match &*self.kind {
83 ParserErrorKind::Parsing(errors) => {
84 for err in errors {
85 writeln!(f, "{}", err)?;
86 }
87 Ok(())
88 }
89 ParserErrorKind::Converting(err) => write!(f, "{}", err),
90 }
91 }
92}
93
94#[cfg(test)]
95mod test {
96 use std::str::FromStr;
97
98 use crate::nodes::ReturnStatement;
99
100 use super::*;
101
102 macro_rules! test_parse {
103 ($($name:ident($input:literal) => $value:expr),* $(,)?) => {
104 $(
105 #[test]
106 fn $name() {
107 let parser = Parser::default();
108 let block = parser.parse($input)
109 .expect(&format!("failed to parse `{}`", $input));
110
111 let expect_block = $value.into();
112 pretty_assertions::assert_eq!(block, expect_block);
113 }
114 )*
115 };
116 }
117
118 test_parse!(
119 empty_string("") => Block::default(),
120 single_line_comment("-- todo") => Block::default(),
121 empty_do("do end") => DoStatement::default(),
122 empty_do_nested("do do end end") => DoStatement::new(DoStatement::default().into()),
123 two_nested_empty_do_in_do_statement("do do end do end end") => DoStatement::new(
124 Block::default().with_statement(DoStatement::default()).with_statement(DoStatement::default())
125 ),
126 triple_nested_do_statements("do do end do do end end end") => DoStatement::new(
127 Block::default()
128 .with_statement(DoStatement::default())
129 .with_statement(DoStatement::new(DoStatement::default().into()))
130 ),
131 do_return_end("do return end") => DoStatement::new(ReturnStatement::default().into()),
132 break_statement("break") => LastStatement::new_break(),
133 return_no_values("return") => ReturnStatement::default(),
134 return_true("return true") => ReturnStatement::one(Expression::from(true)),
135 return_false("return false") => ReturnStatement::one(false),
136 return_nil("return nil") => ReturnStatement::one(Expression::nil()),
137 return_variable_arguments("return ...") => ReturnStatement::one(Expression::variable_arguments()),
138 return_variable("return var") => ReturnStatement::one(Expression::identifier("var")),
139 return_parentheses_true("return (true)") => ReturnStatement::one(
140 Expression::from(true).in_parentheses(),
141 ),
142 return_true_false("return true, false") => ReturnStatement::one(Expression::from(true))
143 .with_expression(false),
144 return_empty_single_quote_string("return ''") => ReturnStatement::one(StringExpression::new("''").unwrap()),
145 return_empty_double_quote_string("return \"\"") => ReturnStatement::one(StringExpression::new("\"\"").unwrap()),
146 return_empty_backtick_string("return ``") => ReturnStatement::one(InterpolatedStringExpression::empty()),
147 return_backtick_string_hello("return `hello`") => ReturnStatement::one(InterpolatedStringExpression::new(
148 vec![StringSegment::from_value("hello").into()]
149 )),
150 return_backtick_string_with_single_value("return `{true}`") => ReturnStatement::one(InterpolatedStringExpression::new(
151 vec![ValueSegment::new(true).into()]
152 )),
153 return_backtick_string_with_prefixed_single_value("return `value = {true}`") => ReturnStatement::one(InterpolatedStringExpression::new(
154 vec![
155 StringSegment::from_value("value = ").into(),
156 ValueSegment::new(true).into(),
157 ]
158 )),
159 return_backtick_string_with_suffixed_single_value("return `{false} -> condition`") => ReturnStatement::one(InterpolatedStringExpression::new(
160 vec![
161 ValueSegment::new(false).into(),
162 StringSegment::from_value(" -> condition").into(),
163 ]
164 )),
165 return_backtick_string_with_prefix_and_suffixed_single_value("return `-> {value} (value)`") => ReturnStatement::one(InterpolatedStringExpression::new(
166 vec![
167 StringSegment::from_value("-> ").into(),
168 ValueSegment::new(Expression::identifier("value")).into(),
169 StringSegment::from_value(" (value)").into(),
170 ]
171 )),
172 return_backtick_string_escape_braces("return `Hello \\{}`") => ReturnStatement::one(InterpolatedStringExpression::new(
173 vec![StringSegment::from_value("Hello {}").into()]
174 )),
175 return_backtick_string_escape_backtick("return `Delimiter: \\``") => ReturnStatement::one(InterpolatedStringExpression::new(
176 vec![StringSegment::from_value("Delimiter: `").into()]
177 )),
178 return_backtick_string_escape_backslash("return `\\\\`") => ReturnStatement::one(InterpolatedStringExpression::new(
179 vec![StringSegment::from_value("\\").into()]
180 )),
181 return_backtick_string_with_table_value("return `{ {} }`") => ReturnStatement::one(InterpolatedStringExpression::new(
182 vec![ValueSegment::new(TableExpression::default()).into()]
183 )),
184 return_backtick_string_with_backtrick_string_value("return `{`a`}`") => ReturnStatement::one(InterpolatedStringExpression::new(
185 vec![ValueSegment::new(
186 InterpolatedStringExpression::new(vec![StringSegment::from_value("a").into()])
187 ).into()]
188 )),
189 empty_while_true_do("while true do end") => WhileStatement::new(Block::default(), true),
190 while_false_do_break("while false do break end") => WhileStatement::new(
191 LastStatement::new_break(),
192 false,
193 ),
194 empty_repeat("repeat until true") => RepeatStatement::new(Block::default(), true),
195 repeat_break("repeat break until true") => RepeatStatement::new(
196 LastStatement::new_break(),
197 true,
198 ),
199 repeat_continue("repeat continue until true") => RepeatStatement::new(
200 LastStatement::new_continue(),
201 true,
202 ),
203 local_assignment_with_no_values("local var") => VariableAssignment::from_variable("var"),
204 multiple_local_assignment_with_no_values("local foo, bar") => VariableAssignment::from_variable("foo")
205 .with_variable("bar"),
206 local_assignment_with_one_value("local var = true") => VariableAssignment::from_variable("var")
207 .with_value(true),
208 local_assignment_named_const("local const = true") => VariableAssignment::from_variable("const")
209 .with_value(true),
210 multiple_local_assignment_with_two_values("local foo, bar = true, false") => VariableAssignment::from_variable("foo")
211 .with_variable("bar")
212 .with_value(true)
213 .with_value(false),
214 const_assignment_with_one_value("const var = true") => VariableAssignment::from_variable("var")
215 .with_assignment_kind(AssignmentKind::Const)
216 .with_value(true),
217 const_assignment_typed_with_one_value("const var: boolean = true") => VariableAssignment::from_variable(
218 TypedIdentifier::new("var").with_type(TypeName::new("boolean"))
219 )
220 .with_assignment_kind(AssignmentKind::Const)
221 .with_value(true),
222 multiple_const_assignment_with_two_values("const foo, bar = true, false") => VariableAssignment::from_variable("foo")
223 .with_variable("bar")
224 .with_assignment_kind(AssignmentKind::Const)
225 .with_value(true)
226 .with_value(false),
227 return_binary_and("return true and false") => ReturnStatement::one(
228 BinaryExpression::new(BinaryOperator::And, true, false),
229 ),
230 return_binary_floor_division("return 10 // 3") => ReturnStatement::one(
231 BinaryExpression::new(BinaryOperator::DoubleSlash, 10, 3),
232 ),
233 return_zero("return 0") => ReturnStatement::one(
234 NumberExpression::from_str("0").unwrap(),
235 ),
236 return_one("return 1") => ReturnStatement::one(
237 NumberExpression::from_str("1").unwrap(),
238 ),
239 return_float("return 1.5") => ReturnStatement::one(
240 NumberExpression::from_str("1.5").unwrap(),
241 ),
242 return_zero_point_five("return .5") => ReturnStatement::one(
243 NumberExpression::from_str(".5").unwrap(),
244 ),
245 return_not_true("return not true") => ReturnStatement::one(
246 UnaryExpression::new(UnaryOperator::Not, true),
247 ),
248 return_variable_length("return #array") => ReturnStatement::one(
249 UnaryExpression::new(
250 UnaryOperator::Length,
251 Expression::identifier("array"),
252 ),
253 ),
254 return_minus_variable("return -num") => ReturnStatement::one(
255 UnaryExpression::new(
256 UnaryOperator::Minus,
257 Expression::identifier("num"),
258 ),
259 ),
260 call_function("call()") => FunctionCall::from_name("call"),
261 call_indexed_table("foo.bar()") => FunctionCall::from_prefix(
262 FieldExpression::new(Prefix::from_name("foo"), "bar")
263 ),
264 call_indexed_with_empty_type_instantiation("foo.bar<<>>()") => FunctionCall::from_prefix(
265 TypeInstantiationExpression::new(
266 FieldExpression::new(Prefix::from_name("foo"), "bar"),
267 Vec::new()
268 )
269 ),
270 call_indexed_with_empty_type_instantiation_with_spaces("foo.bar < < > > ()") => FunctionCall::from_prefix(
271 TypeInstantiationExpression::new(
272 FieldExpression::new(Prefix::from_name("foo"), "bar"),
273 Vec::new()
274 )
275 ),
276 call_indexed_with_type_instantiation_of_one_type("foo.bar<<string>>()") => FunctionCall::from_prefix(
277 TypeInstantiationExpression::new(
278 FieldExpression::new(Prefix::from_name("foo"), "bar"),
279 vec![TypeName::new("string").into()]
280 )
281 ),
282 call_indexed_with_type_instantiation_of_two_types("foo.bar< <string, number> >()") => FunctionCall::from_prefix(
283 TypeInstantiationExpression::new(
284 FieldExpression::new(Prefix::from_name("foo"), "bar"),
285 vec![TypeName::new("string").into(), TypeName::new("number").into()]
286 )
287 ),
288 call_method("foo:bar()") => FunctionCall::from_name("foo").with_method("bar"),
289 call_method_with_one_argument("foo:bar(true)") => FunctionCall::from_name("foo")
290 .with_method("bar")
291 .with_argument(true),
292 call_function_with_one_argument("call(true)") => FunctionCall::from_name("call")
293 .with_argument(true),
294 call_function_with_two_arguments("call(true, false)") => FunctionCall::from_name("call")
295 .with_argument(true)
296 .with_argument(false),
297 call_chain_empty("call()()") => FunctionCall::from_prefix(
298 FunctionCall::from_name("call")
299 ),
300 call_chain_with_args("call(true)(false)") => FunctionCall::from_prefix(
301 FunctionCall::from_name("call").with_argument(true)
302 ).with_argument(false),
303 call_method_chain_empty("call():method()") => FunctionCall::from_prefix(
304 FunctionCall::from_name("call")
305 ).with_method("method"),
306 call_method_chain_with_arguments("call(true):method(false)") => FunctionCall::from_prefix(
307 FunctionCall::from_name("call").with_argument(true)
308 ).with_method("method").with_argument(false),
309 call_index_chain_empty("call().method()") => FunctionCall::from_prefix(
310 FieldExpression::new(FunctionCall::from_name("call"), "method")
311 ),
312 call_with_empty_table_argument("call{}") => FunctionCall::from_name("call")
313 .with_arguments(TableExpression::default()),
314 call_with_empty_string_argument("call''") => FunctionCall::from_name("call")
315 .with_arguments(StringExpression::empty()),
316 return_call_function("return call()") => ReturnStatement::one(
317 FunctionCall::from_name("call"),
318 ),
319 return_call_indexed_table("return foo.bar()") => ReturnStatement::one(
320 FunctionCall::from_prefix(FieldExpression::new(Prefix::from_name("foo"), "bar")),
321 ),
322 return_call_method("return foo:bar()") => ReturnStatement::one(
323 FunctionCall::from_name("foo").with_method("bar"),
324 ),
325 return_call_method_with_one_argument("return foo:bar(true)") => ReturnStatement::one(
326 FunctionCall::from_name("foo").with_method("bar").with_argument(true),
327 ),
328 return_call_function_with_one_argument("return call(true)") => ReturnStatement::one(
329 FunctionCall::from_name("call").with_argument(true),
330 ),
331 return_call_function_with_two_arguments("return call(true, false)") => ReturnStatement::one(
332 FunctionCall::from_name("call")
333 .with_argument(true)
334 .with_argument(false),
335 ),
336 return_call_chain_empty("return call()()") => ReturnStatement::one(
337 FunctionCall::from_prefix(FunctionCall::from_name("call")),
338 ),
339 return_call_chain_with_args("return call(true)(false)") => ReturnStatement::one(
340 FunctionCall::from_prefix(
341 FunctionCall::from_name("call").with_argument(true)
342 ).with_argument(false),
343 ),
344 return_call_method_chain_empty("return call():method()") => ReturnStatement::one(
345 FunctionCall::from_prefix(FunctionCall::from_name("call")).with_method("method"),
346 ),
347 return_call_method_chain_with_arguments("return call(true):method(false)")
348 => ReturnStatement::one(
349 FunctionCall::from_prefix(FunctionCall::from_name("call").with_argument(true))
350 .with_method("method")
351 .with_argument(false),
352 ),
353 return_call_index_chain_empty("return call().method()") => ReturnStatement::one(
354 FunctionCall::from_prefix(FieldExpression::new(FunctionCall::from_name("call"), "method")),
355 ),
356 return_call_new_empty_function("return (function() end)()") => ReturnStatement::one(
357 FunctionCall::from_prefix(
358 ParentheseExpression::new(FunctionExpression::default())
359 ),
360 ),
361 return_call_variable_argument("return (...)()") => ReturnStatement::one(
362 FunctionCall::from_prefix(ParentheseExpression::new(Expression::variable_arguments())),
363 ),
364 return_call_variable_in_parentheses("return (var)()") => ReturnStatement::one(
365 FunctionCall::from_prefix(ParentheseExpression::new(Expression::identifier("var"))),
366 ),
367 return_call_variable_in_double_parentheses("return ((var))()") => ReturnStatement::one(
368 FunctionCall::from_prefix(
369 ParentheseExpression::new(Expression::identifier("var").in_parentheses())
370 ),
371 ),
372 return_field_expression("return math.huge") => ReturnStatement::one(
373 FieldExpression::new(Prefix::from_name("math"), "huge")
374 ),
375 return_type_instantiation("return foo << string >>") => ReturnStatement::one(
376 TypeInstantiationExpression::new(Prefix::from_name("foo"), vec![TypeName::new("string").into()])
377 ),
378 return_type_instantiation_indexed("return foo << string >>.bar") => ReturnStatement::one(
379 FieldExpression::new(
380 TypeInstantiationExpression::new(Prefix::from_name("foo"), vec![TypeName::new("string").into()]),
381 "bar"
382 )
383 ),
384 index_field_function_call("return call().result") => ReturnStatement::one(
385 FieldExpression::new(FunctionCall::from_name("call"), "result"),
386 ),
387 return_index_expression("return value[true]") => ReturnStatement::one(
388 IndexExpression::new(Prefix::from_name("value"), true)
389 ),
390 return_empty_table("return {}") => ReturnStatement::one(TableExpression::default()),
391 return_array_with_one_element("return {true}") => ReturnStatement::one(
392 TableExpression::default().append_array_value(true)
393 ),
394 return_array_with_two_elements("return {true, false}") => ReturnStatement::one(
395 TableExpression::default()
396 .append_array_value(true)
397 .append_array_value(false)
398
399 ),
400 return_array_with_one_field("return { field = true }") => ReturnStatement::one(
401 TableExpression::default().append_field("field", true)
402 ),
403 return_array_with_one_key_expression("return { [false] = true }") => ReturnStatement::one(
404 TableExpression::default().append_index(false, true)
405 ),
406 assign_variable("var = true") => AssignStatement::from_variable(
407 Variable::new("var"),
408 true,
409 ),
410 assign_two_variables("var, var2 = true, false") => AssignStatement::from_variable(
411 Variable::new("var"),
412 true,
413 ).append_assignment(Variable::new("var2"), false),
414 assign_one_variable_with_two_values("var = 0b1010, ...") => AssignStatement::new(
415 vec![Variable::new("var")],
416 vec!["0b1010".parse::<NumberExpression>().unwrap().into(), Expression::variable_arguments()],
417 ),
418 assign_field("var.field = true") => AssignStatement::from_variable(
419 FieldExpression::new(Prefix::from_name("var"), "field"),
420 true,
421 ),
422 assign_field_and_variable("var.field, other = true, 1 + value") =>
423 AssignStatement::from_variable(
424 FieldExpression::new(Prefix::from_name("var"), "field"),
425 true,
426 ).append_assignment(
427 Variable::new("other"),
428 BinaryExpression::new(BinaryOperator::Plus, 1.0, Expression::identifier("value"))
429 ),
430 assign_index("var[false] = true") => AssignStatement::from_variable(
431 IndexExpression::new(Prefix::from_name("var"), false),
432 true,
433 ),
434 return_empty_function("return function() end") => ReturnStatement::one(
435 FunctionExpression::default(),
436 ),
437 return_empty_function_with_one_param("return function(a) end") => ReturnStatement::one(
438 FunctionExpression::default().with_parameter("a"),
439 ),
440 return_empty_function_with_two_params("return function(a, b) end") => ReturnStatement::one(
441 FunctionExpression::default().with_parameter("a").with_parameter("b"),
442 ),
443 return_empty_variadic_function("return function(...) end") => ReturnStatement::one(
444 FunctionExpression::default().variadic(),
445 ),
446 return_empty_variadic_function_with_one_param("return function(a, ...) end")
447 => ReturnStatement::one(
448 FunctionExpression::default().with_parameter("a").variadic(),
449 ),
450 return_function_that_returns("return function() return true end")
451 => ReturnStatement::one(
452 FunctionExpression::from_block(ReturnStatement::one(Expression::from(true)))
453 ),
454 return_function_with_native_attribute("return @native function() return end")
455 => ReturnStatement::one(
456 FunctionExpression::from_block(ReturnStatement::default()).with_attribute(NamedAttribute::new("native"))
457 ),
458 empty_if_statement("if true then end") => IfStatement::create(true, Block::default()),
459 if_statement_returns("if true then return end") => IfStatement::create(
460 Expression::from(true),
461 ReturnStatement::default(),
462 ),
463 empty_if_statement_with_empty_else("if true then else end")
464 => IfStatement::create(true, Block::default())
465 .with_else_block(Block::default()),
466 empty_if_statement_with_empty_elseif("if true then elseif false then end")
467 => IfStatement::create(true, Block::default())
468 .with_new_branch(false, Block::default()),
469 empty_if_statement_with_empty_elseif_and_empty_else("if true then elseif false then else end")
470 => IfStatement::create(true, Block::default())
471 .with_new_branch(false, Block::default())
472 .with_else_block(Block::default()),
473 empty_if_statement_with_returning_else("if true then else return end")
474 => IfStatement::create(true, Block::default())
475 .with_else_block(ReturnStatement::default()),
476 empty_local_function("local function name() end")
477 => FunctionAssignment::from_name("name", Block::default()),
478 empty_local_function_variadic("local function name(...) end")
479 => FunctionAssignment::from_name("name", Block::default()).variadic(),
480 empty_local_function_variadic_with_one_parameter("local function name(a, ...) end")
481 => FunctionAssignment::from_name("name", Block::default())
482 .with_parameter("a")
483 .variadic(),
484 local_function_return("local function name() return end")
485 => FunctionAssignment::from_name("name", ReturnStatement::default()),
486 local_function_return_with_native_attribute("@native local function name() return end")
487 => FunctionAssignment::from_name("name", ReturnStatement::default()).with_attribute(NamedAttribute::new("native")),
488
489 empty_function_statement("function name() end")
490 => FunctionStatement::from_name("name", Block::default()),
491 empty_function_statement_variadic("function name(...) end")
492 => FunctionStatement::from_name("name", Block::default()).variadic(),
493 empty_function_statement_variadic_with_one_parameter("function name(a, ...) end")
494 => FunctionStatement::from_name("name", Block::default())
495 .with_parameter("a")
496 .variadic(),
497 function_statement_return("function name() return end")
498 => FunctionStatement::from_name("name", ReturnStatement::default()),
499 function_statement_return_with_native_attribute("@native function name() return end")
500 => FunctionStatement::from_name("name", ReturnStatement::default()).with_attribute(NamedAttribute::new("native")),
501 empty_generic_for("for key in pairs(t) do end") => GenericForStatement::new(
502 vec!["key".into()],
503 vec![
504 FunctionCall::from_name("pairs")
505 .with_argument(Expression::identifier("t"))
506 .into(),
507 ],
508 Block::default(),
509 ),
510 empty_generic_for_with_typed_key("for key: string in t do end") => GenericForStatement::new(
511 vec![Identifier::new("key").with_type(TypeName::new("string"))],
512 vec![
513 Expression::identifier("t"),
514 ],
515 Block::default(),
516 ),
517 empty_generic_for_multiple_variables("for key, value in pairs(t) do end") => GenericForStatement::new(
518 vec!["key".into(), "value".into()],
519 vec![
520 FunctionCall::from_name("pairs")
521 .with_argument(Expression::identifier("t"))
522 .into(),
523 ],
524 Block::default(),
525 ),
526 empty_generic_for_multiple_values("for key in next, t do end") => GenericForStatement::new(
527 vec!["key".into()],
528 vec![Expression::identifier("next"), Expression::identifier("t")],
529 Block::default(),
530 ),
531 generic_for_break("for key in pairs(t) do break end") => GenericForStatement::new(
532 vec!["key".into()],
533 vec![
534 FunctionCall::from_name("pairs")
535 .with_argument(Expression::identifier("t"))
536 .into(),
537 ],
538 LastStatement::new_break(),
539 ),
540 empty_numeric_for("for i=start, bound do end") => NumericForStatement::new(
541 "i",
542 Expression::identifier("start"),
543 Expression::identifier("bound"),
544 None,
545 Block::default(),
546 ),
547 empty_numeric_for_with_step("for i=start, bound, step do end") => NumericForStatement::new(
548 "i",
549 Expression::identifier("start"),
550 Expression::identifier("bound"),
551 Some(Expression::identifier("step")),
552 Block::default(),
553 ),
554 numeric_for_that_breaks("for i=start, bound do break end") => NumericForStatement::new(
555 "i",
556 Expression::identifier("start"),
557 Expression::identifier("bound"),
558 None,
559 LastStatement::new_break(),
560 ),
561 compound_increment("var += amount") => CompoundAssignStatement::new(
562 CompoundOperator::Plus,
563 Variable::new("var"),
564 Expression::identifier("amount"),
565 ),
566 compound_floor_division("var //= divider") => CompoundAssignStatement::new(
567 CompoundOperator::DoubleSlash,
568 Variable::new("var"),
569 Expression::identifier("divider"),
570 ),
571 type_function("type function foo() end") => TypeFunctionStatement::from_name(
572 "foo",
573 Block::default(),
574 ),
575 type_function_with_2_typed_parameters("type function foo(a: number, b: string) end")
576 => TypeFunctionStatement::from_name(
577 Identifier::new("foo"),
578 Block::default(),
579 )
580 .with_parameter(Identifier::new("a").with_type(TypeName::new("number")))
581 .with_parameter(Identifier::new("b").with_type(TypeName::new("string"))),
582 empty_type_function_variadic("type function foo(...) end")
583 => TypeFunctionStatement::from_name("foo", Block::default()).variadic(),
584 empty_type_function_variadic_with_one_parameter("type function foo(a, ...) end")
585 => TypeFunctionStatement::from_name("foo", Block::default())
586 .with_parameter("a")
587 .variadic(),
588 type_function_with_2_typed_parameters_and_variadic("type function foo(a: number, b: string, ...) end")
589 => TypeFunctionStatement::from_name("foo", Block::default())
590 .with_parameter(Identifier::new("a").with_type(TypeName::new("number")))
591 .with_parameter(Identifier::new("b").with_type(TypeName::new("string")))
592 .variadic(),
593 type_function_variadic_and_return_type("type function foo(...): number end")
594 => TypeFunctionStatement::from_name("foo", Block::default())
595 .variadic()
596 .with_return_type(TypeName::new("number")),
597 exported_type_function_optional("export type function optional(t) return types.unionof(t, types.singleton(nil)) end")
598 => TypeFunctionStatement::from_name(
599 "optional",
600 ReturnStatement::one(
601 FunctionCall::from_prefix(FieldExpression::new(Prefix::from_name("types"), "unionof"))
602 .with_argument(Expression::identifier("t"))
603 .with_argument(
604 FunctionCall::from_prefix(FieldExpression::new(Prefix::from_name("types"), "singleton"))
605 .with_argument(Expression::nil())
606 )
607 )
608 )
609 .with_parameter("t")
610 .export(),
611 );
612
613 mod parse_with_tokens {
614 use super::*;
615
616 macro_rules! test_parse_block_with_tokens {
617 ($($name:ident($input:literal) => $value:expr),* $(,)?) => {
618 $(
619 #[test]
620 fn $name() {
621 let parser = Parser::default().preserve_tokens();
622 let block = match parser.parse($input) {
623 Ok(block) => block,
624 Err(err) => {
625 panic!(
626 "failed to parse `{}`: {}\nfull-moon result:\n{:#?}",
627 $input,
628 err,
629 full_moon::parse_fallible($input, LuaVersion::luau()).into_result()
630 );
631 }
632 };
633
634 let expect_block = $value;
635
636 pretty_assertions::assert_eq!(block, expect_block);
637 }
638 )*
639 };
640 }
641
642 macro_rules! test_parse_statement_with_tokens {
643 ($($name:ident($input:literal) => $value:expr),* $(,)?) => {
644 test_parse_block_with_tokens!(
645 $(
646 $name($input) => Block::from($value).with_tokens(BlockTokens {
647 semicolons: vec![None],
648 last_semicolon: None,
649 final_token: None,
650 }),
651 )*
652 );
653 };
654 }
655
656 macro_rules! test_parse_last_statement_with_tokens {
657 ($($name:ident($input:literal) => $value:expr),* $(,)?) => {
658 test_parse_block_with_tokens!(
659 $(
660 $name($input) => Block::from($value).with_tokens(BlockTokens {
661 semicolons: Vec::new(),
662 last_semicolon: None,
663 final_token: None,
664 }),
665 )*
666 );
667 };
668 }
669
670 fn create_true(start: usize, whitespace_length: usize) -> Expression {
671 let end = start + 4;
672 let token = Token::new_with_line(start, end, 1);
673 Expression::True(Some(if whitespace_length == 0 {
674 token
675 } else {
676 token.with_trailing_trivia(TriviaKind::Whitespace.at(
677 end,
678 end + whitespace_length,
679 1,
680 ))
681 }))
682 }
683
684 fn create_identifier(
685 identifier: &str,
686 start: usize,
687 whitespace_length: usize,
688 ) -> Identifier {
689 create_identifier_at_line(identifier, start, whitespace_length, 1)
690 }
691
692 fn create_identifier_at_line(
693 identifier: &str,
694 start: usize,
695 whitespace_length: usize,
696 line: usize,
697 ) -> Identifier {
698 let end = start + identifier.len();
699 Identifier::new(identifier).with_token({
700 let token = Token::new_with_line(start, end, line);
701 if whitespace_length != 0 {
702 token.with_trailing_trivia(TriviaKind::Whitespace.at(
703 end,
704 end + whitespace_length,
705 line,
706 ))
707 } else {
708 token
709 }
710 })
711 }
712
713 fn spaced_token(start: usize, end: usize) -> Token {
714 spaced_token_at_line(start, end, 1)
715 }
716
717 fn spaced_token_at_line(start: usize, end: usize, line: usize) -> Token {
718 Token::new_with_line(start, end, line).with_trailing_trivia(TriviaKind::Whitespace.at(
719 end,
720 end + 1,
721 line,
722 ))
723 }
724
725 fn default_block() -> Block {
726 Block::default().with_tokens(BlockTokens {
727 semicolons: Vec::new(),
728 last_semicolon: None,
729 final_token: None,
730 })
731 }
732
733 fn token_at_first_line(start: usize, end: usize) -> Token {
734 Token::new_with_line(start, end, 1)
735 }
736
737 test_parse_last_statement_with_tokens!(
738 return_with_comment("return -- comment") => ReturnStatement::default()
739 .with_tokens(ReturnTokens {
740 r#return: token_at_first_line(0, 6)
741 .with_trailing_trivia(TriviaKind::Whitespace.at(6, 7, 1))
742 .with_trailing_trivia(TriviaKind::Comment.at(7, 17, 1)),
743 commas: Vec::new(),
744 }),
745 return_true("return true") => ReturnStatement::one(create_true(7, 0))
746 .with_tokens(ReturnTokens {
747 r#return: spaced_token(0, 6),
748 commas: Vec::new(),
749 }),
750 return_false("return false") => ReturnStatement::one(
751 Expression::False(Some(token_at_first_line(7, 12)))
752 ).with_tokens(ReturnTokens {
753 r#return: spaced_token(0, 6),
754 commas: Vec::new(),
755 }),
756 return_nil("return nil") => ReturnStatement::one(
757 Expression::Nil(Some(token_at_first_line(7, 10)))
758 ).with_tokens(ReturnTokens {
759 r#return: spaced_token(0, 6),
760 commas: Vec::new(),
761 }),
762 return_variable_arguments("return ...") => ReturnStatement::one(
763 Expression::VariableArguments(Some(token_at_first_line(7, 10)))
764 ).with_tokens(ReturnTokens {
765 r#return: spaced_token(0, 6),
766 commas: Vec::new(),
767 }),
768 return_empty_single_quote_string("return ''") => ReturnStatement::one(
769 StringExpression::empty().with_token(token_at_first_line(7, 9))
770 ).with_tokens(ReturnTokens {
771 r#return: spaced_token(0, 6),
772 commas: Vec::new(),
773 }),
774 return_empty_double_quote_string("return \"\"") => ReturnStatement::one(
775 StringExpression::empty().with_token(token_at_first_line(7, 9))
776 ).with_tokens(ReturnTokens {
777 r#return: spaced_token(0, 6),
778 commas: Vec::new(),
779 }),
780 return_double_quote_string("return \"abc\"") => ReturnStatement::one(
781 StringExpression::from_value("abc").with_token(token_at_first_line(7, 12))
782 ).with_tokens(ReturnTokens {
783 r#return: spaced_token(0, 6),
784 commas: Vec::new(),
785 }),
786 return_empty_backtick_string("return ``") => ReturnStatement::one(
787 InterpolatedStringExpression::empty().with_tokens(
788 InterpolatedStringTokens {
789 opening_tick: token_at_first_line(7, 8),
790 closing_tick: token_at_first_line(8, 9),
791 }
792 )
793 ).with_tokens(ReturnTokens {
794 r#return: spaced_token(0, 6),
795 commas: Vec::new(),
796 }),
797 return_backtick_string_with_escaped_backtick("return `\\``") => ReturnStatement::one(
798 InterpolatedStringExpression::empty()
799 .with_segment(
800 StringSegment::from_value('`').with_token(token_at_first_line(8, 10))
801 )
802 .with_tokens(
803 InterpolatedStringTokens {
804 opening_tick: token_at_first_line(7, 8),
805 closing_tick: token_at_first_line(10, 11),
806 }
807 )
808 ).with_tokens(ReturnTokens {
809 r#return: spaced_token(0, 6),
810 commas: Vec::new(),
811 }),
812 return_backtick_string_hello("return `hello`") => ReturnStatement::one(
813 InterpolatedStringExpression::new(vec![
814 StringSegment::from_value("hello")
815 .with_token(token_at_first_line(8, 13))
816 .into()
817 ])
818 .with_tokens(InterpolatedStringTokens {
819 opening_tick: token_at_first_line(7, 8),
820 closing_tick: token_at_first_line(13, 14),
821 })
822 ).with_tokens(ReturnTokens {
823 r#return: spaced_token(0, 6),
824 commas: Vec::new(),
825 }),
826 return_backtick_string_with_single_value("return `{true}`") => ReturnStatement::one(
827 InterpolatedStringExpression::new(vec![
828 ValueSegment::new(create_true(9, 0)).with_tokens(ValueSegmentTokens {
829 opening_brace: token_at_first_line(8, 9),
830 closing_brace: token_at_first_line(13, 14),
831 }).into()
832 ])
833 .with_tokens(InterpolatedStringTokens {
834 opening_tick: token_at_first_line(7, 8),
835 closing_tick: token_at_first_line(14, 15),
836 })
837 ).with_tokens(ReturnTokens {
838 r#return: spaced_token(0, 6),
839 commas: Vec::new(),
840 }),
841 return_backtick_string_with_prefixed_single_value("return `value = {true}`") => ReturnStatement::one(
842 InterpolatedStringExpression::new(
843 vec![
844 StringSegment::from_value("value = ")
845 .with_token(token_at_first_line(8, 16))
846 .into(),
847 ValueSegment::new(create_true(17, 0))
848 .with_tokens(ValueSegmentTokens {
849 opening_brace: token_at_first_line(16, 17),
850 closing_brace: token_at_first_line(21, 22),
851 }).into(),
852 ]
853 )
854 .with_tokens(InterpolatedStringTokens {
855 opening_tick: token_at_first_line(7, 8),
856 closing_tick: token_at_first_line(22, 23),
857 })
858 ).with_tokens(ReturnTokens {
859 r#return: spaced_token(0, 6),
860 commas: Vec::new(),
861 }),
862 return_backtick_string_with_suffixed_single_value("return `{true} -> condition`") => ReturnStatement::one(
863 InterpolatedStringExpression::new(
864 vec![
865 ValueSegment::new(create_true(9, 0))
866 .with_tokens(ValueSegmentTokens {
867 opening_brace: token_at_first_line(8, 9),
868 closing_brace: token_at_first_line(13, 14),
869 }).into(),
870 StringSegment::from_value(" -> condition")
871 .with_token(token_at_first_line(14, 27))
872 .into(),
873 ]
874 )
875 .with_tokens(InterpolatedStringTokens {
876 opening_tick: token_at_first_line(7, 8),
877 closing_tick: token_at_first_line(27, 28),
878 })
879 ).with_tokens(ReturnTokens {
880 r#return: spaced_token(0, 6),
881 commas: Vec::new(),
882 }),
883 return_backtick_string_with_prefix_and_suffixed_single_value("return `-> {value} (value)`") => ReturnStatement::one(
884 InterpolatedStringExpression::new(
885 vec![
886 StringSegment::from_value("-> ")
887 .with_token(token_at_first_line(8, 11))
888 .into(),
889 ValueSegment::new(create_identifier("value", 12, 0))
890 .with_tokens(ValueSegmentTokens {
891 opening_brace: token_at_first_line(11, 12),
892 closing_brace: token_at_first_line(17, 18),
893 }).into(),
894 StringSegment::from_value(" (value)")
895 .with_token(token_at_first_line(18, 26))
896 .into(),
897 ]
898 )
899 .with_tokens(InterpolatedStringTokens {
900 opening_tick: token_at_first_line(7, 8),
901 closing_tick: token_at_first_line(26, 27),
902 })
903 ).with_tokens(ReturnTokens {
904 r#return: spaced_token(0, 6),
905 commas: Vec::new(),
906 }),
907 return_integer_number("return 123") => ReturnStatement::one(
908 DecimalNumber::new(123.0).with_token(token_at_first_line(7, 10))
909 ).with_tokens(ReturnTokens {
910 r#return: spaced_token(0, 6),
911 commas: Vec::new(),
912 }),
913 return_float("return 12.34 -- value") => ReturnStatement::one(
914 DecimalNumber::new(12.34).with_token(
915 spaced_token(7, 12).with_trailing_trivia(TriviaKind::Comment.at(13, 21, 1))
916 )
917 ).with_tokens(ReturnTokens {
918 r#return: spaced_token(0, 6),
919 commas: Vec::new(),
920 }),
921 return_binary_number("return 0b1010") => ReturnStatement::one(
922 BinaryNumber::new(0b1010, false).with_token(token_at_first_line(7, 13))
923 ).with_tokens(ReturnTokens {
924 r#return: spaced_token(0, 6),
925 commas: Vec::new(),
926 }),
927 return_hexadecimal_number("return 0x12EF") => ReturnStatement::one(
928 HexNumber::new(0x12EF, false).with_token(token_at_first_line(7, 13))
929 ).with_tokens(ReturnTokens {
930 r#return: spaced_token(0, 6),
931 commas: Vec::new(),
932 }),
933 return_empty_table("return {--[[ inside ]]}") => ReturnStatement::one(
934 TableExpression::default().with_tokens(TableTokens {
935 opening_brace: token_at_first_line(7, 8)
936 .with_trailing_trivia(TriviaKind::Comment.at(8, 22, 1)),
937 closing_brace: token_at_first_line(22, 23),
938 separators: Vec::new(),
939 })
940 ).with_tokens(ReturnTokens {
941 r#return: spaced_token(0, 6),
942 commas: Vec::new(),
943 }),
944 return_array_with_one_element("return { true} ") => ReturnStatement::one(
945 TableExpression::default()
946 .append_array_value(create_true(9, 0))
947 .with_tokens(TableTokens {
948 opening_brace: spaced_token(7, 8),
949 closing_brace: spaced_token(13, 14),
950 separators: Vec::new(),
951 })
952 ).with_tokens(ReturnTokens {
953 r#return: spaced_token(0, 6),
954 commas: Vec::new(),
955 }),
956 return_array_with_two_elements("return {true, true}") => ReturnStatement::one(
957 TableExpression::default()
958 .append_array_value(create_true(8, 0))
959 .append_array_value(create_true(14, 0))
960 .with_tokens(TableTokens {
961 opening_brace: token_at_first_line(7, 8),
962 closing_brace: token_at_first_line(18, 19),
963 separators: vec![spaced_token(12, 13)],
964 })
965 ).with_tokens(ReturnTokens {
966 r#return: spaced_token(0, 6),
967 commas: Vec::new(),
968 }),
969 return_array_with_one_field("return { field = true; }") => ReturnStatement::one(
970 TableExpression::default()
971 .append_entry(
972 TableFieldEntry::new(
973 create_identifier("field", 9, 1),
974 create_true(17, 0),
975 ).with_token(spaced_token(15, 16))
976 )
977 .with_tokens(TableTokens {
978 opening_brace: spaced_token(7, 8),
979 closing_brace: token_at_first_line(23, 24),
980 separators: vec![spaced_token(21, 22)],
981 })
982 ).with_tokens(ReturnTokens {
983 r#return: spaced_token(0, 6),
984 commas: Vec::new(),
985 }),
986 return_array_with_one_key_expression("return { [var] = true }") => ReturnStatement::one(
987 TableExpression::default()
988 .append_entry(
989 TableIndexEntry::new(
990 create_identifier("var", 10, 0),
991 create_true(17, 1),
992 ).with_tokens(TableIndexEntryTokens {
993 opening_bracket: token_at_first_line(9, 10),
994 closing_bracket: spaced_token(13, 14),
995 equal: spaced_token(15, 16),
996 })
997 )
998 .with_tokens(TableTokens {
999 opening_brace: spaced_token(7, 8),
1000 closing_brace: token_at_first_line(22, 23),
1001 separators: Vec::new(),
1002 })
1003 ).with_tokens(ReturnTokens {
1004 r#return: spaced_token(0, 6),
1005 commas: Vec::new(),
1006 }),
1007 return_field_expression("return math.huge") => ReturnStatement::one(
1008 FieldExpression::new(
1009 Prefix::from_name(create_identifier("math", 7, 0)),
1010 create_identifier("huge", 12, 0)
1011 ).with_token(token_at_first_line(11, 12))
1012 ).with_tokens(ReturnTokens {
1013 r#return: spaced_token(0, 6),
1014 commas: Vec::new(),
1015 }),
1016 return_double_field_expression("return table.ok .result") => ReturnStatement::one(
1017 FieldExpression::new(
1018 FieldExpression::new(
1019 Prefix::from_name(create_identifier("table", 7, 0)),
1020 create_identifier("ok", 13, 1)
1021 ).with_token(token_at_first_line(12, 13)),
1022 create_identifier("result", 17, 0)
1023 ).with_token(token_at_first_line(16, 17))
1024 ).with_tokens(ReturnTokens {
1025 r#return: spaced_token(0, 6),
1026 commas: Vec::new(),
1027 }),
1028 return_index_expression("return value [ true ] ") => ReturnStatement::one(
1029 IndexExpression::new(
1030 create_identifier("value", 7, 1),
1031 create_true(15, 1)
1032 ).with_tokens(IndexExpressionTokens {
1033 opening_bracket: spaced_token(13, 14),
1034 closing_bracket: spaced_token(20, 21),
1035 })
1036 ).with_tokens(ReturnTokens {
1037 r#return: spaced_token(0, 6),
1038 commas: Vec::new(),
1039 }),
1040 return_true_and_true("return true and true") => ReturnStatement::default()
1041 .with_expression(
1042 BinaryExpression::new(
1043 BinaryOperator::And,
1044 create_true(7, 1),
1045 create_true(16, 0),
1046 ).with_token(spaced_token(12, 15))
1047 )
1048 .with_tokens(ReturnTokens {
1049 r#return: spaced_token(0, 6),
1050 commas: Vec::new(),
1051 }),
1052 return_not_true("return not true") => ReturnStatement::default()
1053 .with_expression(
1054 UnaryExpression::new(
1055 UnaryOperator::Not,
1056 create_true(11, 0),
1057 ).with_token(spaced_token(7, 10))
1058 )
1059 .with_tokens(ReturnTokens {
1060 r#return: spaced_token(0, 6),
1061 commas: Vec::new(),
1062 }),
1063 return_parenthese_expression("return ( true )") => ReturnStatement::default()
1064 .with_expression(
1065 ParentheseExpression::new(create_true(9, 1))
1066 .with_tokens(
1067 ParentheseTokens {
1068 left_parenthese: spaced_token(7, 8),
1069 right_parenthese: token_at_first_line(14, 15),
1070 }
1071 )
1072 )
1073 .with_tokens(ReturnTokens {
1074 r#return: spaced_token(0, 6),
1075 commas: Vec::new(),
1076 }),
1077 return_type_cast("return var :: T") => ReturnStatement::one(
1078 TypeCastExpression::new(
1079 create_identifier("var", 7, 1),
1080 TypeName::new(create_identifier("T", 14, 0))
1081 ).with_token(spaced_token(11, 13))
1082 ).with_tokens(ReturnTokens {
1083 r#return: spaced_token(0, 6),
1084 commas: Vec::new(),
1085 }),
1086 return_type_cast_to_intersection_with_right_type_in_parenthese("return var :: nil&(''|true)") => ReturnStatement::one(
1087 TypeCastExpression::new(
1088 create_identifier("var", 7, 1),
1089 IntersectionType::new(
1090 Type::Nil(Some(token_at_first_line(14, 17))),
1091 ParentheseType::new(
1092 UnionType::new(
1093 StringType::from_value("").with_token(token_at_first_line(19, 21)),
1094 Type::True(Some(token_at_first_line(22, 26)))
1095 ).with_tokens(UnionTypeTokens {
1096 leading_token: None,
1097 separators: vec![token_at_first_line(21, 22)],
1098 })
1099 )
1100 .with_tokens(ParentheseTypeTokens {
1101 left_parenthese: token_at_first_line(18, 19),
1102 right_parenthese: token_at_first_line(26, 27),
1103 })
1104 ).with_tokens(IntersectionTypeTokens {
1105 leading_token: None,
1106 separators: vec![token_at_first_line(17, 18)],
1107 })
1108 ).with_token(spaced_token(11, 13))
1109 ).with_tokens(ReturnTokens {
1110 r#return: spaced_token(0, 6),
1111 commas: Vec::new(),
1112 }),
1113 return_type_cast_to_intersection_with_function_type_and_name(
1114 "return var :: (Ox:false,qv:fX)->zmTaj...&T"
1115 )=> ReturnStatement::one(
1116 TypeCastExpression::new(
1117 create_identifier("var", 7, 1),
1118 IntersectionType::new(
1119 FunctionType::new(
1120 GenericTypePack::new(create_identifier("zmTaj", 32, 0))
1121 .with_token(token_at_first_line(37, 40))
1122 )
1123 .with_argument(
1124 FunctionArgumentType::new(Type::False(Some(token_at_first_line(18, 23))))
1125 .with_name(create_identifier("Ox", 15, 0))
1126 .with_token(token_at_first_line(17, 18))
1127 )
1128 .with_argument(
1129 FunctionArgumentType::new(TypeName::new(create_identifier("fX", 27, 0)))
1130 .with_name(create_identifier("qv", 24, 0))
1131 .with_token(token_at_first_line(26, 27))
1132 )
1133 .with_tokens(FunctionTypeTokens {
1134 opening_parenthese: token_at_first_line(14, 15),
1135 closing_parenthese: token_at_first_line(29, 30),
1136 arrow: token_at_first_line(30, 32),
1137 commas: vec![token_at_first_line(23, 24)],
1138 }),
1139 TypeName::new(create_identifier("T", 41, 0)),
1140 ).with_tokens(IntersectionTypeTokens {
1141 leading_token: None,
1142 separators: vec![token_at_first_line(40, 41)],
1143 })
1144 ).with_token(spaced_token(11, 13))
1145 ).with_tokens(ReturnTokens {
1146 r#return: spaced_token(0, 6),
1147 commas: Vec::new(),
1148 }),
1149 return_empty_function("return function ( --[[params]]) end") => ReturnStatement::one(
1150 FunctionExpression::from_block(default_block())
1151 .with_tokens(FunctionBodyTokens {
1152 function: token_at_first_line(7, 15)
1153 .with_trailing_trivia(TriviaKind::Whitespace.at(15, 17, 1)),
1154 opening_parenthese: token_at_first_line(17, 18)
1155 .with_trailing_trivia(TriviaKind::Whitespace.at(18, 19, 1))
1156 .with_trailing_trivia(TriviaKind::Comment.at(19, 31, 1)),
1157 closing_parenthese: spaced_token(31, 32),
1158 end: token_at_first_line(33, 36),
1159 parameter_commas: Vec::new(),
1160 variable_arguments: None,
1161 variable_arguments_colon: None,
1162 return_type_colon: None,
1163 }),
1164 ).with_tokens(ReturnTokens {
1165 r#return: spaced_token(0, 6),
1166 commas: Vec::new(),
1167 }),
1168 return_empty_function_with_boolean_return_type("return function(): boolean end") => ReturnStatement::one(
1169 FunctionExpression::from_block(default_block())
1170 .with_return_type(
1171 TypeName::new(create_identifier("boolean", 19, 1))
1172 )
1173 .with_tokens(FunctionBodyTokens {
1174 function: token_at_first_line(7, 15),
1175 opening_parenthese: token_at_first_line(15, 16),
1176 closing_parenthese: token_at_first_line(16, 17),
1177 end: token_at_first_line(27, 30),
1178 parameter_commas: Vec::new(),
1179 variable_arguments: None,
1180 variable_arguments_colon: None,
1181 return_type_colon: Some(spaced_token(17, 18)),
1182 }),
1183 ).with_tokens(ReturnTokens {
1184 r#return: spaced_token(0, 6),
1185 commas: Vec::new(),
1186 }),
1187 return_empty_function_with_void_return_type("return function(): () end") => ReturnStatement::one(
1188 FunctionExpression::from_block(default_block())
1189 .with_return_type(
1190 TypePack::default()
1191 .with_tokens(TypePackTokens {
1192 left_parenthese: token_at_first_line(19,20),
1193 right_parenthese: spaced_token(20,21),
1194 commas:Vec::new(),
1195 })
1196 )
1197 .with_tokens(FunctionBodyTokens {
1198 function: token_at_first_line(7, 15),
1199 opening_parenthese: token_at_first_line(15, 16),
1200 closing_parenthese: token_at_first_line(16, 17),
1201 end: token_at_first_line(22, 25),
1202 parameter_commas: Vec::new(),
1203 variable_arguments: None,
1204 variable_arguments_colon: None,
1205 return_type_colon: Some(spaced_token(17, 18)),
1206 }),
1207 ).with_tokens(ReturnTokens {
1208 r#return: spaced_token(0, 6),
1209 commas: Vec::new(),
1210 }),
1211 return_empty_function_return_type_pack_with_one_type("return function(): (true) end") => ReturnStatement::one(
1212 FunctionExpression::from_block(default_block())
1213 .with_return_type(
1214 TypePack::default()
1215 .with_type(Type::True(Some(token_at_first_line(20, 24))))
1216 .with_tokens(TypePackTokens {
1217 left_parenthese: token_at_first_line(19, 20),
1218 right_parenthese: spaced_token(24, 25),
1219 commas: Vec::new(),
1220 })
1221 )
1222 .with_tokens(FunctionBodyTokens {
1223 function: token_at_first_line(7, 15),
1224 opening_parenthese: token_at_first_line(15, 16),
1225 closing_parenthese: token_at_first_line(16, 17),
1226 end: token_at_first_line(26, 29),
1227 parameter_commas: Vec::new(),
1228 variable_arguments: None,
1229 variable_arguments_colon: None,
1230 return_type_colon: Some(spaced_token(17, 18)),
1231 }),
1232 ).with_tokens(ReturnTokens {
1233 r#return: spaced_token(0, 6),
1234 commas: Vec::new(),
1235 }),
1236 return_empty_function_return_variadic_pack("return function(): ...string end") => ReturnStatement::one(
1237 FunctionExpression::from_block(default_block())
1238 .with_return_type(
1239 VariadicTypePack::new(TypeName::new(create_identifier("string", 22, 1)))
1240 .with_token(token_at_first_line(19, 22))
1241 )
1242 .with_tokens(FunctionBodyTokens {
1243 function: token_at_first_line(7, 15),
1244 opening_parenthese: token_at_first_line(15, 16),
1245 closing_parenthese: token_at_first_line(16, 17),
1246 end: token_at_first_line(29, 32),
1247 parameter_commas: Vec::new(),
1248 variable_arguments: None,
1249 variable_arguments_colon: None,
1250 return_type_colon: Some(spaced_token(17, 18)),
1251 }),
1252 ).with_tokens(ReturnTokens {
1253 r#return: spaced_token(0, 6),
1254 commas: Vec::new(),
1255 }),
1256 return_empty_function_return_generic_pack("return function(): T... end") => ReturnStatement::one(
1257 FunctionExpression::from_block(default_block())
1258 .with_return_type(
1259 GenericTypePack::new(create_identifier("T", 19, 0))
1260 .with_token(spaced_token(20, 23))
1261 )
1262 .with_tokens(FunctionBodyTokens {
1263 function: token_at_first_line(7, 15),
1264 opening_parenthese: token_at_first_line(15, 16),
1265 closing_parenthese: token_at_first_line(16, 17),
1266 end: token_at_first_line(24, 27),
1267 parameter_commas: Vec::new(),
1268 variable_arguments: None,
1269 variable_arguments_colon: None,
1270 return_type_colon: Some(spaced_token(17, 18)),
1271 }),
1272 ).with_tokens(ReturnTokens {
1273 r#return: spaced_token(0, 6),
1274 commas: Vec::new(),
1275 }),
1276 return_empty_function_return_type_pack_with_variadic_pack("return function(): (...string) end") => ReturnStatement::one(
1277 FunctionExpression::from_block(default_block())
1278 .with_return_type(
1279 TypePack::default()
1280 .with_variadic_type(
1281 VariadicTypePack::new(TypeName::new(create_identifier("string", 23, 0)))
1282 .with_token(token_at_first_line(20, 23))
1283 )
1284 .with_tokens(TypePackTokens {
1285 left_parenthese: token_at_first_line(19, 20),
1286 right_parenthese: spaced_token(29, 30),
1287 commas: Vec::new()
1288 })
1289 )
1290 .with_tokens(FunctionBodyTokens {
1291 function: token_at_first_line(7, 15),
1292 opening_parenthese: token_at_first_line(15, 16),
1293 closing_parenthese: token_at_first_line(16, 17),
1294 end: token_at_first_line(31, 34),
1295 parameter_commas: Vec::new(),
1296 variable_arguments: None,
1297 variable_arguments_colon: None,
1298 return_type_colon: Some(spaced_token(17, 18)),
1299 }),
1300 ).with_tokens(ReturnTokens {
1301 r#return: spaced_token(0, 6),
1302 commas: Vec::new(),
1303 }),
1304 return_empty_function_return_type_pack_with_generic_pack("return function(): (T...) end") => ReturnStatement::one(
1305 FunctionExpression::from_block(default_block())
1306 .with_return_type(
1307 TypePack::default()
1308 .with_variadic_type(
1309 GenericTypePack::new(create_identifier("T", 20, 0))
1310 .with_token(token_at_first_line(21, 24))
1311 )
1312 .with_tokens(TypePackTokens {
1313 left_parenthese: token_at_first_line(19, 20),
1314 right_parenthese: spaced_token(24, 25),
1315 commas: Vec::new()
1316 })
1317 )
1318 .with_tokens(FunctionBodyTokens {
1319 function: token_at_first_line(7, 15),
1320 opening_parenthese: token_at_first_line(15, 16),
1321 closing_parenthese: token_at_first_line(16, 17),
1322 end: token_at_first_line(26, 29),
1323 parameter_commas: Vec::new(),
1324 variable_arguments: None,
1325 variable_arguments_colon: None,
1326 return_type_colon: Some(spaced_token(17, 18)),
1327 }),
1328 ).with_tokens(ReturnTokens {
1329 r#return: spaced_token(0, 6),
1330 commas: Vec::new(),
1331 }),
1332 return_empty_function_return_type_pack_with_two_types("return function(): (true, false) end") => ReturnStatement::one(
1333 FunctionExpression::from_block(default_block())
1334 .with_return_type(
1335 TypePack::default()
1336 .with_type(Type::True(Some(token_at_first_line(20, 24))))
1337 .with_type(Type::False(Some(token_at_first_line(26, 31))))
1338 .with_tokens(TypePackTokens {
1339 left_parenthese: token_at_first_line(19, 20),
1340 right_parenthese: spaced_token(31, 32),
1341 commas: vec![spaced_token(24, 25)],
1342 })
1343 )
1344 .with_tokens(FunctionBodyTokens {
1345 function: token_at_first_line(7, 15),
1346 opening_parenthese: token_at_first_line(15, 16),
1347 closing_parenthese: token_at_first_line(16, 17),
1348 end: token_at_first_line(33, 36),
1349 parameter_commas: Vec::new(),
1350 variable_arguments: None,
1351 variable_arguments_colon: None,
1352 return_type_colon: Some(spaced_token(17, 18)),
1353 }),
1354 ).with_tokens(ReturnTokens {
1355 r#return: spaced_token(0, 6),
1356 commas: Vec::new(),
1357 }),
1358 return_empty_function_return_type_pack_with_two_types_and_variadic_pack("return function(): (true, false, ...string) end") => ReturnStatement::one(
1359 FunctionExpression::from_block(default_block())
1360 .with_return_type(
1361 TypePack::default()
1362 .with_type(Type::True(Some(token_at_first_line(20, 24))))
1363 .with_type(Type::False(Some(token_at_first_line(26, 31))))
1364 .with_variadic_type(
1365 VariadicTypePack::new(TypeName::new(create_identifier("string", 36, 0)))
1366 .with_token(token_at_first_line(33, 36))
1367 )
1368 .with_tokens(TypePackTokens {
1369 left_parenthese: token_at_first_line(19, 20),
1370 right_parenthese: spaced_token(42, 43),
1371 commas: vec![spaced_token(24, 25), spaced_token(31, 32)],
1372 })
1373 )
1374 .with_tokens(FunctionBodyTokens {
1375 function: token_at_first_line(7, 15),
1376 opening_parenthese: token_at_first_line(15, 16),
1377 closing_parenthese: token_at_first_line(16, 17),
1378 end: token_at_first_line(44, 47),
1379 parameter_commas: Vec::new(),
1380 variable_arguments: None,
1381 variable_arguments_colon: None,
1382 return_type_colon: Some(spaced_token(17, 18)),
1383 }),
1384 ).with_tokens(ReturnTokens {
1385 r#return: spaced_token(0, 6),
1386 commas: Vec::new(),
1387 }),
1388 return_empty_function_with_one_param("return function(a )end") => ReturnStatement::one(
1389 FunctionExpression::from_block(default_block()).with_parameter(
1390 create_identifier("a", 16, 1)
1391 ).with_tokens(FunctionBodyTokens {
1392 function: token_at_first_line(7, 15),
1393 opening_parenthese: token_at_first_line(15, 16),
1394 closing_parenthese: token_at_first_line(18, 19),
1395 end: token_at_first_line(19, 22),
1396 parameter_commas: Vec::new(),
1397 variable_arguments: None,
1398 variable_arguments_colon: None,
1399 return_type_colon: None,
1400 }),
1401 ).with_tokens(ReturnTokens {
1402 r#return: spaced_token(0, 6),
1403 commas: Vec::new(),
1404 }),
1405 return_empty_function_with_one_typed_param("return function(a : string)end") => ReturnStatement::one(
1406 FunctionExpression::from_block(default_block())
1407 .with_parameter(
1408 TypedIdentifier::from(create_identifier("a", 16, 1))
1409 .with_colon_token(spaced_token(18, 19))
1410 .with_type(TypeName::new(create_identifier("string", 20, 0))),
1411 )
1412 .with_tokens(FunctionBodyTokens {
1413 function: token_at_first_line(7, 15),
1414 opening_parenthese: token_at_first_line(15, 16),
1415 closing_parenthese: token_at_first_line(26, 27),
1416 end: token_at_first_line(27, 30),
1417 parameter_commas: Vec::new(),
1418 variable_arguments: None,
1419 variable_arguments_colon: None,
1420 return_type_colon: None,
1421 }),
1422 ).with_tokens(ReturnTokens {
1423 r#return: spaced_token(0, 6),
1424 commas: Vec::new(),
1425 }),
1426 return_empty_function_with_two_params("return function(a, b--[[foo]]) end") => ReturnStatement::one(
1427 FunctionExpression::from_block(default_block())
1428 .with_parameter(Identifier::new("a").with_token(token_at_first_line(16, 17)))
1429 .with_parameter(
1430 Identifier::new("b").with_token(
1431 token_at_first_line(19, 20).with_trailing_trivia(TriviaKind::Comment.at(20, 29, 1))
1432 )
1433 )
1434 .with_tokens(FunctionBodyTokens {
1435 function: token_at_first_line(7, 15),
1436 opening_parenthese: token_at_first_line(15, 16),
1437 closing_parenthese: spaced_token(29, 30),
1438 end: token_at_first_line(31, 34),
1439 parameter_commas: vec![spaced_token(17, 18)],
1440 variable_arguments: None,
1441 variable_arguments_colon: None,
1442 return_type_colon: None,
1443 }),
1444 ).with_tokens(ReturnTokens {
1445 r#return: spaced_token(0, 6),
1446 commas: Vec::new(),
1447 }),
1448 return_empty_variadic_function("return function(... ) end") => ReturnStatement::one(
1449 FunctionExpression::from_block(default_block())
1450 .variadic()
1451 .with_tokens(FunctionBodyTokens {
1452 function: token_at_first_line(7, 15),
1453 opening_parenthese: token_at_first_line(15, 16),
1454 closing_parenthese: spaced_token(20, 21),
1455 end: token_at_first_line(22, 25),
1456 parameter_commas: Vec::new(),
1457 variable_arguments: Some(spaced_token(16, 19)),
1458 variable_arguments_colon: None,
1459 return_type_colon: None,
1460 }),
1461 ).with_tokens(ReturnTokens {
1462 r#return: spaced_token(0, 6),
1463 commas: Vec::new(),
1464 }),
1465 return_empty_typed_variadic_function("return function(... : string ) end") => ReturnStatement::one(
1466 FunctionExpression::from_block(default_block())
1467 .with_variadic_type(
1468 TypeName::new(create_identifier("string", 22, 1))
1469 )
1470 .with_tokens(FunctionBodyTokens {
1471 function: token_at_first_line(7, 15),
1472 opening_parenthese: token_at_first_line(15, 16),
1473 closing_parenthese: spaced_token(29, 30),
1474 end: token_at_first_line(31, 34),
1475 parameter_commas: Vec::new(),
1476 variable_arguments: Some(spaced_token(16, 19)),
1477 variable_arguments_colon: Some(spaced_token(20, 21)),
1478 return_type_colon: None,
1479 }),
1480 ).with_tokens(ReturnTokens {
1481 r#return: spaced_token(0, 6),
1482 commas: Vec::new(),
1483 }),
1484 return_empty_function_with_generic_return_type("return function<T>(): T end") => ReturnStatement::one(
1485 FunctionExpression::from_block(default_block())
1486 .with_return_type(
1487 TypeName::new(create_identifier("T", 22, 1))
1488 )
1489 .with_generic_parameters(
1490 GenericParameters::from_type_variable(create_identifier("T", 16, 0))
1491 .with_tokens(GenericParametersTokens {
1492 opening_list: token_at_first_line(15, 16),
1493 closing_list: token_at_first_line(17, 18),
1494 commas: Vec::new(),
1495 })
1496 )
1497 .with_tokens(FunctionBodyTokens {
1498 function: token_at_first_line(7, 15),
1499 opening_parenthese: token_at_first_line(18, 19),
1500 closing_parenthese: token_at_first_line(19, 20),
1501 end: token_at_first_line(24, 27),
1502 parameter_commas: Vec::new(),
1503 variable_arguments: None,
1504 variable_arguments_colon: None,
1505 return_type_colon: Some(spaced_token(20, 21)),
1506 }),
1507 ).with_tokens(ReturnTokens {
1508 r#return: spaced_token(0, 6),
1509 commas: Vec::new(),
1510 }),
1511 return_function_with_native_attribute("return @native function() end") => ReturnStatement::one(
1512 FunctionExpression::from_block(default_block())
1513 .with_attribute(
1514 NamedAttribute::new(create_identifier("native", 8, 1))
1515 .with_token(token_at_first_line(7, 8))
1516 )
1517 .with_tokens(FunctionBodyTokens {
1518 function: token_at_first_line(15, 23),
1519 opening_parenthese: token_at_first_line(23, 24),
1520 closing_parenthese: spaced_token(24, 25),
1521 end: token_at_first_line(26, 29),
1522 parameter_commas: Vec::new(),
1523 variable_arguments: None,
1524 variable_arguments_colon: None,
1525 return_type_colon: None,
1526 }),
1527 ).with_tokens(ReturnTokens {
1528 r#return: spaced_token(0, 6),
1529 commas: Vec::new(),
1530 }),
1531 return_function_with_native_and_deprecated_attribute("return @native @deprecated function() end") => ReturnStatement::one(
1532 FunctionExpression::from_block(default_block())
1533 .with_attribute(
1534 NamedAttribute::new(create_identifier("native", 8, 1))
1535 .with_token(token_at_first_line(7, 8))
1536 )
1537 .with_attribute(
1538 NamedAttribute::new(create_identifier("deprecated", 16, 1))
1539 .with_token(token_at_first_line(15, 16))
1540 )
1541 .with_tokens(FunctionBodyTokens {
1542 function: token_at_first_line(27, 35),
1543 opening_parenthese: token_at_first_line(35, 36),
1544 closing_parenthese: spaced_token(36, 37),
1545 end: token_at_first_line(38, 41),
1546 parameter_commas: Vec::new(),
1547 variable_arguments: None,
1548 variable_arguments_colon: None,
1549 return_type_colon: None,
1550 }),
1551 ).with_tokens(ReturnTokens {
1552 r#return: spaced_token(0, 6),
1553 commas: Vec::new(),
1554 }),
1555 return_two_values("return true , true--end") => ReturnStatement::default()
1556 .with_expression(create_true(7, 1))
1557 .with_expression(Expression::True(Some(
1558 token_at_first_line(15, 19).with_trailing_trivia(TriviaKind::Comment.at(19, 24, 1))
1559 )))
1560 .with_tokens(ReturnTokens {
1561 r#return: spaced_token(0, 6),
1562 commas: vec![
1563 token_at_first_line(12, 13).with_trailing_trivia(TriviaKind::Whitespace.at(13, 15, 1))
1564 ],
1565 }),
1566 return_variable("return var") => ReturnStatement::default()
1567 .with_expression(
1568 Identifier::new("var").with_token(token_at_first_line(7, 10))
1569 )
1570 .with_tokens(ReturnTokens {
1571 r#return: spaced_token(0, 6),
1572 commas: Vec::new(),
1573 }),
1574 break_statement("break") => LastStatement::Break(Some(token_at_first_line(0, 5))),
1575 break_statement_with_comment("break-- bye") => LastStatement::Break(Some(
1576 token_at_first_line(0, 5).with_trailing_trivia(TriviaKind::Comment.at(5, 11, 1))
1577 )),
1578 continue_statement("continue") => LastStatement::Continue(Some(token_at_first_line(0, 8))),
1579 continue_statement_with_comment("continue-- bye") => LastStatement::Continue(Some(
1580 token_at_first_line(0, 8).with_trailing_trivia(TriviaKind::Comment.at(8, 14, 1))
1581 )),
1582 );
1583
1584 test_parse_statement_with_tokens!(
1585 empty_local_function("local function name ()end") => FunctionAssignment::from_name(
1586 create_identifier("name", 15, 1),
1587 default_block()
1588 ).with_tokens(FunctionAssignmentTokens {
1589 keyword: spaced_token(0, 5),
1590 function_body: FunctionBodyTokens {
1591 function: spaced_token(6, 14),
1592 opening_parenthese: token_at_first_line(20, 21),
1593 closing_parenthese: token_at_first_line(21, 22),
1594 end: token_at_first_line(22, 25),
1595 parameter_commas: Vec::new(),
1596 variable_arguments: None,
1597 variable_arguments_colon: None,
1598 return_type_colon: None,
1599 },
1600 }),
1601 empty_const_function("const function name ()end") => FunctionAssignment::from_name(
1602 create_identifier("name", 15, 1),
1603 default_block()
1604 )
1605 .with_assignment_kind(AssignmentKind::Const)
1606 .with_tokens(FunctionAssignmentTokens {
1607 keyword: spaced_token(0, 5),
1608 function_body: FunctionBodyTokens {
1609 function: spaced_token(6, 14),
1610 opening_parenthese: token_at_first_line(20, 21),
1611 closing_parenthese: token_at_first_line(21, 22),
1612 end: token_at_first_line(22, 25),
1613 parameter_commas: Vec::new(),
1614 variable_arguments: None,
1615 variable_arguments_colon: None,
1616 return_type_colon: None,
1617 },
1618 }),
1619 empty_local_function_variadic("local function name(...)end") => FunctionAssignment::from_name(
1620 Identifier::new("name").with_token(token_at_first_line(15, 19)),
1621 default_block(),
1622 )
1623 .variadic()
1624 .with_tokens(FunctionAssignmentTokens {
1625 keyword: spaced_token(0, 5),
1626 function_body: FunctionBodyTokens {
1627 function: spaced_token(6, 14),
1628 opening_parenthese: token_at_first_line(19, 20),
1629 closing_parenthese: token_at_first_line(23, 24),
1630 end: token_at_first_line(24, 27),
1631 parameter_commas: Vec::new(),
1632 variable_arguments: Some(token_at_first_line(20, 23)),
1633 variable_arguments_colon: None,
1634 return_type_colon: None,
1635 },
1636 }),
1637 empty_local_function_with_two_parameters("local function name(a,b) end")
1638 => FunctionAssignment::from_name(
1639 Identifier::new("name").with_token(token_at_first_line(15, 19)),
1640 default_block(),
1641 )
1642 .with_parameter(Identifier::new("a").with_token(token_at_first_line(20, 21)))
1643 .with_parameter(Identifier::new("b").with_token(token_at_first_line(22, 23)))
1644 .with_tokens(FunctionAssignmentTokens {
1645 keyword: spaced_token(0, 5),
1646 function_body: FunctionBodyTokens {
1647 function: spaced_token(6, 14),
1648 opening_parenthese: token_at_first_line(19, 20),
1649 closing_parenthese: spaced_token(23, 24),
1650 end: token_at_first_line(25, 28),
1651 parameter_commas: vec![token_at_first_line(21, 22)],
1652 variable_arguments: None,
1653 variable_arguments_colon: None,
1654 return_type_colon: None,
1655 },
1656 }),
1657 empty_local_function_with_generic_return_type("local function fn<T>(): T end")
1658 => FunctionAssignment::from_name(create_identifier("fn", 15, 0), default_block())
1659 .with_return_type(
1660 TypeName::new(create_identifier("T", 24, 1))
1661 )
1662 .with_generic_parameters(
1663 GenericParameters::from_type_variable(create_identifier("T", 18, 0))
1664 .with_tokens(GenericParametersTokens {
1665 opening_list: token_at_first_line(17, 18),
1666 closing_list: token_at_first_line(19, 20),
1667 commas: Vec::new(),
1668 })
1669 )
1670 .with_tokens(FunctionAssignmentTokens {
1671 keyword: spaced_token(0, 5),
1672 function_body: FunctionBodyTokens {
1673 function: spaced_token(6, 14),
1674 opening_parenthese: token_at_first_line(20, 21),
1675 closing_parenthese: token_at_first_line(21, 22),
1676 end: token_at_first_line(26, 29),
1677 parameter_commas: Vec::new(),
1678 variable_arguments: None,
1679 variable_arguments_colon: None,
1680 return_type_colon: Some(spaced_token(22, 23)),
1681 }
1682 }),
1683 empty_local_function_with_two_generic_type("local function fn<T, U>() end")
1684 => FunctionAssignment::from_name(create_identifier("fn", 15, 0), default_block())
1685 .with_generic_parameters(
1686 GenericParameters::from_type_variable(create_identifier("T", 18, 0))
1687 .with_type_variable(create_identifier("U", 21, 0))
1688 .with_tokens(GenericParametersTokens {
1689 opening_list: token_at_first_line(17, 18),
1690 closing_list: token_at_first_line(22, 23),
1691 commas: vec![spaced_token(19, 20)],
1692 })
1693 )
1694 .with_tokens(FunctionAssignmentTokens {
1695 keyword: spaced_token(0, 5),
1696 function_body: FunctionBodyTokens {
1697 function: spaced_token(6, 14),
1698 opening_parenthese: token_at_first_line(23, 24),
1699 closing_parenthese: spaced_token(24, 25),
1700 end: token_at_first_line(26, 29),
1701 parameter_commas: Vec::new(),
1702 variable_arguments: None,
1703 variable_arguments_colon: None,
1704 return_type_colon: None,
1705 }
1706 }),
1707 call_function("call()") => FunctionCall::from_name(
1708 create_identifier("call", 0, 0)
1709 ).with_arguments(TupleArguments::default().with_tokens(TupleArgumentsTokens {
1710 opening_parenthese: token_at_first_line(4, 5),
1711 closing_parenthese: token_at_first_line(5, 6),
1712 commas: Vec::new(),
1713 })).with_tokens(FunctionCallTokens {
1714 colon: None,
1715 type_instantiation_tokens: None,
1716 }),
1717 call_indexed_table("foo.bar()") => FunctionCall::from_prefix(
1718 FieldExpression::new(
1719 create_identifier("foo", 0, 0),
1720 create_identifier("bar", 4, 0)
1721 ).with_token(token_at_first_line(3, 4))
1722 ).with_arguments(TupleArguments::default().with_tokens(TupleArgumentsTokens {
1723 opening_parenthese: token_at_first_line(7, 8),
1724 closing_parenthese: token_at_first_line(8, 9),
1725 commas: Vec::new(),
1726 })).with_tokens(FunctionCallTokens {
1727 colon: None,
1728 type_instantiation_tokens: None,
1729 }),
1730 call_method("foo: bar()") => FunctionCall::from_name(create_identifier("foo", 0, 0))
1731 .with_method(create_identifier("bar", 5, 0))
1732 .with_arguments(TupleArguments::default().with_tokens(TupleArgumentsTokens {
1733 opening_parenthese: token_at_first_line(8, 9),
1734 closing_parenthese: token_at_first_line(9, 10),
1735 commas: Vec::new(),
1736 }))
1737 .with_tokens(FunctionCallTokens {
1738 colon: Some(spaced_token(3, 4)),
1739 type_instantiation_tokens: None,
1740 }),
1741 call_method_with_type_instantiation_of_one_type("foo: bar< <T>--[[]]> ()") => FunctionCall::from_name(create_identifier("foo", 0, 0))
1742 .with_type_instantiation_method(
1743 create_identifier("bar", 5, 0),
1744 vec![TypeName::new(create_identifier("T", 11, 0)).into()]
1745 )
1746 .with_arguments(TupleArguments::default().with_tokens(TupleArgumentsTokens {
1747 opening_parenthese: token_at_first_line(22, 23),
1748 closing_parenthese: token_at_first_line(23, 24),
1749 commas: Vec::new(),
1750 }))
1751 .with_tokens(FunctionCallTokens {
1752 colon: Some(spaced_token(3, 4)),
1753 type_instantiation_tokens: Some(TypeInstantiationTokens {
1754 first_opening_list: spaced_token(8, 9),
1755 second_opening_list: token_at_first_line(10, 11),
1756 first_closing_list: token_at_first_line(12, 13)
1757 .with_trailing_trivia(TriviaKind::Comment.at(13, 19, 1)),
1758 second_closing_list: token_at_first_line(19, 20)
1759 .with_trailing_trivia(TriviaKind::Whitespace.at(20, 22, 1)),
1760 commas: Vec::new(),
1761 }),
1762 }),
1763 call_method_with_one_argument("foo:bar( true )") => FunctionCall::from_name(
1764 create_identifier("foo", 0, 0)
1765 )
1766 .with_method(create_identifier("bar", 4, 0))
1767 .with_arguments(
1768 TupleArguments::default()
1769 .with_argument(create_true(9, 1))
1770 .with_tokens(TupleArgumentsTokens {
1771 opening_parenthese: spaced_token(7, 8),
1772 closing_parenthese: token_at_first_line(14, 15),
1773 commas: Vec::new(),
1774 })
1775 )
1776 .with_tokens(FunctionCallTokens {
1777 colon: Some(token_at_first_line(3, 4)),
1778 type_instantiation_tokens: None,
1779 }),
1780 call_function_with_one_argument("call ( true ) ") => FunctionCall::from_name(
1781 create_identifier("call", 0, 1)
1782 )
1783 .with_arguments(
1784 TupleArguments::default()
1785 .with_argument(create_true(7, 1))
1786 .with_tokens(TupleArgumentsTokens {
1787 opening_parenthese: spaced_token(5, 6),
1788 closing_parenthese: spaced_token(12, 13),
1789 commas: Vec::new(),
1790 })
1791 )
1792 .with_tokens(FunctionCallTokens {
1793 colon: None,
1794 type_instantiation_tokens: None,
1795 }),
1796 call_function_with_two_arguments("call(true, true)") => FunctionCall::from_name(
1797 create_identifier("call", 0, 0)
1798 )
1799 .with_arguments(
1800 TupleArguments::default()
1801 .with_argument(create_true(5, 0))
1802 .with_argument(create_true(11, 0))
1803 .with_tokens(TupleArgumentsTokens {
1804 opening_parenthese: token_at_first_line(4, 5),
1805 closing_parenthese: token_at_first_line(15, 16),
1806 commas: vec![spaced_token(9, 10)],
1807 })
1808 )
1809 .with_tokens(FunctionCallTokens {
1810 colon: None,
1811 type_instantiation_tokens: None,
1812 }),
1813 call_chain_with_args("call(true)( )") => FunctionCall::from_prefix(
1814 FunctionCall::from_name(create_identifier("call", 0, 0))
1815 .with_arguments(
1816 TupleArguments::default()
1817 .with_argument(create_true(5, 0))
1818 .with_tokens(TupleArgumentsTokens {
1819 opening_parenthese: token_at_first_line(4, 5),
1820 closing_parenthese: token_at_first_line(9, 10),
1821 commas: Vec::new(),
1822 })
1823 )
1824 .with_tokens(FunctionCallTokens {
1825 colon: None,
1826 type_instantiation_tokens: None,
1827 }),
1828 )
1829 .with_arguments(TupleArguments::default().with_tokens(TupleArgumentsTokens {
1830 opening_parenthese: spaced_token(10, 11),
1831 closing_parenthese: token_at_first_line(12, 13),
1832 commas: Vec::new(),
1833 }))
1834 .with_tokens(FunctionCallTokens {
1835 colon: None,
1836 type_instantiation_tokens: None,
1837 }),
1838 call_with_empty_table_argument("call{ }") => FunctionCall::from_name(
1839 create_identifier("call", 0, 0)
1840 ).with_arguments(TableExpression::default().with_tokens(TableTokens {
1841 opening_brace: spaced_token(4, 5),
1842 closing_brace: token_at_first_line(6, 7),
1843 separators: Vec::new(),
1844 })).with_tokens(FunctionCallTokens {
1845 colon: None,
1846 type_instantiation_tokens: None,
1847 }),
1848 call_with_empty_string_argument("call ''") => FunctionCall::from_name(
1849 create_identifier("call", 0, 1)
1850 ).with_arguments(
1851 StringExpression::empty().with_token(token_at_first_line(5, 7))
1852 ).with_tokens(FunctionCallTokens {
1853 colon: None,
1854 type_instantiation_tokens: None,
1855 }),
1856 empty_do("do end") => DoStatement::new(default_block())
1857 .with_tokens(DoTokens {
1858 r#do: spaced_token(0, 2),
1859 end: token_at_first_line(3, 6),
1860 }),
1861 empty_do_with_long_comment("do --[[ hello ]] end") => DoStatement::new(default_block())
1862 .with_tokens(DoTokens {
1863 r#do: token_at_first_line(0, 2)
1864 .with_trailing_trivia(TriviaKind::Whitespace.at(2, 3, 1))
1865 .with_trailing_trivia(TriviaKind::Comment.at(3, 16, 1))
1866 .with_trailing_trivia(TriviaKind::Whitespace.at(16, 17, 1)),
1867 end: token_at_first_line(17, 20),
1868 }),
1869 assign_variable("var = true") => AssignStatement::from_variable(
1870 create_identifier("var", 0, 1),
1871 Expression::True(Some(token_at_first_line(6, 10))),
1872 ).with_tokens(AssignTokens {
1873 equal: spaced_token(4, 5),
1874 variable_commas: Vec::new(),
1875 value_commas: Vec::new(),
1876 }),
1877 assign_two_variables_with_two_values("var, var2 = true, true") => AssignStatement::from_variable(
1878 Identifier::new("var").with_token(token_at_first_line(0, 3)),
1879 create_true(12, 0),
1880 ).append_assignment(
1881 create_identifier("var2", 5, 1),
1882 Expression::True(Some(token_at_first_line(18, 22))),
1883 ).with_tokens(AssignTokens {
1884 equal: spaced_token(10, 11),
1885 variable_commas: vec![spaced_token(3, 4)],
1886 value_commas: vec![spaced_token(16, 17)],
1887 }),
1888 empty_function_statement("function name() end")
1889 => FunctionStatement::new(
1890 FunctionName::from_name(
1891 Identifier::new("name").with_token(token_at_first_line(9, 13))
1892 ).with_tokens(FunctionNameTokens { periods: Vec::new(), colon: None }),
1893 default_block(),
1894 Vec::new(),
1895 false,
1896 ).with_tokens(FunctionBodyTokens {
1897 function: spaced_token(0, 8),
1898 opening_parenthese: token_at_first_line(13, 14),
1899 closing_parenthese: spaced_token(14, 15),
1900 end: token_at_first_line(16, 19),
1901 parameter_commas: Vec::new(),
1902 variable_arguments: None,
1903 variable_arguments_colon: None,
1904 return_type_colon: None,
1905 }),
1906 empty_function_statement_with_field("function name.field ()end")
1907 => FunctionStatement::new(
1908 FunctionName::from_name(
1909 Identifier::new("name").with_token(token_at_first_line(9, 13))
1910 ).with_field(
1911 Identifier::new("field").with_token(spaced_token(14, 19))
1912 ).with_tokens(FunctionNameTokens {
1913 periods: vec![token_at_first_line(13, 14)],
1914 colon: None,
1915 }),
1916 default_block(),
1917 Vec::new(),
1918 false,
1919 ).with_tokens(FunctionBodyTokens {
1920 function: spaced_token(0, 8),
1921 opening_parenthese: token_at_first_line(20, 21),
1922 closing_parenthese: token_at_first_line(21, 22),
1923 end: token_at_first_line(22, 25),
1924 parameter_commas: Vec::new(),
1925 variable_arguments: None,
1926 variable_arguments_colon: None,
1927 return_type_colon: None,
1928 }),
1929 empty_function_statement_with_method("function name:method ()end")
1930 => FunctionStatement::new(
1931 FunctionName::from_name(
1932 Identifier::new("name").with_token(token_at_first_line(9, 13))
1933 )
1934 .with_method(create_identifier("method", 14, 1))
1935 .with_tokens(FunctionNameTokens {
1936 periods: Vec::new(),
1937 colon: Some(token_at_first_line(13, 14)),
1938 }),
1939 default_block(),
1940 Vec::new(),
1941 false,
1942 ).with_tokens(FunctionBodyTokens {
1943 function: spaced_token(0, 8),
1944 opening_parenthese: token_at_first_line(21, 22),
1945 closing_parenthese: token_at_first_line(22, 23),
1946 end: token_at_first_line(23, 26),
1947 parameter_commas: Vec::new(),
1948 variable_arguments: None,
1949 variable_arguments_colon: None,
1950 return_type_colon: None,
1951 }),
1952 empty_function_statement_variadic("function name(...) end")
1953 => FunctionStatement::new(
1954 FunctionName::from_name(
1955 Identifier::new("name").with_token(token_at_first_line(9, 13))
1956 ).with_tokens(FunctionNameTokens { periods: Vec::new(), colon: None }),
1957 default_block(),
1958 Vec::new(),
1959 true,
1960 ).with_tokens(FunctionBodyTokens {
1961 function: spaced_token(0, 8),
1962 opening_parenthese: token_at_first_line(13, 14),
1963 closing_parenthese: token_at_first_line(17, 18)
1964 .with_trailing_trivia(TriviaKind::Whitespace.at(18, 19, 1)),
1965 end: token_at_first_line(19, 22),
1966 parameter_commas: Vec::new(),
1967 variable_arguments: Some(token_at_first_line(14, 17)),
1968 variable_arguments_colon: None,
1969 return_type_colon: None,
1970 }),
1971 empty_function_statement_variadic_with_one_parameter("function name(a,...)end")
1972 => FunctionStatement::new(
1973 FunctionName::from_name(
1974 Identifier::new("name").with_token(token_at_first_line(9, 13))
1975 ).with_tokens(FunctionNameTokens { periods: Vec::new(), colon: None }),
1976 default_block(),
1977 vec![
1978 Identifier::new("a").with_token(token_at_first_line(14, 15)).into()
1979 ],
1980 true,
1981 ).with_tokens(FunctionBodyTokens {
1982 function: spaced_token(0, 8),
1983 opening_parenthese: token_at_first_line(13, 14),
1984 closing_parenthese: token_at_first_line(19, 20),
1985 end: token_at_first_line(20, 23),
1986 parameter_commas: vec![
1987 token_at_first_line(15, 16),
1988 ],
1989 variable_arguments: Some(token_at_first_line(16, 19)),
1990 variable_arguments_colon: None,
1991 return_type_colon: None,
1992 }),
1993 empty_function_with_generic_return_type("function fn<T>(): T end")
1994 => FunctionStatement::new(
1995 FunctionName::from_name(create_identifier("fn", 9, 0))
1996 .with_tokens(FunctionNameTokens { periods: Vec::new(), colon: None }),
1997 default_block(),
1998 Vec::new(),
1999 false
2000 )
2001 .with_return_type(TypeName::new(create_identifier("T", 18, 1)))
2002 .with_generic_parameters(
2003 GenericParameters::from_type_variable(create_identifier("T", 12, 0))
2004 .with_tokens(GenericParametersTokens {
2005 opening_list: token_at_first_line(11, 12),
2006 closing_list: token_at_first_line(13, 14),
2007 commas: Vec::new(),
2008 })
2009 )
2010 .with_tokens(FunctionBodyTokens {
2011 function: spaced_token(0, 8),
2012 opening_parenthese: token_at_first_line(14, 15),
2013 closing_parenthese: token_at_first_line(15, 16),
2014 end: token_at_first_line(20, 23),
2015 parameter_commas: Vec::new(),
2016 variable_arguments: None,
2017 variable_arguments_colon: None,
2018 return_type_colon: Some(spaced_token(16, 17)),
2019 }),
2020 empty_generic_for("for key in foo do end") => GenericForStatement::new(
2021 vec![
2022 create_identifier("key", 4, 1).into(),
2023 ],
2024 vec![
2025 create_identifier("foo", 11, 1).into(),
2026 ],
2027 default_block(),
2028 ).with_tokens(GenericForTokens {
2029 r#for: spaced_token(0, 3),
2030 r#in: spaced_token(8, 10),
2031 r#do: spaced_token(15, 17),
2032 end: token_at_first_line(18, 21),
2033 identifier_commas: Vec::new(),
2034 value_commas: Vec::new(),
2035 }),
2036 empty_generic_for_with_typed_key("for key: Key in foo do end") => GenericForStatement::new(
2037 vec![
2038 create_identifier("key", 4, 0)
2039 .with_type(TypeName::new(create_identifier("Key", 9, 1)))
2040 .with_colon_token(spaced_token(7, 8)),
2041 ],
2042 vec![
2043 create_identifier("foo", 16, 1).into(),
2044 ],
2045 default_block(),
2046 ).with_tokens(GenericForTokens {
2047 r#for: spaced_token(0, 3),
2048 r#in: spaced_token(13, 15),
2049 r#do: spaced_token(20, 22),
2050 end: token_at_first_line(23, 26),
2051 identifier_commas: Vec::new(),
2052 value_commas: Vec::new(),
2053 }),
2054 empty_generic_for_multiple_variables("for key, value in foo do end") => GenericForStatement::new(
2055 vec![
2056 create_identifier("key", 4, 0).into(),
2057 create_identifier("value", 9, 1).into(),
2058 ],
2059 vec![
2060 create_identifier("foo", 18, 1).into(),
2061 ],
2062 default_block(),
2063 ).with_tokens(GenericForTokens {
2064 r#for: spaced_token(0, 3),
2065 r#in: spaced_token(15, 17),
2066 r#do: spaced_token(22, 24),
2067 end: token_at_first_line(25, 28),
2068 identifier_commas: vec![spaced_token(7, 8)],
2069 value_commas: Vec::new(),
2070 }),
2071 empty_generic_for_multiple_values("for key in next , t do end") => GenericForStatement::new(
2072 vec![create_identifier("key", 4, 1).into()],
2073 vec![
2074 create_identifier("next", 11, 1).into(),
2075 create_identifier("t", 18, 1).into(),
2076 ],
2077 default_block(),
2078 ).with_tokens(GenericForTokens {
2079 r#for: spaced_token(0, 3),
2080 r#in: spaced_token(8, 10),
2081 r#do: spaced_token(20, 22),
2082 end: token_at_first_line(23, 26),
2083 identifier_commas: Vec::new(),
2084 value_commas: vec![
2085 token_at_first_line(16, 17).with_trailing_trivia(TriviaKind::Whitespace.at(17, 18, 1)),
2086 ],
2087 }),
2088 empty_if_statement("if true then end") => IfStatement::create(
2089 create_true(3, 1),
2090 default_block()
2091 ).with_tokens(IfStatementTokens {
2092 r#if: token_at_first_line(0, 2).with_trailing_trivia(TriviaKind::Whitespace.at(2, 3, 1)),
2093 then: token_at_first_line(8, 12).with_trailing_trivia(TriviaKind::Whitespace.at(12, 13, 1)),
2094 end: token_at_first_line(13, 16),
2095 r#else: None,
2096 }),
2097 empty_if_statement_with_empty_else("if true then else end") => IfStatement::create(
2098 create_true(3, 1),
2099 default_block()
2100 )
2101 .with_else_block(default_block())
2102 .with_tokens(IfStatementTokens {
2103 r#if: spaced_token(0, 2),
2104 then: spaced_token(8, 12),
2105 end: token_at_first_line(18, 21),
2106 r#else: Some(spaced_token(13, 17)),
2107 }),
2108 empty_if_statement_with_empty_elseif("if true then elseif true then end")
2109 => IfStatement::create(create_true(3, 1), default_block())
2110 .with_branch(
2111 IfBranch::new(create_true(20, 1), default_block())
2112 .with_tokens(IfBranchTokens {
2113 elseif: spaced_token(13, 19),
2114 then: spaced_token(25, 29),
2115 })
2116 )
2117 .with_tokens(IfStatementTokens {
2118 r#if: spaced_token(0, 2),
2119 then: spaced_token(8, 12),
2120 end: token_at_first_line(30, 33),
2121 r#else: None,
2122 }),
2123 local_assignment_with_no_values("local var ") => VariableAssignment::from_variable(
2124 create_identifier("var", 6, 1),
2125 ).with_tokens(VariableAssignmentTokens {
2126 keyword: spaced_token(0, 5),
2127 equal: None,
2128 variable_commas: Vec::new(),
2129 value_commas: Vec::new(),
2130 }),
2131 local_assignment_typed_with_no_values("local var : string") => VariableAssignment::from_variable(
2132 create_identifier("var", 6, 1)
2133 .with_type(TypeName::new(create_identifier("string", 12, 0)))
2134 .with_colon_token(spaced_token(10, 11)),
2135 ).with_tokens(VariableAssignmentTokens {
2136 keyword: spaced_token(0, 5),
2137 equal: None,
2138 variable_commas: Vec::new(),
2139 value_commas: Vec::new(),
2140 }),
2141 const_assignment_with_one_value("const var = true") => VariableAssignment::from_variable(
2142 create_identifier("var", 6, 1),
2143 )
2144 .with_assignment_kind(AssignmentKind::Const)
2145 .with_value(create_true(12, 0))
2146 .with_tokens(VariableAssignmentTokens {
2147 keyword: spaced_token(0, 5),
2148 equal: Some(spaced_token(10, 11)),
2149 variable_commas: Vec::new(),
2150 value_commas: Vec::new()
2151 }),
2152 local_assignment_intersection_typed_with_no_values("local var : &string") => VariableAssignment::from_variable(
2153 create_identifier("var", 6, 1)
2154 .with_type(
2155 IntersectionType::from(vec![
2156 TypeName::new(create_identifier("string", 13, 0)).into(),
2157 ])
2158 .with_tokens(IntersectionTypeTokens {
2159 leading_token: Some(token_at_first_line(12, 13)),
2160 separators: Vec::new(),
2161 })
2162 )
2163 .with_colon_token(spaced_token(10, 11)),
2164 ).with_tokens(VariableAssignmentTokens {
2165 keyword: spaced_token(0, 5),
2166 equal: None,
2167 variable_commas: Vec::new(),
2168 value_commas: Vec::new(),
2169 }),
2170 multiple_local_assignment_with_no_values("local foo, bar") => VariableAssignment::from_variable(
2171 create_identifier("foo", 6, 0)
2172 )
2173 .with_variable(create_identifier("bar", 11, 0))
2174 .with_tokens(VariableAssignmentTokens {
2175 keyword: spaced_token(0, 5),
2176 equal: None,
2177 variable_commas: vec![spaced_token(9, 10)],
2178 value_commas: Vec::new(),
2179 }),
2180 multiple_local_assignment_typed_with_no_values("local foo: T, bar: U") => VariableAssignment::from_variable(
2181 create_identifier("foo", 6, 0)
2182 .with_type(TypeName::new(create_identifier("T", 11, 0)))
2183 .with_colon_token(spaced_token(9, 10))
2184 )
2185 .with_variable(
2186 create_identifier("bar", 14, 0)
2187 .with_type(TypeName::new(create_identifier("U", 19, 0)))
2188 .with_colon_token(spaced_token(17, 18))
2189 )
2190 .with_tokens(VariableAssignmentTokens {
2191 keyword: spaced_token(0, 5),
2192 equal: None,
2193 variable_commas: vec![spaced_token(12, 13)],
2194 value_commas: Vec::new(),
2195 }),
2196 multiple_local_assignment_with_two_values("local foo, bar = true, true")
2197 => VariableAssignment::from_variable(
2198 create_identifier("foo", 6, 0)
2199 )
2200 .with_variable(create_identifier("bar", 11, 1))
2201 .with_value(create_true(17, 0))
2202 .with_value(create_true(23, 0))
2203 .with_tokens(VariableAssignmentTokens {
2204 keyword: spaced_token(0, 5),
2205 equal: Some(spaced_token(15, 16)),
2206 variable_commas: vec![spaced_token(9, 10)],
2207 value_commas: vec![spaced_token(21, 22)],
2208 }),
2209 empty_numeric_for("for i = start,bound do end") => NumericForStatement::new(
2210 create_identifier("i", 4, 1),
2211 create_identifier("start", 8, 0),
2212 create_identifier("bound", 14, 1),
2213 None,
2214 default_block(),
2215 ).with_tokens(NumericForTokens {
2216 r#for: spaced_token(0, 3),
2217 equal: spaced_token(6, 7),
2218 r#do: spaced_token(20, 22),
2219 end: token_at_first_line(23, 26),
2220 end_comma: token_at_first_line(13, 14),
2221 step_comma: None,
2222 }),
2223 empty_numeric_for_with_typed_identifier("for i: number = start,bound do end") => NumericForStatement::new(
2224 create_identifier("i", 4, 0)
2225 .with_type(TypeName::new(create_identifier("number", 7, 1)))
2226 .with_colon_token(spaced_token(5, 6)),
2227 create_identifier("start", 16, 0),
2228 create_identifier("bound", 22, 1),
2229 None,
2230 default_block(),
2231 ).with_tokens(NumericForTokens {
2232 r#for: spaced_token(0, 3),
2233 equal: spaced_token(14, 15),
2234 r#do: spaced_token(28, 30),
2235 end: token_at_first_line(31, 34),
2236 end_comma: token_at_first_line(21, 22),
2237 step_comma: None,
2238 }),
2239 empty_numeric_for_with_step("for i = start , bound , step do end")
2240 => NumericForStatement::new(
2241 create_identifier("i", 4, 1),
2242 create_identifier("start", 8, 1),
2243 create_identifier("bound", 16, 1),
2244 Some(create_identifier("step", 24, 1).into()),
2245 default_block(),
2246 ).with_tokens(NumericForTokens {
2247 r#for: spaced_token(0, 3),
2248 equal: spaced_token(6, 7),
2249 r#do: spaced_token(29, 31),
2250 end: token_at_first_line(32, 35),
2251 end_comma: spaced_token(14, 15),
2252 step_comma: Some(spaced_token(22, 23)),
2253 }),
2254 empty_repeat("repeat until true") => RepeatStatement::new(
2255 default_block(),
2256 create_true(13, 0),
2257 ).with_tokens(RepeatTokens {
2258 repeat: spaced_token(0, 6),
2259 until: spaced_token(7, 12),
2260 }),
2261 empty_while("while true do end") => WhileStatement::new(
2262 default_block(),
2263 create_true(6, 1),
2264 ).with_tokens(WhileTokens {
2265 r#while: token_at_first_line(0, 5)
2266 .with_trailing_trivia(TriviaKind::Whitespace.at(5, 6, 1)),
2267 r#do: token_at_first_line(11, 13)
2268 .with_trailing_trivia(TriviaKind::Whitespace.at(13, 14, 1)),
2269 end: token_at_first_line(14, 17),
2270 }),
2271 compound_increment("var += amount") => CompoundAssignStatement::new(
2272 CompoundOperator::Plus,
2273 create_identifier("var", 0, 1),
2274 create_identifier("amount", 7, 0),
2275 ).with_tokens(CompoundAssignTokens { operator: spaced_token(4, 6) }),
2276 type_declaration_to_boolean("type NewType = boolean") => TypeDeclarationStatement::new(
2277 create_identifier("NewType", 5, 1),
2278 TypeName::new(create_identifier("boolean", 15, 0))
2279 ).with_tokens(TypeDeclarationTokens {
2280 r#type: spaced_token(0, 4),
2281 equal: spaced_token(13, 14),
2282 export: None,
2283 }),
2284 exported_type_declaration_to_boolean("export type NewType = boolean") => TypeDeclarationStatement::new(
2285 create_identifier("NewType", 12, 1),
2286 TypeName::new(create_identifier("boolean", 22, 0))
2287 )
2288 .export()
2289 .with_tokens(TypeDeclarationTokens {
2290 r#type: spaced_token(7, 11),
2291 equal: spaced_token(20, 21),
2292 export: Some(spaced_token(0, 6)),
2293 }),
2294 type_declaration_to_nil("type NewType = nil") => TypeDeclarationStatement::new(
2295 create_identifier("NewType", 5, 1),
2296 Type::Nil(Some(token_at_first_line(15, 18)))
2297 ).with_tokens(TypeDeclarationTokens {
2298 r#type: spaced_token(0, 4),
2299 equal: spaced_token(13, 14),
2300 export: None,
2301 }),
2302 type_declaration_to_single_quote_string_type("type Key = 'key'") => TypeDeclarationStatement::new(
2303 create_identifier("Key", 5, 1),
2304 StringType::new("'key'").unwrap().with_token(token_at_first_line(11, 16)),
2305 ).with_tokens(TypeDeclarationTokens {
2306 r#type: spaced_token(0, 4),
2307 equal: spaced_token(9, 10),
2308 export: None,
2309 }),
2310 type_declaration_to_double_quote_string_type("type Key = \"key\"") => TypeDeclarationStatement::new(
2311 create_identifier("Key", 5, 1),
2312 StringType::new("\"key\"").unwrap().with_token(token_at_first_line(11, 16)),
2313 ).with_tokens(TypeDeclarationTokens {
2314 r#type: spaced_token(0, 4),
2315 equal: spaced_token(9, 10),
2316 export: None,
2317 }),
2318 type_declaration_to_long_string_type("type Key = [[key]]") => TypeDeclarationStatement::new(
2319 create_identifier("Key", 5, 1),
2320 StringType::new("[[key]]").unwrap().with_token(token_at_first_line(11, 18)),
2321 ).with_tokens(TypeDeclarationTokens {
2322 r#type: spaced_token(0, 4),
2323 equal: spaced_token(9, 10),
2324 export: None,
2325 }),
2326 type_declaration_to_boolean_array("type Array = { boolean }") => TypeDeclarationStatement::new(
2327 create_identifier("Array", 5, 1),
2328 ArrayType::new(TypeName::new(create_identifier("boolean", 15, 1)))
2329 .with_tokens(ArrayTypeTokens {
2330 opening_brace: spaced_token(13, 14),
2331 closing_brace: token_at_first_line(23, 24),
2332 })
2333 ).with_tokens(TypeDeclarationTokens {
2334 r#type: spaced_token(0, 4),
2335 equal: spaced_token(11, 12),
2336 export: None,
2337 }),
2338 type_declaration_to_type_field_array("type Array = { Mod.Name }") => TypeDeclarationStatement::new(
2339 create_identifier("Array", 5, 1),
2340 ArrayType::new(
2341 TypeField::new(
2342 create_identifier("Mod", 15, 0),
2343 TypeName::new(create_identifier("Name", 19, 1))
2344 ).with_token(token_at_first_line(18, 19))
2345 )
2346 .with_tokens(ArrayTypeTokens {
2347 opening_brace: spaced_token(13, 14),
2348 closing_brace: token_at_first_line(24, 25),
2349 })
2350 ).with_tokens(TypeDeclarationTokens {
2351 r#type: spaced_token(0, 4),
2352 equal: spaced_token(11, 12),
2353 export: None,
2354 }),
2355 type_declaration_to_optional_boolean("type T = boolean?") => TypeDeclarationStatement::new(
2356 create_identifier("T", 5, 1),
2357 OptionalType::new(TypeName::new(create_identifier("boolean", 9, 0)))
2358 .with_token(token_at_first_line(16, 17))
2359 ).with_tokens(TypeDeclarationTokens {
2360 r#type: spaced_token(0, 4),
2361 equal: spaced_token(7, 8),
2362 export: None,
2363 }),
2364 type_declaration_to_union_boolean_nil("type T = boolean | nil") => TypeDeclarationStatement::new(
2365 create_identifier("T", 5, 1),
2366 UnionType::new(
2367 TypeName::new(create_identifier("boolean", 9, 1)),
2368 Type::Nil(Some(token_at_first_line(19, 22)))
2369 )
2370 .with_tokens(UnionTypeTokens {
2371 leading_token: None,
2372 separators: vec![spaced_token(17, 18)]
2373 })
2374 ).with_tokens(TypeDeclarationTokens {
2375 r#type: spaced_token(0, 4),
2376 equal: spaced_token(7, 8),
2377 export: None,
2378 }),
2379 type_declaration_to_intersection_of_type_names("type T = U & V") => TypeDeclarationStatement::new(
2380 create_identifier("T", 5, 1),
2381 IntersectionType::new(
2382 TypeName::new(create_identifier("U", 9, 1)),
2383 TypeName::new(create_identifier("V", 13, 0)),
2384 )
2385 .with_tokens(IntersectionTypeTokens {
2386 leading_token: None,
2387 separators: vec![spaced_token(11, 12)]
2388 })
2389 ).with_tokens(TypeDeclarationTokens {
2390 r#type: spaced_token(0, 4),
2391 equal: spaced_token(7, 8),
2392 export: None,
2393 }),
2394 type_declaration_to_intersections_of_type_names("type T = U & V & W") => TypeDeclarationStatement::new(
2395 create_identifier("T", 5, 1),
2396 IntersectionType::new(
2397 TypeName::new(create_identifier("U", 9, 1)),
2398 TypeName::new(create_identifier("V", 13, 1)),
2399 ).with_type(
2400 TypeName::new(create_identifier("W", 17, 0)),
2401 )
2402 .with_tokens(IntersectionTypeTokens {
2403 leading_token: None,
2404 separators: vec![
2405 spaced_token(11, 12),
2406 spaced_token(15, 16),
2407 ]
2408 })
2409 ).with_tokens(TypeDeclarationTokens {
2410 r#type: spaced_token(0, 4),
2411 equal: spaced_token(7, 8),
2412 export: None,
2413 }),
2414 type_declaration_to_table_with_one_prop("type T = { key: string }") => TypeDeclarationStatement::new(
2415 create_identifier("T", 5, 1),
2416 TableType::default()
2417 .with_property(
2418 TablePropertyType::new(
2419 create_identifier("key", 11, 0),
2420 TypeName::new(create_identifier("string", 16, 1)),
2421 )
2422 .with_tokens(TablePropertyTypeTokens {
2423 colon: spaced_token(14, 15),
2424 modifier: None,
2425 })
2426 )
2427 .with_tokens(TableTypeTokens {
2428 opening_brace: spaced_token(9, 10),
2429 closing_brace: token_at_first_line(23, 24),
2430 separators: Vec::new(),
2431 })
2432 ).with_tokens(TypeDeclarationTokens {
2433 r#type: spaced_token(0, 4),
2434 equal: spaced_token(7, 8),
2435 export: None,
2436 }),
2437 type_declaration_to_table_with_one_read_prop("type T = { read key: string }") => TypeDeclarationStatement::new(
2438 create_identifier("T", 5, 1),
2439 TableType::default()
2440 .with_property(
2441 TablePropertyType::new(
2442 create_identifier("key", 16, 0),
2443 TypeName::new(create_identifier("string", 21, 1)),
2444 )
2445 .with_modifier(TablePropertyModifier::Read)
2446 .with_tokens(TablePropertyTypeTokens {
2447 colon: spaced_token(19, 20),
2448 modifier: Some(spaced_token(11, 15)),
2449 })
2450 )
2451 .with_tokens(TableTypeTokens {
2452 opening_brace: spaced_token(9, 10),
2453 closing_brace: token_at_first_line(28, 29),
2454 separators: Vec::new(),
2455 })
2456 ).with_tokens(TypeDeclarationTokens {
2457 r#type: spaced_token(0, 4),
2458 equal: spaced_token(7, 8),
2459 export: None,
2460 }),
2461 type_declaration_to_table_with_one_write_prop("type T = { write key: string }") => TypeDeclarationStatement::new(
2462 create_identifier("T", 5, 1),
2463 TableType::default()
2464 .with_property(
2465 TablePropertyType::new(
2466 create_identifier("key", 17, 0),
2467 TypeName::new(create_identifier("string", 22, 1)),
2468 )
2469 .with_modifier(TablePropertyModifier::Write)
2470 .with_tokens(TablePropertyTypeTokens {
2471 colon: spaced_token(20, 21),
2472 modifier: Some(spaced_token(11, 16)),
2473 })
2474 )
2475 .with_tokens(TableTypeTokens {
2476 opening_brace: spaced_token(9, 10),
2477 closing_brace: token_at_first_line(29, 30),
2478 separators: Vec::new(),
2479 })
2480 ).with_tokens(TypeDeclarationTokens {
2481 r#type: spaced_token(0, 4),
2482 equal: spaced_token(7, 8),
2483 export: None,
2484 }),
2485 type_declaration_to_table_with_one_prop_and_separator("type T = { key: string, }") => TypeDeclarationStatement::new(
2486 create_identifier("T", 5, 1),
2487 TableType::default()
2488 .with_property(
2489 TablePropertyType::new(
2490 create_identifier("key", 11, 0),
2491 TypeName::new(create_identifier("string", 16, 0)),
2492 )
2493 .with_tokens(TablePropertyTypeTokens {
2494 colon: spaced_token(14, 15),
2495 modifier: None,
2496 })
2497 )
2498 .with_tokens(TableTypeTokens {
2499 opening_brace: spaced_token(9, 10),
2500 closing_brace: token_at_first_line(24, 25),
2501 separators: vec![spaced_token(22, 23)],
2502 })
2503 ).with_tokens(TypeDeclarationTokens {
2504 r#type: spaced_token(0, 4),
2505 equal: spaced_token(7, 8),
2506 export: None,
2507 }),
2508 type_declaration_to_table_with_two_props("type T = { key: string, key2 : nil }") => TypeDeclarationStatement::new(
2509 create_identifier("T", 5, 1),
2510 TableType::default()
2511 .with_property(
2512 TablePropertyType::new(
2513 create_identifier("key", 11, 0),
2514 TypeName::new(create_identifier("string", 16, 0)),
2515 )
2516 .with_tokens(TablePropertyTypeTokens {
2517 colon: spaced_token(14, 15),
2518 modifier: None,
2519 })
2520 )
2521 .with_property(
2522 TablePropertyType::new(
2523 create_identifier("key2", 24, 1),
2524 Type::Nil(Some(spaced_token(31, 34)))
2525 )
2526 .with_tokens(TablePropertyTypeTokens {
2527 colon: spaced_token(29, 30),
2528 modifier: None,
2529 })
2530 )
2531 .with_tokens(TableTypeTokens {
2532 opening_brace: spaced_token(9, 10),
2533 closing_brace: token_at_first_line(35, 36),
2534 separators: vec![spaced_token(22, 23)],
2535 })
2536 ).with_tokens(TypeDeclarationTokens {
2537 r#type: spaced_token(0, 4),
2538 equal: spaced_token(7, 8),
2539 export: None,
2540 }),
2541 type_declaration_to_table_with_two_props_and_modifiers("type T = { read key: string, write key2 : nil }") => TypeDeclarationStatement::new(
2542 create_identifier("T", 5, 1),
2543 TableType::default()
2544 .with_property(
2545 TablePropertyType::new(
2546 create_identifier("key", 16, 0),
2547 TypeName::new(create_identifier("string", 21, 0)),
2548 )
2549 .with_modifier(TablePropertyModifier::Read)
2550 .with_tokens(TablePropertyTypeTokens {
2551 colon: spaced_token(19, 20),
2552 modifier: Some(spaced_token(11, 15)),
2553 })
2554 )
2555 .with_property(
2556 TablePropertyType::new(
2557 create_identifier("key2", 35, 1),
2558 Type::Nil(Some(spaced_token(42, 45)))
2559 )
2560 .with_modifier(TablePropertyModifier::Write)
2561 .with_tokens(TablePropertyTypeTokens {
2562 colon: spaced_token(40, 41),
2563 modifier: Some(spaced_token(29, 34)),
2564 })
2565 )
2566 .with_tokens(TableTypeTokens {
2567 opening_brace: spaced_token(9, 10),
2568 closing_brace: token_at_first_line(46, 47),
2569 separators: vec![spaced_token(27, 28)],
2570 })
2571 ).with_tokens(TypeDeclarationTokens {
2572 r#type: spaced_token(0, 4),
2573 equal: spaced_token(7, 8),
2574 export: None,
2575 }),
2576 type_declaration_to_table_with_two_props_using_semicolon("type T = { key: string; key2 : nil }") => TypeDeclarationStatement::new(
2577 create_identifier("T", 5, 1),
2578 TableType::default()
2579 .with_property(
2580 TablePropertyType::new(
2581 create_identifier("key", 11, 0),
2582 TypeName::new(create_identifier("string", 16, 0)),
2583 )
2584 .with_tokens(TablePropertyTypeTokens {
2585 colon: spaced_token(14, 15),
2586 modifier: None,
2587 })
2588 )
2589 .with_property(
2590 TablePropertyType::new(
2591 create_identifier("key2", 24, 1),
2592 Type::Nil(Some(spaced_token(31, 34)))
2593 )
2594 .with_tokens(TablePropertyTypeTokens {
2595 colon: spaced_token(29, 30),
2596 modifier: None,
2597 })
2598 )
2599 .with_tokens(TableTypeTokens {
2600 opening_brace: spaced_token(9, 10),
2601 closing_brace: token_at_first_line(35, 36),
2602 separators: vec![spaced_token(22, 23)],
2603 })
2604 ).with_tokens(TypeDeclarationTokens {
2605 r#type: spaced_token(0, 4),
2606 equal: spaced_token(7, 8),
2607 export: None,
2608 }),
2609 type_declaration_to_table_with_indexer_type_and_property("type T = { [number]: string, n: number }") => TypeDeclarationStatement::new(
2610 create_identifier("T", 5, 1),
2611 TableType::default()
2612 .with_indexer_type(
2613 TableIndexerType::new(
2614 TypeName::new(create_identifier("number", 12, 0)),
2615 TypeName::new(create_identifier("string", 21, 0)),
2616 )
2617 .with_tokens(TableIndexTypeTokens {
2618 opening_bracket: token_at_first_line(11, 12),
2619 closing_bracket: token_at_first_line(18, 19),
2620 colon: spaced_token(19, 20),
2621 modifier: None,
2622 })
2623 )
2624 .with_property(
2625 TablePropertyType::new(
2626 create_identifier("n", 29, 0),
2627 TypeName::new(create_identifier("number", 32, 1))
2628 ).with_tokens(TablePropertyTypeTokens {
2629 colon: spaced_token(30, 31),
2630 modifier: None,
2631 })
2632 )
2633 .with_tokens(TableTypeTokens {
2634 opening_brace: spaced_token(9, 10),
2635 closing_brace: token_at_first_line(39, 40),
2636 separators: vec![spaced_token(27, 28)],
2637 })
2638 ).with_tokens(TypeDeclarationTokens {
2639 r#type: spaced_token(0, 4),
2640 equal: spaced_token(7, 8),
2641 export: None,
2642 }),
2643 type_declaration_to_table_with_property_and_indexer_type("type T = { n: number, [number]: string }") => TypeDeclarationStatement::new(
2644 create_identifier("T", 5, 1),
2645 TableType::default()
2646 .with_property(
2647 TablePropertyType::new(
2648 create_identifier("n", 11, 0),
2649 TypeName::new(create_identifier("number", 14, 0))
2650 ).with_tokens(TablePropertyTypeTokens {
2651 colon: spaced_token(12, 13),
2652 modifier: None,
2653 })
2654 )
2655 .with_indexer_type(
2656 TableIndexerType::new(
2657 TypeName::new(create_identifier("number", 23, 0)),
2658 TypeName::new(create_identifier("string", 32, 1)),
2659 )
2660 .with_tokens(TableIndexTypeTokens {
2661 opening_bracket: token_at_first_line(22, 23),
2662 closing_bracket: token_at_first_line(29, 30),
2663 colon: spaced_token(30, 31),
2664 modifier: None,
2665 })
2666 )
2667 .with_tokens(TableTypeTokens {
2668 opening_brace: spaced_token(9, 10),
2669 closing_brace: token_at_first_line(39, 40),
2670 separators: vec![spaced_token(20, 21)],
2671 })
2672 ).with_tokens(TypeDeclarationTokens {
2673 r#type: spaced_token(0, 4),
2674 equal: spaced_token(7, 8),
2675 export: None,
2676 }),
2677 type_declaration_to_table_with_literal_property("type T = { ['end']: boolean }") => TypeDeclarationStatement::new(
2678 create_identifier("T", 5, 1),
2679 TableType::default()
2680 .with_property(
2681 TableLiteralPropertyType::new(
2682 StringType::from_value("end")
2683 .with_token(token_at_first_line(12, 17)),
2684 TypeName::new(create_identifier("boolean", 20, 1)),
2685 )
2686 .with_tokens(TableIndexTypeTokens {
2687 opening_bracket: token_at_first_line(11, 12),
2688 closing_bracket: token_at_first_line(17, 18),
2689 colon: spaced_token(18, 19),
2690 modifier: None,
2691 })
2692 )
2693 .with_tokens(TableTypeTokens {
2694 opening_brace: spaced_token(9, 10),
2695 closing_brace: token_at_first_line(28, 29),
2696 separators: Vec::new(),
2697 })
2698 ).with_tokens(TypeDeclarationTokens {
2699 r#type: spaced_token(0, 4),
2700 equal: spaced_token(7, 8),
2701 export: None,
2702 }),
2703 type_declaration_to_table_with_indexer_type("type T = { [string]: boolean }") => TypeDeclarationStatement::new(
2704 create_identifier("T", 5, 1),
2705 TableType::default()
2706 .with_indexer_type(
2707 TableIndexerType::new(
2708 TypeName::new(create_identifier("string", 12, 0)),
2709 TypeName::new(create_identifier("boolean", 21, 1)),
2710 )
2711 .with_tokens(TableIndexTypeTokens {
2712 opening_bracket: token_at_first_line(11, 12),
2713 closing_bracket: token_at_first_line(18, 19),
2714 colon: spaced_token(19, 20),
2715 modifier: None,
2716 })
2717 )
2718 .with_tokens(TableTypeTokens {
2719 opening_brace: spaced_token(9, 10),
2720 closing_brace: token_at_first_line(29, 30),
2721 separators: Vec::new(),
2722 })
2723 ).with_tokens(TypeDeclarationTokens {
2724 r#type: spaced_token(0, 4),
2725 equal: spaced_token(7, 8),
2726 export: None,
2727 }),
2728 type_declaration_to_table_with_read_indexer_type("type T = { read [string]: boolean }") => TypeDeclarationStatement::new(
2729 create_identifier("T", 5, 1),
2730 TableType::default()
2731 .with_indexer_type(
2732 TableIndexerType::new(
2733 TypeName::new(create_identifier("string", 17, 0)),
2734 TypeName::new(create_identifier("boolean", 26, 1)),
2735 )
2736 .with_modifier(TablePropertyModifier::Read)
2737 .with_tokens(TableIndexTypeTokens {
2738 opening_bracket: token_at_first_line(16, 17),
2739 closing_bracket: token_at_first_line(23, 24),
2740 colon: spaced_token(24, 25),
2741 modifier: Some(spaced_token(11, 15)),
2742 })
2743 )
2744 .with_tokens(TableTypeTokens {
2745 opening_brace: spaced_token(9, 10),
2746 closing_brace: token_at_first_line(34, 35),
2747 separators: Vec::new(),
2748 })
2749 ).with_tokens(TypeDeclarationTokens {
2750 r#type: spaced_token(0, 4),
2751 equal: spaced_token(7, 8),
2752 export: None,
2753 }),
2754 type_declaration_to_table_with_write_indexer_type("type T = { write [string]: boolean }") => TypeDeclarationStatement::new(
2755 create_identifier("T", 5, 1),
2756 TableType::default()
2757 .with_indexer_type(
2758 TableIndexerType::new(
2759 TypeName::new(create_identifier("string", 18, 0)),
2760 TypeName::new(create_identifier("boolean", 27, 1)),
2761 )
2762 .with_modifier(TablePropertyModifier::Write)
2763 .with_tokens(TableIndexTypeTokens {
2764 opening_bracket: token_at_first_line(17, 18),
2765 closing_bracket: token_at_first_line(24, 25),
2766 colon: spaced_token(25, 26),
2767 modifier: Some(spaced_token(11, 16)),
2768 })
2769 )
2770 .with_tokens(TableTypeTokens {
2771 opening_brace: spaced_token(9, 10),
2772 closing_brace: token_at_first_line(35, 36),
2773 separators: Vec::new(),
2774 })
2775 ).with_tokens(TypeDeclarationTokens {
2776 r#type: spaced_token(0, 4),
2777 equal: spaced_token(7, 8),
2778 export: None,
2779 }),
2780 type_declaration_to_type_of_expression("type T = typeof( nil )") => TypeDeclarationStatement::new(
2781 create_identifier("T", 5, 1),
2782 ExpressionType::new(Expression::Nil(Some(spaced_token(17, 20))))
2783 .with_tokens(ExpressionTypeTokens {
2784 r#typeof: token_at_first_line(9, 15),
2785 opening_parenthese: spaced_token(15, 16),
2786 closing_parenthese: token_at_first_line(21, 22),
2787 })
2788 ).with_tokens(TypeDeclarationTokens {
2789 r#type: spaced_token(0, 4),
2790 equal: spaced_token(7, 8),
2791 export: None,
2792 }),
2793 type_declaration_to_void_callback("type T = () -> ()") => TypeDeclarationStatement::new(
2794 create_identifier("T", 5, 1),
2795 FunctionType::new(
2796 TypePack::default()
2797 .with_tokens(TypePackTokens {
2798 left_parenthese: token_at_first_line(15, 16),
2799 right_parenthese: token_at_first_line(16, 17),
2800 commas: Vec::new(),
2801 })
2802 )
2803 .with_tokens(FunctionTypeTokens {
2804 opening_parenthese: token_at_first_line(9, 10),
2805 closing_parenthese: spaced_token(10, 11),
2806 arrow: spaced_token(12, 14),
2807 commas: Vec::new(),
2808 })
2809 ).with_tokens(TypeDeclarationTokens {
2810 r#type: spaced_token(0, 4),
2811 equal: spaced_token(7, 8),
2812 export: None,
2813 }),
2814 type_declaration_to_optional_void_callback("type T = () -> ()?") => TypeDeclarationStatement::new(
2815 create_identifier("T", 5, 1),
2816 OptionalType::new(
2817 FunctionType::new(
2818 TypePack::default().with_tokens(TypePackTokens {
2819 left_parenthese: token_at_first_line(15, 16),
2820 right_parenthese: token_at_first_line(16, 17),
2821 commas: Vec::new(),
2822 })
2823 )
2824 .with_tokens(FunctionTypeTokens {
2825 opening_parenthese: token_at_first_line(9, 10),
2826 closing_parenthese: spaced_token(10, 11),
2827 arrow: spaced_token(12, 14),
2828 commas: Vec::new(),
2829 })
2830 ).with_token(token_at_first_line(17, 18))
2831 ).with_tokens(TypeDeclarationTokens {
2832 r#type: spaced_token(0, 4),
2833 equal: spaced_token(7, 8),
2834 export: None,
2835 }),
2836 type_declaration_to_intersection_of_void_callback_and_string("type T = () -> () & string") => TypeDeclarationStatement::new(
2837 create_identifier("T", 5, 1),
2838 IntersectionType::new(
2839 FunctionType::new(
2840 TypePack::default().with_tokens(TypePackTokens {
2841 left_parenthese: token_at_first_line(15, 16),
2842 right_parenthese: spaced_token(16, 17),
2843 commas: Vec::new(),
2844 })
2845 )
2846 .with_tokens(FunctionTypeTokens {
2847 opening_parenthese: token_at_first_line(9, 10),
2848 closing_parenthese: spaced_token(10, 11),
2849 arrow: spaced_token(12, 14),
2850 commas: Vec::new(),
2851 }),
2852 TypeName::new(create_identifier("string", 20, 0))
2853 ).with_tokens(IntersectionTypeTokens {
2854 leading_token: None,
2855 separators: vec![spaced_token(18, 19)]
2856 })
2857 ).with_tokens(TypeDeclarationTokens {
2858 r#type: spaced_token(0, 4),
2859 equal: spaced_token(7, 8),
2860 export: None,
2861 }),
2862 type_declaration_to_union_of_void_callback_and_string("type T = () -> () | string") => TypeDeclarationStatement::new(
2863 create_identifier("T", 5, 1),
2864 UnionType::new(
2865 FunctionType::new(
2866 TypePack::default().with_tokens(TypePackTokens {
2867 left_parenthese: token_at_first_line(15, 16),
2868 right_parenthese: spaced_token(16, 17),
2869 commas: Vec::new(),
2870 })
2871 )
2872 .with_tokens(FunctionTypeTokens {
2873 opening_parenthese: token_at_first_line(9, 10),
2874 closing_parenthese: spaced_token(10, 11),
2875 arrow: spaced_token(12, 14),
2876 commas: Vec::new(),
2877 }),
2878 TypeName::new(create_identifier("string", 20, 0))
2879 ).with_tokens(UnionTypeTokens {
2880 leading_token: None,
2881 separators: vec![spaced_token(18, 19)]
2882 })
2883 ).with_tokens(TypeDeclarationTokens {
2884 r#type: spaced_token(0, 4),
2885 equal: spaced_token(7, 8),
2886 export: None,
2887 }),
2888 type_declaration_to_callback_returning_type("type T = () -> boolean") => TypeDeclarationStatement::new(
2889 create_identifier("T", 5, 1),
2890 FunctionType::new(TypeName::new(create_identifier("boolean", 15, 0)))
2891 .with_tokens(FunctionTypeTokens {
2892 opening_parenthese: token_at_first_line(9, 10),
2893 closing_parenthese: spaced_token(10, 11),
2894 arrow: spaced_token(12, 14),
2895 commas: Vec::new(),
2896 })
2897 ).with_tokens(TypeDeclarationTokens {
2898 r#type: spaced_token(0, 4),
2899 equal: spaced_token(7, 8),
2900 export: None,
2901 }),
2902 type_declaration_to_callback_returning_multiple_intersected_types("type T = () -> A & B & C") => TypeDeclarationStatement::new(
2903 create_identifier("T", 5, 1),
2904 FunctionType::new(
2905 IntersectionType::new(
2906 TypeName::new(create_identifier("A", 15, 1)),
2907 TypeName::new(create_identifier("B", 19, 1)),
2908 )
2909 .with_type(TypeName::new(create_identifier("C", 23, 0)))
2910 .with_tokens(IntersectionTypeTokens {
2911 leading_token: None,
2912 separators: vec![
2913 spaced_token(17, 18),
2914 spaced_token(21, 22),
2915 ]
2916 }),
2917 )
2918 .with_tokens(FunctionTypeTokens {
2919 opening_parenthese: token_at_first_line(9, 10),
2920 closing_parenthese: spaced_token(10, 11),
2921 arrow: spaced_token(12, 14),
2922 commas: Vec::new(),
2923 })
2924 ).with_tokens(TypeDeclarationTokens {
2925 r#type: spaced_token(0, 4),
2926 equal: spaced_token(7, 8),
2927 export: None,
2928 }),
2929 type_declaration_to_callback_returning_optional_type("type T = () -> boolean?") => TypeDeclarationStatement::new(
2930 create_identifier("T", 5, 1),
2931 FunctionType::new(
2932 OptionalType::new(TypeName::new(create_identifier("boolean", 15, 0)))
2933 .with_token(token_at_first_line(22, 23))
2934 )
2935 .with_tokens(FunctionTypeTokens {
2936 opening_parenthese: token_at_first_line(9, 10),
2937 closing_parenthese: spaced_token(10, 11),
2938 arrow: spaced_token(12, 14),
2939 commas: Vec::new(),
2940 })
2941 ).with_tokens(TypeDeclarationTokens {
2942 r#type: spaced_token(0, 4),
2943 equal: spaced_token(7, 8),
2944 export: None,
2945 }),
2946 type_declaration_to_callback_returning_variadic_type_name("type T = () -> ...string") => TypeDeclarationStatement::new(
2947 create_identifier("T", 5, 1),
2948 FunctionType::new(
2949 VariadicTypePack::new(TypeName::new(create_identifier("string", 18, 0)))
2950 .with_token(token_at_first_line(15, 18))
2951 )
2952 .with_tokens(FunctionTypeTokens {
2953 opening_parenthese: token_at_first_line(9, 10),
2954 closing_parenthese: spaced_token(10, 11),
2955 arrow: spaced_token(12, 14),
2956 commas: Vec::new(),
2957 })
2958 ).with_tokens(TypeDeclarationTokens {
2959 r#type: spaced_token(0, 4),
2960 equal: spaced_token(7, 8),
2961 export: None,
2962 }),
2963 type_declaration_to_callback_returning_variadic_optional("type T = () -> ...string?") => TypeDeclarationStatement::new(
2964 create_identifier("T", 5, 1),
2965 FunctionType::new(
2966 VariadicTypePack::new(
2967 OptionalType::new(
2968 TypeName::new(create_identifier("string", 18, 0))
2969 ).with_token(token_at_first_line(24, 25))
2970 ).with_token(token_at_first_line(15, 18))
2971 )
2972 .with_tokens(FunctionTypeTokens {
2973 opening_parenthese: token_at_first_line(9, 10),
2974 closing_parenthese: spaced_token(10, 11),
2975 arrow: spaced_token(12, 14),
2976 commas: Vec::new(),
2977 })
2978 ).with_tokens(TypeDeclarationTokens {
2979 r#type: spaced_token(0, 4),
2980 equal: spaced_token(7, 8),
2981 export: None,
2982 }),
2983 type_declaration_to_callback_returning_variadic_string_literal("type T = () -> ...'ok'") => TypeDeclarationStatement::new(
2984 create_identifier("T", 5, 1),
2985 FunctionType::new(
2986 VariadicTypePack::new(
2987 StringType::from_value("ok").with_token(token_at_first_line(18, 22))
2988 ).with_token(token_at_first_line(15, 18))
2989 )
2990 .with_tokens(FunctionTypeTokens {
2991 opening_parenthese: token_at_first_line(9, 10),
2992 closing_parenthese: spaced_token(10, 11),
2993 arrow: spaced_token(12, 14),
2994 commas: Vec::new(),
2995 })
2996 ).with_tokens(TypeDeclarationTokens {
2997 r#type: spaced_token(0, 4),
2998 equal: spaced_token(7, 8),
2999 export: None,
3000 }),
3001 type_declaration_to_callback_returning_variadic_false_type("type T = () -> ...false") => TypeDeclarationStatement::new(
3002 create_identifier("T", 5, 1),
3003 FunctionType::new(
3004 VariadicTypePack::new(Type::False(Some(token_at_first_line(18, 23))))
3005 .with_token(token_at_first_line(15, 18))
3006 )
3007 .with_tokens(FunctionTypeTokens {
3008 opening_parenthese: token_at_first_line(9, 10),
3009 closing_parenthese: spaced_token(10, 11),
3010 arrow: spaced_token(12, 14),
3011 commas: Vec::new(),
3012 })
3013 ).with_tokens(TypeDeclarationTokens {
3014 r#type: spaced_token(0, 4),
3015 equal: spaced_token(7, 8),
3016 export: None,
3017 }),
3018 type_declaration_to_callback_returning_intersection_type("type T = () -> string & T") => TypeDeclarationStatement::new(
3019 create_identifier("T", 5, 1),
3020 FunctionType::new(
3021 IntersectionType::new(
3022 TypeName::new(create_identifier("string", 15, 1)),
3023 TypeName::new(create_identifier("T", 24, 0)),
3024 ).with_tokens(IntersectionTypeTokens {
3025 leading_token: None,
3026 separators: vec![spaced_token(22, 23)]
3027 })
3028 )
3029 .with_tokens(FunctionTypeTokens {
3030 opening_parenthese: token_at_first_line(9, 10),
3031 closing_parenthese: spaced_token(10, 11),
3032 arrow: spaced_token(12, 14),
3033 commas: Vec::new(),
3034 })
3035 ).with_tokens(TypeDeclarationTokens {
3036 r#type: spaced_token(0, 4),
3037 equal: spaced_token(7, 8),
3038 export: None,
3039 }),
3040 type_declaration_to_callback_returning_union_type("type T = () -> string | T") => TypeDeclarationStatement::new(
3041 create_identifier("T", 5, 1),
3042 FunctionType::new(
3043 UnionType::new(
3044 TypeName::new(create_identifier("string", 15, 1)),
3045 TypeName::new(create_identifier("T", 24, 0)),
3046 ).with_tokens(UnionTypeTokens {
3047 leading_token: None,
3048 separators: vec![spaced_token(22, 23)]
3049 })
3050 )
3051 .with_tokens(FunctionTypeTokens {
3052 opening_parenthese: token_at_first_line(9, 10),
3053 closing_parenthese: spaced_token(10, 11),
3054 arrow: spaced_token(12, 14),
3055 commas: Vec::new(),
3056 })
3057 ).with_tokens(TypeDeclarationTokens {
3058 r#type: spaced_token(0, 4),
3059 equal: spaced_token(7, 8),
3060 export: None,
3061 }),
3062 type_declaration_to_callback_returning_variadic_intersection_type("type T = () -> ...string & T") => TypeDeclarationStatement::new(
3063 create_identifier("T", 5, 1),
3064 FunctionType::new(
3065 VariadicTypePack::new(
3066 IntersectionType::new(
3067 TypeName::new(create_identifier("string", 18, 1)),
3068 TypeName::new(create_identifier("T", 27, 0)),
3069 ).with_tokens(IntersectionTypeTokens {
3070 leading_token: None,
3071 separators: vec![spaced_token(25, 26)]
3072 })
3073 )
3074 .with_token(token_at_first_line(15, 18))
3075 )
3076 .with_tokens(FunctionTypeTokens {
3077 opening_parenthese: token_at_first_line(9, 10),
3078 closing_parenthese: spaced_token(10, 11),
3079 arrow: spaced_token(12, 14),
3080 commas: Vec::new(),
3081 })
3082 ).with_tokens(TypeDeclarationTokens {
3083 r#type: spaced_token(0, 4),
3084 equal: spaced_token(7, 8),
3085 export: None,
3086 }),
3087 type_declaration_to_callback_returning_variadic_union_type("type T = () -> ...string | boolean") => TypeDeclarationStatement::new(
3088 create_identifier("T", 5, 1),
3089 FunctionType::new(
3090 VariadicTypePack::new(
3091 UnionType::new(
3092 TypeName::new(create_identifier("string", 18, 1)),
3093 TypeName::new(create_identifier("boolean", 27, 0)),
3094 ).with_tokens(UnionTypeTokens {
3095 leading_token: None,
3096 separators: vec![spaced_token(25, 26)]
3097 })
3098 )
3099 .with_token(token_at_first_line(15, 18))
3100 )
3101 .with_tokens(FunctionTypeTokens {
3102 opening_parenthese: token_at_first_line(9, 10),
3103 closing_parenthese: spaced_token(10, 11),
3104 arrow: spaced_token(12, 14),
3105 commas: Vec::new(),
3106 })
3107 ).with_tokens(TypeDeclarationTokens {
3108 r#type: spaced_token(0, 4),
3109 equal: spaced_token(7, 8),
3110 export: None,
3111 }),
3112 type_declaration_to_callback_returning_generic_type_pack("type T = () -> U...") => TypeDeclarationStatement::new(
3113 create_identifier("T", 5, 1),
3114 FunctionType::new(
3115 GenericTypePack::new(create_identifier("U", 15, 0))
3116 .with_token(token_at_first_line(16, 19))
3117 )
3118 .with_tokens(FunctionTypeTokens {
3119 opening_parenthese: token_at_first_line(9, 10),
3120 closing_parenthese: spaced_token(10, 11),
3121 arrow: spaced_token(12, 14),
3122 commas: Vec::new(),
3123 })
3124 ).with_tokens(TypeDeclarationTokens {
3125 r#type: spaced_token(0, 4),
3126 equal: spaced_token(7, 8),
3127 export: None,
3128 }),
3129 type_declaration_to_callback_with_one_argument_returning_type("type T = (string) -> boolean") => TypeDeclarationStatement::new(
3130 create_identifier("T", 5, 1),
3131 FunctionType::new(TypeName::new(create_identifier("boolean", 21, 0)))
3132 .with_argument(TypeName::new(create_identifier("string", 10, 0)))
3133 .with_tokens(FunctionTypeTokens {
3134 opening_parenthese: token_at_first_line(9, 10),
3135 closing_parenthese: spaced_token(16, 17),
3136 arrow: spaced_token(18, 20),
3137 commas: Vec::new(),
3138 })
3139 ).with_tokens(TypeDeclarationTokens {
3140 r#type: spaced_token(0, 4),
3141 equal: spaced_token(7, 8),
3142 export: None,
3143 }),
3144 type_declaration_to_callback_with_variadic_type_returning_type("type T = (...string) -> boolean") => TypeDeclarationStatement::new(
3145 create_identifier("T", 5, 1),
3146 FunctionType::new(TypeName::new(create_identifier("boolean", 24, 0)))
3147 .with_variadic_type(
3148 VariadicTypePack::new(TypeName::new(create_identifier("string", 13, 0)))
3149 .with_token(token_at_first_line(10, 13))
3150 )
3151 .with_tokens(FunctionTypeTokens {
3152 opening_parenthese: token_at_first_line(9, 10),
3153 closing_parenthese: spaced_token(19, 20),
3154 arrow: spaced_token(21, 23),
3155 commas: Vec::new(),
3156 })
3157 ).with_tokens(TypeDeclarationTokens {
3158 r#type: spaced_token(0, 4),
3159 equal: spaced_token(7, 8),
3160 export: None,
3161 }),
3162 type_declaration_to_callback_with_variadic_optional_type_returning_type("type T = (...string?) -> boolean") => TypeDeclarationStatement::new(
3163 create_identifier("T", 5, 1),
3164 FunctionType::new(TypeName::new(create_identifier("boolean", 25, 0)))
3165 .with_variadic_type(
3166 VariadicTypePack::new(
3167 OptionalType::new(TypeName::new(create_identifier("string", 13, 0)))
3168 .with_token(token_at_first_line(19, 20))
3169 )
3170 .with_token(token_at_first_line(10, 13))
3171 )
3172 .with_tokens(FunctionTypeTokens {
3173 opening_parenthese: token_at_first_line(9, 10),
3174 closing_parenthese: spaced_token(20, 21),
3175 arrow: spaced_token(22, 24),
3176 commas: Vec::new(),
3177 })
3178 ).with_tokens(TypeDeclarationTokens {
3179 r#type: spaced_token(0, 4),
3180 equal: spaced_token(7, 8),
3181 export: None,
3182 }),
3183 type_declaration_to_generic_callback("type T = <R>() -> R") => TypeDeclarationStatement::new(
3184 create_identifier("T", 5, 1),
3185 FunctionType::new(
3186 TypeName::new(create_identifier("R", 18, 0))
3187 )
3188 .with_generic_parameters(
3189 GenericParameters::from_type_variable(create_identifier("R", 10, 0))
3190 .with_tokens(GenericParametersTokens {
3191 opening_list: token_at_first_line(9, 10),
3192 closing_list: token_at_first_line(11, 12),
3193 commas: Vec::new(),
3194 })
3195 )
3196 .with_tokens(FunctionTypeTokens {
3197 opening_parenthese: token_at_first_line(12, 13),
3198 closing_parenthese: spaced_token(13, 14),
3199 arrow: spaced_token(15, 17),
3200 commas: Vec::new(),
3201 })
3202 ).with_tokens(TypeDeclarationTokens {
3203 r#type: spaced_token(0, 4),
3204 equal: spaced_token(7, 8),
3205 export: None,
3206 }),
3207 type_declaration_to_generic_callback_with_two_types("type T = <R, R2>() -> R") => TypeDeclarationStatement::new(
3208 create_identifier("T", 5, 1),
3209 FunctionType::new(
3210 TypeName::new(create_identifier("R", 22, 0))
3211 )
3212 .with_generic_parameters(
3213 GenericParameters::from_type_variable(create_identifier("R", 10, 0))
3214 .with_type_variable(create_identifier("R2", 13, 0))
3215 .with_tokens(GenericParametersTokens {
3216 opening_list: token_at_first_line(9, 10),
3217 closing_list: token_at_first_line(15, 16),
3218 commas: vec![spaced_token(11, 12)],
3219 })
3220 )
3221 .with_tokens(FunctionTypeTokens {
3222 opening_parenthese: token_at_first_line(16, 17),
3223 closing_parenthese: spaced_token(17, 18),
3224 arrow: spaced_token(19, 21),
3225 commas: Vec::new(),
3226 })
3227 ).with_tokens(TypeDeclarationTokens {
3228 r#type: spaced_token(0, 4),
3229 equal: spaced_token(7, 8),
3230 export: None,
3231 }),
3232 type_declaration_to_generic_callback_with_generic_type_pack("type T = <R...>() -> R...") => TypeDeclarationStatement::new(
3233 create_identifier("T", 5, 1),
3234 FunctionType::new(
3235 GenericTypePack::new(create_identifier("R", 21, 0))
3236 .with_token(token_at_first_line(22, 25))
3237 )
3238 .with_generic_parameters(
3239 GenericParameters::from_generic_type_pack(
3240 GenericTypePack::new(create_identifier("R", 10, 0))
3241 .with_token(token_at_first_line(11, 14))
3242 )
3243 .with_tokens(GenericParametersTokens {
3244 opening_list: token_at_first_line(9, 10),
3245 closing_list: token_at_first_line(14, 15),
3246 commas: Vec::new(),
3247 })
3248 )
3249 .with_tokens(FunctionTypeTokens {
3250 opening_parenthese: token_at_first_line(15, 16),
3251 closing_parenthese: spaced_token(16, 17),
3252 arrow: spaced_token(18, 20),
3253 commas: Vec::new(),
3254 })
3255 ).with_tokens(TypeDeclarationTokens {
3256 r#type: spaced_token(0, 4),
3257 equal: spaced_token(7, 8),
3258 export: None,
3259 }),
3260 type_declaration_to_generic_array("type Array<T> = { T }") => TypeDeclarationStatement::new(
3261 create_identifier("Array", 5, 0),
3262 ArrayType::new(TypeName::new(create_identifier("T", 18, 1)))
3263 .with_tokens(ArrayTypeTokens {
3264 opening_brace: spaced_token(16, 17),
3265 closing_brace: token_at_first_line(20, 21),
3266 })
3267 )
3268 .with_generic_parameters(
3269 GenericParametersWithDefaults::from_type_variable(
3270 create_identifier("T", 11, 0)
3271 )
3272 .with_tokens(GenericParametersTokens {
3273 opening_list: token_at_first_line(10, 11),
3274 closing_list: spaced_token(12, 13),
3275 commas: Vec::new(),
3276 })
3277 )
3278 .with_tokens(TypeDeclarationTokens {
3279 r#type: spaced_token(0, 4),
3280 equal: spaced_token(14, 15),
3281 export: None,
3282 }),
3283 type_declaration_to_generic_intersection("type T < U, V > = U & V") => TypeDeclarationStatement::new(
3284 create_identifier("T", 5, 1),
3285 IntersectionType::new(
3286 TypeName::new(create_identifier("U", 18, 1)),
3287 TypeName::new(create_identifier("V", 22, 0)),
3288 ).with_tokens(IntersectionTypeTokens {
3289 leading_token: None,
3290 separators: vec![spaced_token(20, 21)]
3291 })
3292 )
3293 .with_generic_parameters(
3294 GenericParametersWithDefaults::from_type_variable(
3295 create_identifier("U", 9, 0)
3296 )
3297 .with_type_variable(create_identifier("V", 12, 1))
3298 .with_tokens(GenericParametersTokens {
3299 opening_list: spaced_token(7, 8),
3300 closing_list: spaced_token(14, 15),
3301 commas: vec![spaced_token(10, 11)],
3302 })
3303 )
3304 .with_tokens(TypeDeclarationTokens {
3305 r#type: spaced_token(0, 4),
3306 equal: spaced_token(16, 17),
3307 export: None,
3308 }),
3309 type_declaration_with_generic_param_with_boolean_default("type T<A=boolean> = A?") => TypeDeclarationStatement::new(
3310 create_identifier("T", 5, 0),
3311 OptionalType::new(
3312 TypeName::new(create_identifier("A", 20, 0)),
3313 ).with_token(token_at_first_line(21, 22))
3314 )
3315 .with_generic_parameters(
3316 GenericParametersWithDefaults::from_type_variable_with_default(
3317 TypeVariableWithDefault::new(
3318 create_identifier("A", 7, 0),
3319 TypeName::new(create_identifier("boolean", 9, 0))
3320 ).with_token(token_at_first_line(8, 9))
3321 )
3322 .with_tokens(GenericParametersTokens {
3323 opening_list: token_at_first_line(6, 7),
3324 closing_list: spaced_token(16, 17),
3325 commas: Vec::new(),
3326 })
3327 )
3328 .with_tokens(TypeDeclarationTokens {
3329 r#type: spaced_token(0, 4),
3330 equal: spaced_token(18, 19),
3331 export: None,
3332 }),
3333 type_declaration_with_generic_param_with_parenthese_default("type T<A=(boolean)> = A?") => TypeDeclarationStatement::new(
3334 create_identifier("T", 5, 0),
3335 OptionalType::new(
3336 TypeName::new(create_identifier("A", 22, 0)),
3337 ).with_token(token_at_first_line(23, 24))
3338 )
3339 .with_generic_parameters(
3340 GenericParametersWithDefaults::from_type_variable_with_default(
3341 TypeVariableWithDefault::new(
3342 create_identifier("A", 7, 0),
3343 ParentheseType::new(TypeName::new(create_identifier("boolean", 10, 0)))
3344 .with_tokens(ParentheseTypeTokens {
3345 left_parenthese: token_at_first_line(9, 10),
3346 right_parenthese: token_at_first_line(17, 18),
3347 })
3348 ).with_token(token_at_first_line(8, 9))
3349 )
3350 .with_tokens(GenericParametersTokens {
3351 opening_list: token_at_first_line(6, 7),
3352 closing_list: spaced_token(18, 19),
3353 commas: Vec::new(),
3354 })
3355 )
3356 .with_tokens(TypeDeclarationTokens {
3357 r#type: spaced_token(0, 4),
3358 equal: spaced_token(20, 21),
3359 export: None,
3360 }),
3361 type_declaration_to_generic_union_with_default_type("type T<A, B=Error> = A | B") => TypeDeclarationStatement::new(
3362 create_identifier("T", 5, 0),
3363 UnionType::new(
3364 TypeName::new(create_identifier("A", 21, 1)),
3365 TypeName::new(create_identifier("B", 25, 0)),
3366 ).with_tokens(UnionTypeTokens {
3367 leading_token: None,
3368 separators: vec![spaced_token(23, 24)]
3369 })
3370 )
3371 .with_generic_parameters(
3372 GenericParametersWithDefaults::from_type_variable(
3373 create_identifier("A", 7, 0)
3374 )
3375 .with_type_variable_with_default(
3376 TypeVariableWithDefault::new(
3377 create_identifier("B", 10, 0),
3378 TypeName::new(create_identifier("Error", 12, 0))
3379 ).with_token(token_at_first_line(11, 12))
3380 ).unwrap()
3381 .with_tokens(GenericParametersTokens {
3382 opening_list: token_at_first_line(6, 7),
3383 closing_list: spaced_token(17, 18),
3384 commas: vec![spaced_token(8, 9)],
3385 })
3386 )
3387 .with_tokens(TypeDeclarationTokens {
3388 r#type: spaced_token(0, 4),
3389 equal: spaced_token(19, 20),
3390 export: None,
3391 }),
3392 type_declaration_with_generic_type_pack("type T<R...> = () -> R...") => TypeDeclarationStatement::new(
3393 create_identifier("T", 5, 0),
3394 FunctionType::new(
3395 GenericTypePack::new(create_identifier("R", 21, 0))
3396 .with_token(token_at_first_line(22, 25))
3397 )
3398 .with_tokens(FunctionTypeTokens {
3399 opening_parenthese: token_at_first_line(15, 16),
3400 closing_parenthese: spaced_token(16, 17),
3401 arrow: spaced_token(18, 20),
3402 commas: Vec::new(),
3403 })
3404 )
3405 .with_generic_parameters(
3406 GenericParametersWithDefaults::from_generic_type_pack(
3407 GenericTypePack::new(
3408 create_identifier("R", 7, 0),
3409 ).with_token(token_at_first_line(8, 11))
3410 )
3411 .with_tokens(GenericParametersTokens {
3412 opening_list: token_at_first_line(6, 7),
3413 closing_list: spaced_token(11, 12),
3414 commas: Vec::new(),
3415 })
3416 )
3417 .with_tokens(TypeDeclarationTokens {
3418 r#type: spaced_token(0, 4),
3419 equal: spaced_token(13, 14),
3420 export: None,
3421 }),
3422 type_declaration_with_variable_and_generic_type_pack("type T<A, R...> = (A) -> R...") => TypeDeclarationStatement::new(
3423 create_identifier("T", 5, 0),
3424 FunctionType::new(
3425 GenericTypePack::new(create_identifier("R", 25, 0))
3426 .with_token(token_at_first_line(26, 29))
3427 )
3428 .with_argument(TypeName::new(create_identifier("A", 19, 0)))
3429 .with_tokens(FunctionTypeTokens {
3430 opening_parenthese: token_at_first_line(18, 19),
3431 closing_parenthese: spaced_token(20, 21),
3432 arrow: spaced_token(22, 24),
3433 commas: Vec::new(),
3434 })
3435 )
3436 .with_generic_parameters(
3437 GenericParametersWithDefaults::from_type_variable(
3438 create_identifier("A", 7,0 )
3439 )
3440 .with_generic_type_pack(
3441 GenericTypePack::new(
3442 create_identifier("R", 10, 0),
3443 ).with_token(token_at_first_line(11, 14))
3444 ).unwrap()
3445 .with_tokens(GenericParametersTokens {
3446 opening_list: token_at_first_line(6, 7),
3447 closing_list: spaced_token(14, 15),
3448 commas: vec![spaced_token(8, 9)],
3449 })
3450 )
3451 .with_tokens(TypeDeclarationTokens {
3452 r#type: spaced_token(0, 4),
3453 equal: spaced_token(16, 17),
3454 export: None,
3455 }),
3456 type_declaration_with_generic_type_pack_with_default_tuple("type T<R...=()> = () -> R...") => TypeDeclarationStatement::new(
3457 create_identifier("T", 5, 0),
3458 FunctionType::new(
3459 GenericTypePack::new(create_identifier("R", 24, 0))
3460 .with_token(token_at_first_line(25, 28))
3461 )
3462 .with_tokens(FunctionTypeTokens {
3463 opening_parenthese: token_at_first_line(18, 19),
3464 closing_parenthese: spaced_token(19, 20),
3465 arrow: spaced_token(21, 23),
3466 commas: Vec::new(),
3467 })
3468 )
3469 .with_generic_parameters(
3470 GenericParametersWithDefaults::from_generic_type_pack_with_default(
3471 GenericTypePackWithDefault::new(
3472 GenericTypePack::new(
3473 create_identifier("R", 7, 0),
3474 ).with_token(token_at_first_line(8, 11)),
3475 TypePack::default().with_tokens(TypePackTokens {
3476 left_parenthese: token_at_first_line(12, 13),
3477 right_parenthese: token_at_first_line(13, 14),
3478 commas: Vec::new(),
3479 })
3480 ).with_token(token_at_first_line(11, 12))
3481 )
3482 .with_tokens(GenericParametersTokens {
3483 opening_list: token_at_first_line(6, 7),
3484 closing_list: spaced_token(14, 15),
3485 commas: Vec::new(),
3486 })
3487 )
3488 .with_tokens(TypeDeclarationTokens {
3489 r#type: spaced_token(0, 4),
3490 equal: spaced_token(16, 17),
3491 export: None,
3492 }),
3493 type_declaration_with_generic_type_pack_with_default_variadic_pack("type T<R...=...string> = () -> R...") => TypeDeclarationStatement::new(
3494 create_identifier("T", 5, 0),
3495 FunctionType::new(
3496 GenericTypePack::new(create_identifier("R", 31, 0))
3497 .with_token(token_at_first_line(32, 35))
3498 )
3499 .with_tokens(FunctionTypeTokens {
3500 opening_parenthese: token_at_first_line(25, 26),
3501 closing_parenthese: spaced_token(26, 27),
3502 arrow: spaced_token(28, 30),
3503 commas: Vec::new(),
3504 })
3505 )
3506 .with_generic_parameters(
3507 GenericParametersWithDefaults::from_generic_type_pack_with_default(
3508 GenericTypePackWithDefault::new(
3509 GenericTypePack::new(
3510 create_identifier("R", 7, 0),
3511 ).with_token(token_at_first_line(8, 11)),
3512 VariadicTypePack::new(
3513 TypeName::new(create_identifier("string", 15, 0))
3514 ).with_token(token_at_first_line(12, 15)),
3515 ).with_token(token_at_first_line(11, 12))
3516 )
3517 .with_tokens(GenericParametersTokens {
3518 opening_list: token_at_first_line(6, 7),
3519 closing_list: spaced_token(21, 22),
3520 commas: Vec::new(),
3521 })
3522 )
3523 .with_tokens(TypeDeclarationTokens {
3524 r#type: spaced_token(0, 4),
3525 equal: spaced_token(23, 24),
3526 export: None,
3527 }),
3528 type_declaration_with_generic_type_pack_with_default_generic_pack("type T<R...=A...> = () -> R...") => TypeDeclarationStatement::new(
3529 create_identifier("T", 5, 0),
3530 FunctionType::new(
3531 GenericTypePack::new(create_identifier("R", 26, 0))
3532 .with_token(token_at_first_line(27, 30))
3533 )
3534 .with_tokens(FunctionTypeTokens {
3535 opening_parenthese: token_at_first_line(20, 21),
3536 closing_parenthese: spaced_token(21, 22),
3537 arrow: spaced_token(23, 25),
3538 commas: Vec::new(),
3539 })
3540 )
3541 .with_generic_parameters(
3542 GenericParametersWithDefaults::from_generic_type_pack_with_default(
3543 GenericTypePackWithDefault::new(
3544 GenericTypePack::new(
3545 create_identifier("R", 7, 0),
3546 ).with_token(token_at_first_line(8, 11)),
3547 GenericTypePack::new(create_identifier("A", 12, 0))
3548 .with_token(token_at_first_line(13, 16)),
3549 ).with_token(token_at_first_line(11, 12))
3550 )
3551 .with_tokens(GenericParametersTokens {
3552 opening_list: token_at_first_line(6, 7),
3553 closing_list: spaced_token(16, 17),
3554 commas: Vec::new(),
3555 })
3556 )
3557 .with_tokens(TypeDeclarationTokens {
3558 r#type: spaced_token(0, 4),
3559 equal: spaced_token(18, 19),
3560 export: None,
3561 }),
3562 type_declaration_to_generic_type("type T = Array<string>") => TypeDeclarationStatement::new(
3563 create_identifier("T", 5, 1),
3564 TypeName::new(create_identifier("Array", 9, 0))
3565 .with_type_parameters(
3566 TypeParameters::new(TypeName::new(create_identifier("string", 15, 0)))
3567 .with_tokens(TypeParametersTokens {
3568 opening_list: token_at_first_line(14, 15),
3569 closing_list: token_at_first_line(21, 22),
3570 commas: Vec::new(),
3571 })
3572 )
3573 )
3574 .with_tokens(TypeDeclarationTokens {
3575 r#type: spaced_token(0, 4),
3576 equal: spaced_token(7, 8),
3577 export: None,
3578 }),
3579 type_declaration_to_generic_type_with_two_types("type T = Dict<string, boolean>") => TypeDeclarationStatement::new(
3580 create_identifier("T", 5, 1),
3581 TypeName::new(create_identifier("Dict", 9, 0))
3582 .with_type_parameters(
3583 TypeParameters::new(TypeName::new(create_identifier("string", 14, 0)))
3584 .with_parameter(TypeName::new(create_identifier("boolean", 22, 0)))
3585 .with_tokens(TypeParametersTokens {
3586 opening_list: token_at_first_line(13, 14),
3587 closing_list: token_at_first_line(29, 30),
3588 commas: vec![spaced_token(20, 21)],
3589 })
3590 )
3591 )
3592 .with_tokens(TypeDeclarationTokens {
3593 r#type: spaced_token(0, 4),
3594 equal: spaced_token(7, 8),
3595 export: None,
3596 }),
3597 type_declaration_to_generic_type_with_type_pack("type T = Fn<()>") => TypeDeclarationStatement::new(
3598 create_identifier("T", 5, 1),
3599 TypeName::new(create_identifier("Fn", 9, 0))
3600 .with_type_parameters(
3601 TypeParameters::new(
3602 TypePack::default().with_tokens(TypePackTokens {
3603 left_parenthese: token_at_first_line(12, 13),
3604 right_parenthese: token_at_first_line(13, 14),
3605 commas: Vec::new(),
3606 })
3607 )
3608 .with_tokens(TypeParametersTokens {
3609 opening_list: token_at_first_line(11, 12),
3610 closing_list: token_at_first_line(14, 15),
3611 commas: Vec::new(),
3612 })
3613 )
3614 )
3615 .with_tokens(TypeDeclarationTokens {
3616 r#type: spaced_token(0, 4),
3617 equal: spaced_token(7, 8),
3618 export: None,
3619 }),
3620 type_declaration_to_generic_type_with_generic_type_pack("type T = Fn<A...>") => TypeDeclarationStatement::new(
3621 create_identifier("T", 5, 1),
3622 TypeName::new(create_identifier("Fn", 9, 0))
3623 .with_type_parameters(
3624 TypeParameters::new(
3625 GenericTypePack::new(create_identifier("A", 12, 0))
3626 .with_token(token_at_first_line(13, 16))
3627 )
3628 .with_tokens(TypeParametersTokens {
3629 opening_list: token_at_first_line(11, 12),
3630 closing_list: token_at_first_line(16, 17),
3631 commas: Vec::new(),
3632 })
3633 )
3634 )
3635 .with_tokens(TypeDeclarationTokens {
3636 r#type: spaced_token(0, 4),
3637 equal: spaced_token(7, 8),
3638 export: None,
3639 }),
3640 type_declaration_to_generic_type_with_variadic_type_pack("type T = Fn<...A>") => TypeDeclarationStatement::new(
3641 create_identifier("T", 5, 1),
3642 TypeName::new(create_identifier("Fn", 9, 0))
3643 .with_type_parameters(
3644 TypeParameters::new(
3645 VariadicTypePack::new(TypeName::new(create_identifier("A", 15, 0)))
3646 .with_token(token_at_first_line(12, 15))
3647 )
3648 .with_tokens(TypeParametersTokens {
3649 opening_list: token_at_first_line(11, 12),
3650 closing_list: token_at_first_line(16, 17),
3651 commas: Vec::new(),
3652 })
3653 )
3654 )
3655 .with_tokens(TypeDeclarationTokens {
3656 r#type: spaced_token(0, 4),
3657 equal: spaced_token(7, 8),
3658 export: None,
3659 }),
3660 type_declaration_to_generic_type_with_variadic_string_literal_type_pack("type T = Fn<...'ok'>") => TypeDeclarationStatement::new(
3661 create_identifier("T", 5, 1),
3662 TypeName::new(create_identifier("Fn", 9, 0))
3663 .with_type_parameters(
3664 TypeParameters::new(
3665 VariadicTypePack::new(
3666 StringType::from_value("ok").with_token(token_at_first_line(15, 19))
3667 )
3668 .with_token(token_at_first_line(12, 15))
3669 )
3670 .with_tokens(TypeParametersTokens {
3671 opening_list: token_at_first_line(11, 12),
3672 closing_list: token_at_first_line(19, 20),
3673 commas: Vec::new(),
3674 })
3675 )
3676 )
3677 .with_tokens(TypeDeclarationTokens {
3678 r#type: spaced_token(0, 4),
3679 equal: spaced_token(7, 8),
3680 export: None,
3681 }),
3682 type_declaration_to_generic_type_in_namespace("type T = M.Array<string>") => TypeDeclarationStatement::new(
3683 create_identifier("T", 5, 1),
3684 TypeField::new(
3685 create_identifier("M", 9, 0),
3686 TypeName::new(create_identifier("Array", 11, 0))
3687 .with_type_parameters(
3688 TypeParameters::new(TypeName::new(create_identifier("string", 17, 0)))
3689 .with_tokens(TypeParametersTokens {
3690 opening_list: token_at_first_line(16, 17),
3691 closing_list: token_at_first_line(23, 24),
3692 commas: Vec::new(),
3693 })
3694 )
3695 ).with_token(token_at_first_line(10, 11))
3696 )
3697 .with_tokens(TypeDeclarationTokens {
3698 r#type: spaced_token(0, 4),
3699 equal: spaced_token(7, 8),
3700 export: None,
3701 }),
3702 type_function_statement_empty("type function nothing() end") => TypeFunctionStatement::from_name(
3703 create_identifier("nothing", 14, 0),
3704 Block::default().with_tokens(BlockTokens {
3705 semicolons: Vec::new(),
3706 last_semicolon: None,
3707 final_token: None,
3708 }),
3709 ).with_tokens(TypeFunctionStatementTokens {
3710 r#type: spaced_token(0, 4),
3711 function_body: FunctionBodyTokens {
3712 function: spaced_token(5, 13),
3713 opening_parenthese: token_at_first_line(21, 22),
3714 closing_parenthese: spaced_token(22, 23),
3715 end: token_at_first_line(24, 27),
3716 parameter_commas: Vec::new(),
3717 variable_arguments: None,
3718 variable_arguments_colon: None,
3719 return_type_colon: None,
3720 },
3721 export: None,
3722 }),
3723 exported_type_function_statement_empty("export type function nothing() end") => TypeFunctionStatement::from_name(
3724 create_identifier("nothing", 21, 0),
3725 Block::default().with_tokens(BlockTokens {
3726 semicolons: Vec::new(),
3727 last_semicolon: None,
3728 final_token: None,
3729 }),
3730 )
3731 .export()
3732 .with_tokens(TypeFunctionStatementTokens {
3733 r#type: spaced_token(7, 11),
3734 function_body: FunctionBodyTokens {
3735 function: spaced_token(12, 20),
3736 opening_parenthese: token_at_first_line(28, 29),
3737 closing_parenthese: spaced_token(29, 30),
3738 end: token_at_first_line(31, 34),
3739 parameter_commas: Vec::new(),
3740 variable_arguments: None,
3741 variable_arguments_colon: None,
3742 return_type_colon: None,
3743 },
3744 export: Some(spaced_token(0, 6)),
3745 }),
3746 );
3747
3748 test_parse_block_with_tokens!(
3749 empty_block("") => Block::default()
3750 .with_tokens(BlockTokens {
3751 semicolons: vec![],
3752 last_semicolon: None,
3753 final_token: None,
3754 }),
3755 single_line("\n") => Block::default()
3756 .with_tokens(BlockTokens {
3757 semicolons: vec![],
3758 last_semicolon: None,
3759 final_token: Some(Token::new_with_line(1, 1, 2)
3760 .with_leading_trivia(TriviaKind::Whitespace.at(0, 1, 1))),
3761 }),
3762 single_line_comment("-- todo") => Block::default()
3763 .with_tokens(BlockTokens {
3764 semicolons: vec![],
3765 last_semicolon: None,
3766 final_token: Some(token_at_first_line(7, 7)
3767 .with_leading_trivia(TriviaKind::Comment.at(0, 7, 1))),
3768 }),
3769 multiple_line_comments("-- todo\n -- one\n") => Block::default()
3770 .with_tokens(BlockTokens {
3771 semicolons: vec![],
3772 last_semicolon: None,
3773 final_token: Some(
3774 Token::new_with_line(17, 17, 3)
3775 .with_leading_trivia(TriviaKind::Comment.at(0, 7, 1))
3776 .with_leading_trivia(TriviaKind::Whitespace.at(7, 8, 1))
3777 .with_leading_trivia(TriviaKind::Whitespace.at(8, 10, 2))
3778 .with_leading_trivia(TriviaKind::Comment.at(10, 16, 2))
3779 .with_leading_trivia(TriviaKind::Whitespace.at(16, 17, 2))
3780 ),
3781 }),
3782 single_multiline_comment("--[[\n todo\n]]") => Block::default()
3783 .with_tokens(BlockTokens {
3784 semicolons: vec![],
3785 last_semicolon: None,
3786 final_token: Some(Token::new_with_line(16, 16, 3)
3787 .with_leading_trivia(TriviaKind::Comment.at(0, 16, 1))),
3788 }),
3789 return_nothing_with_semicolon("return;") => Block::from(
3790 ReturnStatement::default()
3791 .with_tokens(ReturnTokens {
3792 r#return: token_at_first_line(0, 6),
3793 commas: Vec::new(),
3794 }),
3795 ).with_tokens(BlockTokens {
3796 semicolons: vec![],
3797 last_semicolon: Some(token_at_first_line(6, 7)),
3798 final_token: None,
3799 }),
3800 return_nothing_with_semicolon_and_comment("return; -- return nothing") => Block::from(
3801 ReturnStatement::default()
3802 .with_tokens(ReturnTokens {
3803 r#return: token_at_first_line(0, 6),
3804 commas: Vec::new(),
3805 }),
3806 ).with_tokens(BlockTokens {
3807 semicolons: vec![],
3808 last_semicolon: Some(
3809 token_at_first_line(6, 7)
3810 .with_trailing_trivia(TriviaKind::Whitespace.at(7, 8, 1))
3811 .with_trailing_trivia(TriviaKind::Comment.at(8, 25, 1))
3812 ),
3813 final_token: None,
3814 }),
3815 two_local_declarations("local a;\nlocal b;\n") => Block::from(
3816 VariableAssignment::from_variable(create_identifier("a", 6, 0))
3817 .with_tokens(VariableAssignmentTokens {
3818 keyword: spaced_token(0, 5),
3819 equal: None,
3820 variable_commas: Vec::new(),
3821 value_commas: Vec::new(),
3822 })
3823 ).with_statement(
3824 VariableAssignment::from_variable(create_identifier_at_line("b", 15, 0, 2))
3825 .with_tokens(VariableAssignmentTokens {
3826 keyword: spaced_token_at_line(9, 14, 2),
3827 equal: None,
3828 variable_commas: Vec::new(),
3829 value_commas: Vec::new(),
3830 })
3831 ).with_tokens(BlockTokens {
3832 semicolons: vec![
3833 Some(spaced_token(7, 8)),
3834 Some(spaced_token_at_line(16, 17, 2)),
3835 ],
3836 last_semicolon: None,
3837 final_token: None,
3838 }),
3839 );
3840 }
3841}