From 6b01e3634a16414d4c0c0ee2d078fdb327271f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 27 Apr 2023 10:45:00 +0200 Subject: [PATCH] feat!: check for unused dependencies by default (#1177) To make the dependency check more thorough, check for unused deps by default. --------- Co-authored-by: Alex Potsides --- package.json | 2 +- src/cmds/dependency-check.js | 2 +- src/config/user.js | 5 +---- src/dependency-check.js | 13 ++++++++++++- test/dependency-check.js | 16 ++++++++-------- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index ad0b0efe7..73904f510 100644 --- a/package.json +++ b/package.json @@ -212,7 +212,7 @@ "lint": "node src/index.js lint", "test": "node src/index.js test", "docs": "node src/index.js docs", - "dep-check": "node src/index.js dep-check", + "dep-check": "node src/index.js dep-check --unused false", "doc-check": "node src/index.js doc-check", "test:node": "node src/index.js test -t node --cov", "test:chrome": "node src/index.js test -t browser --cov", diff --git a/src/cmds/dependency-check.js b/src/cmds/dependency-check.js index 31cfad5ac..3feff2db5 100644 --- a/src/cmds/dependency-check.js +++ b/src/cmds/dependency-check.js @@ -36,7 +36,7 @@ export default { .option('u', { alias: 'unused', describe: 'Checks for unused dependencies', - default: false, + default: true, type: 'boolean' }) }, diff --git a/src/config/user.js b/src/config/user.js index e00aa53db..7018c032a 100644 --- a/src/config/user.js +++ b/src/config/user.js @@ -112,10 +112,7 @@ const defaults = { // dependency check cmd options dependencyCheck: { unused: false, - ignore: [ - '@types/*', - 'aegir' - ] + ignore: [] }, exec: { bail: true diff --git a/src/dependency-check.js b/src/dependency-check.js index 89570b101..443eb02ce 100644 --- a/src/dependency-check.js +++ b/src/dependency-check.js @@ -2,6 +2,17 @@ import Listr from 'listr' import depcheck from 'depcheck' import { cwd } from 'process' +const ignoredDevDependencies = [ + '@types/*', + 'aegir', + 'mkdirp', + 'rimraf', + 'protons', + 'eslint*', + '@types/*', + '@semantic-release/*' +] + /** * @typedef {import("listr").ListrTaskWrapper} Task * @typedef {import("./types").GlobalOptions} GlobalOptions @@ -24,7 +35,7 @@ const tasks = new Listr( '**/*.cjs': depcheck.parser.es6, '**/*.mjs': depcheck.parser.es6 }, - ignoreMatches: ['eslint*', '@types/*', '@semantic-release/*'].concat(ctx.fileConfig.dependencyCheck.ignore).concat(ctx.ignore) + ignoreMatches: ignoredDevDependencies.concat(ctx.fileConfig.dependencyCheck.ignore).concat(ctx.ignore) }) if (Object.keys(result.missing).length > 0 || (ctx.unused && (result.dependencies.length > 0 || result.devDependencies.length > 0))) { throw new Error( diff --git a/test/dependency-check.js b/test/dependency-check.js index 3ae8240cd..11f8b84e7 100644 --- a/test/dependency-check.js +++ b/test/dependency-check.js @@ -13,7 +13,7 @@ const bin = require.resolve('../src/index.js') describe('dependency check', () => { it('should fail for missing deps', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/fail') }) ).to.eventually.be.rejectedWith('execa') @@ -21,7 +21,7 @@ describe('dependency check', () => { it('should fail for missing deps in an esm project', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/esm-fail') }) ).to.eventually.be.rejected() @@ -32,7 +32,7 @@ describe('dependency check', () => { it('should fail for missing deps in an ts project', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/ts-fail') }) ).to.eventually.be.rejected() @@ -43,7 +43,7 @@ describe('dependency check', () => { it('should pass when there are no missing deps', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/pass') }) ).to.eventually.be.fulfilled() @@ -51,7 +51,7 @@ describe('dependency check', () => { it('should pass when there are no missing deps in an esm project', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/esm-pass') }) ).to.eventually.be.fulfilled() @@ -59,7 +59,7 @@ describe('dependency check', () => { it('should pass when there are no missing deps in an ts project', async () => { await expect( - execa(bin, ['dependency-check'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join(__dirname, 'fixtures/dependency-check/ts-pass') }) ).to.eventually.be.fulfilled() @@ -67,7 +67,7 @@ describe('dependency check', () => { it('should check unused', async () => { await expect( - execa(bin, ['dependency-check', '--unused'], { + execa(bin, ['dependency-check'], { cwd: path.join(__dirname, 'fixtures/dependency-check/pass') }) ).to.eventually.be.rejected.with.property('message').that.include( @@ -131,7 +131,7 @@ describe('dependency check', () => { it('should pass for modules used in .aegir.js', async () => { await expect( - execa(bin, ['dependency-check', '--unused'], { + execa(bin, ['dependency-check', '-u', 'false'], { cwd: path.join( __dirname, 'fixtures/dependency-check/with-aegir-config'