Skip to content

Commit

Permalink
✨ add es/no-object-keys rule (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavnikiforov authored and mysticatea committed Dec 26, 2019
1 parent 8f0280f commit 85eb055
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/rules/no-object-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# disallow the `Object.keys` method (es/no-object-keys)

This rule reports ES5 `Object.keys` method as errors.

## Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad" code="/*eslint es/no-object-keys: error */
const keys = Object.keys(obj)
" />

## 📚 References

- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v2.0.0/lib/rules/no-object-keys.js)
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v2.0.0/tests/lib/rules/no-object-keys.js)
43 changes: 43 additions & 0 deletions lib/rules/no-object-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const { READ, ReferenceTracker } = require("eslint-utils")

module.exports = {
meta: {
docs: {
description: "disallow the `Object.keys` method.",
category: "ES5",
recommended: false,
url:
"http://mysticatea.github.io/eslint-plugin-es/rules/no-object-keys.html",
},
fixable: null,
messages: {
forbidden: "ES5 '{{name}}' method is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return {
"Program:exit"() {
const tracker = new ReferenceTracker(context.getScope())
for (const { node, path } of tracker.iterateGlobalReferences({
Object: {
keys: { [READ]: true },
},
})) {
context.report({
node,
messageId: "forbidden",
data: { name: path.join(".") },
})
}
},
}
},
}
18 changes: 18 additions & 0 deletions tests/lib/rules/no-object-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-object-keys.js")

new RuleTester().run("no-object-keys", rule, {
valid: ["Object", "Object.assign", "let Object = 0; Object.keys"],
invalid: [
{
code: "Object.keys",
errors: ["ES5 'Object.keys' method is forbidden."],
},
],
})

0 comments on commit 85eb055

Please sign in to comment.