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): [no-misused-object-likes] add rule#10104
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
1f024a20e39ae1b08e8a554cd241813dd7dad28a4ac0ba704139d0e70af7510c1ebe07605c8e20ccee09a5658fd2d21b9a8dec282File 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.{assign|entries|hasOwn|keys|values}(...)` and the `in` operator on Map/Set objects.' | ||
| --- | ||
| 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-misused-object-likes** for documentation. | ||
| Methods like `Object.assign()`, `Object.entries()`, `Object.hasOwn()`, `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,80 @@ | ||
| import type { TSESTree } from '@typescript-eslint/utils'; | ||
| import type * as ts from 'typescript'; | ||
| import { isSymbolFromDefaultLibrary } from '@typescript-eslint/type-utils'; | ||
| import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
| import { | ||
| createRule, | ||
| getParserServices, | ||
| getStaticMemberAccessValue, | ||
| } from '../util'; | ||
| const METHODS = ['assign', 'entries', 'hasOwn', 'keys', 'values']; | ||
| export default createRule({ | ||
| name: 'no-misused-object-likes', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: `Disallow using \`Object.{${METHODS.join(`|`)}}(...)\` and the \`in\` operator on Map/Set objects`, | ||
| requiresTypeChecking: true, | ||
| }, | ||
| messages: { | ||
| misusedObjectLike: | ||
| "Don't use {{used}} on {{objectClass}} objects — it will not properly check the contents.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const getSymbolIfFromDefaultLibrary = ( | ||
| node: TSESTree.Node, | ||
| ): ts.Symbol | undefined => { | ||
| const services = getParserServices(context); | ||
| const symbol = services.getTypeAtLocation(node).getSymbol(); | ||
| return isSymbolFromDefaultLibrary(services.program, symbol) | ||
| ? symbol | ||
| : undefined; | ||
| }; | ||
| const checkClassAndReport = (node: TSESTree.Node, used: string): void => { | ||
| const objectClass = getSymbolIfFromDefaultLibrary(node)?.name; | ||
| if (objectClass && /^(Readonly|Weak)?(Map|Set)$/.test(objectClass)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'misusedObjectLike', | ||
| data: { objectClass, used }, | ||
| }); | ||
| } | ||
| }; | ||
| return { | ||
| BinaryExpression(node): void { | ||
| if (node.operator === 'in') { | ||
| checkClassAndReport(node.right, 'the `in` operator'); | ||
| } | ||
| }, | ||
| CallExpression(node): void { | ||
| const { arguments: args, callee } = node; | ||
| if ( | ||
| args.length !== 1 || | ||
| callee.type !== AST_NODE_TYPES.MemberExpression | ||
| ) { | ||
| return; | ||
| } | ||
| const { object } = callee; | ||
| if ( | ||
| object.type !== AST_NODE_TYPES.Identifier || | ||
| object.name !== 'Object' || | ||
| getSymbolIfFromDefaultLibrary(object) | ||
| ) { | ||
| return; | ||
| } | ||
| const method = getStaticMemberAccessValue(callee, context); | ||
| if (typeof method === 'string' && METHODS.includes(method)) { | ||
| checkClassAndReport(args[0], `\`Object.${method}()\``); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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,120 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import rule from '../../src/rules/no-misused-object-likes'; | ||
| import { getFixturesRootDir } from '../RuleTester'; | ||
| const rootPath = getFixturesRootDir(); | ||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| parserOptions: { | ||
| project: './tsconfig.json', | ||
| tsconfigRootDir: rootPath, | ||
| }, | ||
| }, | ||
| }); | ||
| ruleTester.run('no-misused-object-likes', rule, { | ||
| valid: [ | ||
| ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.keys(map); | ||
| `, | ||
| ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.values(map); | ||
| `, | ||
| ` | ||
| class ExMap extends Map {} | ||
| const map = new ExMap(); | ||
| Object.entries(map); | ||
| `, | ||
| ` | ||
| const test = {}; | ||
| Object.entries(test); | ||
| `, | ||
| ` | ||
| const test = {}; | ||
| Object.keys(test); | ||
| `, | ||
| ` | ||
| const test = {}; | ||
| Object.values(test); | ||
| `, | ||
| ` | ||
| const test = []; | ||
| Object.keys(test); | ||
| `, | ||
| ` | ||
| const test = []; | ||
| Object.values(test); | ||
| `, | ||
| ` | ||
| const test = []; | ||
| Object.entries(test); | ||
| `, | ||
| ` | ||
| const test = 123; | ||
| Object.keys(test); | ||
| `, | ||
| 'Object.values(new (class Map {})());', | ||
| ` | ||
| const Object = { keys: () => {} }; | ||
| Object.keys(new Map()); | ||
| `, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.keys(map); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.entries(map); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const map = new Map(); | ||
| const result = Object.values(map); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.keys(set); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.entries(set); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const set = new Set(); | ||
| const result = Object.values(set); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { | ||
| code: ` | ||
| const test = new WeakMap(); | ||
| Object.keys(test); | ||
| `, | ||
| errors: [{ messageId: 'misusedObjectLike' }], | ||
| }, | ||
| { code: '4 in new Set();', errors: [{ messageId: 'misusedObjectLike' }] }, | ||
| ], | ||
| }); |
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.
Uh oh!
There was an error while loading.Please reload this page.