Skip to content

JSDoc declaration emit should reuse input nodes where possible when serializing typedefs #41760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
18 changes: 17 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5961,6 +5961,18 @@ namespace ts {
return setOriginalNode(typeToTypeNodeHelper(getTypeFromTypeNode(node), context), node);
}
if (isLiteralImportTypeNode(node)) {
const nodeSymbol = getNodeLinks(node).resolvedSymbol;
if (isInJSDoc(node) &&
nodeSymbol &&
(
// The import type resolved using jsdoc fallback logic
(!node.isTypeOf && !(nodeSymbol.flags & SymbolFlags.Type)) ||
// The import type had type arguments autofilled by js fallback logic
!(length(node.typeArguments) >= getMinTypeArgumentCount(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(nodeSymbol)))
)
) {
return setOriginalNode(typeToTypeNodeHelper(getTypeFromTypeNode(node), context), node);
}
return factory.updateImportTypeNode(
node,
factory.updateLiteralTypeNode(node.argument, rewriteModuleSpecifier(node, node.argument.literal)),
Expand Down Expand Up @@ -6536,8 +6548,12 @@ namespace ts {
const commentText = jsdocAliasDecl ? jsdocAliasDecl.comment || jsdocAliasDecl.parent.comment : undefined;
const oldFlags = context.flags;
context.flags |= NodeBuilderFlags.InTypeAlias;
const typeNode = jsdocAliasDecl && jsdocAliasDecl.typeExpression
&& isJSDocTypeExpression(jsdocAliasDecl.typeExpression)
&& serializeExistingTypeNode(context, jsdocAliasDecl.typeExpression.type, includePrivateSymbol, bundled)
|| typeToTypeNodeHelper(aliasType, context);
addResult(setSyntheticLeadingComments(
factory.createTypeAliasDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, getInternalSymbolName(symbol, symbolName), typeParamDecls, typeToTypeNodeHelper(aliasType, context)),
factory.createTypeAliasDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, getInternalSymbolName(symbol, symbolName), typeParamDecls, typeNode),
!commentText ? [] : [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "*\n * " + commentText.replace(/\n/g, "\n * ") + "\n ", pos: -1, end: -1, hasTrailingNewLine: true }]
), modifierFlags);
context.flags = oldFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ type Input = {
/**
* Imports
*/
type HookHandler = (arg: Context) => void;
type HookHandler = import("./hook").HookHandler;
/**
* State type definition
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export {testFn, testFnTypes};

//// [file.d.ts]
export namespace myTypes {
type typeA = string | RegExp | (string | RegExp)[];
type typeA = string | RegExp | Array<string | RegExp>;
type typeB = {
/**
* - Prop 1.
Expand All @@ -66,19 +66,14 @@ export namespace myTypes {
*/
prop2: string;
};
type typeC = Function | typeB;
type typeC = myTypes.typeB | Function;
const myTypes: {
[x: string]: any;
};
}
/**
* @namespace myTypes
* @global
* @type {Object<string,*>}
*/
export const myTypes: {
[x: string]: any;
};
//// [file2.d.ts]
export namespace testFnTypes {
type input = boolean | Function | myTypes.typeB;
type input = boolean | myTypes.typeC;
}
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const myTypes: {
[x: string]: any;
};
export namespace myTypes {
type typeA = string | RegExp | (string | RegExp)[];
type typeA = string | RegExp | Array<string | RegExp>;
type typeB = {
/**
* - Prop 1.
Expand All @@ -74,7 +74,7 @@ export namespace myTypes {
*/
prop2: string;
};
type typeC = Function | typeB;
type typeC = myTypes.typeB | Function;
}
//// [file2.d.ts]
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
Expand All @@ -94,6 +94,6 @@ export const testFnTypes: {
[x: string]: any;
};
export namespace testFnTypes {
type input = boolean | Function | myTypes.typeB;
type input = boolean | myTypes.typeC;
}
import { myTypes } from "./file.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
tests/cases/conformance/jsdoc/declarations/file.js(8,1): error TS9006: Declaration emit for this file requires using private name 'Base' from module '"tests/cases/conformance/jsdoc/declarations/base"'. An explicit type annotation may unblock declaration emit.


==== tests/cases/conformance/jsdoc/declarations/base.js (0 errors) ====
class Base {
constructor() {}
}

const BaseFactory = () => {
return new Base();
};

BaseFactory.Base = Base;

module.exports = BaseFactory;

==== tests/cases/conformance/jsdoc/declarations/file.js (1 errors) ====
/** @typedef {import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
~~~~~
!!! error TS9006: Declaration emit for this file requires using private name 'Base' from module '"tests/cases/conformance/jsdoc/declarations/base"'. An explicit type annotation may unblock declaration emit.
return base;
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsParameterTagReusesInputNodeInEmit1.ts] ////

//// [base.js]
class Base {
constructor() {}
}

const BaseFactory = () => {
return new Base();
};

BaseFactory.Base = Base;

module.exports = BaseFactory;

//// [file.js]
/** @typedef {import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
return base;
};


//// [base.js]
class Base {
constructor() { }
}
const BaseFactory = () => {
return new Base();
};
BaseFactory.Base = Base;
module.exports = BaseFactory;
//// [file.js]
/** @typedef {import('./base')} BaseFactory */
/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
return base;
};


//// [base.d.ts]
export = BaseFactory;
declare function BaseFactory(): Base;
declare namespace BaseFactory {
export { Base };
}
declare class Base {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/conformance/jsdoc/declarations/base.js ===
class Base {
>Base : Symbol(Base, Decl(base.js, 0, 0))

constructor() {}
}

const BaseFactory = () => {
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))

return new Base();
>Base : Symbol(Base, Decl(base.js, 0, 0))

};

BaseFactory.Base = Base;
>BaseFactory.Base : Symbol(BaseFactory.Base, Decl(base.js, 6, 2))
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))
>Base : Symbol(BaseFactory.Base, Decl(base.js, 6, 2))
>Base : Symbol(Base, Decl(base.js, 0, 0))

module.exports = BaseFactory;
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/base", Decl(base.js, 0, 0))
>module : Symbol(export=, Decl(base.js, 8, 24))
>exports : Symbol(export=, Decl(base.js, 8, 24))
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))

=== tests/cases/conformance/jsdoc/declarations/file.js ===
/** @typedef {import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
>test : Symbol(test, Decl(file.js, 7, 5))
>base : Symbol(base, Decl(file.js, 7, 14))

return base;
>base : Symbol(base, Decl(file.js, 7, 14))

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/conformance/jsdoc/declarations/base.js ===
class Base {
>Base : Base

constructor() {}
}

const BaseFactory = () => {
>BaseFactory : { (): Base; Base: typeof Base; }
>() => { return new Base();} : { (): Base; Base: typeof Base; }

return new Base();
>new Base() : Base
>Base : typeof Base

};

BaseFactory.Base = Base;
>BaseFactory.Base = Base : typeof Base
>BaseFactory.Base : typeof Base
>BaseFactory : { (): Base; Base: typeof Base; }
>Base : typeof Base
>Base : typeof Base

module.exports = BaseFactory;
>module.exports = BaseFactory : { (): Base; Base: typeof Base; }
>module.exports : { (): Base; Base: typeof Base; }
>module : { "\"tests/cases/conformance/jsdoc/declarations/base\"": { (): Base; Base: typeof Base; }; }
>exports : { (): Base; Base: typeof Base; }
>BaseFactory : { (): Base; Base: typeof Base; }

=== tests/cases/conformance/jsdoc/declarations/file.js ===
/** @typedef {import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
>test : (base: InstanceType<BaseFactory["Base"]>) => Base
>(base) => { return base;} : (base: InstanceType<BaseFactory["Base"]>) => Base
>base : Base

return base;
>base : Base

};

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsParameterTagReusesInputNodeInEmit2.ts] ////

//// [base.js]
class Base {
constructor() {}
}

const BaseFactory = () => {
return new Base();
};

BaseFactory.Base = Base;

module.exports = BaseFactory;

//// [file.js]
/** @typedef {typeof import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
return base;
};


//// [base.js]
class Base {
constructor() { }
}
const BaseFactory = () => {
return new Base();
};
BaseFactory.Base = Base;
module.exports = BaseFactory;
//// [file.js]
/** @typedef {typeof import('./base')} BaseFactory */
/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
return base;
};


//// [base.d.ts]
export = BaseFactory;
declare function BaseFactory(): Base;
declare namespace BaseFactory {
export { Base };
}
declare class Base {
}
//// [file.d.ts]
/** @typedef {typeof import('./base')} BaseFactory */
/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
declare function test(base: InstanceType<BaseFactory["Base"]>): InstanceType<BaseFactory["Base"]>;
type BaseFactory = typeof import('./base');
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== tests/cases/conformance/jsdoc/declarations/base.js ===
class Base {
>Base : Symbol(Base, Decl(base.js, 0, 0))

constructor() {}
}

const BaseFactory = () => {
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))

return new Base();
>Base : Symbol(Base, Decl(base.js, 0, 0))

};

BaseFactory.Base = Base;
>BaseFactory.Base : Symbol(BaseFactory.Base, Decl(base.js, 6, 2))
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))
>Base : Symbol(BaseFactory.Base, Decl(base.js, 6, 2))
>Base : Symbol(Base, Decl(base.js, 0, 0))

module.exports = BaseFactory;
>module.exports : Symbol("tests/cases/conformance/jsdoc/declarations/base", Decl(base.js, 0, 0))
>module : Symbol(export=, Decl(base.js, 8, 24))
>exports : Symbol(export=, Decl(base.js, 8, 24))
>BaseFactory : Symbol(BaseFactory, Decl(base.js, 4, 5), Decl(base.js, 6, 2))

=== tests/cases/conformance/jsdoc/declarations/file.js ===
/** @typedef {typeof import('./base')} BaseFactory */

/**
*
* @param {InstanceType<BaseFactory["Base"]>} base
* @returns {InstanceType<BaseFactory["Base"]>}
*/
const test = (base) => {
>test : Symbol(test, Decl(file.js, 7, 5))
>base : Symbol(base, Decl(file.js, 7, 14))

return base;
>base : Symbol(base, Decl(file.js, 7, 14))

};

Loading