Skip to content

Commit bcfdbd5

Browse files
jzabalaljharb
authored andcommitted
[Fix] no-this-in-sfc/component detection: add arrow function to list of allowed position for component
1 parent 7227571 commit bcfdbd5

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

lib/util/Components.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ function componentRule(rule, context) {
670670
case 'AssignmentExpression':
671671
case 'Property':
672672
case 'ReturnStatement':
673-
case 'ExportDefaultDeclaration': {
673+
case 'ExportDefaultDeclaration':
674+
case 'ArrowFunctionExpression': {
674675
return true;
675676
}
676677
case 'SequenceExpression': {

tests/lib/rules/no-this-in-sfc.js

+47-19
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,6 @@ ruleTester.run('no-this-in-sfc', rule, {
112112
};
113113
}
114114
}`
115-
}, {
116-
code: `
117-
class Foo {
118-
bar() {
119-
() => () => {
120-
this.something();
121-
return null;
122-
};
123-
}
124-
}`
125115
}, {
126116
code: `
127117
class Foo {
@@ -131,15 +121,6 @@ ruleTester.run('no-this-in-sfc', rule, {
131121
};
132122
}`,
133123
parser: parsers.BABEL_ESLINT
134-
}, {
135-
code: `
136-
class Foo {
137-
bar = () => () => {
138-
this.something();
139-
return null;
140-
};
141-
}`,
142-
parser: parsers.BABEL_ESLINT
143124
}, {
144125
code: `
145126
export const Example = ({ prop }) => {
@@ -151,6 +132,21 @@ ruleTester.run('no-this-in-sfc', rule, {
151132
};
152133
};`,
153134
parser: parsers.BABEL_ESLINT
135+
}, {
136+
code: `
137+
export const prepareLogin = new ValidatedMethod({
138+
name: "user.prepare",
139+
validate: new SimpleSchema({
140+
}).validator(),
141+
run({ remember }) {
142+
if (Meteor.isServer) {
143+
const connectionId = this.connection.id; // react/no-this-in-sfc
144+
return Methods.prepareLogin(connectionId, remember);
145+
}
146+
return null;
147+
},
148+
});
149+
`
154150
}],
155151
invalid: [{
156152
code: `
@@ -217,6 +213,27 @@ ruleTester.run('no-this-in-sfc', rule, {
217213
return <div onClick={onClick}>{this.props.foo}</div>;
218214
}`,
219215
errors: [{message: ERROR_MESSAGE}, {message: ERROR_MESSAGE}]
216+
}, {
217+
code: `
218+
class Foo {
219+
bar() {
220+
return () => {
221+
this.something();
222+
return null;
223+
}
224+
}
225+
}`,
226+
errors: [{message: ERROR_MESSAGE}]
227+
}, {
228+
code: `
229+
class Foo {
230+
bar = () => () => {
231+
this.something();
232+
return null;
233+
};
234+
}`,
235+
parser: parsers.BABEL_ESLINT,
236+
errors: [{message: ERROR_MESSAGE}]
220237
}, {
221238
code: `
222239
class Foo {
@@ -230,5 +247,16 @@ ruleTester.run('no-this-in-sfc', rule, {
230247
}
231248
}`,
232249
errors: [{message: ERROR_MESSAGE}]
250+
}, {
251+
code: `
252+
class Foo {
253+
bar() {
254+
() => () => {
255+
this.something();
256+
return null;
257+
};
258+
}
259+
}`,
260+
errors: [{message: ERROR_MESSAGE}]
233261
}]
234262
});

0 commit comments

Comments
 (0)