Skip to content

Fix insertImportDeclaration doing nothing on the root collection - #768

Open
olucasleitedev wants to merge 1 commit into
meta:mainfrom
olucasleitedev:fix/insert-import-declaration-file-root
Open

olucasleitedev wants to merge 1 commit into
meta:mainfrom
olucasleitedev:fix/insert-import-declaration-file-root

Conversation

@olucasleitedev

Copy link
Copy Markdown

Problem

insertImportDeclaration() silently does nothing when called on the collection returned by j(source), which is how it is normally used:

const j = require('jscodeshift');
const root = j('import a from "a";\nconst x = 1;\n');
root.insertImportDeclaration('b', [j.importDefaultSpecifier(j.identifier('B'))]);
console.log(root.toSource());

Output (unchanged):

import a from "a";
const x = 1;

The method only inserts into Program nodes, but the root collection holds the File node. The existing tests did not catch this because they build the collection from recast.parse(...).program.

Fix

When a path is a File, the import is inserted into its program. Collections of Program nodes behave as before. The duplicate check (hasImportDeclaration) already searches descendants, so it works for both.

Verification

Same script after the change:

import B from "b";
import a from "a";
const x = 1;

The new test in ImportDeclaration-test.js fails on main (Expected: 1, Received: 0) and passes with this change, and all collection test suites pass. A patch changeset is included.

insertImportDeclaration only inserted the new import into Program
nodes. The collection returned by j(source) holds the File node, so
calling it there silently did nothing. Insert into the program of a
File as well.
@vercel

vercel Bot commented Dec 4, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@meta-cla

meta-cla Bot commented Dec 4, 2026

Copy link
Copy Markdown

Hi @olucasleitedev!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@changeset-bot

changeset-bot Bot commented Dec 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0607d7a

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

This branch has not been deployed

No deployments
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.

1 participant