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

no-invalid-this: false positive for method syntax #4034

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions src/rules/noInvalidThisRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,63 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

const enum parentType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, enums should be CamelCase: ParentType.

Suggested change
const enum parentType {
const enum ParentType {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got it: #4376

None,
Class,
ClassMethod,
BoundRegularFunction,
UnboundRegularFunction,
}
const thisAllowedParents = new Set([parentType.ClassMethod, parentType.BoundRegularFunction]);

function walk(ctx: Lint.WalkContext<boolean>): void {
const { sourceFile, options: checkFuncInMethod } = ctx;

let currentParent: parentType = parentType.None;
let inClass = false;
let inFunctionInClass = false;

ts.forEachChild(sourceFile, function cb(node: ts.Node) {
const originalParent = currentParent;
const originalInClass = inClass;
switch (node.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
if (!inClass) {
inClass = true;
ts.forEachChild(node, cb);
inClass = false;
return;
}
break;
inClass = true;
currentParent = parentType.Class;
ts.forEachChild(node, cb);
currentParent = originalParent;
inClass = originalInClass;
return;

case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.Constructor:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.FunctionExpression:
if ((node as ts.FunctionLikeDeclaration).parameters.some(isThisParameter)) {
if (currentParent === parentType.Class) {
currentParent = parentType.ClassMethod;
ts.forEachChild(node, cb);
currentParent = originalParent;
return;
}
if (inClass) {
inFunctionInClass = true;
} else {
currentParent
= (node as ts.FunctionLikeDeclaration).parameters.some(isThisParameter)
? parentType.BoundRegularFunction
: parentType.UnboundRegularFunction;
ts.forEachChild(node, cb);
inFunctionInClass = false;
currentParent = originalParent;
return;
}
break;

case ts.SyntaxKind.ThisKeyword:
if (!inClass) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
} else if (checkFuncInMethod && inFunctionInClass) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
if (!thisAllowedParents.has(currentParent)) {
if (!inClass) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_OUTSIDE);
} else if (checkFuncInMethod) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_INSIDE);
}
}
return;
}
Expand Down
40 changes: 40 additions & 0 deletions test/rules/no-invalid-this/enabled/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function setSomeProperty(this: { someProperty: number }) {
this.someProperty = 7;
}

function bindProperty(this: { someProperty: number }, fn: Function) {
return fn.bind(this);
}

const objectLiteralStyle = {
bindProperty: function(this: { someProperty: number }, fn: Function) {
return fn.bind(this);
},
};

const objectMethodStyle = {
bindProperty(this: { someProperty: number }, fn: Function) {
return fn.bind(this);
},
};

export function f<T>(this: Observable<T>): Observable<T> {
const nestedFunction = function(this) => {
console.log(this)
Expand All @@ -10,6 +30,7 @@ class ContextualThisClass {
let nestedFunction: (this: number[]) => number[] = function(this) {
[3,4].forEach(function(nr){
console.log(this);
~~~~ [the "this" keyword is disallowed in function bodies inside class methods, use arrow functions instead]
});
return this.map(i=>i);
};
Expand Down Expand Up @@ -67,6 +88,25 @@ class AClass {
[3,4].forEach(badFunction);
let badFunction = nr => console.log(this.x === nr);
}

public fMethod() {
const objectLiteralStyle = {
bindProperty: function(this: { someProperty: number }, fn: Function) {
return fn.bind(this);
},
};

const objectMethodStyle = {
bindProperty(this: { someProperty: number }, fn: Function) {
return fn.bind(this);
},
};

return {
objectLiteralStyle,
objectMethodStyle,
};
}
}

const AClassExpression = class {
Expand Down