From 470d588607ff0c9f98a9b570d182cdb708d134cf Mon Sep 17 00:00:00 2001 From: Nikita Chuklinov Date: Sat, 27 Jun 2020 01:32:41 +0300 Subject: [PATCH] Added exceptMethods and additionalMethods options to no-es6-methods --- src/rules/no-es6-methods.js | 112 ++++++++++++++++++++++++++++++++-- tests/rules/no-es6-methods.js | 13 +++- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/src/rules/no-es6-methods.js b/src/rules/no-es6-methods.js index b313f28..dd0b2d2 100644 --- a/src/rules/no-es6-methods.js +++ b/src/rules/no-es6-methods.js @@ -52,15 +52,116 @@ module.exports = { docs: { description: 'Forbid methods added in ES6' }, - schema: [] + schema: [{ + type: 'object', + properties: { + exceptMethods: { + type: 'array', + items: { + type: 'string' + } + }, + additionalMethods: { + type: 'array', + items: { + type: 'string' + } + } + } + }] }, create(context) { + const options = Object.assign({ exceptMethods: [] }, context.options[0]); + const exceptMethods = new Set(options.exceptMethods); + const additionalMethods = new Set(options.additionalMethods); + return { CallExpression(node) { if(!node.callee || !node.callee.property) { return; } const functionName = node.callee.property.name; + const allArrayFunctions = [ + 'concat', + 'copyWithin', + 'entries', + 'every', + 'fill', + 'filter', + 'find', + 'findIndex', + 'flat', + 'flatMap', + 'forEach', + 'from', + 'includes', + 'indexOf', + 'isArray', + 'join', + 'keys', + 'lastIndexOf', + 'length', + 'map', + 'of', + 'pop', + 'push', + 'reduce', + 'reduceRight', + 'reverse', + 'shift', + 'slice', + 'some', + 'sort', + 'splice', + 'toLocaleString', + 'toSource', + 'toString', + 'unshift', + 'values' + ]; + const allStringFunctions = [ + 'charAt', + 'charCodeAt', + 'codePointAt', + 'concat', + 'endsWith', + 'fromCharCode', + 'fromCodePoint', + 'includes', + 'indexOf', + 'lastIndexOf', + 'length', + 'localeCompare', + 'match', + 'matchAll', + 'normalize', + 'padEnd', + 'padStart', + 'raw', + 'repeat', + 'replace', + 'replaceAll', + 'search', + 'slice', + 'split', + 'startsWith', + 'substring', + 'toLocaleLowerCase', + 'toLocaleUpperCase', + 'toLowerCase', + 'toSource', + 'toString', + 'toUpperCase', + 'trim', + 'trimEnd', + 'trimStart' + ]; + const allFunctions = [].concat( + allArrayFunctions, + allStringFunctions + ) + const additionalFunctions = allFunctions.filter(name => additionalMethods.has(name)); + const es6ArrayFunctions = [ 'find', 'findIndex', @@ -74,11 +175,14 @@ module.exports = { 'includes', 'repeat' ]; - const es6Functions = [].concat( + const baseEs6Functions = [].concat( es6ArrayFunctions, - es6StringFunctions + es6StringFunctions, + additionalFunctions ); - if (es6Functions.indexOf(functionName) > -1 && !isPermitted(node.callee)) { + const es6functions = baseEs6Functions.filter((name) => !exceptMethods.has(name)); + + if (es6functions.indexOf(functionName) > -1 && !isPermitted(node.callee)) { context.report({ node: node.callee.property, message: 'ES6 methods not allowed: ' + functionName diff --git a/tests/rules/no-es6-methods.js b/tests/rules/no-es6-methods.js index 83cfbaa..6c9476f 100644 --- a/tests/rules/no-es6-methods.js +++ b/tests/rules/no-es6-methods.js @@ -8,10 +8,19 @@ module.exports = { '_.chain([1, 2, 3]).reverse().find().value()', '$("#myDiv").find(".child")', 'jQuery("#myDiv").parent().find(".child")', - '$myForm.find(".child")' + '$myForm.find(".child")', + '[[1, 2], [3, 4]].flat()', + { code: 'jqVariable.find(x => x == 3);', options: [{ exceptMethods: ['find'] }] }, + { code: 'a.includes("123");', options: [{ exceptMethods: ['includes'] }] }, ], invalid: [ { code: '[1, 2, 3].find(x => x == 3);', errors: [{ message: 'ES6 methods not allowed: find' }] }, - { code: '[0, 0, 0].fill(7, 1);', errors: [{ message: 'ES6 methods not allowed: fill' }] } + { code: '[0, 0, 0].fill(7, 1);', errors: [{ message: 'ES6 methods not allowed: fill' }] }, + { code: '[1, 2, 3].filter(x => x !== 3);', + options: [{ exceptMethods: ['map'], additionalMethods: ['filter'] }], + errors: [{ message: 'ES6 methods not allowed: filter' }] }, + { code: '[[1, 2], [3, 4]].flat()', + options: [{ exceptMethods: ['map'], additionalMethods: ['flat'] }], + errors: [{ message: 'ES6 methods not allowed: flat' }] } ] };