Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rslint_parser): Parse 2nd param for JsImportCallExpression (#1923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Jan 13, 2022
1 parent 5ba587b commit 7991631
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 197 deletions.
4 changes: 1 addition & 3 deletions crates/rome_formatter/src/ts/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ impl ToFormatElement for JsImportCallExpression {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Ok(format_elements![
formatter.format_token(&self.import_token()?)?,
formatter.format_token(&self.l_paren_token()?)?,
formatter.format_node(self.argument()?)?,
formatter.format_token(&self.r_paren_token()?)?,
formatter.format_node(self.arguments()?)?,
])
}
}
2 changes: 1 addition & 1 deletion crates/rome_formatter/tests/specs/js/import/import_call.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import(x)
import('x')
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, {assert: {type:'json'}})
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ expression: import_call.js
# Input
import(x)
import('x')
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, {assert: {type:'json'}})

---
# Output
import(x);
import("x");
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
import(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, {
assert: { type: "json" },
});

20 changes: 3 additions & 17 deletions crates/rslint_parser/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,14 +1143,8 @@ impl JsImportCallExpression {
pub fn import_token(&self) -> SyntaxResult<SyntaxToken> {
support::required_token(&self.syntax, 0usize)
}
pub fn l_paren_token(&self) -> SyntaxResult<SyntaxToken> {
support::required_token(&self.syntax, 1usize)
}
pub fn argument(&self) -> SyntaxResult<JsAnyExpression> {
support::required_node(&self.syntax, 2usize)
}
pub fn r_paren_token(&self) -> SyntaxResult<SyntaxToken> {
support::required_token(&self.syntax, 3usize)
pub fn arguments(&self) -> SyntaxResult<JsCallArguments> {
support::required_node(&self.syntax, 1usize)
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -5689,15 +5683,7 @@ impl std::fmt::Debug for JsImportCallExpression {
"import_token",
&support::DebugSyntaxResult(self.import_token()),
)
.field(
"l_paren_token",
&support::DebugSyntaxResult(self.l_paren_token()),
)
.field("argument", &support::DebugSyntaxResult(self.argument()))
.field(
"r_paren_token",
&support::DebugSyntaxResult(self.r_paren_token()),
)
.field("arguments", &support::DebugSyntaxResult(self.arguments()))
.finish()
}
}
Expand Down
18 changes: 2 additions & 16 deletions crates/rslint_parser/src/ast/generated/syntax_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2905,7 +2905,7 @@ impl SyntaxFactory for JsSyntaxFactory {
}
JS_IMPORT_CALL_EXPRESSION => {
let mut elements = (&children).into_iter();
let mut slots: RawNodeSlots<4usize> = RawNodeSlots::default();
let mut slots: RawNodeSlots<2usize> = RawNodeSlots::default();
let mut current_element = elements.next();
if let Some(element) = &current_element {
if element.kind() == T![import] {
Expand All @@ -2915,21 +2915,7 @@ impl SyntaxFactory for JsSyntaxFactory {
}
slots.next_slot();
if let Some(element) = &current_element {
if element.kind() == T!['('] {
slots.mark_present();
current_element = elements.next();
}
}
slots.next_slot();
if let Some(element) = &current_element {
if JsAnyExpression::can_cast(element.kind()) {
slots.mark_present();
current_element = elements.next();
}
}
slots.next_slot();
if let Some(element) = &current_element {
if element.kind() == T![')'] {
if JsCallArguments::can_cast(element.kind()) {
slots.mark_present();
current_element = elements.next();
}
Expand Down
59 changes: 50 additions & 9 deletions crates/rslint_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,21 +1091,62 @@ fn parse_primary_expression(p: &mut Parser) -> ParsedSyntax {
p.error(err);
m.complete(p, JS_UNKNOWN)
}
} else {
// test_err import_call_no_arg
// let a = import();
// foo();

} else if p.at(T!['(']) {
// test import_call
// import("foo")
// import("foo", { assert: { type: 'json' } })

// test_err import_call
// test_err import_invalid_args
// import()
p.expect(T!['(']);
parse_expr_or_assignment(p)
.or_add_diagnostic(p, js_parse_error::expected_expression_assignment);
// import(...["foo"])
// import("foo", { assert: { type: 'json' } }, "bar")

let args = p.start();
p.bump(T!['(']);
let args_list = p.start();

let mut progress = ParserProgress::default();
let mut error_range_start = p.cur_tok().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_tok().start();
}

if p.at(T![...]) {
let err = p
.err_builder("`...` is not allowed in `import()`")
.primary(p.cur_tok().range(), "");
p.error(err);
} else {
parse_expr_or_assignment(p)
.or_add_diagnostic(p, js_parse_error::expected_expression_assignment);
}

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("`import()` requires exactly one or two arguments. ")
.primary(error_range_start..p.cur_tok().end(), "");
p.error(err);
}

p.expect(T![')']);
args.complete(p, JS_CALL_ARGUMENTS);
m.complete(p, JS_IMPORT_CALL_EXPRESSION)
} else {
return Absent;
}
}
BACKTICK => parse_template_literal(p, Absent),
Expand Down
1 change: 0 additions & 1 deletion crates/rslint_parser/test_data/inline/err/import_call.js

This file was deleted.

38 changes: 0 additions & 38 deletions crates/rslint_parser/test_data/inline/err/import_call.rast

This file was deleted.

This file was deleted.

93 changes: 0 additions & 93 deletions crates/rslint_parser/test_data/inline/err/import_call_no_arg.rast

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import()
import(...["foo"])
import("foo", { assert: { type: 'json' } }, "bar")
Loading

0 comments on commit 7991631

Please sign in to comment.