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