diff --git a/.eslintrc.json b/.eslintrc.json index 7416d71..ae4a65e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,6 +4,7 @@ "ecmaVersion": 2015 }, "env": { - "node": true + "node": true, + "es6": true } } diff --git a/README.md b/README.md index 3a4cf47..7baaaab 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ List of supported rules ----------------------- - `es5/no-es6-methods` Forbid ES2015 [methods](http://babeljs.io/learn-es2015/#math--number--string--object-apis) for `Array` and `String` - - `es5/no-es6-static-methods` Forbid ES2015 [static methods](http://babeljs.io/learn-es2015/#math--number--string--object-apis) for `Array`, `Math`, `Number`, and `Object` + - `es5/no-es6-static-methods` Forbid ES2015 [static methods](http://babeljs.io/learn-es2015/#math--number--string--object-apis) for `Array`, `Math`, `Number`, and `Object`. You can enable specific functions: `"es5/no-es6-static-methods": ["error", { exceptMethods: ["Math.imul"] }]` - `es5/no-arrow-functions`:wrench:: Forbid [arrow-functions](https://babeljs.io/learn-es2015/#ecmascript-2015-features-arrows-and-lexical-this). - `es5/no-binary-and-octal-literals`:wrench:: Forbid [binary and octal literals](https://babeljs.io/learn-es2015/#binary-and-octal-literals). - `es5/no-block-scoping`: Forbid `let` and `const` declarations. You can enable them using options: `"es5/no-block-scoping": ["error", { "let": true }]` diff --git a/src/rules/no-es6-static-methods.js b/src/rules/no-es6-static-methods.js index 3a55997..1a18014 100644 --- a/src/rules/no-es6-static-methods.js +++ b/src/rules/no-es6-static-methods.js @@ -5,27 +5,42 @@ module.exports = { docs: { description: 'Forbid methods added in ES6' }, - schema: [] + schema: [{ + type: 'object', + properties: { + exceptMethods: { + type: 'array', + items: { + type: 'string' + } + } + } + }] }, create(context) { + const options = Object.assign({ exceptMethods: [] }, context.options[0]); + const exceptMethods = new Set(options.exceptMethods); + + const es6StaticFunctions = [ + 'Array.from', + 'Array.of', + 'Math.acosh', + 'Math.hypot', + 'Math.trunc', + 'Math.imul', + 'Math.sign', + 'Number.isNaN', + 'Number.isFinite', + 'Number.isSafeInteger', + 'Object.assign', + ]; + const staticFunctions = es6StaticFunctions.filter((name) => !exceptMethods.has(name)); + return { CallExpression(node) { if(node.callee && node.callee.property && node.callee.object) { const functionName = node.callee.object.name + '.' + node.callee.property.name; - const es6StaticFunctions = [ - 'Array.from', - 'Array.of', - 'Math.acosh', - 'Math.hypot', - 'Math.trunc', - 'Math.imul', - 'Math.sign', - 'Number.isNaN', - 'Number.isFinite', - 'Number.isSafeInteger', - 'Object.assign', - ]; - if(es6StaticFunctions.indexOf(functionName) > -1) { + if(staticFunctions.includes(functionName)) { context.report({ node: node.callee.property, message: 'ES6 static methods not allowed: ' + functionName diff --git a/tests/rules/no-es6-static-methods.js b/tests/rules/no-es6-static-methods.js index 524a326..2793c12 100644 --- a/tests/rules/no-es6-static-methods.js +++ b/tests/rules/no-es6-static-methods.js @@ -3,10 +3,12 @@ module.exports = { valid: [ 'Object.create()', - 'isNaN()' + 'isNaN()', + { code: 'Math.imul()', options: [{ exceptMethods: ['Math.imul'] }] } ], invalid: [ { code: 'Object.assign()', errors: [{ message: 'ES6 static methods not allowed: Object.assign' }] }, - { code: 'Number.isNaN()', errors: [{ message: 'ES6 static methods not allowed: Number.isNaN' }] } + { code: 'Number.isNaN()', errors: [{ message: 'ES6 static methods not allowed: Number.isNaN' }] }, + { code: 'Math.imul()', options: [{ exceptMethods: ['Number.isNaN'] }], errors: [{ message: 'ES6 static methods not allowed: Math.imul' }] } ] };