Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/flavor-plugin-typescript/src/parser/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ impl<'a> Parser<'a> {
pub(super) fn parse_for_statement(&mut self) {
self.builder.start_node(kind::FOR_STATEMENT);
self.bump();
if self.at(kind::KEYWORD_AWAIT) {
self.bump();
}
self.parenthesized_condition("expected '(' to start for header");
self.statement_or_block();
self.builder.finish_node();
Expand Down
12 changes: 11 additions & 1 deletion crates/flavor-plugin-typescript/src/parser/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ impl<'a> Parser<'a> {
let mut segment_stops = stops.to_vec();
segment_stops.push(kind::PIPE);
segment_stops.push(kind::AMPERSAND);
self.parse_type_segment_until(&segment_stops);
if self.at(kind::OPEN_BRACE) && stops.contains(&kind::OPEN_BRACE) {
if self.is_mapped_type_start() {
self.parse_mapped_type();
} else {
self.parse_object_type();
}
} else if self.at_any(stops) {
break;
} else {
self.parse_type_segment_until(&segment_stops);
}
if self.at(kind::PIPE) || self.at(kind::AMPERSAND) {
self.bump();
self.ensure_progress(start, "type");
Expand Down
38 changes: 38 additions & 0 deletions crates/flavor-plugin-typescript/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ mod declarations {
assert!(output.diagnostics.is_empty());
}

#[test]
fn function_return_object_type() {
let output = ts_output(
"function splitPath(path: string): { dir: string; name: string } { return { dir: '', name: path }; }",
);

assert!(has_node(&output, kind::FUNCTION_DECLARATION));
assert!(has_node(&output, kind::RETURN_TYPE));
assert!(has_node(&output, kind::OBJECT_TYPE));
assert!(has_node(&output, kind::BLOCK));
assert!(output.diagnostics.is_empty(), "{:?}", output.diagnostics);
}

#[test]
fn object_intersection_return() {
let output = ts_output(
"type Options = { raw: boolean }; function parseArgs(args: string[]): Options & { help: boolean } { return { raw: false, help: false }; }",
);

assert!(has_node(&output, kind::FUNCTION_DECLARATION));
assert!(has_node(&output, kind::RETURN_TYPE));
assert!(has_node(&output, kind::INTERSECTION_TYPE));
assert!(has_node(&output, kind::OBJECT_TYPE));
assert!(output.diagnostics.is_empty(), "{:?}", output.diagnostics);
}

#[test]
fn decorated_class() {
let output = ts_output("@sealed() class Example<T> extends Base { value!: string; }");
Expand Down Expand Up @@ -268,6 +294,18 @@ mod control_flow {
assert!(has_node(&output, kind::CONTINUE_STATEMENT));
assert!(output.diagnostics.is_empty());
}

#[test]
fn async_iterator_statement() {
let output = ts_output(
"async function list(path: string): Promise<void> { for await (const entry of Deno.readDir(path)) { console.log(entry.name); } }",
);

assert!(has_node(&output, kind::FUNCTION_DECLARATION));
assert!(has_node(&output, kind::FOR_STATEMENT));
assert!(has_token(&output, kind::KEYWORD_AWAIT));
assert!(output.diagnostics.is_empty(), "{:?}", output.diagnostics);
}
}

mod expressions {
Expand Down