Skip to content

fix: keep unit aliases in imports-order autofix - #803

Open
SahilRajputdotexe wants to merge 2 commits into
protofire:developfrom
SahilRajputdotexe:fix/imports-order-unit-alias
Open

SahilRajputdotexe wants to merge 2 commits into
protofire:developfrom
SahilRajputdotexe:fix/imports-order-unit-alias

Conversation

@SahilRajputdotexe

Copy link
Copy Markdown

The imports-order autofix rebuilds every import statement, but it only knows two shapes: import { A } from 'path' and a bare import 'path'. An import with a unit alias falls into the second one, so --fix drops the alias:

import * as Utils from './libraries/Utils.sol';
import './Types.sol' as Types;

becomes

import "./libraries/Utils.sol";
import "./Types.sol";

and the fixed file no longer compiles.

This rebuilds those imports with their alias, in the form they were written. Both forms are added to the imports-order e2e fixture.

The autofix rebuilt every import without symbol aliases as a bare
import 'path', so import * as X from 'path' and import 'path' as X lost
their alias and the fixed file no longer compiled.

Rebuild those imports with the alias, in the form it was written.

@vschernoff vschernoff left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. Verified by running it rather than only reading the diff.

The bug reproduces on develop — --fix drops the alias entirely:

before   import * as Utils from './libraries/Utils.sol';
after    import "./libraries/Utils.sol";

Both forms are preserved with this PR:

import * as Utils from "./libraries/Utils.sol";
import "./Types.sol" as Types;

The implementation is safe. I checked the parser output directly: whenever unitAlias is set, both unitAliasIdentifier and pathLiteral are present with ranges, so the range[0] comparison cannot throw — and it is guarded inside if (node.unitAlias) regardless. The comparison correctly distinguishes the two forms.

One thing to note, not a blocker and not caused by this PR: no-unused-import --fix produces import * as from "..." — invalid Solidity — on the same import shape. imports-order reports no violation on that file, so it is a different rule entirely. Filed as #806 so it is not assumed fixed by this.

@dbale-arg dbale-arg assigned dbale-arg and unassigned dbale-arg Oct 8, 2026

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.

3 participants