Skip to content

New: no-unused-modules rule#1142

Merged
ljharb merged 20 commits into
import-js:masterfrom
rfermann:master
Apr 7, 2019
Merged

New: no-unused-modules rule#1142
ljharb merged 20 commits into
import-js:masterfrom
rfermann:master

Conversation

@rfermann
Copy link
Copy Markdown
Contributor

@rfermann rfermann commented Jul 23, 2018

As discussed in #186, this rule adds the possibility to find modules without any exports and/or exports within a module, which aren't used in other modules

ToDos:

  • implement feature/fix bug
  • update docs
  • write tests
  • make a note in change log

This is the first rule I created. Writing tests for it may therefor take some additional time.

Signed-off-by: René Fermann <rene.fermann@gmx.de>
@coveralls
Copy link
Copy Markdown

coveralls commented Jul 23, 2018

Coverage Status

Coverage increased (+3.6%) to 97.775% when pulling d35b7ff on rfermann:master into af976b9 on benmosher:master.

Comment thread docs/rules/no-unused-modules.md Outdated
rfermann added 5 commits July 30, 2018 18:26
Signed-off-by: René Fermann <rene.fermann@gmx.de>
Signed-off-by: René Fermann <rene.fermann@gmx.de>
…ilename

Signed-off-by: René Fermann <rene.fermann@gmx.de>
Signed-off-by: René Fermann <rene.fermann@gmx.de>
Signed-off-by: René Fermann <rene.fermann@gmx.de>
@rfermann

This comment has been minimized.

Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread tests/src/rules/no-unused-modules.js Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
@coreyfarrell
Copy link
Copy Markdown

While looking into how ignore is implemented I noticed a couple language features which are not tested.

import *

I'm unsure what should happen when import * as localVar is used, any limitations should be documented. Take the following source:

import * as localVar from './file-1';
localVar.something();

Can we detect that we're actually using the named export something? If not I think the import * syntax should cause all exports of ./file-1 to be marked as used and we should document how it works. Probably suggest also using a rule that forbids use of import *.

export from

Given file-2.js containing export {something} from './file-1', tests should verify this is recognized as:

  1. An import, so even if nothing else imports ./file-1 it should not be reported as unused.
  2. An export, if nothing imports something from file-2.js it is reported.

dynamic import

I suspect dynamic import cannot be detected, this should be mentioned in the docs.

@ljharb
Copy link
Copy Markdown
Member

ljharb commented Aug 3, 2018

Yes, an import * should mark all exports as used, i think.

… sanity checks

Signed-off-by: René Fermann <rene.fermann@gmx.de>
Signed-off-by: René Fermann <rene.fermann@gmx.de>
@rfermann
Copy link
Copy Markdown
Contributor Author

Support for import * from 'somewhere', export * from 'somewhere' and export { stuff } from 'somewhere' has been added with the last 2 commits.
The documentation has been adjusted accordingly.

How can I analyze, why the AppVeyor build is failing all the time? It btw also fails for tests I haven't touched at all 😕

@rfermann rfermann changed the title WIP: New: no-unused-modules rule New: no-unused-modules rule Sep 1, 2018
@rfermann
Copy link
Copy Markdown
Contributor Author

rfermann commented Sep 1, 2018

@ljharb: do you have any ideas how to tackle the failing AppVeyor build?

@ljharb
Copy link
Copy Markdown
Member

ljharb commented Sep 1, 2018

@rfermann i believe it's on master, and unrelated to this PR.

@rfermann
Copy link
Copy Markdown
Contributor Author

rfermann commented Sep 2, 2018

Okay, good to know.

What else needs to be done from my site to let this PR be merged? Should I adjust the change log? Or is that one of these tasks you will perform @ljharb?

@ljharb
Copy link
Copy Markdown
Member

ljharb commented Sep 9, 2018

@rfermann you don't have to worry about the changelog; the appveyor issue should be fixed on master if you rebase.

…orts', revised docs

Signed-off-by: René Fermann <rene.fermann@gmx.de>
Signed-off-by: René Fermann <rene.fermann@gmx.de>
@rfermann
Copy link
Copy Markdown
Contributor Author

@ljharb , @benmosher: what else am I expected to do to get this PR merged? Is it the outstanding rebase which is blocking the merge?

Copy link
Copy Markdown
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Doing a fresh review - looks good overall. Just a few comments.

Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread src/rules/no-unused-modules.js
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
…riable, fixed documentation

Signed-off-by: Fermann <rene.fermann@capgemini.com>
Copy link
Copy Markdown
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Please ensure all files have trailing newlines.

Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread docs/rules/no-unused-modules.md Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
@rfermann
Copy link
Copy Markdown
Contributor Author

Thanks for your latest feedback. I implemented your suggestions.

Please ensure all files have trailing newlines.

I don't get that. Did I forgot the trailing newline anywhere? I checked the files I pushed today and all already had a trailing newline 😕

Comment thread docs/rules/no-unused-modules.md
Comment thread tests/files/no-unused-modules/file-b.js Outdated
Comment thread tests/src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread package.json Outdated
Comment thread src/rules/no-unused-modules.js
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
specifiers.some(({ type }) => type === IMPORT_DEFAULT_SPECIFIER)

module.exports = {
isNodeModule,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this exported?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Basically for testing, just as you supposed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This shouldn't need direct testing; let's please not export it.

Comment thread src/rules/no-unused-modules.js Outdated
Comment thread src/rules/no-unused-modules.js Outdated
Comment thread tests/src/rules/no-unused-modules.js Outdated
Copy link
Copy Markdown
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

@rfermann hey! i know it's been awhile; are you still interested in addressing the outstanding comments, rebasing, and ensuring the tests pass?

Comment thread src/rules/no-unused-modules.js Outdated
specifiers.some(({ type }) => type === IMPORT_DEFAULT_SPECIFIER)

module.exports = {
isNodeModule,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This shouldn't need direct testing; let's please not export it.


module.exports = {
meta: {
docs: { url: docsUrl('no-unused-modules') },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't the name no-unused-modules misleading? This rule reports individual export declarations, but the name suggests it reports whole files as unused.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

my understanding is that it reports on both - both unused files, and unused exports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just as @ljharb says, it reports on both:

  • modules without any exports are reported here
  • and modules with unused exports are reported here and here

However this rule currently does not report, if a module only consists of unused exports. Each of these exports is reported separately.
I am not sure whether it makes sense to have one single report being triggered if none of the existing exports is used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It does make sense to me in that case to only provide one report; just as I’d expect when a module has no exports.

Copy link
Copy Markdown
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Thanks, let's give this a shot :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants