Skip to content

Commit

Permalink
chore: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
fireairforce committed Oct 10, 2024
1 parent 2e5e3f2 commit d95fc97
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 5,158 deletions.
3 changes: 2 additions & 1 deletion crates/biome_js_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,12 +1450,13 @@ fn parse_primary_expression(p: &mut JsParser, context: ExpressionContext) -> Par
// test js import_call
// import("foo")
// import("foo", { assert: { type: 'json' } })
// import("foo", { with: { 'resolution-mode': 'import' } })

// test_err js import_invalid_args
// import()
// import(...["foo"])
// import("foo", { assert: { type: 'json' } }, "bar")

// import("foo", { with: { type: 'json' } }, "bar")
let args = p.start();
p.bump(T!['(']);
let args_list = p.start();
Expand Down
41 changes: 33 additions & 8 deletions crates/biome_js_parser/src/syntax/typescript/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ use crate::parser::{RecoveryError, RecoveryResult};
use crate::prelude::*;
use crate::state::{EnterType, SignatureFlags};
use crate::syntax::expr::{
is_at_binary_operator, is_at_expression, is_at_identifier, is_nth_at_identifier,
is_nth_at_identifier_or_keyword, parse_big_int_literal_expression, parse_identifier,
parse_literal_expression, parse_name, parse_number_literal_expression,
parse_reference_identifier, parse_template_elements, ExpressionContext,
is_at_binary_operator, is_at_expression, is_at_identifier, is_nth_at_identifier, is_nth_at_identifier_or_keyword, parse_big_int_literal_expression, parse_identifier, parse_literal_expression, parse_name, parse_number_literal_expression, parse_reference_identifier, parse_template_elements, ExpressionContext
};
use crate::syntax::function::{
parse_formal_parameter, parse_parameter_list, skip_parameter_start, ParameterContext,
};
use crate::syntax::js_parse_error::{
decorators_not_allowed, expected_identifier, expected_object_member_name, expected_parameter,
expected_parameters, expected_property_or_signature, modifier_already_seen,
modifier_must_precede_modifier,
decorators_not_allowed, expected_identifier, expected_object_member_name, expected_parameter, expected_parameters, expected_property_or_signature, modifier_already_seen, modifier_must_precede_modifier
};
use crate::syntax::metavariable::parse_metavariable;
use crate::syntax::object::{
Expand All @@ -29,6 +24,7 @@ use crate::syntax::typescript::ts_parse_error::{
ts_in_out_modifier_cannot_appear_on_a_type_parameter,
};
use biome_parser::parse_lists::{ParseNodeList, ParseSeparatedList};
use biome_parser::ParserProgress;
use enumflags2::{bitflags, make_bitflags, BitFlags};
use smallvec::SmallVec;

Expand Down Expand Up @@ -1150,6 +1146,7 @@ fn parse_ts_mapped_type_optional_modifier_clause(p: &mut JsParser) -> ParsedSynt
// type C = typeof import("test").a.b.c.d.e.f;
// type D = import("test")<string>;
// type E = import("test").C<string>;
// type F = typeof import("test", { with: { 'resolution-mode': 'import' } });
fn parse_ts_import_type(p: &mut JsParser, context: TypeContext) -> ParsedSyntax {
if !p.at(T![typeof]) && !p.at(T![import]) {
return Absent;
Expand All @@ -1159,7 +1156,35 @@ fn parse_ts_import_type(p: &mut JsParser, context: TypeContext) -> ParsedSyntax
p.eat(T![typeof]);
p.expect(T![import]);
p.expect(T!['(']);
p.expect(JS_STRING_LITERAL);
let args_list = p.start();

let mut progress = ParserProgress::default();
let mut error_range_start = p.cur_range().start();
let mut args_count = 0;

while !p.at(EOF) && !p.at(T![')']) {
progress.assert_progressing(p);
args_count += 1;

if args_count == 3 {
error_range_start = p.cur_range().start();
}

if p.at(T![,]) {
p.bump_any();
} else {
break;
}
}
args_list.complete(p, JS_CALL_ARGUMENT_LIST);

if args_count == 0 || args_count > 2 {
let err = p.err_builder(
"`typeof import()` requires exactly one or two arguments. ",
error_range_start..p.cur_range().end(),
);
p.error(err);
}
p.expect(T![')']);

if p.at(T![.]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import()
import(...["foo"])
import("foo", { assert: { type: 'json' } }, "bar")
import("foo", { with: { type: 'json' } }, "bar")
104 changes: 98 additions & 6 deletions crates/biome_js_parser/test_data/inline/err/import_invalid_args.rast
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,62 @@ JsModule {
},
semicolon_token: missing (optional),
},
JsExpressionStatement {
expression: JsImportCallExpression {
import_token: IMPORT_KW@78..85 "import" [Newline("\n")] [],
arguments: JsCallArguments {
l_paren_token: L_PAREN@85..86 "(" [] [],
args: JsCallArgumentList [
JsStringLiteralExpression {
value_token: JS_STRING_LITERAL@86..91 "\"foo\"" [] [],
},
COMMA@91..93 "," [] [Whitespace(" ")],
JsObjectExpression {
l_curly_token: L_CURLY@93..95 "{" [] [Whitespace(" ")],
members: JsObjectMemberList [
JsPropertyObjectMember {
name: JsLiteralMemberName {
value: IDENT@95..99 "with" [] [],
},
colon_token: COLON@99..101 ":" [] [Whitespace(" ")],
value: JsObjectExpression {
l_curly_token: L_CURLY@101..103 "{" [] [Whitespace(" ")],
members: JsObjectMemberList [
JsPropertyObjectMember {
name: JsLiteralMemberName {
value: IDENT@103..107 "type" [] [],
},
colon_token: COLON@107..109 ":" [] [Whitespace(" ")],
value: JsStringLiteralExpression {
value_token: JS_STRING_LITERAL@109..116 "'json'" [] [Whitespace(" ")],
},
},
],
r_curly_token: R_CURLY@116..118 "}" [] [Whitespace(" ")],
},
},
],
r_curly_token: R_CURLY@118..119 "}" [] [],
},
COMMA@119..121 "," [] [Whitespace(" ")],
JsStringLiteralExpression {
value_token: JS_STRING_LITERAL@121..126 "\"bar\"" [] [],
},
],
r_paren_token: R_PAREN@126..127 ")" [] [],
},
},
semicolon_token: missing (optional),
},
],
eof_token: EOF@78..79 "" [Newline("\n")] [],
eof_token: EOF@127..128 "" [Newline("\n")] [],
}

0: JS_MODULE@0..79
0: JS_MODULE@0..128
0: (empty)
1: (empty)
2: JS_DIRECTIVE_LIST@0..0
3: JS_MODULE_ITEM_LIST@0..78
3: JS_MODULE_ITEM_LIST@0..127
0: JS_EXPRESSION_STATEMENT@0..8
0: JS_IMPORT_CALL_EXPRESSION@0..8
0: IMPORT_KW@0..6 "import" [] []
Expand Down Expand Up @@ -158,7 +205,39 @@ JsModule {
0: JS_STRING_LITERAL@72..77 "\"bar\"" [] []
2: R_PAREN@77..78 ")" [] []
1: (empty)
4: EOF@78..79 "" [Newline("\n")] []
3: JS_EXPRESSION_STATEMENT@78..127
0: JS_IMPORT_CALL_EXPRESSION@78..127
0: IMPORT_KW@78..85 "import" [Newline("\n")] []
1: JS_CALL_ARGUMENTS@85..127
0: L_PAREN@85..86 "(" [] []
1: JS_CALL_ARGUMENT_LIST@86..126
0: JS_STRING_LITERAL_EXPRESSION@86..91
0: JS_STRING_LITERAL@86..91 "\"foo\"" [] []
1: COMMA@91..93 "," [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@93..119
0: L_CURLY@93..95 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@95..118
0: JS_PROPERTY_OBJECT_MEMBER@95..118
0: JS_LITERAL_MEMBER_NAME@95..99
0: IDENT@95..99 "with" [] []
1: COLON@99..101 ":" [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@101..118
0: L_CURLY@101..103 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@103..116
0: JS_PROPERTY_OBJECT_MEMBER@103..116
0: JS_LITERAL_MEMBER_NAME@103..107
0: IDENT@103..107 "type" [] []
1: COLON@107..109 ":" [] [Whitespace(" ")]
2: JS_STRING_LITERAL_EXPRESSION@109..116
0: JS_STRING_LITERAL@109..116 "'json'" [] [Whitespace(" ")]
2: R_CURLY@116..118 "}" [] [Whitespace(" ")]
2: R_CURLY@118..119 "}" [] []
3: COMMA@119..121 "," [] [Whitespace(" ")]
4: JS_STRING_LITERAL_EXPRESSION@121..126
0: JS_STRING_LITERAL@121..126 "\"bar\"" [] []
2: R_PAREN@126..127 ")" [] []
1: (empty)
4: EOF@127..128 "" [Newline("\n")] []
--
import_invalid_args.js:1:8 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand All @@ -178,7 +257,7 @@ import_invalid_args.js:2:8 parse ━━━━━━━━━━━━━━━
> 2 │ import(...["foo"])
│ ^^^^^^^^^^
3 │ import("foo", { assert: { type: 'json' } }, "bar")
4 │
4 │ import("foo", { with: { type: 'json' } }, "bar")

--
import_invalid_args.js:3:45 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Expand All @@ -189,9 +268,22 @@ import_invalid_args.js:3:45 parse ━━━━━━━━━━━━━━━
2 │ import(...["foo"])
> 3 │ import("foo", { assert: { type: 'json' } }, "bar")
│ ^^^^^^
4 │
4 │ import("foo", { with: { type: 'json' } }, "bar")
5 │

--
import_invalid_args.js:4:43 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× `import()` requires exactly one or two arguments.

2 │ import(...["foo"])
3 │ import("foo", { assert: { type: 'json' } }, "bar")
> 4 │ import("foo", { with: { type: 'json' } }, "bar")
│ ^^^^^^
5 │

--
import()
import(...["foo"])
import("foo", { assert: { type: 'json' } }, "bar")
import("foo", { with: { type: 'json' } }, "bar")
1 change: 1 addition & 0 deletions crates/biome_js_parser/test_data/inline/ok/import_call.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import("foo")
import("foo", { assert: { type: 'json' } })
import("foo", { with: { 'resolution-mode': 'import' } })
80 changes: 76 additions & 4 deletions crates/biome_js_parser/test_data/inline/ok/import_call.rast
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,58 @@ JsModule {
},
semicolon_token: missing (optional),
},
JsExpressionStatement {
expression: JsImportCallExpression {
import_token: IMPORT_KW@57..64 "import" [Newline("\n")] [],
arguments: JsCallArguments {
l_paren_token: L_PAREN@64..65 "(" [] [],
args: JsCallArgumentList [
JsStringLiteralExpression {
value_token: JS_STRING_LITERAL@65..70 "\"foo\"" [] [],
},
COMMA@70..72 "," [] [Whitespace(" ")],
JsObjectExpression {
l_curly_token: L_CURLY@72..74 "{" [] [Whitespace(" ")],
members: JsObjectMemberList [
JsPropertyObjectMember {
name: JsLiteralMemberName {
value: IDENT@74..78 "with" [] [],
},
colon_token: COLON@78..80 ":" [] [Whitespace(" ")],
value: JsObjectExpression {
l_curly_token: L_CURLY@80..82 "{" [] [Whitespace(" ")],
members: JsObjectMemberList [
JsPropertyObjectMember {
name: JsLiteralMemberName {
value: JS_STRING_LITERAL@82..99 "'resolution-mode'" [] [],
},
colon_token: COLON@99..101 ":" [] [Whitespace(" ")],
value: JsStringLiteralExpression {
value_token: JS_STRING_LITERAL@101..110 "'import'" [] [Whitespace(" ")],
},
},
],
r_curly_token: R_CURLY@110..112 "}" [] [Whitespace(" ")],
},
},
],
r_curly_token: R_CURLY@112..113 "}" [] [],
},
],
r_paren_token: R_PAREN@113..114 ")" [] [],
},
},
semicolon_token: missing (optional),
},
],
eof_token: EOF@57..58 "" [Newline("\n")] [],
eof_token: EOF@114..115 "" [Newline("\n")] [],
}

0: JS_MODULE@0..58
0: JS_MODULE@0..115
0: (empty)
1: (empty)
2: JS_DIRECTIVE_LIST@0..0
3: JS_MODULE_ITEM_LIST@0..57
3: JS_MODULE_ITEM_LIST@0..114
0: JS_EXPRESSION_STATEMENT@0..13
0: JS_IMPORT_CALL_EXPRESSION@0..13
0: IMPORT_KW@0..6 "import" [] []
Expand Down Expand Up @@ -109,4 +152,33 @@ JsModule {
2: R_CURLY@55..56 "}" [] []
2: R_PAREN@56..57 ")" [] []
1: (empty)
4: EOF@57..58 "" [Newline("\n")] []
2: JS_EXPRESSION_STATEMENT@57..114
0: JS_IMPORT_CALL_EXPRESSION@57..114
0: IMPORT_KW@57..64 "import" [Newline("\n")] []
1: JS_CALL_ARGUMENTS@64..114
0: L_PAREN@64..65 "(" [] []
1: JS_CALL_ARGUMENT_LIST@65..113
0: JS_STRING_LITERAL_EXPRESSION@65..70
0: JS_STRING_LITERAL@65..70 "\"foo\"" [] []
1: COMMA@70..72 "," [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@72..113
0: L_CURLY@72..74 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@74..112
0: JS_PROPERTY_OBJECT_MEMBER@74..112
0: JS_LITERAL_MEMBER_NAME@74..78
0: IDENT@74..78 "with" [] []
1: COLON@78..80 ":" [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@80..112
0: L_CURLY@80..82 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@82..110
0: JS_PROPERTY_OBJECT_MEMBER@82..110
0: JS_LITERAL_MEMBER_NAME@82..99
0: JS_STRING_LITERAL@82..99 "'resolution-mode'" [] []
1: COLON@99..101 ":" [] [Whitespace(" ")]
2: JS_STRING_LITERAL_EXPRESSION@101..110
0: JS_STRING_LITERAL@101..110 "'import'" [] [Whitespace(" ")]
2: R_CURLY@110..112 "}" [] [Whitespace(" ")]
2: R_CURLY@112..113 "}" [] []
2: R_PAREN@113..114 ")" [] []
1: (empty)
4: EOF@114..115 "" [Newline("\n")] []
48 changes: 48 additions & 0 deletions crates/biome_js_parser/test_data/inline/ok/ts_import_type.rast
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,51 @@ JsModule {
2: R_ANGLE@166..167 ">" [] []
5: SEMICOLON@167..168 ";" [] []
4: EOF@168..169 "" [Newline("\n")] []


0: JS_MODULE@0..75
0: (empty)
1: (empty)
2: JS_DIRECTIVE_LIST@0..0
3: JS_MODULE_ITEM_LIST@0..74
0: TS_TYPE_ALIAS_DECLARATION@0..23
0: TYPE_KW@0..5 "type" [] [Whitespace(" ")]
1: TS_IDENTIFIER_BINDING@5..7
0: IDENT@5..7 "F" [] [Whitespace(" ")]
2: (empty)
3: EQ@7..9 "=" [] [Whitespace(" ")]
4: TS_BOGUS_TYPE@9..23
0: TYPEOF_KW@9..16 "typeof" [] [Whitespace(" ")]
1: IMPORT_KW@16..22 "import" [] []
2: L_PAREN@22..23 "(" [] []
3: JS_CALL_ARGUMENT_LIST@23..23
5: (empty)
1: JS_EXPRESSION_STATEMENT@23..72
0: JS_SEQUENCE_EXPRESSION@23..72
0: JS_STRING_LITERAL_EXPRESSION@23..29
0: JS_STRING_LITERAL@23..29 "\"test\"" [] []
1: COMMA@29..31 "," [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@31..72
0: L_CURLY@31..33 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@33..71
0: JS_PROPERTY_OBJECT_MEMBER@33..71
0: JS_LITERAL_MEMBER_NAME@33..37
0: IDENT@33..37 "with" [] []
1: COLON@37..39 ":" [] [Whitespace(" ")]
2: JS_OBJECT_EXPRESSION@39..71
0: L_CURLY@39..41 "{" [] [Whitespace(" ")]
1: JS_OBJECT_MEMBER_LIST@41..69
0: JS_PROPERTY_OBJECT_MEMBER@41..69
0: JS_LITERAL_MEMBER_NAME@41..58
0: JS_STRING_LITERAL@41..58 "'resolution-mode'" [] []
1: COLON@58..60 ":" [] [Whitespace(" ")]
2: JS_STRING_LITERAL_EXPRESSION@60..69
0: JS_STRING_LITERAL@60..69 "'import'" [] [Whitespace(" ")]
2: R_CURLY@69..71 "}" [] [Whitespace(" ")]
2: R_CURLY@71..72 "}" [] []
1: (empty)
2: JS_BOGUS_STATEMENT@72..73
0: R_PAREN@72..73 ")" [] []
3: JS_EMPTY_STATEMENT@73..74
0: SEMICOLON@73..74 ";" [] []
4: EOF@74..75 "" [Newline("\n")] []
6 changes: 1 addition & 5 deletions crates/biome_js_parser/test_data/inline/ok/ts_import_type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
type A = typeof import("test");
type B = import("test");
type C = typeof import("test").a.b.c.d.e.f;
type D = import("test")<string>;
type E = import("test").C<string>;
type F = typeof import("test", { with: { 'resolution-mode': 'import' } });
Loading

0 comments on commit d95fc97

Please sign in to comment.