-
Notifications
You must be signed in to change notification settings - Fork 886
Conversation
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
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.
LGTM with suggestions
src/rules/noSelfThisRule.ts
Outdated
[ | ||
true, | ||
{ | ||
[ALLOWED_THIS_NAMES]: ["self"], |
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.
since this is a regex, the value should be ^self$
src/rules/noSelfThisRule.ts
Outdated
&& node.name.kind === ts.SyntaxKind.Identifier | ||
&& this.variableNameIsBanned(node.name.text) | ||
) { | ||
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING_FACTORY(node.name.text)); |
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.
addFailureAtNode
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.
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;
Will do.
Seems reasonable. I'll add it as an on-by-default option. |
Also fixed tests to be under the right name...
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.
Just some minor nits and suggestions
src/rules/noThisReassignmentRule.ts
Outdated
|
||
import * as Lint from "../index"; | ||
|
||
const ALLOW_THIS_DESTRUCTURING = "allow-this-destructuring"; |
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.
simply "allow-destructuring"?
src/rules/noThisReassignmentRule.ts
Outdated
import * as Lint from "../index"; | ||
|
||
const ALLOW_THIS_DESTRUCTURING = "allow-this-destructuring"; | ||
const ALLOWED_THIS_NAMES = "allowed-this-names"; |
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.
"allowed-names"?
src/rules/noThisReassignmentRule.ts
Outdated
* \`${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", |
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'd prefer no-this-assign(ment)
, because you cannot reassign this
anyway
src/rules/noThisReassignmentRule.ts
Outdated
typescriptOnly: false, | ||
}; | ||
|
||
public static readonly FAILURE_STRING_BINDINGS = "Don't reassign members of `this` to local variables."; |
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.
s/reassign/assign/
src/rules/noThisReassignmentRule.ts
Outdated
const allowedThisNames: string[] = []; | ||
let allowThisDestructuring = false; | ||
|
||
for (const ruleArgument of this.ruleArguments as RuleArgument[]) { |
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.
why the loop? there should only be one object in the config
src/rules/noThisReassignmentRule.ts
Outdated
[ | ||
true, | ||
{ | ||
[ALLOWED_THIS_NAMES]: ["self"], |
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'd still prefer "^self$"
here to show that this is a regular expression
src/rules/noThisReassignmentRule.ts
Outdated
} | ||
break; | ||
|
||
case ts.SyntaxKind.ObjectBindingPattern: |
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.
make this the default
clause. that will also handle array destructuring
src/configs/all.ts
Outdated
@@ -51,6 +51,7 @@ export const rules = { | |||
"no-namespace": true, | |||
"no-non-null-assertion": true, | |||
"no-reference": true, | |||
"no-this-reassignment": true, |
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.
also add this to tslint:latest
Also not using a for loop for options and added to :latest.
Oh, almost forgot! This was made with help from @sethbrenith; he should be mentioned in any contributors list. |
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. |
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