Skip to content

Commit

Permalink
Fix require-optimization test for function declaration
Browse files Browse the repository at this point in the history
* [FIX] In require-optimization corrected test for function declarations in classes

* changed example In require-optimization
  • Loading branch information
Tom910 authored and yannickcr committed Aug 11, 2016
1 parent 90ebb64 commit 8661917
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/rules/require-optimization.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ module.exports = {
* @returns {Boolean} True if node is decorated name with a custom decorated, false if not.
*/
var hasCustomDecorator = function (node) {
var allowLenght = allowDecorators.length;
var allowLength = allowDecorators.length;

if (allowLenght && node.decorators && node.decorators.length) {
for (var i = 0; i < allowLenght; i++) {
if (allowLength && node.decorators && node.decorators.length) {
for (var i = 0; i < allowLength; i++) {
for (var j = 0, l = node.decorators.length; j < l; j++) {
if (
node.decorators[j].expression &&
Expand Down Expand Up @@ -161,6 +161,24 @@ module.exports = {
});
};

/**
* Checks if we are declaring function in class
* @returns {Boolean} True if we are declaring function in class, false if not.
*/
var isFunctionInClass = function () {
var blockNode;
var scope = context.getScope();
while (scope) {
blockNode = scope.block;
if (blockNode && blockNode.type === 'ClassDeclaration') {
return true;
}
scope = scope.upper;
}

return false;
};

return {
ArrowFunctionExpression: function (node) {
// Stateless Functional Components cannot be optimized (yet)
Expand All @@ -175,11 +193,19 @@ module.exports = {
},

FunctionDeclaration: function (node) {
// Skip if the function is declared in the class
if (isFunctionInClass()) {
return;
}
// Stateless Functional Components cannot be optimized (yet)
markSCUAsDeclared(node);
},

FunctionExpression: function (node) {
// Skip if the function is declared in the class
if (isFunctionInClass()) {
return;
}
// Stateless Functional Components cannot be optimized (yet)
markSCUAsDeclared(node);
},
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/require-optimization.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ ruleTester.run('react-require-optimization', rule, {
'}'
].join('\n'),
parserOptions: parserOptions
}, {
code: [
'import React, {Component} from "react";',
'@reactMixin.decorate(PureRenderMixin)',
'class YourComponent extends Component {',
' componetnDidMount () {}',
' render() {}',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
code: [
'import React from "react";' +
Expand Down Expand Up @@ -123,6 +134,21 @@ ruleTester.run('react-require-optimization', rule, {
message: MESSAGE
}],
parserOptions: parserOptions
}, {
code: [
'import React from "react";',
'class YourComponent extends React.Component {',
' handleClick() {}',
' render() {',
' return <div onClick={this.handleClick}>123</div>',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: MESSAGE
}],
parserOptions: parserOptions
}, {
code: [
'import React, {Component} from "react";' +
Expand Down

0 comments on commit 8661917

Please sign in to comment.