-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): add no-commented-out rule (#262)
- Loading branch information
Showing
5 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Disallow commented out tests (no-commented-out-tests) | ||
|
||
This rule raises a warning about commented out tests. It's similar to | ||
no-disabled-tests rule. | ||
|
||
## Rule Details | ||
|
||
The rule uses fuzzy matching to do its best to determine what constitutes a | ||
commented out test, checking for a presence of `it(`, `describe(`, `it.skip(`, | ||
etc. in code comments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
// describe('foo', () => {}); | ||
// it('foo', () => {}); | ||
// test('foo', () => {}); | ||
|
||
// describe.skip('foo', () => {}); | ||
// it.skip('foo', () => {}); | ||
// test.skip('foo', () => {}); | ||
|
||
// describe['skip']('bar', () => {}); | ||
// it['skip']('bar', () => {}); | ||
// test['skip']('bar', () => {}); | ||
|
||
// xdescribe('foo', () => {}); | ||
// xit('foo', () => {}); | ||
// xtest('foo', () => {}); | ||
|
||
/* | ||
describe('foo', () => {}); | ||
*/ | ||
``` | ||
|
||
These patterns would not be considered warnings: | ||
|
||
```js | ||
describe('foo', () => {}); | ||
it('foo', () => {}); | ||
test('foo', () => {}); | ||
|
||
describe.only('bar', () => {}); | ||
it.only('bar', () => {}); | ||
test.only('bar', () => {}); | ||
|
||
// foo('bar', () => {}); | ||
``` | ||
|
||
### Limitations | ||
|
||
The plugin looks at the literal function names within test code, so will not | ||
catch more complex examples of commented out tests, such as: | ||
|
||
```js | ||
// const testSkip = test.skip; | ||
// testSkip('skipped test', () => {}); | ||
|
||
// const myTest = test; | ||
// myTest('does not have function body'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
'use strict'; | ||
|
||
const { RuleTester } = require('eslint'); | ||
const rule = require('../no-commented-out-tests'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-commented-out-tests', rule, { | ||
valid: [ | ||
'// foo("bar", function () {})', | ||
'describe("foo", function () {})', | ||
'it("foo", function () {})', | ||
'describe.only("foo", function () {})', | ||
'it.only("foo", function () {})', | ||
'test("foo", function () {})', | ||
'test.only("foo", function () {})', | ||
'var appliedSkip = describe.skip; appliedSkip.apply(describe)', | ||
'var calledSkip = it.skip; calledSkip.call(it)', | ||
'({ f: function () {} }).f()', | ||
'(a || b).f()', | ||
'itHappensToStartWithIt()', | ||
'testSomething()', | ||
[ | ||
'import { pending } from "actions"', | ||
'', | ||
'test("foo", () => {', | ||
' expect(pending()).toEqual({})', | ||
'})', | ||
].join('\n'), | ||
[ | ||
'const { pending } = require("actions")', | ||
'', | ||
'test("foo", () => {', | ||
' expect(pending()).toEqual({})', | ||
'})', | ||
].join('\n'), | ||
[ | ||
'test("foo", () => {', | ||
' const pending = getPending()', | ||
' expect(pending()).toEqual({})', | ||
'})', | ||
].join('\n'), | ||
[ | ||
'test("foo", () => {', | ||
' expect(pending()).toEqual({})', | ||
'})', | ||
'', | ||
'function pending() {', | ||
' return {}', | ||
'}', | ||
].join('\n'), | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: '// describe("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// describe["skip"]("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// describe[\'skip\']("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// it.skip("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// it.only("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// it["skip"]("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// test.skip("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// test["skip"]("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// xdescribe("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// xit("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// fit("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// xtest("foo", function () {})', | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: `// test( | ||
// "foo", function () {} | ||
// )`, | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: `/* test | ||
( | ||
"foo", function () {} | ||
) | ||
*/`, | ||
errors: [ | ||
{ message: 'Some tests seem to be commented', column: 1, line: 1 }, | ||
], | ||
}, | ||
{ | ||
code: '// it("has title but no callback")', | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 1, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// it()', | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 1, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// test.someNewMethodThatMightBeAddedInTheFuture()', | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 1, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// test["someNewMethodThatMightBeAddedInTheFuture"]()', | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 1, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: '// test("has title but no callback")', | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 1, | ||
line: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
foo() | ||
/* | ||
describe("has title but no callback", () => {}) | ||
*/ | ||
bar()`, | ||
errors: [ | ||
{ | ||
message: 'Some tests seem to be commented', | ||
column: 7, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
const { getDocsUrl } = require('./util'); | ||
|
||
const message = 'Some tests seem to be commented'; | ||
|
||
function hasTests(node) { | ||
return /(x|f)?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/m.test( | ||
node.value, | ||
); | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
function checkNode(node) { | ||
if (!hasTests(node)) return; | ||
|
||
context.report({ | ||
message, | ||
node, | ||
}); | ||
} | ||
|
||
return { | ||
Program() { | ||
const comments = sourceCode.getAllComments(); | ||
|
||
comments.filter(token => token.type !== 'Shebang').forEach(checkNode); | ||
}, | ||
}; | ||
}, | ||
}; |