Skip to content
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
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ecmaVersion": 2015
},
"env": {
"node": true
"node": true,
"es6": true
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]`
Expand Down
45 changes: 30 additions & 15 deletions src/rules/no-es6-static-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/rules/no-es6-static-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }] }
]
};