Giter VIP home page Giter VIP logo

Comments (5)

jaydenseric avatar jaydenseric commented on May 26, 2024 1

I ended up writing from scratch and publishing find-unused-exports; A Node.js CLI and equivalent JS API to find unused ECMAScript module exports in a project.

It can resolve index files, custom extensions, etc. One thing I was not expecting was for it to be so fast — find-unused-exports takes less than a second for our project whereas Shrimpit for some reason takes quite a few minutes. It also supports ignoring specific exports known to be unused (e.g. the default export for a Next.js page) via ignore comments, making the tool fit for use as part of the test script for CI.

from shrimpit.

yamafaktory avatar yamafaktory commented on May 26, 2024

Sorry for the late reply... missed this notification... anyway, yes this is a bug indeed and it should definitely handle that by default 👍 .

from shrimpit.

jaydenseric avatar jaydenseric commented on May 26, 2024

I got started on a Shimpit PR that fixed most of the problems with index files, but gave up before getting every type of import/export to work. If you're interested, I might be able to share what I had in a draft PR.

from shrimpit.

yamafaktory avatar yamafaktory commented on May 26, 2024

Sure, thanks!

from shrimpit.

jaydenseric avatar jaydenseric commented on May 26, 2024

The strategy is to not attempt to guess the absolute import file paths at the time they are discovered, but to record the import specifiers more faithfully:

{
- "location": "test/core/b/b.js",
+ "location": "test/core/b/b",
   "name": "test",
   "unnamedDefault": true,
}

Then later when analysing what exports are unused, search for the files in the list, resolving file extensions and index files in order of preference (it's good to match the Node.js resolution algorithm).

Rather than do a fork and draft PR, here are the relevant diffs (minus updated tests and snapshots)…

shrimpit/lib.js

Line 282 in 6486693

const { exports, imports } = this.modules

     const { exports, imports } = this.modules
     const unresolved = exports.reduce((acc, item) => {
       if (
-        imports.filter(element =>
-          element.unnamedDefault
+        imports.filter(imprt =>
+          imprt.unnamedDefault
             ? // Skip the name in the comparison as a default unnamed export
               // can be imported with any name.
               // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
               this.deepStrictEqual(
-                { location: element.location, unnamedDefault: true },
+                { location: `${imprt.location}.js`, unnamedDefault: true },
                 {
                   location: item.location,
                   unnamedDefault: item.unnamedDefault,
                 },
               )
             : // Compare the raw element & item.
-              this.deepStrictEqual(element, item),
+              this.deepStrictEqual(
+                {
+                  ...imprt,
+                  location: `${imprt.location}.js`,
+                },
+                item,
+              ),
+        ).length === 0 &&
+        imports.filter(imprt =>
+          imprt.unnamedDefault
+            ? this.deepStrictEqual(
+                {
+                  location: `${imprt.location}${path.sep}index.js`,
+                  unnamedDefault: true,
+                },
+                {
+                  location: item.location,
+                  unnamedDefault: item.unnamedDefault,
+                },
+              )
+            : this.deepStrictEqual(
+                {
+                  ...imprt,
+                  location: `${imprt.location}${path.sep}index.js`,
+                },
+                item,
+              ),
         ).length === 0
       ) {
         acc.push(item)

That's not the best way to write it; this was just my work in progress to patch the way the code currently works.

shrimpit/lib.js

Line 410 in 6486693

const getLocation = location =>

     const getLocation = location =>
-      this.joinPaths(
-        this.getDir(extPath).join(path.sep),
-        location + this.getExt(extPath),
-      )
+      path.join(this.getDir(extPath).join(path.sep), location)

A side note: I avoided using this.joinPaths; it's better to path.join directly. Unhelpful methods like this could be removed to simplify the code and make it easier to understand what's going on:

shrimpit/lib.js

Lines 207 to 209 in 6486693

joinPaths(...paths) {
return path.join(...paths)
}

I think namespace imports were not working at the time I abandoned the effort? I'm struggling to remember to be honest it was a few weeks ago.

from shrimpit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.