Skip to content

Fix insertBefore and insertAfter on exported declarations - #748

Merged
Daniel15 merged 1 commit into
meta:mainfrom
i098:fix/jscodeshift-insert-667
Oct 13, 2026
Merged

Daniel15 merged 1 commit into
meta:mainfrom
i098:fix/jscodeshift-insert-667

Conversation

@i098

@i098 i098 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Problem

insertBefore() and insertAfter() throw when the selected path is the declaration of an export statement, for example export function bar() {} or export interface Foo {}:

const j = require('jscodeshift').withParser('ts');
const root = j('export interface Foo { a: string }\nexport function bar() {}\n');
root.find(j.TSInterfaceDeclaration).insertAfter(j.expressionStatement(j.identifier('X')));
Error: { type: ExportNamedDeclaration, ... } does not match type array

The declaration of ExportNamedDeclaration and ExportDefaultDeclaration is a single child, not a list item, so path.insertBefore / path.insertAfter from ast-types cannot insert next to it. The same happens for exported classes, variables, and type aliases.

Fix

When the path is the declaration of an ExportNamedDeclaration or ExportDefaultDeclaration, the new nodes are inserted next to the export statement instead. All other paths behave as before.

Verification

Same script after the change:

root.find(j.TSInterfaceDeclaration).insertAfter(j.expressionStatement(j.identifier('X')));
root.find(j.FunctionDeclaration).insertBefore(j.expressionStatement(j.identifier('Y')));
console.log(root.toSource());
export interface Foo { a: string }
X;
Y;
export function bar() {}

Two new tests (named and default export, for insertBefore and insertAfter) fail on main with the error above and pass with this change. Full suite:

$ yarn test
Test Suites: 19 passed, 19 total
Tests:       1 skipped, 221 passed, 222 total

A patch changeset is included.

Fixes #667

I am new to open source, and this pull request was made with AI assistance.

The declaration of an export statement is a single child of the export
node, not a list item, so path.insertBefore and path.insertAfter threw
"does not match type array". Insert next to the export statement instead.

Fixes meta#667.
@vercel

vercel Bot commented Oct 12, 2026

Copy link
Copy Markdown

@i098 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 37935a9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jscodeshift Patch

Not sure what this means? Click here to learn what changesets are.

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

@meta-cla meta-cla Bot added the CLA Signed label Oct 12, 2026
@Daniel15
Daniel15 merged commit 938bc3c into meta:main Oct 13, 2026
1 of 2 checks passed
@Daniel15

Copy link
Copy Markdown
Member

Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot call insertAfter or insertBefore on any exported declaration

2 participants