diff --git a/docs/rules/README.md b/docs/rules/README.md index 67aa97f0..afda41b4 100644 --- a/docs/rules/README.md +++ b/docs/rules/README.md @@ -191,6 +191,7 @@ There are multiple configs that enable all rules in this category: `plugin:es/no | [es/no-date-now](./no-date-now.md) | disallow the `Date.now` method. | | | [es/no-json](./no-json.md) | disallow the `JSON` class. | | | [es/no-keyword-properties](./no-keyword-properties.md) | disallow reserved words as property names. | | +| [es/no-object-create](./no-object-create.md) | disallow the `Object.create` method. | | | [es/no-object-defineproperties](./no-object-defineproperties.md) | disallow the `Object.defineProperties` method. | | | [es/no-object-defineproperty](./no-object-defineproperty.md) | disallow the `Object.defineProperty` method. | | | [es/no-object-freeze](./no-object-freeze.md) | disallow the `Object.freeze` method. | | diff --git a/docs/rules/no-object-create.md b/docs/rules/no-object-create.md new file mode 100644 index 00000000..7ce36ffd --- /dev/null +++ b/docs/rules/no-object-create.md @@ -0,0 +1,19 @@ +# es/no-object-create +> disallow the `Object.create` method + +- ✅ The following configurations enable this rule: `plugin:es/no-new-in-es5` and `plugin:es/restrict-to-es3` + +This rule reports ES5 `Object.create` method as errors. + +## Examples + +⛔ Examples of **incorrect** code for this rule: + + + +## 📚 References + +- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-object-create.js) +- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-object-create.js) diff --git a/lib/configs/no-new-in-es5.js b/lib/configs/no-new-in-es5.js index a3f92e71..a764136c 100644 --- a/lib/configs/no-new-in-es5.js +++ b/lib/configs/no-new-in-es5.js @@ -21,6 +21,7 @@ module.exports = { "es/no-date-now": "error", "es/no-json": "error", "es/no-keyword-properties": "error", + "es/no-object-create": "error", "es/no-object-defineproperties": "error", "es/no-object-defineproperty": "error", "es/no-object-freeze": "error", diff --git a/lib/index.js b/lib/index.js index 5091fc9e..d1f57e82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -128,6 +128,7 @@ module.exports = { "no-number-parseint": require("./rules/no-number-parseint"), "no-numeric-separators": require("./rules/no-numeric-separators"), "no-object-assign": require("./rules/no-object-assign"), + "no-object-create": require("./rules/no-object-create"), "no-object-defineproperties": require("./rules/no-object-defineproperties"), "no-object-defineproperty": require("./rules/no-object-defineproperty"), "no-object-entries": require("./rules/no-object-entries"), diff --git a/lib/rules/no-object-create.js b/lib/rules/no-object-create.js new file mode 100644 index 00000000..8ce5fe1f --- /dev/null +++ b/lib/rules/no-object-create.js @@ -0,0 +1,43 @@ +/** + * @author Yosuke Ota + * See LICENSE file in root directory for full license. + */ +"use strict" + +const { ReferenceTracker, READ } = require("eslint-utils") + +module.exports = { + meta: { + docs: { + description: "disallow the `Object.create` method.", + category: "ES5", + recommended: false, + url: + "http://mysticatea.github.io/eslint-plugin-es/rules/no-object-create.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: { + create: { [READ]: true }, + }, + })) { + context.report({ + node, + messageId: "forbidden", + data: { name: path.join(".") }, + }) + } + }, + } + }, +} diff --git a/tests/lib/rules/no-object-create.js b/tests/lib/rules/no-object-create.js new file mode 100644 index 00000000..c61b7f6c --- /dev/null +++ b/tests/lib/rules/no-object-create.js @@ -0,0 +1,18 @@ +/** + * @author Yosuke Ota + * See LICENSE file in root directory for full license. + */ +"use strict" + +const RuleTester = require("../../tester") +const rule = require("../../../lib/rules/no-object-create.js") + +new RuleTester().run("no-object-create", rule, { + valid: ["Object", "Object.foo", "let Object = 0; Object.create"], + invalid: [ + { + code: "Object.create", + errors: ["ES5 'Object.create' method is forbidden."], + }, + ], +})