Skip to content
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

docs: deprecate factory function type declarations #293

Closed
wants to merge 2 commits into from
Closed
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
135 changes: 27 additions & 108 deletions build-utils/declarationTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,126 +9,20 @@ function visitor(factory, node, options) {
!isClassIncluded(node.name.escapedText, options)) {
return node;
}
// if (isClassDerived(node)) {
// // workaround for https://github.com/microsoft/TypeScript/issues/46503
// return splitClassIntoConstAndInterface(factory, node);
// }
return createNodeWithFactories(factory, node);
}

function isClassIncluded(className, options) {
return options.classes.includes(className);
}

function isClassDerived(node) {
return node.heritageClauses[0] !== undefined;
}

function splitClassIntoConstAndInterface(factory, node) {
return [
createConstNodeWithStaticMethods(factory, node),
createInterfaceWithMethods(factory, node)
];
}

function createConstNodeWithStaticMethods(factory, node) {
return factory.createVariableStatement(
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
factory.createVariableDeclarationList(
[factory.createVariableDeclaration(
node.name,
undefined,
factory.createTypeLiteralNode([
// constructors
...node.members
.filter(ts.isConstructorDeclaration)
.flatMap(member => [
factory.createConstructSignature(
undefined,
member.parameters,
factory.createTypeReferenceNode(
node.name,
undefined
)
),
factory.createCallSignature(
undefined,
member.parameters,
factory.createTypeReferenceNode(
node.name,
undefined
)
)
].map(node => copyComments(node, member))
),
// static properties
...node.members
.filter(member => hasModifier(ts.SyntaxKind.StaticKeyword, member))
.filter(member => !hasModifier(ts.SyntaxKind.PrivateKeyword, member))
.map(member => declarationToSignature(factory, member))
]
),
undefined
)],
ts.NodeFlags.Const | ts.NodeFlags.Ambient | ts.NodeFlags.ContextFlags
)
);
}

function createInterfaceWithMethods(factory, node) {
const result = factory.createInterfaceDeclaration(
undefined,
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
node.name,
undefined,
node.heritageClauses,
// instance properties
node.members
.filter(member => !ts.isConstructorDeclaration(member))
.filter(member => !hasModifier(ts.SyntaxKind.StaticKeyword, member))
.filter(member => !hasModifier(ts.SyntaxKind.PrivateKeyword, member))
.map(member => declarationToSignature(factory, member))
);
return copyComments(result, node);
}

function hasModifier(kind, node) {
return node.modifiers?.some?.(modifier => modifier.kind === kind);
}

function declarationToSignature(factory, node) {
let result;
switch (node.kind) {
case ts.SyntaxKind.PropertyDeclaration:
result = factory.createPropertySignature(
undefined,
node.name,
undefined,
node.type
);
break;
case ts.SyntaxKind.MethodDeclaration:
result = factory.createMethodSignature(
undefined,
node.name,
undefined,
undefined,
node.parameters,
node.type
);
break;
default:
throw new Error(`Could not convert node of kind ${node.kind} to signature.`);
}
return copyComments(result, node);
}

function createNodeWithFactories(factory, node) {
return [
...node.members
.filter(ts.isConstructorDeclaration)
.map(member => member.parameters)
.map(parameters => createFunctionDeclaration(factory, node, parameters)),
.map(parameters => createFunctionDeclaration(factory, node, parameters))
.map(node => addFactoryDeprecationToComment(node)),
node
];
}
Expand All @@ -155,3 +49,28 @@ function copyComments(node, original) {
ts.setSyntheticTrailingComments(node, ts.getSyntheticTrailingComments(original));
return node;
}

const FACTORY_DEPRECATION_SUFFIX = `*
* @deprecated Use the 'new' keyword.
`;

const EMPTY_COMMENTS_FALLBACK = [{
kind: 3,
pos: -1,
end: -1,
hasTrailingNewLine: true,
text: '',
}];

function addFactoryDeprecationToComment(node) {
const comments = ts.getSyntheticLeadingComments(node) ?? EMPTY_COMMENTS_FALLBACK;

const commentsWithDeprecation = comments
.map(comment => ({
...comment,
text: comment.text + FACTORY_DEPRECATION_SUFFIX,
}));

ts.setSyntheticLeadingComments(node, commentsWithDeprecation);
return node;
}
Loading