Skip to content
Draft
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
3 changes: 3 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,9 @@ func (p *Printer) shouldEmitComments(node *ast.Node) bool {
}

func (p *Printer) shouldWriteComment(comment ast.CommentRange) bool {
if p.Options.OnlyPrintJSDocStyle && p.currentSourceFile != nil && isConvertedJSDocDeclarationComment(p.currentSourceFile.Text(), comment) {
return false
}
return !p.Options.OnlyPrintJSDocStyle ||
p.currentSourceFile != nil && isJSDocLikeText(p.currentSourceFile.Text(), comment) ||
p.currentSourceFile != nil && IsPinnedComment(p.currentSourceFile.Text(), comment)
Expand Down
19 changes: 19 additions & 0 deletions internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,25 @@ func isJSDocLikeText(text string, comment ast.CommentRange) bool {
text[comment.Pos()+3] != '/'
}

func isConvertedJSDocDeclarationComment(text string, comment ast.CommentRange) bool {
if !isJSDocLikeText(text, comment) {
return false
}
for line := range strings.Lines(text[comment.Pos():comment.End()]) {
line = strings.TrimSpace(line)
line = strings.TrimSpace(strings.TrimPrefix(line, "*"))
if isJSDocTagLine(line, "typedef") || isJSDocTagLine(line, "callback") {
return true
}
}
return false
}

func isJSDocTagLine(line string, tag string) bool {
tag = "@" + tag
return strings.HasPrefix(line, tag) && (len(line) == len(tag) || line[len(tag)] == ' ' || line[len(tag)] == '\t')
}

func IsPinnedComment(text string, comment ast.CommentRange) bool {
return comment.Kind == ast.KindMultiLineCommentTrivia &&
comment.Len() > 5 &&
Expand Down
5 changes: 4 additions & 1 deletion internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,7 @@ func (tx *DeclarationTransformer) preservePartialJsDoc(updated *ast.Node, origin
if description == "" {
return
}
tx.EmitContext().SetCommentRange(updated, core.NewTextRange(original.End(), original.End()))
comment := "*\n * " + strings.ReplaceAll(description, "\n", "\n * ") + "\n "
tx.EmitContext().AddSyntheticLeadingComment(updated, ast.KindMultiLineCommentTrivia, comment, true /*hasTrailingNewLine*/)
}
Expand Down Expand Up @@ -1665,13 +1666,15 @@ func (tx *DeclarationTransformer) transformTopLevelDeclaration(input *ast.Node)

func (tx *DeclarationTransformer) transformTypeAliasDeclaration(input *ast.TypeAliasDeclaration) *ast.Node {
tx.needsDeclare = false
return tx.Factory().UpdateTypeAliasDeclaration(
result := tx.Factory().UpdateTypeAliasDeclaration(
input,
tx.ensureModifiers(input.AsNode()),
input.Name(),
tx.Visitor().VisitNodes(input.TypeParameters),
tx.Visitor().Visit(input.Type),
)
tx.preservePartialJsDoc(result, input.AsNode())
return result
}

func (tx *DeclarationTransformer) transformInterfaceDeclaration(input *ast.InterfaceDeclaration) *ast.Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ let v1 = { x: "test" };
export type Foo = {
x: string;
};
/**
* @typedef {{x: string}} Foo
*/
declare const _default: {
a: number;
b: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ let v1 = { x: "test" };
export type Foo = {
x: string;
};
/**
* @typedef {{x: string}} Foo
*/
export declare const x = 1;
//// [b.d.ts]
export {};
5 changes: 0 additions & 5 deletions testdata/baselines/reference/compiler/jsDocCallbackExport1.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ function f1() {}

//// [x.d.ts]
type Foo = (x: string) => number;
/**
* @callback Foo
* @param {string} x
* @returns {number}
*/
declare function f1(): void;
5 changes: 0 additions & 5 deletions testdata/baselines/reference/compiler/jsDocCallbackExport2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ export function f1() {}

//// [x.d.ts]
export type Foo = (x: string) => number;
/**
* @callback Foo
* @param {string} x
* @returns {number}
*/
export declare function f1(): void;
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ export function Button(props) {


//// [index.d.ts]
/**
* @typedef {Object} ButtonProps
* @property {string} label The button label
* @property {string | null | undefined} [data-test-name] Test automation attribute
* @property {string | null | undefined} [aria-label] Accessibility label
*/
export type ButtonProps = {
/**
* The button label
Expand All @@ -52,8 +46,3 @@ export type ButtonProps = {
*/
export declare function Button(props: ButtonProps): ButtonProps;
export type ButtonPropsCallback = (props_like?: ButtonProps) => ButtonProps;
/**
* @callback ButtonPropsCallback
* @param {ButtonProps} [props-like]
* @returns {ButtonProps}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/jsdocTypedefDeclarationComment.ts] ////

//// [main.js]
export const value = 0;

/**
* Comment on the `Foo` type
*
* @typedef {Object} Foo
* @property {boolean} bool Whether `.bool` is true or not
*/




//// [main.d.ts]
export declare const value = 0;
/**
* Comment on the `Foo` type
*/
export type Foo = {
/**
* Whether `.bool` is true or not
*/
bool: boolean;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/jsdocTypedefDeclarationComment.ts] ////

=== main.js ===
export const value = 0;
>value : Symbol(value, Decl(main.js, 0, 12))

/**
* Comment on the `Foo` type
*
* @typedef {Object} Foo
* @property {boolean} bool Whether `.bool` is true or not
*/

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [tests/cases/compiler/jsdocTypedefDeclarationComment.ts] ////

=== main.js ===
export const value = 0;
>value : 0
>0 : 0

/**
* Comment on the `Foo` type
*
* @typedef {Object} Foo
* @property {boolean} bool Whether `.bool` is true or not
*/

Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export function fnWithPartialAnnotationOnDefaultparam(x = /** @type {P} */(somet


//// [input.d.ts]
/**
* @typedef {{ } & { name?: string }} P
*/
export type P = {} & {
name?: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export function fnWithPartialAnnotationOnDefaultparam(x = /** @type {P} */(somet


//// [input.d.ts]
/**
* @typedef {{ } & { name?: string }} P
*/
export type P = {} & {
name?: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ import Test from './test/Test.js';
export type Options = {
test?: typeof import("./Test.js").default;
};
/**
* @typedef {Object} Options
* @property {typeof import("./Test.js").default} [test]
*/
declare class X extends Test {
test: import("./Test.js").default | undefined;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ module.exports = function loader(options) { };


//// [index.d.ts]
/**
* @typedef Options
* @property {string} opt
*/
export type Options = {
opt: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ class C3 extends C1 {


//// [a.d.ts]
/**
* @typedef A
* @property {string} a
*/
type A = {
a: string;
};
type B = {
b: number;
};
/**
* @typedef B
* @property {number} b
*/
declare class C1 {
/**
* @type {A}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ NewAjax.prototype.case6_unexpectedlyResolvesPathToNodeModules;


//// [index.d.ts]
/**
* @typedef {import('@lion/ajax').LionRequestInit} LionRequestInit
*/
export type LionRequestInit = import('@lion/ajax').LionRequestInit;
export declare class NewAjax {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,5 @@ declare const example2: {
};
type callback = (error: any, result: any) => any;
declare const example3: {
/**
* @overload evaluate(options = {}, [callback])
* Evaluate something
* @note Something interesting
* @param options [map]
* @return [string] returns evaluation result
* @return [null] returns nothing if callback provided
* @callback callback function (error, result)
* If callback is provided it will be called with evaluation result
* @param error [Error]
* @param result [String]
* @see callback
*/
Comment on lines -141 to -153

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this diff should not go away?

evaluate: (options: any, callback: any) => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export const blah = handleParamGovernance({});
//// [types.d.ts]
export {};
export type ParamStateRecord = Record<Keyword, ParamValueTyped>;
/**
* @typedef {Record<Keyword, ParamValueTyped>} ParamStateRecord a Record containing
* keyword pairs with descriptions of parameters under governance.
*/
Comment on lines -38 to -41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I am a bit confused..

Which actually counts as the comment for the generated type? The one on top of typedef, or the one follows right after the jsdoc declaration? Or both? Any precedence?

It worths to check how TS 6 behaves in all these cases. 🤔

//// [index.d.ts]
export declare const blah: {
publicMixin: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,5 @@ var ooscope2 = s => s.length > 0;
export type ValueGetter_2 = (name: string) => boolean | number | string | undefined;
export declare class Preferences {
assignability: string;
/**
* @callback ValueGetter_2
* @param {string} name
* @returns {boolean|number|string|undefined}
*/
constructor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ function eachPerson(callback) {


//// [cb_nested.d.ts]
/**
* @callback WorksWithPeopleCallback
* @param {Object} person
* @param {string} person.name
* @param {number} [person.age]
* @returns {void}
*/
type WorksWithPeopleCallback = (person: {
name: string;
age?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ var res = x('a', 'b');


//// [callbackTagVariadicType.d.ts]
/**
* @callback Foo
* @param {...string} args
* @returns {number}
*/
export type Foo = (...args: string) => number;
/** @type {Foo} */
export declare const x: Foo;
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,3 @@ export declare class Encoder<T> implements IEncoder<T> {
encode(value: T): Uint8Array<ArrayBuffer>;
}
export type IEncoder<T> = import('./interface').Encoder<T>;
/**
* @template T
* @typedef {import('./interface').Encoder<T>} IEncoder
*/
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,3 @@ export type HandlerOptions = {
*/
name: string;
};
/**
* @typedef {Object} HandlerOptions
* @property {String} name
* Should be able to export a type alias at the same time.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ export default Bar;
declare const _default = 12;
export default _default;
export type default = string | number;
/**
* @typedef {string | number} default
*/
//// [index6.d.ts]
export default function func(): void;
export type default = string | number;
/**
* @typedef {string | number} default
*/
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,11 @@ declare class Cls {
}
export default Cls;
export type default = string | number;
/**
* @typedef {string | number} default
*/
//// [index2.d.ts]
export default class C {
}
export type default = string | number;
/**
* @typedef {string | number} default
*/
//// [index3.d.ts]
declare const x = 12;
export { x as default };
export type default = string | number;
/**
* @typedef {string | number} default
*/
Loading