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

[Fix] In require-optimization corrected test for function declaration #744

Merged
merged 2 commits into from
Aug 11, 2016
Merged
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
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 () {}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be componentDidMount?

Copy link
Contributor Author

@Tom910 Tom910 Aug 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace on a new example

' 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