-
-
Notifications
You must be signed in to change notification settings - Fork 30
Add new rule no-only-tests
#145
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,55 @@ | ||
# Disallow the test case property `only` (no-only-tests) | ||
|
||
The [`only` property](https://eslint.org/docs/developer-guide/unit-tests#running-individual-tests) can be used as of [ESLint 7.29](https://eslint.org/blog/2021/06/eslint-v7.29.0-released#highlights) for running individual rule test cases with less-noisy debugging. This feature should be only used in development, as it prevents all the tests from running. Mistakenly checking-in a test case with this property can cause CI tests to incorrectly pass. | ||
|
||
## Rule Details | ||
|
||
This rule flags a violation when a test case is using `only`. Note that this rule is not autofixable since automatically deleting the property would prevent developers from being able to use it during development. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-only-tests: error */ | ||
|
||
const { RuleTester } = require('eslint'); | ||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('my-rule', myRule, { | ||
valid: [ | ||
{ | ||
code: 'const valid = 42;', | ||
only: true, | ||
}, | ||
RuleTester.only('const valid = 42;'), | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const invalid = 42;', | ||
only: true, | ||
errors: [/* ... */], | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/no-only-tests: error */ | ||
|
||
const { RuleTester } = require('eslint'); | ||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('my-rule', myRule, { | ||
valid: [ | ||
'const valid = 42;', | ||
{ code: 'const valid = 42;' }, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const invalid = 42;', | ||
errors: [/* ... */], | ||
}, | ||
], | ||
}); | ||
``` |
This file contains hidden or 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,112 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
/** | ||
* Checks if the given token is a comma token or not. | ||
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} `true` if the token is a comma token. | ||
*/ | ||
function isCommaToken (token) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, didn't realize they came from there, fixed #156 |
||
return token.value === ',' && token.type === 'Punctuator'; | ||
} | ||
|
||
/** | ||
* Checks if the given token is an opening brace token or not. | ||
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} `true` if the token is an opening brace token. | ||
*/ | ||
function isOpeningBraceToken (token) { | ||
return token.value === '{' && token.type === 'Punctuator'; | ||
} | ||
|
||
/** | ||
* Checks if the given token is a closing brace token or not. | ||
* From: https://github.com/eslint/eslint/blob/master/lib/rules/utils/ast-utils.js | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} `true` if the token is a closing brace token. | ||
*/ | ||
function isClosingBraceToken (token) { | ||
return token.value === '}' && token.type === 'Punctuator'; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow the test case property `only`', | ||
category: 'Tests', | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
foundOnly: | ||
'The test case property `only` can be used during development, but should not be checked-in, since it prevents all the tests from running.', | ||
removeOnly: 'Remove `only`.', | ||
}, | ||
hasSuggestions: true, | ||
}, | ||
|
||
create (context) { | ||
return { | ||
Program (ast) { | ||
for (const testRun of utils.getTestInfo(context, ast)) { | ||
for (const test of [...testRun.valid, ...testRun.invalid]) { | ||
if (test.type === 'ObjectExpression') { | ||
// Test case object: { code: 'const x = 123;', ... } | ||
|
||
const onlyProperty = test.properties.find( | ||
property => | ||
property.key.type === 'Identifier' && | ||
property.key.name === 'only' && | ||
property.value.type === 'Literal' && | ||
property.value.value | ||
); | ||
|
||
if (onlyProperty) { | ||
context.report({ | ||
node: onlyProperty, | ||
messageId: 'foundOnly', | ||
suggest: [ | ||
{ | ||
messageId: 'removeOnly', | ||
*fix (fixer) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const tokenBefore = sourceCode.getTokenBefore(onlyProperty); | ||
const tokenAfter = sourceCode.getTokenAfter(onlyProperty); | ||
if ( | ||
(isCommaToken(tokenBefore) && isCommaToken(tokenAfter)) || // In middle of properties | ||
(isOpeningBraceToken(tokenBefore) && isCommaToken(tokenAfter)) // At beginning of properties | ||
) { | ||
yield fixer.remove(tokenAfter); // Remove extra comma. | ||
} | ||
if (isCommaToken(tokenBefore) && isClosingBraceToken(tokenAfter)) { // At end of properties | ||
yield fixer.remove(tokenBefore); // Remove extra comma. | ||
} | ||
|
||
yield fixer.remove(onlyProperty); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} else if ( | ||
test.type === 'CallExpression' && | ||
test.callee.type === 'MemberExpression' && | ||
test.callee.object.type === 'Identifier' && | ||
test.callee.object.name === 'RuleTester' && | ||
test.callee.property.type === 'Identifier' && | ||
test.callee.property.name === 'only' | ||
) { | ||
// RuleTester.only('const x = 123;'); | ||
context.report({ node: test.callee, messageId: 'foundOnly' }); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can add a suggestion to fix it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a suggestion to remove the
only: true
property.In a follow-up PR, I will update the README to make it more clear which rules provide suggestions.