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