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
17 changes: 5 additions & 12 deletions src/core/generate/rs/src/bindings/typescript_native/comments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::conversion_functions_generator::TopLevelNodes;
use swc_core::common::{
BytePos, DUMMY_SP, Span,
comments::{Comment, CommentKind},
comments::{Comment, CommentKind, Comments},
};

// Simple monotonic position source for synthetic spans
Expand All @@ -24,19 +24,13 @@ fn make_comment(docs: &[String]) -> Option<Comment> {
None
} else {
// Join all doc lines into a single block comment, with each line prefixed by a space
let mut comment_text = String::new();
comment_text.push_str("*\n");
let mut comment_text = String::from("*\n");
for line in docs {
comment_text.push(' ');
comment_text.push_str(&format!("* {}", line));
comment_text.push('\n');
}
// Remove trailing newline
if comment_text.ends_with('\n') {
comment_text.pop();
comment_text.push_str(&format!(" * {}\n", line));
}

comment_text.push('\n');
// Add a space at the end to align the block comment final line ("*/") properly
comment_text.push(' ');

Some(Comment {
span: DUMMY_SP,
Expand All @@ -47,7 +41,6 @@ fn make_comment(docs: &[String]) -> Option<Comment> {
}

pub fn add_comments(top_level_nodes: &mut TopLevelNodes, docs: &[String]) -> Span {
use swc_core::common::comments::Comments;
let (_, comments, cursor) = top_level_nodes;
match docs.len() {
0 => DUMMY_SP,
Expand Down
32 changes: 16 additions & 16 deletions tests/snapshots/generate/example/example.d.ts.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ export type my_variant = {
__kind__: "a";
/**
* Doc comment for my_variant field a
*/
*/
a: {
/**
* Doc comment for my_variant field a field b
*/
*/
b: string;
};
} | {
__kind__: "c";
/**
* Doc comment for my_variant field c
*/
*/
c: {
d: string;
e: Array<{
Expand All @@ -45,7 +45,7 @@ export interface nested {
_1_: bigint;
/**
* Doc comment for nested record
*/
*/
_2_: [bigint, bigint];
_3_: {
_0_: bigint;
Expand All @@ -60,18 +60,18 @@ export type res = {
__kind__: "Ok";
/**
* Doc comment for Ok variant
*/
*/
Ok: [bigint, bigint];
} | {
__kind__: "Err";
/**
* Doc comment for Err variant
*/
*/
Err: {
/**
* Doc comment for error field in Err variant,
* on multiple lines
*/
*/
error: string;
};
};
Expand All @@ -89,15 +89,15 @@ export type nested_res = {
__kind__: "Ok";
/**
* Doc comment for Ok in nested variant
*/
*/
Ok: {
content: string;
};
} | {
__kind__: "Err";
/**
* Doc comment for Err in nested variant
*/
*/
Err: [bigint];
};
};
Expand All @@ -116,7 +116,7 @@ export type tree = {
export interface nested_records {
/**
* Doc comment for nested_records field nested
*/
*/
nested?: {
nested_field: string;
};
Expand All @@ -128,11 +128,11 @@ export interface brokerInterface {
export type list = node | null;
/**
* Doc comment for prim type
*/
*/
export type my_type = Principal;
/**
* Doc comment for List
*/
*/
export type List = {
head: bigint;
tail: List;
Expand Down Expand Up @@ -170,11 +170,11 @@ export enum Variant_a_b {
}
/**
* Doc comment for service
*/
*/
export interface exampleInterface {
/**
* Doc comment for f1 method of service
*/
*/
f1(arg0: list, test: Uint8Array | number[], arg2: boolean | null): Promise<void>;
g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]>;
h(arg0: Array<string | null>, arg1: {
Expand All @@ -190,7 +190,7 @@ export interface exampleInterface {
}>;
/**
* Doc comment for i method of service
*/
*/
i: f;
x(arg0: a, arg1: b): Promise<[a | null, b | null, {
__kind__: "Ok";
Expand All @@ -206,7 +206,7 @@ export interface exampleInterface {
g(arg0: list): Promise<[B, tree, stream]>;
/**
* Doc comment for imported bbbbb service method
*/
*/
bbbbb(arg0: b): Promise<void>;
}
export type ProcessErrorFn = (error: unknown) => never;
Expand Down
32 changes: 16 additions & 16 deletions tests/snapshots/generate/example/example.ts.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ export type my_variant = {
__kind__: "a";
/**
* Doc comment for my_variant field a
*/
*/
a: {
/**
* Doc comment for my_variant field a field b
*/
*/
b: string;
};
} | {
__kind__: "c";
/**
* Doc comment for my_variant field c
*/
*/
c: {
d: string;
e: Array<{
Expand All @@ -79,7 +79,7 @@ export interface nested {
_1_: bigint;
/**
* Doc comment for nested record
*/
*/
_2_: [bigint, bigint];
_3_: {
_0_: bigint;
Expand All @@ -94,18 +94,18 @@ export type res = {
__kind__: "Ok";
/**
* Doc comment for Ok variant
*/
*/
Ok: [bigint, bigint];
} | {
__kind__: "Err";
/**
* Doc comment for Err variant
*/
*/
Err: {
/**
* Doc comment for error field in Err variant,
* on multiple lines
*/
*/
error: string;
};
};
Expand All @@ -123,15 +123,15 @@ export type nested_res = {
__kind__: "Ok";
/**
* Doc comment for Ok in nested variant
*/
*/
Ok: {
content: string;
};
} | {
__kind__: "Err";
/**
* Doc comment for Err in nested variant
*/
*/
Err: [bigint];
};
};
Expand All @@ -150,7 +150,7 @@ export type tree = {
export interface nested_records {
/**
* Doc comment for nested_records field nested
*/
*/
nested?: {
nested_field: string;
};
Expand All @@ -162,11 +162,11 @@ export interface brokerInterface {
export type list = node | null;
/**
* Doc comment for prim type
*/
*/
export type my_type = Principal;
/**
* Doc comment for List
*/
*/
export type List = {
head: bigint;
tail: List;
Expand Down Expand Up @@ -205,11 +205,11 @@ export enum Variant_a_b {
export type ProcessErrorFn = (error: unknown) => never;
/**
* Doc comment for service
*/
*/
export interface exampleInterface {
/**
* Doc comment for f1 method of service
*/
*/
f1(arg0: list, test: Uint8Array | number[], arg2: boolean | null): Promise<void>;
g1(arg0: my_type, arg1: List, arg2: List | null, arg3: nested): Promise<[bigint, Principal, nested_res]>;
h(arg0: Array<string | null>, arg1: {
Expand All @@ -225,7 +225,7 @@ export interface exampleInterface {
}>;
/**
* Doc comment for i method of service
*/
*/
i: f;
x(arg0: a, arg1: b): Promise<[a | null, b | null, {
__kind__: "Ok";
Expand All @@ -241,7 +241,7 @@ export interface exampleInterface {
g(arg0: list): Promise<[B, tree, stream]>;
/**
* Doc comment for imported bbbbb service method
*/
*/
bbbbb(arg0: b): Promise<void>;
}
import type { A as _A, B as _B, List as _List, a as _a, b as _b, list as _list, my_variant as _my_variant, nested as _nested, nested_records as _nested_records, nested_res as _nested_res, node as _node, res as _res, stream as _stream, tree as _tree } from "./declarations/example.did.d.ts";
Expand Down
Loading
Loading