Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [no-unnecessary-parameter-property-assignment] add new rule #8903

Merged
merged 10 commits into from
Jul 6, 2024

Conversation

yeonjuan
Copy link
Contributor

@yeonjuan yeonjuan commented Apr 11, 2024

PR Checklist

Overview

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @yeonjuan!

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 on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Apr 11, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit b2690e4
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66813ec59074a1000808408a
😎 Deploy Preview https://deploy-preview-8903--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Apr 11, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit b2690e4. 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 💌 from NxCloud.

@yeonjuan yeonjuan force-pushed the new-rule/7045 branch 4 times, most recently from f10ba89 to 95cbd19 Compare April 13, 2024 15:17
@yeonjuan yeonjuan marked this pull request as ready for review April 13, 2024 16:10
@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label May 28, 2024
@JoshuaKGoldberg JoshuaKGoldberg changed the title feat(eslint-plugin): [no-unnecessary-property-assignment] add new rule feat(eslint-plugin): [no-unnecessary-parameter-property-assignment] add new rule Jun 2, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

This is looking great! Sorry for the long review time on it. It looked more intimidating from the outside to me than it ended up being 😅. A testament to the nice clean code on the inside!

Because scope analysis tends to be tricky, I went with a little more scrutiny than average on granular code nitpicking.

Brown cartoon cat ("Goma") nodding with sunglasses and crossed arms. Caption: "YES"

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 2, 2024
@yeonjuan
Copy link
Contributor Author

yeonjuan commented Jun 4, 2024

@JoshuaKGoldberg Thank you for your review. I fixed it.

@yeonjuan yeonjuan requested a review from JoshuaKGoldberg June 12, 2024 15:37
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jun 12, 2024
Copy link
Contributor

Choose a reason for hiding this comment

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

More test cases I have in mind:

  1. this.foo = this.foo

  2. setTimeout(() => this.foo = foo, 0)

  3. bitwise/shifting operators?

declare const key: 'foo';

class Foo {
  constructor(private foo: string) {
    this[key] = foo;
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

setTimeout(() => this.foo = foo, 0)

That's covered enough by the (() => {})() IMO. This just generally needs to check a call expression.

bitwise/shifting operators`

I think we're fine with the existing set of operators. They're similar enough in the AST that we don't need to be exhaustive.

declare const key: 'foo'

Covered by declare const name: string IMO

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

This looks lovely, thanks! 🧹

I only have small nitpicks left - happy to apply these before our Monday release.

</TabItem>
</Tabs>

{/* Intentionally Omitted: When Not To Use It */}
Copy link
Member

Choose a reason for hiding this comment

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

[Docs] To mention: if you don't use parameter properties, you can ignore this rule.

>
> See **https://typescript-eslint.io/rules/no-unnecessary-parameter-property-assignment** for documentation.

Parameter properties let you create and initialize a member in one place.
Copy link
Member

Choose a reason for hiding this comment

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

[Docs] This explanation is pretty sparse. We've been trying to flesh out our docs to be more useful. How about linking to the TypeScript docs? https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties

Suggested change
Parameter properties let you create and initialize a member in one place.
[TypeScript's parameter properties](https://www.typescriptlang.org/docs/handbook/2/classes.html#parameter-properties) allow creating and initializing a member in one place.

Comment on lines 7 to 11
type MessageIds = 'unnecessaryAssign';

const UNNECESSARY_OPERATORS = new Set(['=', '&&=', '||=', '??=']);

export default createRule<[], MessageIds>({
Copy link
Member

Choose a reason for hiding this comment

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

[Style] No need to explicitly declare those type arguments ✨

Suggested change
type MessageIds = 'unnecessaryAssign';
const UNNECESSARY_OPERATORS = new Set(['=', '&&=', '||=', '??=']);
export default createRule<[], MessageIds>({
const UNNECESSARY_OPERATORS = new Set(['=', '&&=', '||=', '??=']);
export default createRule({

docs: {
description:
'Disallow unnecessary assignment of constructor property parameter',
requiresTypeChecking: false,
Copy link
Member

Choose a reason for hiding this comment

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

[Style] TIL three existing rules declare this explicitly ... but they don't need to:

Suggested change
requiresTypeChecking: false,

Copy link
Member

Choose a reason for hiding this comment

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

setTimeout(() => this.foo = foo, 0)

That's covered enough by the (() => {})() IMO. This just generally needs to check a call expression.

bitwise/shifting operators`

I think we're fine with the existing set of operators. They're similar enough in the AST that we don't need to be exhaustive.

declare const key: 'foo'

Covered by declare const name: string IMO

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jun 29, 2024
@yeonjuan
Copy link
Contributor Author

@JoshuaKGoldberg Thanks! I fixed! d22cb8b

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

🚀 thanks!

@JoshuaKGoldberg JoshuaKGoldberg merged commit 451e738 into typescript-eslint:main Jul 6, 2024
61 checks passed
description:
'Disallow unnecessary assignment of constructor property parameter',
},
fixable: 'code',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this true? I'm not able to get it to fix my code, and I don't see where a fixer is passed to context.report.

Copy link
Member

Choose a reason for hiding this comment

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

Aha! You're right - this was an oversight. eslint/eslint#18008 would have caught it.

Are you up for filing an issue and/or sending a PR @dasa? It'd be a great contribution 😁

dasa added a commit to dasa/typescript-eslint that referenced this pull request Jul 9, 2024
JoshuaKGoldberg pushed a commit that referenced this pull request Jul 9, 2024
…remove `fixable` from `meta` (#9527)

chore: remove `fixable` from `meta`

No fixer is actually present.

Ref: #8903 (comment)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge enhancement: new plugin rule New rule request for eslint-plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: forbid unnecessary assignment of parameter properties
5 participants