Skip to content
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

improve: prefer-noop supports ifContainsComment flag #341

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 35 additions & 9 deletions docs/rules/prefer-noop.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,56 @@

When defining an empty function (e.g. for callbacks) it can be more readable to use `_.noop` instead

## Rule Details
## Options

This rule takes no arguments.
This rule takes one argument, an object with one possible property - `ifContainsComment` (`ifContainsComment` default to false).

The following patterns are considered warnings:
### ifContainsComment=false

```js

functionWithCallback(function(){});
When `ifContainsComment` is `false`, any empty function is prohibited.

The following patterns are considered warnings when `ifContainsComment=false`:

```js
const emptyFunction = ()=> {};

functionWithCallback(function(){
// TODO: add error-handling
});
```

The following patterns are not considered warnings:
The following patterns are not considered warnings when `ifContainsComment=false`:

```js
const sqr = x => x * x;

functionWithCallback(function(x){return x + 1});
functionWithCallback(function(x){
// simple addition for now
return x + 1
});
```

const sqr = x => x * x;
### ifContainsComment=true

When `ifContainsComment` is `true`, empty functions are prohibited, but functions that contain only a comment are not considered empty.

The following patterns are considered warnings when `ifContainsComment=true`:

```js
const emptyFunction = ()=> {};

functionWithCallback(function(){});
```

The following patterns are not considered warnings when `ifContainsComment=true`:

```js
const emptyFunction = ()=> {};

functionWithCallback(function(){
// TODO: add error-handling
});
```

## When Not To Use It

Expand Down
32 changes: 29 additions & 3 deletions src/rules/prefer-noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,45 @@ const getDocsUrl = require('../util/getDocsUrl')
module.exports = {
meta: {
type: 'problem',
schema: [],
schema: [
{
type: 'object',
properties: {
ifContainsComment: {
type: 'boolean'
}
}
}
],
docs: {
url: getDocsUrl('prefer-noop')
}
},

create(context) {
const {getFirstFunctionLine} = require('../util/astUtil')
const ifContainsComment = context.options[0] && context.options[0].ifContainsComment

function containsComment(node) {
const code = context.getSourceCode()
const comments = code.getCommentsInside(node)
return comments.length > 0
}

function reportIfEmptyFunction(node) {
if (!getFirstFunctionLine(node) && node.parent.type !== 'MethodDefinition' && !node.generator && !node.async) {
context.report({node, message: 'Prefer _.noop over an empty function'})
if (node.parent.type === 'MethodDefinition' || node.generator || node.async) {
return
}

if (getFirstFunctionLine(node)) {
return
}

if (ifContainsComment && containsComment(node)) {
return
}

context.report({node, message: 'Prefer _.noop over an empty function'})
}

return {
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/prefer-noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ ruleTester.run('prefer-noop', rule, {
'x = a => a.b',
'class A { m() {}}',
'var x = function * () {}',
{code: 'var x = async function () {}', parserOptions: {ecmaVersion: 8}}
{code: 'var x = async function () {}', parserOptions: {ecmaVersion: 8}},
{code: 'x = () => { /* no-op for now */ }', options: [{ifContainsComment: true}]}
].map(withDefaultPragma),
invalid: [
'functionWithCb(function() {})',
'x = function(){/* */}',
'CallCb(()=> {})'
'CallCb(()=> {})',
{code: 'x = () => { /* no-op for now */ }', options: [{ifContainsComment: false}]}
].map(toErrorObject).map(withDefaultPragma)
})