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

Implement static-this rule (#4428) #4474

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const rules = {
"prefer-conditional-expression": true,
radix: true,
"restrict-plus-operands": true,
"static-this": true,
"strict-boolean-expressions": true,
"strict-type-predicates": true,
"switch-default": true,
Expand Down
91 changes: 91 additions & 0 deletions src/rules/staticThisRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @license
* Copyright 2019 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as utils from "tsutils";
import * as ts from "typescript";

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

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "static-this",
description: "Ban the use of `this` in static methods.",
options: null,
optionsDescription: "",
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING = "Unallowed `this` usage in static context";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new StaticThisWalker(sourceFile, this.getOptions()));
}
}

const enum ParentType {
None,
Class,
ClassEntity,
}

class StaticThisWalker extends Lint.RuleWalker {
public walk(sourceFile: ts.SourceFile) {
let currentParent: ParentType = ParentType.None;

const cb = (node: ts.Node): void => {
const originalParent = currentParent;

switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
currentParent = ParentType.Class;
ts.forEachChild(node, cb);
currentParent = originalParent;
return;

case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.FunctionExpression:
if (
ParentType.Class === currentParent &&
utils.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword)
) {
currentParent = ParentType.ClassEntity;
ts.forEachChild(node, cb);
currentParent = originalParent;
}
return;

case ts.SyntaxKind.ThisKeyword:
if (ParentType.ClassEntity === currentParent) {
return this.addFailureAtNode(node, Rule.FAILURE_STRING);
}
}

ts.forEachChild(node, cb);
};

return ts.forEachChild(sourceFile, cb);
}
}
14 changes: 14 additions & 0 deletions test/rules/static-this/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class StaticThis {
static const staticValue = this.value;
~~~~ [Unallowed `this` usage in static context]
static getInstance() {
return this;
~~~~ [Unallowed `this` usage in static context]
}

const value = 5;

getValue() {
return this.value;
}
}
5 changes: 5 additions & 0 deletions test/rules/static-this/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"static-this": 1
}
}