Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-misused-object-like] add rule#9369

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

Conversation

@gibson
Copy link

PR Checklist

Overview

This MR will close the problem with usageObject.keys(),Object.values() andObject.entries() for object like classesMap andSet. On this classes that methods always returns empty array instead of any meaningful data and doesn't cause and warnings or errors

epmatsw reacted with heart emoji
@typescript-eslint
Copy link
Contributor

Thanks for the PR,@gibson!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint.

@netlify
Copy link

netlifybot commentedJun 17, 2024
edited
Loading

Deploy Preview fortypescript-eslint failed.

NameLink
🔨 Latest commit716096d
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/666feffa052e5400085851b4

@nx-cloud
Copy link

nx-cloudbot commentedJun 17, 2024
edited
Loading

☁️ Nx Cloud Report

CI is running/has finished running commands for commit716096d. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 fromNxCloud.

@SimonSchick
Copy link

SimonSchick commentedJun 21, 2024
edited
Loading

This is missing support forReadonlyMapReadonlySet,WeakMap andWeakSet (and possibly others) and will likely not work with inheritance?

It may be worth-while to check if the subject of the check has any.get(arg) and.set(arg1, arg2) methods to catch issues withHeaders and friends as well, this may be slow however.

> new Headers({a:1})Headers { a: '1' }> Object.values(new Headers({a:1}))[]> Object.entries(new Headers({a:1}))[]

Overall this is a good PR, it might also be good to integrate#5677 into this and generalize this rule asno-misused-map-like-usage?

@github-actions
Copy link

Uh oh!@SimonSchick, the image you shared is missing helpful alt text. Check#9369 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text atBasic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically bygithub/accessibility-alt-text-bot.

Copy link

@SimonSchickSimonSchick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This would be extremely useful when migrating from object dictionary use to map/set use!

requiresTypeChecking: false,
},
messages: {
objectKeysForMap: "Don't use `Object.keys()` for Map objects",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this may be overly specific, granularity forSetLike andMapLike is likely sufficient since it would be weird to allow callingObject.values( on a Map but banningObject.keys

docs: {
description:
'Enforce check `Object.values(...)`, `Object.keys(...)`, `Object.entries(...)` usage with Map/Set objects',
requiresTypeChecking: false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Pretty sure this is required?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yup, theservices.getTypeAtLocation below necessitates it.

}

return {
'CallExpression[callee.object.name=Object][callee.property.name=keys][arguments.length=1]':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is missing various other ones such asObject.assign/groupBy/hasOwn?, it might be better to move this check into thecheckObjectMethodCall function and have a list of banned methods there.

JoshuaKGoldberg reacted with thumbs up emoji
checkObjectValuesForSet?: boolean;
checkObjectEntriesForSet?: boolean;
},
];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Question] Why allow such a granular set of options? Is there a known use case where someone would want to skip, say, onlyObject.values?

Copy link
Member

@JoshuaKGoldbergJoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A good start! There are some functional gaps that'll need to be filled in around more object support and resiliency to user shenanigans. Excited to see this - thanks for getting it started! 🙌

const callee = callExpression.callee as MemberExpression;
const objectMethod = (callee.property as Identifier).name;

if (argumentTypeName === 'Map') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Bug] If a user defines their ownMap class, this will falsely report:

classMap{value=1;}constmap=newMap();Object.values(map);

Comment on lines +95 to +96
const callee = callExpression.callee as MemberExpression;
const objectMethod = (callee.property as Identifier).name;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Bug] Using anas assertion on AST nodes is generally a sign of either:

  • 🍎 The node is known to be a more specific type than what you've told the type system
  • 🍌 The code isn't properly handling edge cases

TheCallExpression[callee.object.name=Object][callee.property.name=keys][arguments.length=1] below makes me think it's 🍎. Which means you'd want to use a type for the node.

typeCallExpressionWithStuff=TSESTree.CallExpression&{callee:{object:{name:'Object';// ...

}

return {
'CallExpression[callee.object.name=Object][callee.property.name=keys][arguments.length=1]':

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

object.name=Object

[Bug] False report case:

constObject={keys:(value)=>[value]};exportconstkeys=Object.keys(newMap());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Testing] Some missing test cases:

  • Intersections and unions: withMap,Set, and/or other types
  • Readonlies:ReadonlyMap,ReadonlySet

}
}

return {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Bug] Looking at built-in global objects inhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#keyed_collections, I think at leastWeakMap andWeakSet should also be handled by this rule.

@JoshuaKGoldbergJoshuaKGoldberg added the awaiting responseIssues waiting for a reply from the OP or another party labelJun 27, 2024
@SimonSchick
Copy link

@gibson are you interested in continuing work on this? Otherwise I may be inclined to take a stab at this myself.

@gibson
Copy link
Author

@SimonSchick will continue in closes time

@JoshuaKGoldberg
Copy link
Member

👋@gibson just checking in, do you still plan on working on this?

@JoshuaKGoldbergJoshuaKGoldberg added the stalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period labelAug 12, 2024
@bradzacherbradzacher changed the title#6807 Prevent misusage of Object.keys(), Object.values(), Obfeat(eslint-plugin): [no-misused-object-like] add ruleAug 25, 2024
@JoshuaKGoldberg
Copy link
Member

Closing this PR as it's been stale for ~6 weeks without activity. Feel free to reopen@gibson if you have time - but no worries if not! If anybody wants to drive it forward, please do post your own PR - and if you use this as a start, consider addingCo-authored-by: @gibson at the end of your PR description. Thanks! 😊

@SimonSchick
Copy link

I might pick this up, I ran into some scenarious/migrations where this rule would've been incredibly valuable.

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsSep 2, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg requested changes

+1 more reviewer

@SimonSchickSimonSchickSimonSchick requested changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

awaiting responseIssues waiting for a reply from the OP or another partystalePRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Rule proposal: PreventObject.values(...) usage withMap

3 participants

@gibson@SimonSchick@JoshuaKGoldberg

[8]ページ先頭

©2009-2025 Movatter.jp