Skip to content

Commit

Permalink
feat!: check for unused dependencies by default (#1177)
Browse files Browse the repository at this point in the history
To make the dependency check more thorough, check for unused deps by default.

---------

Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
mpetrunic and achingbrain committed Apr 27, 2023
1 parent 15f01ea commit 6b01e36
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/dependency-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
.option('u', {
alias: 'unused',
describe: 'Checks for unused dependencies',
default: false,
default: true,
type: 'boolean'
})
},
Expand Down
5 changes: 1 addition & 4 deletions src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ const defaults = {
// dependency check cmd options
dependencyCheck: {
unused: false,
ignore: [
'@types/*',
'aegir'
]
ignore: []
},
exec: {
bail: true
Expand Down
13 changes: 12 additions & 1 deletion src/dependency-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
16 changes: 8 additions & 8 deletions test/dependency-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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')
})

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()
Expand All @@ -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()
Expand All @@ -43,31 +43,31 @@ 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()
})

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()
})

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()
})

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(
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 6b01e36

Please sign in to comment.