Skip to content

build(prune-dts): migrate to ts-morph - #10220

Open
dlarocque wants to merge 6 commits into
mainfrom
dl/prunedts-tsmorph
Open

build(prune-dts): migrate to ts-morph#10220
dlarocque wants to merge 6 commits into
mainfrom
dl/prunedts-tsmorph

Conversation

@dlarocque

@dlarocque dlarocque commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Migrates the prune-dts build script from the raw TypeScript Compiler API to ts-morph v28.0.0.

The current script has subtle AST bugs, is difficult to maintain, and significantly increases the complexity of TypeScript version upgrades due to breaking changes in compiler internals.

The new ts-morph script resolves known bugs, improves maintainability via a modular architecture and a stable semver library, and unblocks future TypeScript upgrades. Going forward, upgrading TypeScript will only require addressing new compiler type errors rather than debugging and fixing AST manipulation logic.

Implements go/prunedts-maintenance-strategy.

Most of the lines changed are .d.ts test snapshots and formatting changes in reference docs. The real diff is ~2k lines added, ~800 deleted`.

Changes

  1. Script migration: Replaced the prune-dts.ts script with a modular script that executes single-responsibility AST manipulation rules in src/rules, with a top-level src/pipeline.ts.
  2. Updated test snapshots: Replaced legacy test snapshots (.d.ts files generated from real builds) with new snapshots generated from this branch.
  3. New unit tests: Added unit and regression tests, now totaling 108 passing tests.
  4. Formatter & linter config: Configured Prettier, ESLint, and the repo license script to ignore .d.ts test snapshots.

Notes

  1. Performance Regression: The legacy script was faster because it performed direct regex and compiler API mutations. ts-morph has known AST wrapper overhead, so script execution runtime scales with .d.ts file size. This build-step overhead is a worthwhile trade-off for long-term maintainability and easier TypeScript upgrades.

Semantic Diffs

1.(Bug Fix) CollectionStageOptions: Changed Query -> CollectionReference in firestore-pipelines. This change is a result of a bug fix. The legacy script replaced CollectionReference with its base class Query. Since CollectionReference is a public API, this was a bug that rendered the previous type non-functional. The new script fixes the bug, and the type is now functional.
2. Additional unused imports pruned: The legacy script retained 5 unused imports not referenced in the public API. The new script removes them.

Validation

It is critical that this migration results in no semantic changes in the released .d.ts files. One way to do this is to run a build on main, and on this branch and comb through the diffs. Unfortunately, there is a lot of noise from formatting changes (lines over length 80 have been folded, blank line changes, etc). To make it easier to review, this PR shows the .d.ts file diffs after a common formatter config has ran over the files: dlarocque/dts-diffs#2 - note that there is still some formatting noise.

@changeset-bot

changeset-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f246f43

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@wiz-9635d3485b

wiz-9635d3485b Bot commented Jul 28, 2026

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities 4 High 2 Medium
Data Finding Sensitive Data -
Secret Finding Secrets -
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings 7 Medium 4 Low
Software Management Finding Software Management Findings -
Total 4 High 9 Medium 4 Low

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the prune-dts script by migrating from the TypeScript Compiler API to ts-morph and modularizing the pruning logic into individual rules. It also updates the test suite, adds Prettier formatting to the output, and introduces a .d.ts comparison script. The review feedback is highly constructive, pointing out critical bugs where modifying heritage clauses during iteration causes ForgottenNodeExceptions, and identifying an O(N^2) performance bottleneck in type substitution that can be resolved by pre-computing subclass mappings. Additionally, the feedback suggests improving JSDoc cleaning to preserve formatting asterisks, grouping duplicate imports in cross-file deduplication, and adding a git cleanliness check to the comparison script.

Comment thread repo-scripts/prune-dts/src/rules/inheritance-flattening.ts
Comment thread repo-scripts/prune-dts/src/rules/inheritance-flattening.ts
Comment thread repo-scripts/prune-dts/src/rules/type-substitution.ts Outdated
Comment thread repo-scripts/prune-dts/compare-pruned-dts.sh Outdated
Comment thread repo-scripts/prune-dts/src/rules/constructor-visibility.ts
Comment thread repo-scripts/prune-dts/src/rules/cross-file-dedup.ts Outdated
@dlarocque
dlarocque force-pushed the dl/prunedts-tsmorph branch from ca520b3 to 8e4700a Compare July 28, 2026 17:50
@dlarocque

Copy link
Copy Markdown
Contributor Author

/gemini review

@dlarocque
dlarocque marked this pull request as ready for review July 28, 2026 19:25
@dlarocque
dlarocque requested review from a team as code owners July 28, 2026 19:25

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the prune-dts script, migrating it from the TypeScript Compiler API to a modular, rule-based pipeline using ts-morph. It introduces dedicated AST transformation rules for constructor visibility, cross-file deduplication, inheritance flattening, private member stripping, and top-level declaration filtering. Additionally, it integrates Prettier formatting for the generated .d.ts files and expands the test suite with unit and package regression tests. The review feedback highlights two important improvements: targeting only the type name node during type substitution to prevent overlapping replacements with nested generic types, and normalizing Windows path separators to forward slashes in cross-file deduplication imports to avoid compilation errors on Windows platforms.

Comment thread repo-scripts/prune-dts/src/rules/type-substitution.ts
Comment thread repo-scripts/prune-dts/src/rules/cross-file-dedup.ts
@@ -21,9 +21,12 @@ import * as path from 'path';
import { Extractor, ExtractorConfig } from 'api-extractor-me';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small (large) request: Would you be able to replace this (including replacing the dependency in package.json) with the latest version of @microsoft/api-extractor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants