Skip to content

Commit

Permalink
fix: set args to after-used for no-unused-vars rule (#103)
Browse files Browse the repository at this point in the history
This PR makes the following code failing linting. IMO this helps with
refactoring and making sure arguments are not left in function calls.

```js
// Lint error: "'c' is defined but never used."
function unused (a, b, c) {
  return b
}
unused()
```
  • Loading branch information
lukekarrys authored Apr 10, 2024
1 parent 436f26c commit acd463d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module.exports = {
'no-unused-expressions': ['error', {
allowShortCircuit: true, allowTernary: true, allowTaggedTemplates: true,
}],
'no-unused-vars': ['error', { vars: 'all', args: 'none', ignoreRestSiblings: true }],
'no-unused-vars': ['error', { vars: 'all', args: 'after-used', ignoreRestSiblings: true }],
'no-use-before-define': ['error', { functions: false, classes: false, variables: false }],
'no-useless-call': 'error',
'no-useless-computed-key': 'error',
Expand Down
43 changes: 30 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
const { ESLint: ESLint8 } = require('eslint')
const t = require('tap')

const code = `const foo = 1
const loadConfig = (desc, Ctor) => new Ctor({
useEslintrc: false,
overrideConfigFile: 'lib/index.js', // this is relative to the root of the repo
})

const lintText = async (engine, code) => {
const [result] = await engine.lintText(code)
return result
}

t.test('eslint8', async t => {
const engine = loadConfig('eslint 8', ESLint8)

t.test('config loads correctly', async t => {
const code = `const foo = 1
const bar = function () {}
bar(foo)
`

t.test('config loads correctly', t => {
const loadConfig = (desc, Ctor) => t.test(async t => {
const engine = new Ctor({
useEslintrc: false,
overrideConfigFile: 'lib/index.js', // this is relative to the root of the repo
})

const [result] = await engine.lintText(code)
const result = await lintText(engine, code)
t.equal(result.errorCount, 0, 'no errors')
})

t.plan(1)
loadConfig('eslint 8', ESLint8)
t.end()
t.test('unused args', async t => {
const code = `function unused (a, b, c) {
return b
}
function allUsed (a, b) {
return b
}
unused()
allUsed()
`
const result = await lintText(engine, code)
t.equal(result.errorCount, 1, 'no errors')
t.equal(result.messages[0].message, "'c' is defined but never used.")
})
})

0 comments on commit acd463d

Please sign in to comment.