Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[enhancement] member-access rule on parameter properties #3325

Merged
merged 5 commits into from Oct 18, 2017
Merged

[enhancement] member-access rule on parameter properties #3325

merged 5 commits into from Oct 18, 2017

Conversation

cyrilgandon
Copy link
Contributor

@cyrilgandon cyrilgandon commented Oct 16, 2017

PR checklist

Overview of change:

Check parameter property for rule member-access.

class myClass {
    constructor (
        readonly x: number, // <--- trigger member-access rule
        public readonly y: number, // <--- trigger member-access rule if no-public
    ) { }
}

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 and check-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

Copy link
Contributor

@ajafff ajafff left a 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)

@@ -96,6 +96,13 @@ function walk(ctx: Lint.WalkContext<Options>) {
}
}
}
if (isConstructorDeclaration(node)) {
Copy link
Contributor

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.

@@ -96,6 +96,13 @@ function walk(ctx: Lint.WalkContext<Options>) {
}
}
}
if (isConstructorDeclaration(node)) {
for (const param of node.parameters) {
if (ts.isParameterPropertyDeclaration(param)) {
Copy link
Contributor

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.

@@ -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";
Copy link
Contributor

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.

);
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;
Copy link
Contributor

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

@ajafff
Copy link
Contributor

ajafff commented Oct 16, 2017

also enable the new option in src/config/all.ts
and make sure to run yarn verify afterwards and fix the new failures in this project caused by these stricter checks

@cyrilgandon
Copy link
Contributor Author

cyrilgandon commented Oct 17, 2017

Great feedback, thank you!
There is no more error when I run yarn verify, because I added the rule option, and there is only "member-access": true, in the tslint.json, with a // TODO: Enable stricter options for these.
I'd be happy to enable those on member-access on a different PR.

@@ -0,0 +1,14 @@
class Members {
constructor(
readonly parameterPropertyR : number,
Copy link
Contributor

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

Copy link
Contributor Author

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) {}
Copy link
Contributor

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

@@ -15,7 +15,7 @@
* limitations under the License.
*/

import { getChildOfKind, getModifier, getNextToken, getTokenAtPosition, isClassLikeDeclaration } from "tsutils";
import * as utils from "tsutils";
Copy link
Contributor

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) {}
Copy link
Contributor

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

Copy link
Contributor Author

@cyrilgandon cyrilgandon left a 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?

@ajafff ajafff merged commit d197ee3 into palantir:master Oct 18, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 18, 2017

Thanks a lot @cyrilgandon

The generated files are no problem. #3310 removes them from the git repository and ignores that folder.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants