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

Fix #4579: false positive static-this in HOC #4580

Merged
merged 1 commit into from Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/rules/staticThisRule.ts
Expand Up @@ -58,13 +58,17 @@ function walk(ctx: Lint.WalkContext<void>) {
const cb = (node: ts.Node): void => {
const originalParentClass = currentParentClass;

if (
utils.isClassLikeDeclaration(node.parent) &&
utils.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword)
) {
currentParentClass = node.parent;
if (utils.isClassLikeDeclaration(node.parent)) {
currentParentClass = undefined;

if (utils.hasModifier(node.modifiers, ts.SyntaxKind.StaticKeyword)) {
currentParentClass = node.parent;
}

ts.forEachChild(node, cb);
currentParentClass = originalParentClass;

return;
}

if (node.kind === ts.SyntaxKind.ThisKeyword && currentParentClass !== undefined) {
Expand Down
12 changes: 12 additions & 0 deletions test/rules/static-this/test.ts.fix
Expand Up @@ -63,3 +63,15 @@ class NamedClassChild extends NamedClass {
return NamedClassChild;
}
}

interface Bar {}

class Foo {
public static higherOrderComponent(): any {
return class implements Bar {
public constructor() {
console.log(this);
}
};
}
}
12 changes: 12 additions & 0 deletions test/rules/static-this/test.ts.lint
Expand Up @@ -73,3 +73,15 @@ class NamedClassChild extends NamedClass {
~~~~ [Use the parent class name instead of `this` when in a `static` context.]
}
}

interface Bar {}

class Foo {
public static higherOrderComponent(): any {
return class implements Bar {
public constructor() {
console.log(this);
}
};
}
}