Replace one import with another approach fails when both are from the same module like in the example bellow
-import { createSecurePair } from 'node:tls';
+import { TLSSocket } from 'node:tls';
const pair = createSecurePair(credentials);
Basically,removeImport will create an edit to remove the entire line, so regardless of the order of removeImport and addImport, the output from commitEdits will be the line removed.
function testRemoveNamedImportESM_AddNamedImportESM() {
const program = parseProgram("javascript", "import { foo } from 'mod';\nconsole.log('hello');\n");
const add = addImport(program, {
type: "named",
specifiers: [{ name: "bar" }],
from: "mod",
});
const remove = removeImport(program, {
type: "named",
specifiers: ['foo'],
from: "mod",
});
assert(remove !== null, "Should return an remove edit");
assert(add !== null, "Should return an add edit");
const result = program.commitEdits([add!, remove!]);
console.log({result})
assert(
result.includes("import { bar } from 'mod'"),
"Foo should be replaced with bar",
);
}
So this is the reason why, in the node/userland-migration, we have the update-binding to handle scenarios where imports need to be added and remove form the same import line.
In our implementation you can add multiple new imports, but only remove one. The reason for this limitation on removals is simply that we haven’t needed to remove multiple yet. However, for @jssg/utils, I think it would be better to start supporting multiple removals/additions from the beginning.
Replace one import with another approach fails when both are from the same module like in the example bellow
Basically,
removeImportwill create an edit to remove the entire line, so regardless of the order ofremoveImportandaddImport, the output fromcommitEditswill be the line removed.So this is the reason why, in the node/userland-migration, we have the update-binding to handle scenarios where imports need to be added and remove form the same import line.
In our implementation you can add multiple new imports, but only remove one. The reason for this limitation on removals is simply that we haven’t needed to remove multiple yet. However, for @jssg/utils, I think it would be better to start supporting multiple removals/additions from the beginning.