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

Added a no-this-reassignment rule #2931

Merged
merged 8 commits into from Jun 19, 2017
Merged

Added a no-this-reassignment rule #2931

merged 8 commits into from Jun 19, 2017

Conversation

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Jun 15, 2017

PR checklist

Overview of change:

Bans phrases like "var self = this;". Port of the tslint-microsoft-contrib no-var-self rule.

CHANGELOG.md entry:

[new-rule] no-this-reassignment

Josh Goldberg added 2 commits June 15, 2017 14:00
Bans phrases like "var self = this;". Port of the [tslint-microsoft-contrib no-var-self rule](https://github.com/Microsoft/tslint-microsoft-contrib/blob/master/src/noVarSelfRule.ts).

This commit does not yet add it to tslint:all.
No violations in the code base
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.

LGTM with suggestions

src/configs/all.ts Outdated Show resolved Hide resolved
[
true,
{
[ALLOWED_THIS_NAMES]: ["self"],
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is a regex, the value should be ^self$

&& node.name.kind === ts.SyntaxKind.Identifier
&& this.variableNameIsBanned(node.name.text)
) {
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING_FACTORY(node.name.text));
Copy link
Contributor

Choose a reason for hiding this comment

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

addFailureAtNode

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

how about naming this no-this-reassignment?

does this work for destructuring? I'd really like to ban constructs like this as well:

const { props, state } = this;

@JoshuaKGoldberg
Copy link
Contributor Author

no-this-reassignment

Will do.

const { props, state } = this;

Seems reasonable. I'll add it as an on-by-default option.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Added a no-self-this rule Added a no-this-reassignment rule Jun 18, 2017
Josh Goldberg added 2 commits June 17, 2017 22:36
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.

Just some minor nits and suggestions


import * as Lint from "../index";

const ALLOW_THIS_DESTRUCTURING = "allow-this-destructuring";
Copy link
Contributor

Choose a reason for hiding this comment

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

simply "allow-destructuring"?

import * as Lint from "../index";

const ALLOW_THIS_DESTRUCTURING = "allow-this-destructuring";
const ALLOWED_THIS_NAMES = "allowed-this-names";
Copy link
Contributor

Choose a reason for hiding this comment

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

"allowed-names"?

* \`${ALLOWED_THIS_NAMES}\` may be specified as a list of regular expressions to match allowed variable names.`,
rationale: "Assigning a variable to `this` instead of properly using arrow lambdas"
+ "may be a symptom of pre-ES6 practices or not manging scope well.",
ruleName: "no-this-reassignment",
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer no-this-assign(ment), because you cannot reassign this anyway

typescriptOnly: false,
};

public static readonly FAILURE_STRING_BINDINGS = "Don't reassign members of `this` to local variables.";
Copy link
Contributor

Choose a reason for hiding this comment

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

s/reassign/assign/

const allowedThisNames: string[] = [];
let allowThisDestructuring = false;

for (const ruleArgument of this.ruleArguments as RuleArgument[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why the loop? there should only be one object in the config

[
true,
{
[ALLOWED_THIS_NAMES]: ["self"],
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd still prefer "^self$" here to show that this is a regular expression

}
break;

case ts.SyntaxKind.ObjectBindingPattern:
Copy link
Contributor

Choose a reason for hiding this comment

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

make this the default clause. that will also handle array destructuring

@@ -51,6 +51,7 @@ export const rules = {
"no-namespace": true,
"no-non-null-assertion": true,
"no-reference": true,
"no-this-reassignment": true,
Copy link
Contributor

Choose a reason for hiding this comment

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

also add this to tslint:latest

Josh Goldberg added 2 commits June 18, 2017 15:35
Also not using a for loop for options and added to :latest.
@adidahiya adidahiya merged commit 5cf7100 into palantir:master Jun 19, 2017
@JoshuaKGoldberg JoshuaKGoldberg deleted the no-self-this branch June 19, 2017 16:53
@JoshuaKGoldberg
Copy link
Contributor Author

Oh, almost forgot! This was made with help from @sethbrenith; he should be mentioned in any contributors list.

@sethbrenith
Copy link

Thanks for the shout-out, but I think this is all you. The only thing I wrote is a fixer for it, which doesn't seem to be included here.

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

4 participants