Skip to content

Commit

Permalink
feat: add es-roikoren/no-object-create rule
Browse files Browse the repository at this point in the history
  • Loading branch information
roikoren755 committed Nov 10, 2021
1 parent fd26659 commit 2238048
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-waves-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-es-roikoren': patch
---

feat: add `es-roikoren/no-object-create` rule
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ There are multiple configs that enable all rules in this category: `plugin:es-ro
| [es-roikoren/no-date-now](./no-date-now.md) | disallow the `Date.now` method. | |
| [es-roikoren/no-json](./no-json.md) | disallow the `JSON` class. | |
| [es-roikoren/no-keyword-properties](./no-keyword-properties.md) | disallow reserved words as property names. | |
| [es-roikoren/no-object-create](./no-object-create.md) | disallow the `Object.create` method | |
| [es-roikoren/no-object-defineproperties](./no-object-defineproperties.md) | disallow the `Object.defineProperties` method. | |
| [es-roikoren/no-object-defineproperty](./no-object-defineproperty.md) | disallow the `Object.defineProperty` method. | |
| [es-roikoren/no-object-freeze](./no-object-freeze.md) | disallow the `Object.freeze` method. | |
Expand Down
20 changes: 20 additions & 0 deletions docs/rules/no-object-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# es-roikoren/no-object-create
> disallow the `Object.create` method
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-es5` and `plugin:es-roikoren/restrict-to-es3`

This rule reports ES5 `Object.create` methods as errors.

## Examples

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

```js
/*eslint es-roikoren/no-object-create: error */
const obj = Object.assign({});
```

## 📚 References

- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/src/rules/no-object-create.ts)
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/tests/src/rules/no-object-create.ts)
5 changes: 3 additions & 2 deletions scripts/new-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ This rule reports ??? as errors.
⛔ Examples of **incorrect** code for this rule:
<eslint-playground type="bad" code="/*eslint es-roikoren/${ruleId}: error */
\`\`\`js
/*eslint es-roikoren/${ruleId}: error */
" />
\`\`\`
`,
);

Expand Down
1 change: 1 addition & 0 deletions src/configs/no-new-in-es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default {
'es-roikoren/no-date-now': 'error',
'es-roikoren/no-json': 'error',
'es-roikoren/no-keyword-properties': 'error',
'es-roikoren/no-object-create': 'error',
'es-roikoren/no-object-defineproperties': 'error',
'es-roikoren/no-object-defineproperty': 'error',
'es-roikoren/no-object-freeze': 'error',
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import noNumberParsefloat from './rules/no-number-parsefloat';
import noNumberParseint from './rules/no-number-parseint';
import noNumericSeparators from './rules/no-numeric-separators';
import noObjectAssign from './rules/no-object-assign';
import noObjectCreate from './rules/no-object-create';
import noObjectDefineproperties from './rules/no-object-defineproperties';
import noObjectDefineproperty from './rules/no-object-defineproperty';
import noObjectEntries from './rules/no-object-entries';
Expand Down Expand Up @@ -262,6 +263,7 @@ export default {
'no-number-parseint': noNumberParseint,
'no-numeric-separators': noNumericSeparators,
'no-object-assign': noObjectAssign,
'no-object-create': noObjectCreate,
'no-object-defineproperties': noObjectDefineproperties,
'no-object-defineproperty': noObjectDefineproperty,
'no-object-entries': noObjectEntries,
Expand Down
19 changes: 19 additions & 0 deletions src/rules/no-object-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ASTUtils } from '@typescript-eslint/experimental-utils';

import { createRule } from '../util/create-rule';
import { referenceTracker } from '../util/reference-tracker';

export const category = 'ES5';
export default createRule<[], 'forbidden'>({
name: 'no-object-create',
meta: {
type: 'problem',
docs: { description: 'disallow the `Object.create` method', recommended: false },
schema: [],
messages: { forbidden: "ES5 '{{name}}' method is forbidden." },
},
defaultOptions: [],
create(context) {
return referenceTracker(context, { Object: { create: { [ASTUtils.ReferenceTracker.READ]: true } } });
},
});
17 changes: 17 additions & 0 deletions tests/src/rules/no-object-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AST_NODE_TYPES } from '@typescript-eslint/types';

import { RuleTester } from '../../tester';
import rule from '../../../src/rules/no-object-create';

const error = {
messageId: 'forbidden' as const,
line: 1,
column: 1,
type: AST_NODE_TYPES.MemberExpression,
data: { name: 'Object.create' },
};

new RuleTester().run('no-object-create', rule, {
valid: ['Object', 'Object.foo', 'let Object = 0; Object.create'],
invalid: [{ code: 'Object.create', errors: [error] }],
});

0 comments on commit 2238048

Please sign in to comment.