Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): addno-object-methods-on-collections rule#11718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| description: 'Disallow using Object.keys, Object.values, and Object.entries on Map and Set instances.' | ||
| --- | ||
| import Tabs from '@theme/Tabs'; | ||
| import TabItem from '@theme/TabItem'; | ||
| > 🛑 This file is source code, not the primary documentation location! 🛑 | ||
| > | ||
| > See **https://typescript-eslint.io/rules/no-object-methods-on-collections** for documentation. | ||
| Methods like `Object.entries()`, `Object.keys()`, and `Object.values()` can be | ||
| used work with collections of data stored in objects. However, when working with `Map` or `Set` objects, even though | ||
| they are collections, using these methods are a mistake because they do not properly write to (in the case of | ||
| `Object.assign()`) or read from the object. | ||
| This rule prevents such methods from being used on `Map` and `Set` objects. | ||
| <Tabs> | ||
| <TabItem value="❌ Incorrect"> | ||
| ```ts | ||
| console.log(Object.values(new Set('abc'))); | ||
| Object.assign(new Map(), { k: 'v' }); | ||
| ``` | ||
| </TabItem> | ||
| <TabItem value="✅ Correct"> | ||
| ```ts | ||
| console.log([...new Set('abc').values()]); | ||
| new Map().set('k', 'v'); | ||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
| {/* Intentionally Omitted: When Not To Use It */} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
| import { createRule } from '../util'; | ||
| const COLLECTION_TYPES = ['Map', 'Set', 'WeakMap', 'WeakSet'] as const; | ||
| const ALTERNATIVES = { | ||
| 'Object.assign': { | ||
| Map: 'map.set(key, value) or new Map([...map, ...otherMap])', | ||
| Set: 'set.add(value) or new Set([...set, ...otherSet])', | ||
| WeakMap: 'map.set(key, value)', | ||
| WeakSet: 'set.add(value)', | ||
| }, | ||
| 'Object.entries': { | ||
| Map: 'Array.from(map.entries())', | ||
| Set: 'Array.from(set.entries())', | ||
| WeakMap: 'Array.from(map.entries())', | ||
| WeakSet: 'Array.from(set.entries())', | ||
| }, | ||
| 'Object.keys': { | ||
| Map: 'Array.from(map.keys())', | ||
| Set: 'Array.from(set.values())', | ||
| WeakMap: 'Array.from(map.keys())', | ||
| WeakSet: 'Array.from(set.values())', | ||
| }, | ||
| 'Object.values': { | ||
| Map: 'Array.from(map.values())', | ||
| Set: 'Array.from(set.values())', | ||
| WeakMap: 'Array.from(map.values())', | ||
| WeakSet: 'Array.from(set.values())', | ||
| }, | ||
| }; | ||
| export default createRule({ | ||
| name: 'no-object-methods-on-collections', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow using Object methods on Map and Set instances', | ||
| requiresTypeChecking: true, | ||
| }, | ||
| messages: { | ||
| noObjectMethodsOnCollections: | ||
| 'Using {{method}}() on a {{type}} is incorrect. Use {{alternative}} instead.', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const parserServices = ESLintUtils.getParserServices(context); | ||
| const checker = parserServices.program.getTypeChecker(); | ||
| return { | ||
| CallExpression(node) { | ||
| // Is it Object.keys/values/entries/assign() call? | ||
| if ( | ||
| node.callee.type !== AST_NODE_TYPES.MemberExpression || | ||
| node.callee.object.type !== AST_NODE_TYPES.Identifier || | ||
| node.callee.object.name !== 'Object' || | ||
| node.callee.property.type !== AST_NODE_TYPES.Identifier || | ||
| !['keys', 'values', 'entries', 'assign'].includes( | ||
| node.callee.property.name, | ||
| ) | ||
| ) { | ||
| return; | ||
| } | ||
| const methodName = node.callee.property.name; | ||
| // For keys/values/entries, we need exactly 1 argument | ||
| // For assign, we need at least 1 argument (the target) | ||
| if (methodName === 'assign') { | ||
| if (node.arguments.length < 1) { | ||
| return; | ||
| } | ||
| } else { | ||
| if (node.arguments.length !== 1) { | ||
| return; | ||
| } | ||
| } | ||
| // Get argument type as a string | ||
| const argument = node.arguments[0]; | ||
| const tsNode = parserServices.esTreeNodeToTSNodeMap.get(argument); | ||
| const type = checker.getTypeAtLocation(tsNode); | ||
| const typeString = checker.typeToString(type); | ||
| // Skip unknown types | ||
| if (typeString === 'any' || typeString === 'unknown') { | ||
| return; | ||
| } | ||
| // Is the argument a collection type? | ||
| const collectionType = COLLECTION_TYPES.find(t => | ||
| typeString.includes(t), | ||
| ); | ||
| if (!collectionType) { | ||
| return; | ||
| } | ||
| const fullMethodName = | ||
| `Object.${methodName}` as keyof typeof ALTERNATIVES; | ||
| const alternative = | ||
| ALTERNATIVES[fullMethodName][collectionType] || | ||
| `the ${collectionType}'s own methods`; | ||
| context.report({ | ||
| node, | ||
| messageId: 'noObjectMethodsOnCollections', | ||
| data: { | ||
| type: collectionType, | ||
| alternative, | ||
| method: fullMethodName, | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import rule from '../../src/rules/no-object-methods-on-collections'; | ||
| import { getFixturesRootDir } from '../RuleTester'; | ||
| const rootDir = getFixturesRootDir(); | ||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| parserOptions: { | ||
| project: './tsconfig.json', | ||
| tsconfigRootDir: rootDir, | ||
| }, | ||
| }, | ||
| }); | ||
| ruleTester.run('no-object-methods-on-collections', rule, { | ||
| valid: [ | ||
| { | ||
| code: ` | ||
| const test = {}; | ||
| Object.entries(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = {}; | ||
| Object.keys(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = {}; | ||
| Object.values(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = []; | ||
| Object.keys(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = []; | ||
| Object.values(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = []; | ||
| Object.entries(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = 123; | ||
| Object.keys(test); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const obj = {}; | ||
| Object.assign(obj, { key: 'value' }); | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| const arr = []; | ||
| Object.assign(arr, { key: 'value' }); | ||
| `, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.keys(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.entries(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.values(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.keys(set); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.entries(set); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.values(set); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.keys(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.values(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.entries(map); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = new WeakMap(); | ||
| Object.keys(test); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = new WeakSet(); | ||
| Object.values(test); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| Object.assign(map, { key: 'value' }); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| Object.assign(set, { key: 'value' }); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new WeakMap(); | ||
| Object.assign(map, { key: 'value' }); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new WeakSet(); | ||
| Object.assign(set, { key: 'value' }); | ||
| `, | ||
| errors: [{ messageId: 'noObjectMethodsOnCollections' }], | ||
| }, | ||
| ], | ||
| }); |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.