Skip to content

Commit

Permalink
[Fix] no-this-in-sfc/component detection: add arrow function to lis…
Browse files Browse the repository at this point in the history
…t of allowed position for component
  • Loading branch information
jzabala authored and ljharb committed Jul 11, 2020
1 parent 7227571 commit bcfdbd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,8 @@ function componentRule(rule, context) {
case 'AssignmentExpression':
case 'Property':
case 'ReturnStatement':
case 'ExportDefaultDeclaration': {
case 'ExportDefaultDeclaration':
case 'ArrowFunctionExpression': {
return true;
}
case 'SequenceExpression': {
Expand Down
66 changes: 47 additions & 19 deletions tests/lib/rules/no-this-in-sfc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ ruleTester.run('no-this-in-sfc', rule, {
};
}
}`
}, {
code: `
class Foo {
bar() {
() => () => {
this.something();
return null;
};
}
}`
}, {
code: `
class Foo {
Expand All @@ -131,15 +121,6 @@ ruleTester.run('no-this-in-sfc', rule, {
};
}`,
parser: parsers.BABEL_ESLINT
}, {
code: `
class Foo {
bar = () => () => {
this.something();
return null;
};
}`,
parser: parsers.BABEL_ESLINT
}, {
code: `
export const Example = ({ prop }) => {
Expand All @@ -151,6 +132,21 @@ ruleTester.run('no-this-in-sfc', rule, {
};
};`,
parser: parsers.BABEL_ESLINT
}, {
code: `
export const prepareLogin = new ValidatedMethod({
name: "user.prepare",
validate: new SimpleSchema({
}).validator(),
run({ remember }) {
if (Meteor.isServer) {
const connectionId = this.connection.id; // react/no-this-in-sfc
return Methods.prepareLogin(connectionId, remember);
}
return null;
},
});
`
}],
invalid: [{
code: `
Expand Down Expand Up @@ -217,6 +213,27 @@ ruleTester.run('no-this-in-sfc', rule, {
return <div onClick={onClick}>{this.props.foo}</div>;
}`,
errors: [{message: ERROR_MESSAGE}, {message: ERROR_MESSAGE}]
}, {
code: `
class Foo {
bar() {
return () => {
this.something();
return null;
}
}
}`,
errors: [{message: ERROR_MESSAGE}]
}, {
code: `
class Foo {
bar = () => () => {
this.something();
return null;
};
}`,
parser: parsers.BABEL_ESLINT,
errors: [{message: ERROR_MESSAGE}]
}, {
code: `
class Foo {
Expand All @@ -230,5 +247,16 @@ ruleTester.run('no-this-in-sfc', rule, {
}
}`,
errors: [{message: ERROR_MESSAGE}]
}, {
code: `
class Foo {
bar() {
() => () => {
this.something();
return null;
};
}
}`,
errors: [{message: ERROR_MESSAGE}]
}]
});

0 comments on commit bcfdbd5

Please sign in to comment.