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