Skip to content

Commit 557e423

Browse files
committed
feat: Add support for import and export assertions.
1 parent e4787fe commit 557e423

File tree

8 files changed

+60
-8
lines changed

8 files changed

+60
-8
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "dprint-plugin-typescript"
33
description = "TypeScript and JavaScript code formatter."
44
keywords = ["formatting", "formatter", "typescript", "javascript"]
5-
version = "0.40.0"
5+
version = "0.40.1"
66
authors = ["David Sherret <[email protected]>"]
77
edition = "2018"
88
license = "MIT"
@@ -28,10 +28,10 @@ dprint-core = { version = "0.35.1", features = ["formatting"] }
2828
fnv = "1.0.3"
2929
swc_common = "0.10.9"
3030
swc_ecmascript = { version = "0.20.0", features = ["parser"] }
31-
swc_ast_view = { version = "0.7.0", package = "dprint-swc-ecma-ast-view" }
31+
swc_ast_view = { version = "0.8.0", package = "dprint-swc-ecma-ast-view" }
3232
serde = { version = "1.0.118", features = ["derive"] }
3333
serde_json = { version = "1.0", optional = true }
3434

3535
[dev-dependencies]
36-
dprint-development = "0.2.2"
36+
dprint-development = "0.2.6"
3737
debug-here = "0.2"

src/parsing/parser.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,11 @@ fn parse_export_named_decl<'a>(node: &'a NamedExport, context: &mut Context<'a>)
867867
items.extend(parse_node(src.into(), context));
868868
}
869869

870+
if let Some(asserts) = node.asserts {
871+
items.push_str(" assert ");
872+
items.extend(parse_node(asserts.into(), context));
873+
}
874+
870875
if context.config.semi_colons.is_true() {
871876
items.push_str(";");
872877
}
@@ -1028,6 +1033,11 @@ fn parse_import_decl<'a>(node: &'a ImportDecl, context: &mut Context<'a>) -> Pri
10281033

10291034
items.extend(parse_node(node.src.into(), context));
10301035

1036+
if let Some(asserts) = node.asserts {
1037+
items.push_str(" assert ");
1038+
items.extend(parse_node(asserts.into(), context));
1039+
}
1040+
10311041
if context.config.semi_colons.is_true() {
10321042
items.push_str(";");
10331043
}
@@ -3209,6 +3219,11 @@ fn parse_export_all<'a>(node: &'a ExportAll, context: &mut Context<'a>) -> Print
32093219
items.push_str("export * from ");
32103220
items.extend(parse_node(node.src.into(), context));
32113221

3222+
if let Some(asserts) = node.asserts {
3223+
items.push_str(" assert ");
3224+
items.extend(parse_node(asserts.into(), context));
3225+
}
3226+
32123227
if context.config.semi_colons.is_true() {
32133228
items.push_str(";");
32143229
}

src/swc/parse_swc_ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn parse_inner(file_path: &Path, file_text: &str) -> Result<ParsedSourceFile, St
4747
ts_config.tsx = should_parse_as_jsx(file_path);
4848
ts_config.dynamic_import = true;
4949
ts_config.decorators = true;
50+
ts_config.import_assertions = true;
5051
let lexer = Lexer::new(
5152
Syntax::Typescript(ts_config),
5253
JscTarget::Es2019,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
~~ lineWidth: 50 ~~
2+
== should format ==
3+
export * from "test" ;
4+
5+
[expect]
6+
export * from "test";
7+
8+
== should format assertions ==
9+
export * from "a" assert {type: "json"};
10+
export * from "testingtesttest" assert { type: "json" };
11+
12+
[expect]
13+
export * from "a" assert { type: "json" };
14+
export * from "testingtesttest" assert {
15+
type: "json",
16+
};

tests/specs/declarations/export/ExportNamedDeclaration_All.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,18 @@ export * from "./a";
139139
export {} from "./a";
140140
export * from "./b";
141141
export {} from "./b";
142+
143+
== should format assertions ==
144+
export { } from "a" assert {a: "a"};
145+
export { asdf } from "test" assert { type: "json" };
146+
export { n1, n2, n3, n4 } from "test" assert { type: "json" };
147+
148+
[expect]
149+
export {} from "a" assert { a: "a" };
150+
export { asdf } from "test" assert { type: "json" };
151+
export {
152+
n1,
153+
n2,
154+
n3,
155+
n4,
156+
} from "test" assert { type: "json" };

tests/specs/declarations/import/ImportDeclaration_All.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,13 @@ import { a } from "test";
138138
import {
139139
b, // test
140140
} from "test";
141+
142+
== should support import assertions ==
143+
import test from "a" assert {type: "json"};
144+
import asdf from "testing.json" assert {type:"json"};
145+
146+
[expect]
147+
import test from "a" assert { type: "json" };
148+
import asdf from "testing.json" assert {
149+
type: "json",
150+
};

tests/specs/statements/exportAllDeclaration/ExportAllDeclaration_All.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)