Skip to content

Add fixer for the no-only-test rule #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The rules will only activate in test files.
- [no-ignored-test-files](docs/rules/no-ignored-test-files.md) - Ensure no tests are written in ignored files.
- [no-invalid-end](docs/rules/no-invalid-end.md) - Ensure `t.end()` is only called inside `test.cb()`.
- [no-nested-tests](docs/rules/no-nested-tests.md) - Ensure no tests are nested.
- [no-only-test](docs/rules/no-only-test.md) - Ensure no `test.only()` are present.
- [no-only-test](docs/rules/no-only-test.md) - Ensure no `test.only()` are present. *(fixable)*
- [no-skip-assert](docs/rules/no-skip-assert.md) - Ensure no assertions are skipped.
- [no-skip-test](docs/rules/no-skip-test.md) - Ensure no tests are skipped.
- [no-statement-after-end](docs/rules/no-statement-after-end.md) - Ensure `t.end()` is the last statement executed.
Expand Down
18 changes: 16 additions & 2 deletions rules/no-only-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const visitIf = require('enhance-visitors').visitIf;
const createAvaRule = require('../create-ava-rule');
const getTestModifier = require('../util').getTestModifier;

const create = context => {
const ava = createAvaRule();
Expand All @@ -13,7 +14,18 @@ const create = context => {
if (ava.hasTestModifier('only')) {
context.report({
node,
message: '`test.only()` should not be used.'
message: '`test.only()` should not be used.',
fix: fixer => {
const range = getTestModifier(node, 'only').range.slice();
const source = context.getSourceCode().getText();
let dotPosition = range[0] - 1;
while (source.charAt(dotPosition) !== '.') {
dotPosition -= 1;
}
let snippet = source.substring(dotPosition, range[1]);
snippet = snippet.replace(/\.|only/g, '');
return fixer.replaceTextRange([dotPosition, range[1]], snippet);
}
});
}
})
Expand All @@ -22,5 +34,7 @@ const create = context => {

module.exports = {
create,
meta: {}
meta: {
fixable: 'code'
}
};
36 changes: 36 additions & 0 deletions test/no-only-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import rule from '../rules/no-only-test';
const ruleTester = avaRuleTester(test, {
env: {
es6: true
},
parserOptions: {
sourceType: 'module'
}
});

Expand All @@ -21,16 +24,49 @@ ruleTester.run('no-only-test', rule, {
'test.only(t => {});'
],
invalid: [
{
code: header + 'test\n\t.only(t => { t.pass(); });',
output: header + 'test\n\t(t => { t.pass(); });',
errors
},
{
code: header + 'test\n .only(t => { t.pass(); });',
output: header + 'test\n (t => { t.pass(); });',
errors
},
{
code: header + 'test\t.only(t => { t.pass(); });',
output: header + 'test\t(t => { t.pass(); });',
errors
},
{
code: header + 'test .only(t => { t.pass(); });',
output: header + 'test (t => { t.pass(); });',
errors
},
{
code: header + 'test.\n\tonly(t => { t.pass(); });',
output: header + 'test\n\t(t => { t.pass(); });',
errors
},
{
code: header + 'test.\n only(t => { t.pass(); });',
output: header + 'test\n (t => { t.pass(); });',
errors
},
{
code: header + 'test.only(t => { t.pass(); });',
output: header + 'test(t => { t.pass(); });',
errors
},
{
code: header + 'test.cb.only(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors
},
{
code: header + 'test.only.cb(t => { t.pass(); t.end(); });',
output: header + 'test.cb(t => { t.pass(); t.end(); });',
errors
}
]
Expand Down
12 changes: 12 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ exports.getTestModifiers = function getTestModifiers(node) {
return [];
};

exports.getTestModifier = function getTestModifier(node, mod) {
if (node.type === 'CallExpression') {
return getTestModifier(node.callee, mod);
} else if (node.type === 'MemberExpression') {
if (node.property.type === 'Identifier' && node.property.name === mod) {
return node.property;
}

return getTestModifier(node.object, mod);
}
};

const getMembers = node => {
const name = node.property.name;

Expand Down