Comments (5)
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.
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.
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.
Sure, thanks!
from shrimpit.
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)…
Line 282 in 6486693
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.
Line 410 in 6486693
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:
Lines 207 to 209 in 6486693
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)
- Add --help HOT 2
- Re-exports support HOT 2
- Error while trying to run it HOT 3
- namespace-imports TypeError HOT 1
- Work with UMD? HOT 2
- Not working at all HOT 5
- Can we get a --fix flag? HOT 1
- create vscode plugin HOT 1
- Work with Webpack Resolvers HOT 5
- Default exported class is not picked up correctly HOT 10
- Wrong output if exports use the same name HOT 3
- Statements inside class property functions are picked up as unused exports HOT 6
- Static class property leades to shrimpit exception HOT 2
- Renamed imports lead to unused exports HOT 3
- Named function expression is mistaken for an export HOT 2
- Version 10 of node.js has been released
- Error on React fragments short syntax HOT 10
- fails HOT 1
- Glob input? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shrimpit.