Skip to content

Commit 8661917

Browse files
Tom910yannickcr
authored andcommitted
Fix require-optimization test for function declaration
* [FIX] In require-optimization corrected test for function declarations in classes * changed example In require-optimization
1 parent 90ebb64 commit 8661917

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lib/rules/require-optimization.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ module.exports = {
7171
* @returns {Boolean} True if node is decorated name with a custom decorated, false if not.
7272
*/
7373
var hasCustomDecorator = function (node) {
74-
var allowLenght = allowDecorators.length;
74+
var allowLength = allowDecorators.length;
7575

76-
if (allowLenght && node.decorators && node.decorators.length) {
77-
for (var i = 0; i < allowLenght; i++) {
76+
if (allowLength && node.decorators && node.decorators.length) {
77+
for (var i = 0; i < allowLength; i++) {
7878
for (var j = 0, l = node.decorators.length; j < l; j++) {
7979
if (
8080
node.decorators[j].expression &&
@@ -161,6 +161,24 @@ module.exports = {
161161
});
162162
};
163163

164+
/**
165+
* Checks if we are declaring function in class
166+
* @returns {Boolean} True if we are declaring function in class, false if not.
167+
*/
168+
var isFunctionInClass = function () {
169+
var blockNode;
170+
var scope = context.getScope();
171+
while (scope) {
172+
blockNode = scope.block;
173+
if (blockNode && blockNode.type === 'ClassDeclaration') {
174+
return true;
175+
}
176+
scope = scope.upper;
177+
}
178+
179+
return false;
180+
};
181+
164182
return {
165183
ArrowFunctionExpression: function (node) {
166184
// Stateless Functional Components cannot be optimized (yet)
@@ -175,11 +193,19 @@ module.exports = {
175193
},
176194

177195
FunctionDeclaration: function (node) {
196+
// Skip if the function is declared in the class
197+
if (isFunctionInClass()) {
198+
return;
199+
}
178200
// Stateless Functional Components cannot be optimized (yet)
179201
markSCUAsDeclared(node);
180202
},
181203

182204
FunctionExpression: function (node) {
205+
// Skip if the function is declared in the class
206+
if (isFunctionInClass()) {
207+
return;
208+
}
183209
// Stateless Functional Components cannot be optimized (yet)
184210
markSCUAsDeclared(node);
185211
},

tests/lib/rules/require-optimization.js

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ ruleTester.run('react-require-optimization', rule, {
3737
'}'
3838
].join('\n'),
3939
parserOptions: parserOptions
40+
}, {
41+
code: [
42+
'import React, {Component} from "react";',
43+
'@reactMixin.decorate(PureRenderMixin)',
44+
'class YourComponent extends Component {',
45+
' componetnDidMount () {}',
46+
' render() {}',
47+
'}'
48+
].join('\n'),
49+
parser: 'babel-eslint',
50+
parserOptions: parserOptions
4051
}, {
4152
code: [
4253
'import React from "react";' +
@@ -123,6 +134,21 @@ ruleTester.run('react-require-optimization', rule, {
123134
message: MESSAGE
124135
}],
125136
parserOptions: parserOptions
137+
}, {
138+
code: [
139+
'import React from "react";',
140+
'class YourComponent extends React.Component {',
141+
' handleClick() {}',
142+
' render() {',
143+
' return <div onClick={this.handleClick}>123</div>',
144+
' }',
145+
'}'
146+
].join('\n'),
147+
parser: 'babel-eslint',
148+
errors: [{
149+
message: MESSAGE
150+
}],
151+
parserOptions: parserOptions
126152
}, {
127153
code: [
128154
'import React, {Component} from "react";' +

0 commit comments

Comments
 (0)