Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 14, 2026

The add-examples-to-dts.ts script used ~150 lines of manual string parsing, regex matching, and array splicing to inject JSDoc comments into generated .d.ts files. This approach was fragile and difficult to maintain.

Changes

Replaced string manipulation with AST-based parsing:

  • Use ts-morph's Project and SourceFile APIs to parse TypeScript declarations
  • Traverse AST via getVariableStatements() and getTypeAliases() instead of regex
  • Manipulate JSDoc using getJsDocs(), getTags(), and addJsDoc() APIs

Extracted duplicated logic:

  • Created addExamplesToNode() helper to handle JSDoc manipulation for any JSDocableNode
  • Eliminates ~70 lines of duplicated code between variable statements and type aliases

Updated build tooling:

  • Added ts-morph for TypeScript AST manipulation
  • Added tsx to execute TypeScript files directly
  • Changed package.json script from node to tsx

Before/After

Before:

const match = /^export declare const (\w+):/.exec(line);
if (match) {
  // 100+ lines of array splicing to find and modify JSDoc
}

After:

for (const statement of sourceFile.getVariableStatements()) {
  if (statement.isExported()) {
    addExamplesToNode(statement, urlExamples);
  }
}

Output remains identical: 312 example URLs added to index.d.ts.

Original prompt

Replace custom code in add-examples-to-dts.ts with ts-morph. This should use proper parsing/generation from ts-morph rather than manually parse and inject strings


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Replace custom code in add-examples-to-dts.ts with ts-morph Replace manual string parsing in add-examples-to-dts.ts with ts-morph Jan 14, 2026
Copilot AI requested a review from fregante January 14, 2026 08:12
@fregante
Copy link
Member

@copilot remove distribution from gitignore and commit the files

Copy link
Contributor Author

Copilot AI commented Jan 14, 2026

@copilot remove distribution from gitignore and commit the files

Done in 62d0611. Removed distribution from .gitignore and committed all distribution files (index.js, index.d.ts, collector.d.ts, index.test.d.ts).

@fregante
Copy link
Member

@copilot Revert last commit

@fregante fregante marked this pull request as ready for review January 14, 2026 08:52
Copy link
Contributor Author

Copilot AI commented Jan 14, 2026

@copilot Revert last commit

Reverted in 127bf09. The .gitignore file now includes distribution again and the distribution files have been removed from the repository.

Copy link
Member

@fregante fregante left a comment

Choose a reason for hiding this comment

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

It one-shotted it, neat!

@fregante fregante merged commit 54f9b25 into main Jan 14, 2026
20 checks passed
@fregante fregante deleted the copilot/replace-custom-code-with-ts-morph branch January 14, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants