-
Notifications
You must be signed in to change notification settings - Fork 889
[enhancement] member-access rule on parameter properties #3325
Conversation
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.
I left some suggestions. Looks pretty good so far.
It's actually very nice that you thought about the edge case where public
cannot simply be removed from parameter properties.
It's better to add a new option for this functionality. "check-parameter-property"
should be fine (singular to keep it consistent with the other options)
src/rules/memberAccessRule.ts
Outdated
@@ -96,6 +96,13 @@ function walk(ctx: Lint.WalkContext<Options>) { | |||
} | |||
} | |||
} | |||
if (isConstructorDeclaration(node)) { |
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.
you can move this check into the for...of
loop above and use child
instead of node
. A ConstructorDeclaration
can only occur in ClassLikeDeclaration.members
.
You can also add && child.body !== undefined
to avoid checking constructor overload signatures.
src/rules/memberAccessRule.ts
Outdated
@@ -96,6 +96,13 @@ function walk(ctx: Lint.WalkContext<Options>) { | |||
} | |||
} | |||
} | |||
if (isConstructorDeclaration(node)) { | |||
for (const param of node.parameters) { | |||
if (ts.isParameterPropertyDeclaration(param)) { |
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.
prefer isParameterProperty
from tsutils
because it does a lot less work.
src/rules/memberAccessRule.ts
Outdated
@@ -159,6 +171,8 @@ function typeToString(node: ts.ClassElement): string { | |||
return "get property accessor"; | |||
case ts.SyntaxKind.SetAccessor: | |||
return "set property accessor"; | |||
case ts.SyntaxKind.Parameter: | |||
return "class parameter property"; |
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.
I think you can omit "class" since parameter properties can only occur in classes.
src/rules/memberAccessRule.ts
Outdated
); | ||
const readonlyKeyword = getModifier(node, ts.SyntaxKind.ReadonlyKeyword); | ||
// public is not optional for parameter property without the readonly modifier | ||
const isPublicOptional = !ts.isParameterPropertyDeclaration(node) || readonlyKeyword !== undefined; |
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.
you don't need to check for parameter properties here again. If a ts.ParameterDeclaration
gets passed to this function, it's always a parameter property.
You can replace the method call with node.kind !== ts.SyntaxKind.Parameter
also enable the new option in |
Great feedback, thank you! |
@@ -0,0 +1,14 @@ | |||
class Members { | |||
constructor( | |||
readonly parameterPropertyR : number, |
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.
please add a test with a decorator to make sure the public
keyword is inserted at the correct position
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.
nice idea, didn't think about decorators at all!
@@ -6,6 +6,9 @@ class { | |||
public/*some comment*/ get y() {} | |||
~~~~~~ [0] | |||
public() {} | |||
constructor(public readonly param : number) {} | |||
~~~~~~ [0] | |||
constructor(public param : number) {} |
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.
add the same for private
and protected
, to make sure it won't regress
src/rules/memberAccessRule.ts
Outdated
@@ -15,7 +15,7 @@ | |||
* limitations under the License. | |||
*/ | |||
|
|||
import { getChildOfKind, getModifier, getNextToken, getTokenAtPosition, isClassLikeDeclaration } from "tsutils"; | |||
import * as utils from "tsutils"; |
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.
We usually tend to avoid namespace imports here. It's fine if the import list spans multiple lines.
Since this is only a matter of personal preference, I leave it up to you whether you want to change it or not.
@@ -6,6 +6,9 @@ class { | |||
public/*some comment*/ get y() {} | |||
~~~~~~ [0] | |||
public() {} | |||
constructor(public readonly param : number) {} |
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.
consider adding the public
keyword to the constructor
to make sure the rule finds both failures
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.
I mistakenly pushes 2 files generated by yarn verify. It should not be part of this commit, but I guess it should be commited at one point. Would you like I revert those?
Thanks a lot @cyrilgandon The generated files are no problem. #3310 removes them from the git repository and ignores that folder. |
PR checklist
Overview of change:
Check parameter property for rule member-access.
Is there anything you'd like reviewers to focus on?
Do we need to treat this as an optional configuration, or should it be by default.
There is already
no-public
,check-accessor
andcheck-constructor
.Does a
check-parameter-property
make sense, or just let the rule be by default like the other properties.CHANGELOG.md entry:
[new-rule-option]
check-parameter-property
for member access rule