Skip to content

Commit

Permalink
Fix API generation inlining methods mechanism (#619)
Browse files Browse the repository at this point in the history
This PR fixes a problem we have when regenerating the API docs with
pages that already have the methods inlined. The `mergeClassMembers.ts`
script replaces all the content under the methods and attributes
sections for the inlined members, but if there are none, the sections
end up empty.

For example, in Qiskit v0.26, we can see how files, like
`docs/api/qiskit/0.26/qiskit.extensions.HamiltonianGate.md`, have the
methods and attributes sections with a table of contents of all its
elements, and after the sections, we find all methods/attributes mixed
up and ordered alphabetically.

The result of applying the script to files like that one was to have the
methods and attributes sections empty, and the actual members listed
below without any change.

```md
## Methods
## Attributes

{all methods and attributes alphabetically sorted}
```

This PR adds an if condition to check when we have new
methods/attributes to inline and when we need to keep the old content,
like the table of contents mentioned before. It also adds a new test to
verify that situation.
  • Loading branch information
arnaucasau authored Jan 11, 2024
1 parent 12691d7 commit 790e937
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
57 changes: 57 additions & 0 deletions scripts/lib/api/mergeClassMembers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,61 @@ Validate options.
`);
expect(merged.length).toEqual(1);
});

test("merge class with methods already inlined", async () => {
const results: Parameters<typeof mergeClassMembers>[0] = [
{
markdown: `## Attributes
| | |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
| [\`RuntimeOptions.backend\`](#qiskit_ibm_runtime.RuntimeOptions.backend "qiskit_ibm_runtime.RuntimeOptions.backend") | |
## Methods
| | |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
| [\`RuntimeOptions.validate\`](#qiskit_ibm_runtime.RuntimeOptions.validate "qiskit_ibm_runtime.RuntimeOptions.validate")(channel) | Validate options. |
\`property backend\`
Inlined attribute
\`validate()\`
Inlined method
`,
meta: {
apiType: "class",
apiName: "RuntimeOptions",
},
url: "/docs/api/qiskit-ibm-runtime/stubs/qiskit_ibm_runtime.RuntimeOptions",
images: [],
isReleaseNotes: false,
},
];
const merged = await mergeClassMembers(results);
expect(merged.find((item) => item.meta.apiType === "class")?.markdown)
.toEqual(`## Attributes
| | |
| ------------------------------------------------------------------------------------------------------------------ | - |
| [\`RuntimeOptions.backend\`](#qiskit_ibm_runtime.RuntimeOptions.backend "qiskit_ibm_runtime.RuntimeOptions.backend") | |
## Methods
| | |
| ------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
| [\`RuntimeOptions.validate\`](#qiskit_ibm_runtime.RuntimeOptions.validate "qiskit_ibm_runtime.RuntimeOptions.validate")(channel) | Validate options. |
\`property backend\`
Inlined attribute
\`validate()\`
Inlined method
`);
expect(merged.length).toEqual(1);
});
});
3 changes: 3 additions & 0 deletions scripts/lib/api/mergeClassMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ async function replaceMembersAfterTitle(
title: string,
members: HtmlToMdResultWithUrl[],
) {
// Keep old content when no members to inline
if (members.length == 0) return;

if (node.type !== "heading") return;
const nodeIndex = tree.children.indexOf(node);
if (nodeIndex === -1) return;
Expand Down

0 comments on commit 790e937

Please sign in to comment.