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

Commit

Permalink
feat: add support for double keywords in assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 30, 2023
1 parent fe41cd5 commit e2fca94
Show file tree
Hide file tree
Showing 17 changed files with 959 additions and 32 deletions.
4 changes: 2 additions & 2 deletions crates/rome_js_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/rome_js_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/rome_js_formatter/src/js/module/import_assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ pub(crate) struct FormatJsImportAssertion;
impl FormatNodeRule<JsImportAssertion> for FormatJsImportAssertion {
fn fmt_fields(&self, node: &JsImportAssertion, f: &mut JsFormatter) -> FormatResult<()> {
let JsImportAssertionFields {
assert_token,
l_curly_token,
assertions,
r_curly_token,
assertion_kind,
} = node.as_fields();

write![f, [assert_token.format(), space(), l_curly_token.format()]]?;
write![
f,
[assertion_kind.format(), space(), l_curly_token.format()]
]?;

if assertions.is_empty() {
let has_dangling = f.comments().has_dangling_comments(node.syntax());
Expand Down
2 changes: 2 additions & 0 deletions crates/rome_js_parser/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ pub fn parse_js_with_cache(
tracing::debug_span!("parse").in_scope(move || {
let (events, errors, tokens) = parse_common(text, source_type);
let mut tree_sink = JsLosslessTreeSink::with_cache(text, &tokens, cache);
dbg!(&tree_sink);
dbg!(&events);
rome_parser::event::process(&mut tree_sink, events, errors);
let (green, parse_errors) = tree_sink.finish();
Parse::new(green, parse_errors)
Expand Down
40 changes: 37 additions & 3 deletions crates/rome_js_parser/src/syntax/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,15 @@ fn parse_any_named_import_specifier(p: &mut JsParser) -> ParsedSyntax {
// import "x" assert
// { type: "json" }

// test import_attribute
// import "x" with { type: "json" }
// import "foo" with { "type": "json" };
// import foo from "foo.json" with { type: "json" };
// import {test} from "foo.json" with { for: "for" }
// import foo_json from "foo.json" with { type: "json", hasOwnProperty: "true" };
// import "x" with
// { type: "json" }

// test_err import_assertion_err
// import "foo" assert { type, "json" };
// import "bar" \u{61}ssert { type: "json" };
Expand All @@ -549,15 +558,40 @@ fn parse_any_named_import_specifier(p: &mut JsParser) -> ParsedSyntax {
// import "x" assert;
// import ipsum from "ipsum.json" assert { type: "json", lazy: true, startAtLine: 1 };
// import { a } from "a.json" assert

// test_err import_attribute_err
// import "foo" with { type, "json" };
// import { foo } with { type: "json" };
// import "lorem"
// assert { type: "json" }
// import foo2 from "foo.json" with { "type": "json", type: "html", "type": "js" };
// import "x" with;
// import ipsum from "ipsum.json" with { type: "json", lazy: true, startAtLine: 1 };
// import { a } from "a.json" with
fn parse_import_assertion(p: &mut JsParser) -> ParsedSyntax {
if !p.at(T![assert]) || p.has_preceding_line_break() {
if p.has_preceding_line_break() {
return Absent;
}
if !p.at(T![assert]) && !p.at(T![with]) {
return Absent;
}

let m = p.start();
p.expect(T![assert]);
p.expect(T!['{']);
match p.cur() {
T![assert] => {
p.expect(T![assert]);
}
T![with] => {
p.expect(T![with]);
}
_ => {
m.abandon(p);
return Absent;
}
};

// bump assert or with
p.expect(T!['{']);
ImportAssertionList::default().parse_list(p);

p.expect(T!['}']);
Expand Down
4 changes: 1 addition & 3 deletions crates/rome_js_parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use std::path::{Path, PathBuf};
#[test]
fn parser_smoke_test() {
let src = r#"
let
// comment
a;
import "x" with { type: "json" }
"#;

let module = parse(src, JsFileSource::tsx());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ JsModule {
value_token: JS_STRING_LITERAL@7..13 "\"foo\"" [] [Whitespace(" ")],
},
assertion: JsImportAssertion {
assert_token: ASSERT_KW@13..20 "assert" [] [Whitespace(" ")],
assertion_kind: ASSERT_KW@13..20 "assert" [] [Whitespace(" ")],
l_curly_token: L_CURLY@20..22 "{" [] [Whitespace(" ")],
assertions: JsImportAssertionEntryList [
JsImportAssertionEntry {
Expand Down Expand Up @@ -76,7 +76,7 @@ JsModule {
from_token: missing (required),
source: missing (required),
assertion: JsImportAssertion {
assert_token: ASSERT_KW@96..103 "assert" [] [Whitespace(" ")],
assertion_kind: ASSERT_KW@96..103 "assert" [] [Whitespace(" ")],
l_curly_token: L_CURLY@103..105 "{" [] [Whitespace(" ")],
assertions: JsImportAssertionEntryList [
JsImportAssertionEntry {
Expand Down Expand Up @@ -136,7 +136,7 @@ JsModule {
value_token: JS_STRING_LITERAL@177..188 "\"foo.json\"" [] [Whitespace(" ")],
},
assertion: JsImportAssertion {
assert_token: ASSERT_KW@188..195 "assert" [] [Whitespace(" ")],
assertion_kind: ASSERT_KW@188..195 "assert" [] [Whitespace(" ")],
l_curly_token: L_CURLY@195..197 "{" [] [Whitespace(" ")],
assertions: JsImportAssertionEntryList [
JsImportAssertionEntry {
Expand Down Expand Up @@ -173,7 +173,7 @@ JsModule {
value_token: JS_STRING_LITERAL@250..254 "\"x\"" [] [Whitespace(" ")],
},
assertion: JsImportAssertion {
assert_token: ASSERT_KW@254..260 "assert" [] [],
assertion_kind: ASSERT_KW@254..260 "assert" [] [],
l_curly_token: missing (required),
assertions: JsImportAssertionEntryList [],
r_curly_token: missing (required),
Expand All @@ -193,7 +193,7 @@ JsModule {
value_token: JS_STRING_LITERAL@280..293 "\"ipsum.json\"" [] [Whitespace(" ")],
},
assertion: JsImportAssertion {
assert_token: ASSERT_KW@293..300 "assert" [] [Whitespace(" ")],
assertion_kind: ASSERT_KW@293..300 "assert" [] [Whitespace(" ")],
l_curly_token: L_CURLY@300..302 "{" [] [Whitespace(" ")],
assertions: JsImportAssertionEntryList [
JsImportAssertionEntry {
Expand Down Expand Up @@ -253,7 +253,7 @@ JsModule {
value_token: JS_STRING_LITERAL@364..373 "\"a.json\"" [] [Whitespace(" ")],
},
assertion: JsImportAssertion {
assert_token: ASSERT_KW@373..379 "assert" [] [],
assertion_kind: ASSERT_KW@373..379 "assert" [] [],
l_curly_token: missing (required),
assertions: JsImportAssertionEntryList [],
r_curly_token: missing (required),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "foo" with { type, "json" };
import { foo } with { type: "json" };
import "lorem"
assert { type: "json" }
import foo2 from "foo.json" with { "type": "json", type: "html", "type": "js" };
import "x" with;
import ipsum from "ipsum.json" with { type: "json", lazy: true, startAtLine: 1 };
import { a } from "a.json" with
Loading

0 comments on commit e2fca94

Please sign in to comment.