Skip to content

Commit 9ec0565

Browse files
GMartignysindresorhus
authored andcommitted
Add auto-fix of misspelled falsey to falsy (#229)
1 parent 1218892 commit 9ec0565

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

docs/rules/use-t-well.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/re
44

55
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`.
66

7+
This rule is partly automatically fixable. It will replace misspelled `falsey` with `falsy`.
8+
79

810
## Fail
911

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The rules will only activate in test files.
9797
- [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.
9898
- [test-ended](docs/rules/test-ended.md) - Ensure callback tests are explicitly ended.
9999
- [test-title](docs/rules/test-title.md) - Ensure tests have a title.
100-
- [use-t-well](docs/rules/use-t-well.md) - Prevent the incorrect use of `t`.
100+
- [use-t-well](docs/rules/use-t-well.md) - Prevent the incorrect use of `t`. *(partly fixable)*
101101
- [use-t](docs/rules/use-t.md) - Ensure test functions use `t` as their parameter.
102102
- [use-test](docs/rules/use-test.md) - Ensure that AVA is imported with `test` as the variable name.
103103
- [use-true-false](docs/rules/use-true-false.md) - Ensure that `t.true()`/`t.false()` are used instead of `t.truthy()`/`t.falsy()`.

rules/use-t-well.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ const isCallExpression = node =>
1212
const getMemberStats = members => {
1313
const initial = {
1414
skip: [],
15+
falsey: [],
1516
method: [],
1617
other: []
1718
};
1819

1920
return members.reduce((res, member) => {
2021
if (member === 'skip') {
2122
res.skip.push(member);
23+
} else if (member === 'falsey') {
24+
res.falsey.push(member);
2225
} else if (isMethod(member)) {
2326
res.method.push(member);
2427
} else {
@@ -94,6 +97,12 @@ const create = context => {
9497
node,
9598
message: 'Too many chained uses of `skip`.'
9699
});
100+
} else if (stats.falsey.length > 0) {
101+
context.report({
102+
node,
103+
message: 'Misspelled `falsy` as `falsey`.',
104+
fix: fixer => fixer.replaceTextRange(node.property.range, 'falsy')
105+
});
97106
} else if (stats.method.length > 1) {
98107
context.report({
99108
node,
@@ -120,6 +129,7 @@ module.exports = {
120129
meta: {
121130
docs: {
122131
url: util.getDocsUrl(__filename)
123-
}
132+
},
133+
fixable: 'code'
124134
}
125135
};

test/use-t-well.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ ruleTester.run('use-t-well', rule, {
124124
{
125125
code: testCase('t.deepEqual.skip.skip(a, a);'),
126126
errors: [error('Too many chained uses of `skip`.')]
127+
},
128+
{
129+
code: testCase('t.falsey(a);'),
130+
output: testCase('t.falsy(a);'),
131+
errors: [error('Misspelled `falsy` as `falsey`.')]
127132
}
128133
]
129134
});

0 commit comments

Comments
 (0)