Skip to content
Open
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
112 changes: 108 additions & 4 deletions src/rules/no-es6-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions tests/rules/no-es6-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }] }
]
};