eslint-plugin-prefer-arrow
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -21,2 +21,5 @@ /** | ||
type: 'boolean' | ||
}, | ||
singleReturnOnly: { | ||
type: 'boolean' | ||
} | ||
@@ -68,7 +71,12 @@ }, | ||
const inspectNode = (node, context) => { | ||
const disallowPrototype = (context.options[0] || {}).disallowPrototype; | ||
const inspectNode = (node, context) => { | ||
const opts = context.options[0] || {}; | ||
const disallowPrototype = opts.disallowPrototype; | ||
const singleReturnOnly = opts.singleReturnOnly; | ||
if(isConstructor(node)) return; | ||
if(disallowPrototype || !isPrototypeAssignment(node)) { | ||
if (singleReturnOnly) { | ||
if (node.body.body.length === 1 && node.body.body[0].type === 'ReturnStatement') | ||
return context.report(node, 'Prefer using arrow functions over plain functions which only return a value'); | ||
} else if(disallowPrototype || !isPrototypeAssignment(node)) { | ||
return context.report(node, isNamed(node) ? | ||
@@ -75,0 +83,0 @@ 'Use const or class constructors instead of named functions' : |
{ | ||
"name": "eslint-plugin-prefer-arrow", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Prefer arrow functions in most cases", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"eslint": "^2.0.0 || ^3.0.0" | ||
"eslint": "^2.0.0 || ^3.0.0", | ||
"mocha": "^3.4.1" | ||
}, | ||
@@ -22,0 +23,0 @@ "keywords": [ |
# eslint-plugin-prefer-arrow | ||
ESLint plugin to prefer arrow functions. By default, the plugin allows usage of `function` as a member of an Object's prototype, but this can be changed with the property `disallowPrototype`. | ||
ESLint plugin to prefer arrow functions. By default, the plugin allows usage of `function` as a member of an Object's prototype, but this can be changed with the property `disallowPrototype`. Alternatively, with the `singleReturnOnly` option, this plugin only reports functions where converting to an arrow function would dramatically simplify the code. | ||
@@ -24,3 +24,4 @@ # Installation | ||
{ | ||
"disallowPrototype": true | ||
"disallowPrototype": true, | ||
"singleReturnOnly": false | ||
} | ||
@@ -31,2 +32,4 @@ ] | ||
# Configuration | ||
There is currently only one configuration option, `disallowPrototype`. If set to true, the plugin will warn if `function` is used anytime. Otherwise, the plugin allows usage of `function` if it is a member of an Object's prototype. | ||
* `disallowPrototype`: If set to true, the plugin will warn if `function` is used anytime. Otherwise, the plugin allows usage of `function` if it is a member of an Object's prototype. | ||
* `singleReturnOnly`: If set to true, the plugin will only warn for `function` declarations which *only* contain a return statement. These often look much better when declared as arrow functions without braces. Works well in conjunction with ESLint's built-in [arrow-body-style](http://eslint.org/docs/rules/arrow-body-style) set to `as-needed`. | ||
@@ -8,2 +8,4 @@ /** | ||
const singleReturnOnly = code => ({code, options: [{singleReturnOnly: true}]}); | ||
var rule = require('../../../lib/rules/prefer-arrow-functions'), | ||
@@ -24,3 +26,12 @@ RuleTester = require('eslint').RuleTester; | ||
'class obj {constructor(foo){this.foo = foo;}}; obj.prototype.func = function() {};', | ||
'class obj {constructor(foo){this.foo = foo;}}; obj.prototype = {func: function() {}};' | ||
'class obj {constructor(foo){this.foo = foo;}}; obj.prototype = {func: function() {}};', | ||
...[ | ||
'var foo = (bar) => {return bar();}', | ||
'function foo(bar) {bar()}', | ||
'var x = function foo(bar) {bar()}', | ||
'var x = function(bar) {bar()}', | ||
'function foo(bar) {/* yo */ bar()}', | ||
'function foo() {}', | ||
'function foo(bar) {bar(); return bar()}', | ||
].map(singleReturnOnly) | ||
], | ||
@@ -32,4 +43,12 @@ invalid: [ | ||
{code: '["Hello", "World"].reduce(function(a, b) { return a + " " + b; })', errors: ['Prefer using arrow functions over plain functions']}, | ||
{code: 'class obj {constructor(foo){this.foo = foo;}}; obj.prototype.func = function() {};', errors: ['Prefer using arrow functions over plain functions'], options: [{disallowPrototype:true}]} | ||
{code: 'class obj {constructor(foo){this.foo = foo;}}; obj.prototype.func = function() {};', errors: ['Prefer using arrow functions over plain functions'], options: [{disallowPrototype:true}]}, | ||
...[ | ||
'function foo() { return 3; }', | ||
'function foo(a) { return 3; }', | ||
'var foo = function() { return "World"; }', | ||
'var foo = function x() { return "World"; }', | ||
'function foo(a) { /* yo */ return 3; }', | ||
'function foo(a) { return a && (3 + a()) ? true : 99; }', | ||
].map(singleReturnOnly).map(testCase => Object.assign({errors: ['Prefer using arrow functions over plain functions which only return a value']}, testCase)) | ||
] | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6837
131
34
2