Skip to content

Commit

Permalink
Add auto-fix of misspelled falsey to falsy (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny authored and sindresorhus committed May 17, 2019
1 parent 1218892 commit 9ec0565
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/rules/use-t-well.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/re

Prevent the use of unknown assertion methods and the access to members other than the assertion methods and `context`, as well as some known misuses of `t`.

This rule is partly automatically fixable. It will replace misspelled `falsey` with `falsy`.


## Fail

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The rules will only activate in test files.
- [prefer-power-assert](docs/rules/prefer-power-assert.md) - Allow only use of the asserts that have no [power-assert](https://github.com/power-assert-js/power-assert) alternative.
- [test-ended](docs/rules/test-ended.md) - Ensure callback tests are explicitly ended.
- [test-title](docs/rules/test-title.md) - Ensure tests have a title.
- [use-t-well](docs/rules/use-t-well.md) - Prevent the incorrect use of `t`.
- [use-t-well](docs/rules/use-t-well.md) - Prevent the incorrect use of `t`. *(partly fixable)*
- [use-t](docs/rules/use-t.md) - Ensure test functions use `t` as their parameter.
- [use-test](docs/rules/use-test.md) - Ensure that AVA is imported with `test` as the variable name.
- [use-true-false](docs/rules/use-true-false.md) - Ensure that `t.true()`/`t.false()` are used instead of `t.truthy()`/`t.falsy()`.
Expand Down
12 changes: 11 additions & 1 deletion rules/use-t-well.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ const isCallExpression = node =>
const getMemberStats = members => {
const initial = {
skip: [],
falsey: [],
method: [],
other: []
};

return members.reduce((res, member) => {
if (member === 'skip') {
res.skip.push(member);
} else if (member === 'falsey') {
res.falsey.push(member);
} else if (isMethod(member)) {
res.method.push(member);
} else {
Expand Down Expand Up @@ -94,6 +97,12 @@ const create = context => {
node,
message: 'Too many chained uses of `skip`.'
});
} else if (stats.falsey.length > 0) {
context.report({
node,
message: 'Misspelled `falsy` as `falsey`.',
fix: fixer => fixer.replaceTextRange(node.property.range, 'falsy')
});
} else if (stats.method.length > 1) {
context.report({
node,
Expand All @@ -120,6 +129,7 @@ module.exports = {
meta: {
docs: {
url: util.getDocsUrl(__filename)
}
},
fixable: 'code'
}
};
5 changes: 5 additions & 0 deletions test/use-t-well.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ ruleTester.run('use-t-well', rule, {
{
code: testCase('t.deepEqual.skip.skip(a, a);'),
errors: [error('Too many chained uses of `skip`.')]
},
{
code: testCase('t.falsey(a);'),
output: testCase('t.falsy(a);'),
errors: [error('Misspelled `falsy` as `falsey`.')]
}
]
});

0 comments on commit 9ec0565

Please sign in to comment.