This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
285 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @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 Lint from "../../index"; | ||
|
||
// tslint:disable: object-literal-sort-keys | ||
export const codeExamples = [ | ||
{ | ||
description: "Disallows the `this` keyword usage in static context.", | ||
config: Lint.Utils.dedent` | ||
"rules": { "static-this": true } | ||
`, | ||
pass: Lint.Utils.dedent` | ||
class Pass { | ||
static getName() { | ||
return 'name' | ||
} | ||
static getFullname() { | ||
return \`full \${Pass.getName()}\` | ||
} | ||
} | ||
`, | ||
fail: Lint.Utils.dedent` | ||
class Fail { | ||
static getName() { | ||
return 'name' | ||
} | ||
static getFullname() { | ||
return \`full \${this.getName()}\` | ||
} | ||
} | ||
`, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @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"; | ||
import { Replacement } from "../language/rule/rule"; | ||
|
||
import { codeExamples } from "./code-examples/staticThis.examples"; | ||
|
||
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.", | ||
hasFix: true, | ||
options: null, | ||
optionsDescription: "", | ||
optionExamples: [true], | ||
/* tslint:disable:max-line-length */ | ||
rationale: Lint.Utils.dedent` | ||
Static \`this\` usage can be confusing for newcomers. | ||
It can also become imprecise when used with extended classes when a static \`this\` of a parent class no longer specifically refers to the parent class. | ||
`, | ||
/* tslint:enable:max-line-length */ | ||
type: "functionality", | ||
typescriptOnly: false, | ||
codeExamples, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = | ||
"Use the parent class name instead of `this` when in a `static` context."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>) { | ||
let currentParentClass: ts.ClassLikeDeclaration | undefined; | ||
|
||
const cb = (node: ts.Node): void => { | ||
const originalParentClass = currentParentClass; | ||
|
||
if ( | ||
utils.isClassLikeDeclaration(node.parent) && | ||
utils.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword) | ||
) { | ||
currentParentClass = node.parent; | ||
ts.forEachChild(node, cb); | ||
currentParentClass = originalParentClass; | ||
} | ||
|
||
if (node.kind === ts.SyntaxKind.ThisKeyword && currentParentClass !== undefined) { | ||
let fix: Replacement | undefined; | ||
if (currentParentClass.name !== undefined) { | ||
fix = Lint.Replacement.replaceNode(node, currentParentClass.name.text); | ||
} | ||
|
||
ctx.addFailureAtNode(node, Rule.FAILURE_STRING, fix); | ||
return; | ||
} | ||
|
||
ts.forEachChild(node, cb); | ||
}; | ||
|
||
ts.forEachChild(ctx.sourceFile, cb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
class StaticThis { | ||
static value = 0; | ||
static staticValue = StaticThis.value; | ||
|
||
static _color = undefined; | ||
|
||
static get color() { | ||
return StaticThis._color; | ||
} | ||
|
||
static set color(color) { | ||
StaticThis._color = color; | ||
} | ||
|
||
static getTypeOf() { | ||
return StaticThis; | ||
} | ||
|
||
private _value = 5; | ||
|
||
getValue() { | ||
return this._value; | ||
} | ||
|
||
get length() { | ||
return this._value; | ||
} | ||
|
||
set length(value) { | ||
this._value = value; | ||
} | ||
} | ||
|
||
class Child extends StaticThis { | ||
static word = 'Hello'; | ||
static staticValue = Child.word; | ||
|
||
static getTypeOf() { | ||
return Child; | ||
} | ||
} | ||
|
||
const AnonymClass = class { | ||
static getTypeOf() { | ||
return this; | ||
} | ||
} | ||
|
||
class AnonymClassChild extends AnonymClass { | ||
static getTypeOf() { | ||
return AnonymClassChild; | ||
} | ||
} | ||
|
||
const NamedClass = class Name { | ||
static getTypeOf() { | ||
return Name; | ||
} | ||
} | ||
|
||
class NamedClassChild extends NamedClass { | ||
static getTypeOf() { | ||
return NamedClassChild; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
class StaticThis { | ||
static value = 0; | ||
static staticValue = this.value; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
|
||
static _color = undefined; | ||
|
||
static get color() { | ||
return this._color; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
|
||
static set color(color) { | ||
this._color = color; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
|
||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
|
||
private _value = 5; | ||
|
||
getValue() { | ||
return this._value; | ||
} | ||
|
||
get length() { | ||
return this._value; | ||
} | ||
|
||
set length(value) { | ||
this._value = value; | ||
} | ||
} | ||
|
||
class Child extends StaticThis { | ||
static word = 'Hello'; | ||
static staticValue = this.word; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
|
||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
} | ||
|
||
const AnonymClass = class { | ||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
} | ||
|
||
class AnonymClassChild extends AnonymClass { | ||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
} | ||
|
||
const NamedClass = class Name { | ||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
} | ||
|
||
class NamedClassChild extends NamedClass { | ||
static getTypeOf() { | ||
return this; | ||
~~~~ [Use the parent class name instead of `this` when in a `static` context.] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"static-this": true | ||
} | ||
} |