Skip to content

New: fixer-return #15

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 8 commits into from
Jul 18, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Then configure the rules you want to use under the rules section.
Name | ✔️ | 🛠 | Description
----- | ----- | ----- | -----
[consistent-output](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/consistent-output.md) | | | Enforces consistent use of output assertions in rule tests
[fixer-return](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/fixer-return.md) | | | Enforces always return from a fixer function
[no-deprecated-report-api](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-deprecated-report-api.md) | ✔️ | 🛠 | Prohibits the deprecated `context.report(node, message)` API
[no-identical-tests](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-identical-tests.md) | | 🛠 | Disallows identical tests
[no-missing-placeholders](https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/no-missing-placeholders.md) | ✔️ | | Disallows missing placeholders in rule report messages
Expand Down
41 changes: 41 additions & 0 deletions docs/rules/fixer-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Enforces always return from a fixer function (fixer-return)

In a fixable rule, missing return from a fixer function will not apply fixes.

## Rule Details

This rule enforces that fixer functions always return a value.

Examples of **incorrect** code for this rule:

```js
/* eslint eslint-plugin/fixer-return: error */
module.exports = {
create: function(context) {
context.report( {
fix: function(fixer) {
fixer.foo();
}
});
}
};
```

Examples of **correct** code for this rule:

```js
/* eslint eslint-plugin/fixer-return: error */
module.exports = {
create: function(context) {
context.report( {
fix: function(fixer) {
return fixer.foo();
}
});
}
};
```

## When Not To Use It

If you don't want to enforce always return from a fixer function, do not enable this rule.
106 changes: 106 additions & 0 deletions lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @fileoverview Enforces always return from a fixer function
* @author 薛定谔的猫<[email protected]>
*/

'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'Expected fixer function to always return a value.',
category: 'Possible Errors',
recommended: false,
},
fixable: null,
},

create (context) {
const message = 'Expected fixer function to always return a value.';
let funcInfo = {
upper: null,
codePath: null,
hasReturn: false,
shouldCheck: false,
node: null,
};
let contextIdentifiers;

/**
* Checks whether or not the last code path segment is reachable.
* Then reports this function if the segment is reachable.
*
* If the last code path segment is reachable, there are paths which are not
* returned or thrown.
*
* @param {ASTNode} node - A node to check.
* @returns {void}
*/
function checkLastSegment (node) {
if (funcInfo.shouldCheck && funcInfo.codePath.currentSegments.some(segment => segment.reachable)) {
context.report({
node,
loc: (node.id || node).loc.start,
message,
});
}
}

return {
Program (node) {
contextIdentifiers = utils.getContextIdentifiers(context, node);
},

// Stacks this function's information.
onCodePathStart (codePath, node) {
const parent = node.parent;
const shouldCheck = node.type === 'FunctionExpression' &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.callee.object) &&
parent.parent.parent.callee.property.name === 'report' &&
utils.getReportInfo(parent.parent.parent.arguments).fix === node;

funcInfo = {
upper: funcInfo,
codePath,
hasReturn: false,
shouldCheck,
node,
};
},

// Pops this function's information.
onCodePathEnd () {
funcInfo = funcInfo.upper;
},

// Checks the return statement is valid.
ReturnStatement (node) {
if (funcInfo.shouldCheck) {
funcInfo.hasReturn = true;

if (!node.argument) {
context.report({
node,
message,
});
}
}
},

// Reports a given function if the last path is reachable.
'FunctionExpression:exit': checkLastSegment,
};
},
};
62 changes: 62 additions & 0 deletions tests/lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @fileoverview enforces always return from a fixer function
* @author 薛定谔的猫<[email protected]>
*/

'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/fixer-return');
const RuleTester = require('eslint').RuleTester;

const ERROR = { message: 'Expected fixer function to always return a value.' };

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run('fixer-return', rule, {
valid: [
`
module.exports = {
create: function(context) {
context.report( {
fix: function(fixer) {
return fixer.foo();
}
});
}
};
`,
`
module.exports = {
create: function(context) {
context.report({
fix: fixer => fixer.foo()
});
}
};
`,
],

invalid: [
{
code: `
module.exports = {
create: function(context) {
context.report({
fix(fixer) {
fixer.foo();
}
});
}
};
`,
errors: [ERROR],
},
],
});