JavaScript: moving an import ignores its import attributes

Open
#9,789 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
devtools

Research direction

Start with rewrite-javascript/rewrite/src/javascript/recipes/change-import.ts, especially ChangeImport and maybeRebind, to trace how moved members are joined to target imports. Check the JavaScript recipe tests for import-move cases and add coverage for matching and differing with/assert attributes. Done means members only join imports with identical attributes; otherwise they retain or receive a separate import statement.

Written by the indexing model from the issue text.

Description

bug javascript

ChangeImport (org.openrewrite.javascript.change-import, via maybeRebind) joins a moved member into an existing import of the target module without comparing import attributes. Attributes change how a module is loaded: with Bun, with {type: "macro"} runs the import at bundle time, so the member silently switches between bundle time and runtime.

1. Moved member loses its attributes

Move buildTime from ./macros/legacy.ts to ./macros/build.ts.

Before

import {buildTime} from "./macros/legacy.ts" with {type: "macro"};
import {gitHash} from "./macros/build.ts";

export const banner = `${buildTime()} ${gitHash()}`;

After (problem)

import {buildTime, gitHash} from "./macros/build.ts"; // ❌ buildTime is no longer a macro

After (expected)

import {buildTime} from "./macros/build.ts" with {type: "macro"};
import {gitHash} from "./macros/build.ts";
2. Plain member gains attributes

Move formatDate from ./util.ts to ./macros/build.ts.

Before

import {formatDate} from "./util.ts";
import {gitHash} from "./macros/build.ts" with {type: "macro"};

After (problem)

import {formatDate, gitHash} from "./macros/build.ts" with {type: "macro"}; // ❌ formatDate becomes a macro

After (expected)

import {formatDate} from "./macros/build.ts";
import {gitHash} from "./macros/build.ts" with {type: "macro"};

Expected rule: join a moved or added member only into an import with the same attributes (with and assert count as different); otherwise keep or add a statement of its own. The same applies to any attribute (type: "json", the legacy assert {…}).

Dominant language
Java
Stars
3.9k
Forks
599
Avg merge
9h 38m
Merged PRs (30d)
557

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openrewrite/rewrite

All issues in openrewrite/rewrite

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.