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

Adding call signature customization support. #27653

Merged
merged 1 commit into from
Nov 6, 2023
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license

import { PropertySignature } from "ts-morph";
import { CallSignatureDeclaration, PropertySignature } from "ts-morph";
import { Declaration } from "../common";

export type Annotation = "Remove";
export function getAnnotation(
declaration: Declaration | PropertySignature
declaration: Declaration | PropertySignature | CallSignatureDeclaration
): Annotation | undefined {
// Check if the property has a `// @azsdk-remove` comment
const leadingCommentRanges = declaration.getLeadingCommentRanges();
Expand All @@ -21,8 +21,7 @@ export function getAnnotation(
if (annotation === "@azsdk-remove") {
return "Remove";
}

return undefined;
}
}
return undefined;
}
76 changes: 75 additions & 1 deletion common/tools/dev-tool/src/util/customization/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license

import { InterfaceDeclaration, SourceFile } from "ts-morph";
import { CallSignatureDeclaration, InterfaceDeclaration, SourceFile, Type } from "ts-morph";
import { getAnnotation } from "./helpers/annotations";

export function augmentInterfaces(
Expand Down Expand Up @@ -31,6 +31,12 @@ export function augmentInterface(

// Merge the properties from the custom interface into the original interface
mergeProperties(customInterface, originalInterface);

// Remove any call signatures marked with // @azsdk-remove
removeCallSignatures(customInterface, originalInterface);

// Merge the call signatures from the custom interface into the original interface
mergeCallSignatures(customInterface, originalInterface);
}

export function mergeProperties(
Expand All @@ -39,6 +45,10 @@ export function mergeProperties(
) {
const customProperties = customInterface.getProperties();
for (const customProperty of customProperties) {
if (getAnnotation(customProperty) === "Remove") {
/* If the property has a `// @azsdk-remove` comment, we don't need to re-add it */
continue;
}
const propertyName = customProperty.getName();
const originalProperty = originalInterface.getProperty(propertyName);

Expand Down Expand Up @@ -68,3 +78,67 @@ export function removeProperties(
}
}
}

function findCallSignature(
interfaceDeclaration: InterfaceDeclaration,
callSignature: CallSignatureDeclaration
): CallSignatureDeclaration | undefined {
function typeEquals(a: Type, b: Type) {
// Need to handle cases where the type is imported
const aStr = a?.getText()?.replace(/import\(\".+\"\)\./, "");
const bStr = b?.getText()?.replace(/import\(\".+\"\)\./, "");
return aStr && bStr && aStr === bStr;
}
return interfaceDeclaration.getCallSignature((signature) => {
if (signature.getParameters().length !== callSignature.getParameters().length) {
return false;
}
signature.getReturnTypeNode;
Copy link
Member

Choose a reason for hiding this comment

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

What is the purpose of this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed, it was just a leftover from a refactor.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, did you open a new PR? This one is already merged without this change.

if (!typeEquals(signature.getReturnType(), callSignature.getReturnType())) {
return false;
}

for (let i = 0; i < signature.getParameters().length; i++) {
if (
!typeEquals(
signature.getParameters()[i].getType(),
callSignature.getParameters()[i].getType()
)
) {
return false;
}
}
return true;
});
}

export function mergeCallSignatures(
customInterface: InterfaceDeclaration,
originalInterface: InterfaceDeclaration
) {
const customCallSignatures = customInterface.getCallSignatures();
for (const customCallSignature of customCallSignatures) {
if (getAnnotation(customCallSignature) === "Remove") {
/* If the call signature has a `// @azsdk-remove` comment, we don't need to re-add it */
continue;
}
const originalCallSignature = findCallSignature(originalInterface, customCallSignature);
if (originalCallSignature) {
originalCallSignature.remove();
}
originalInterface.addCallSignature(customCallSignature.getStructure());
}
}

export function removeCallSignatures(
customInterface: InterfaceDeclaration,
originalInterface: InterfaceDeclaration
) {
const customCallSignatures = customInterface.getCallSignatures();
for (const customCallSignature of customCallSignatures) {
// Check if the signature has a `// @azsdk-remove` comment
if (getAnnotation(customCallSignature) === "Remove") {
findCallSignature(originalInterface, customCallSignature)?.remove();
}
}
}