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

Commit

Permalink
Fix #4579: false positive static-this in HOC (#4580)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbelsky authored and Josh Goldberg committed Mar 16, 2019
1 parent cb15e5c commit 66639f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/rules/staticThisRule.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
};
}
}

0 comments on commit 66639f5

Please sign in to comment.