Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow this in property and call descendants in render assignments #213

Closed
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
29 changes: 24 additions & 5 deletions src/rules/no-this-assign-in-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,39 @@ const rule: Rule.RuleModule = {
inRender = false;
}

/**
* Walk left side assignment members and return first non-member
*
* @param {ESTree.MemberExpression} member Member entered
* @return {ESTree.Node}
*/
function walkMembers(member: ESTree.MemberExpression): ESTree.Node {
if (member.object.type === 'MemberExpression') {
return walkMembers(member.object);
} else {
return member.object;
}
}

/**
* Left side of an assignment expr found
*
* @param {Rule.Node} node Node entered
* @return {void}
*/
function assignmentFound(node: Rule.Node): void {
if (!inRender) {
if (!inRender || node.type !== 'MemberExpression') {
return;
}

context.report({
node: node.parent,
messageId: 'noThis'
});
const nonMember = walkMembers(node);

if (nonMember.type === 'ThisExpression') {
context.report({
node: node.parent,
messageId: 'noThis'
});
}
}

return {
Expand All @@ -111,6 +129,7 @@ const rule: Rule.RuleModule = {
MethodDefinition: (node: ESTree.Node): void =>
methodEnter(node as ESTree.MethodDefinition),
'MethodDefinition:exit': methodExit,
// eslint-disable-next-line max-len
'AssignmentExpression > .left:has(ThisExpression)': (
node: Rule.Node
): void => assignmentFound(node)
Expand Down
12 changes: 12 additions & 0 deletions src/test/rules/no-this-assign-in-render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ ruleTester.run('no-this-assign-in-render', rule, {
let x;
x = this.foo || 123;
}
}`,
`class Foo extends LitElement {
render() {
const x = {};
x[this.prop] = 123;
}
}`,
`class Foo extends LitElement {
render() {
const x = () => ({});
x(this.prop).y = 123;
}
}`
],

Expand Down