-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(eslint-plugin): [no-unnecessary-parameter-property-assignment] add new rule #8903
Conversation
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. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
☁️ Nx Cloud ReportCI 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 targetSent with 💌 from NxCloud. |
f10ba89
to
95cbd19
Compare
There was a problem hiding this 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.
packages/eslint-plugin/docs/rules/no-unnecessary-parameter-property-assignment.mdx
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-parameter-property-assignment.test.ts
Show resolved
Hide resolved
@JoshuaKGoldberg Thank you for your review. I fixed it. |
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-parameter-property-assignment.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-parameter-property-assignment.test.ts
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-parameter-property-assignment.test.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
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:
-
this.foo = this.foo
-
setTimeout(() => this.foo = foo, 0)
declare const key: 'foo';
class Foo {
constructor(private foo: string) {
this[key] = foo;
}
}
There was a problem hiding this comment.
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
There was a problem hiding this 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 */} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
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. |
type MessageIds = 'unnecessaryAssign'; | ||
|
||
const UNNECESSARY_OPERATORS = new Set(['=', '&&=', '||=', '??=']); | ||
|
||
export default createRule<[], MessageIds>({ |
There was a problem hiding this comment.
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 ✨
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, |
There was a problem hiding this comment.
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:
requiresTypeChecking: false, |
There was a problem hiding this comment.
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 Thanks! I fixed! d22cb8b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 thanks!
description: | ||
'Disallow unnecessary assignment of constructor property parameter', | ||
}, | ||
fixable: 'code', |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 😁
No fixer is actually present. Ref: typescript-eslint#8903 (comment)
…remove `fixable` from `meta` (#9527) chore: remove `fixable` from `meta` No fixer is actually present. Ref: #8903 (comment)
PR Checklist
Overview