tusharsadhwani / zigimports

Automatically remove unused imports and globals from Zig files.
MIT License
34 stars 2 forks source link

Some imports need multiple invocations to be removed #3

Closed tusharsadhwani closed 2 months ago

tusharsadhwani commented 2 months ago

From zine:

$ zigimports ./src/**/*.zig --fix
./src/context/Bool.zig - Removed 1 unused import
./src/context/Build.zig - Removed 1 unused import
./src/context/DateTime.zig - Removed 1 unused import
./src/context/Float.zig - Removed 1 unused import
./src/context/Int.zig - Removed 1 unused import
./src/context/Map.zig - Removed 1 unused import
./src/context/Page.zig - Removed 3 unused imports
./src/context/Site.zig - Removed 2 unused imports
./src/context/Template.zig - Removed 1 unused import
./src/context/docgen.zig - Removed 2 unused imports
./src/context/doctypes.zig - Removed 1 unused import
./src/context/utils.zig - Removed 5 unused imports
./src/exes/docgen.zig - Removed 1 unused import
./src/exes/index-assets.zig - Removed 1 unused import
./src/exes/layout.zig - Removed 1 unused import
./src/exes/markdown-renderer.zig - Removed 4 unused imports
./src/exes/server/Reloader.zig - Removed 1 unused import
./src/exes/server/main.zig - Removed 2 unused imports
./src/exes/update-assets.zig - Removed 1 unused import
./src/fuzz/scripty.zig - Removed 2 unused imports
./src/highlight.zig - Removed 1 unused import
./src/render/html.zig - Removed 1 unused import

$ zigimports ./src/**/*.zig --fix
./src/context/Build.zig - Removed 1 unused import
./src/exes/markdown-renderer.zig - Removed 1 unused import

Probably happens because of this:

const t = @import("./t.zig")
const Foo = t.Foo;  // unused

After running zigimports --fix:

const t = @import("./t.zig")  // Now this is unused
sin-ack commented 2 months ago

You could run your removal algorithm iteratively until no more changes happen. It's basically just running it multiple times but automatically.

tusharsadhwani commented 2 months ago

That is indeed the plan, I'll implement that very soon.

On a completely different note, I'm struggling to find the best way that users can integrate zigimports into their projects. I was wondering if making it a part of your build.zig.zon file and then having a simple way to add a zig build lint command that just runs zigimports would be possible.

Sofar what I've found is that if you create a tools/lint.zig file in your project it is trivial to add a zig build lint command. But I am trying to avoid making the users do that much work for setup.

tusharsadhwani commented 2 months ago

Fixed this in https://github.com/tusharsadhwani/zigimports/commit/10dfc7c6b982c33b9968939e7888f58a6bb247ce, it's not the most efficient, but it should do for now.