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

A value recipe with an empty array breaks the linter #425

Closed
dpatti opened this issue Dec 15, 2016 · 5 comments
Closed

A value recipe with an empty array breaks the linter #425

dpatti opened this issue Dec 15, 2016 · 5 comments

Comments

@dpatti
Copy link

dpatti commented Dec 15, 2016

If you lint this code:

angular.module('MyModule').value('emptyArray', []);

certain rules will break the linter with this:

Cannot read property 'type' of undefined
TypeError: Cannot read property 'type' of undefined
    at findFunctionByNode (eslint-plugin-angular/rules/utils/angular-rule.js:202:17)
    at checkCallee (eslint-plugin-angular/rules/utils/angular-rule.js:150:25)

I believe the issue is the assumption that the array always has elements:

/**
 * Find the function expression or function declaration by an Angular component callee.
 */
function findFunctionByNode(callExpressionNode, scope) {
    // ...
    if (node.type === 'ArrayExpression') {
        node = node.elements[node.elements.length - 1];
    }
    // ...
}
@EmmanuelDemey
Copy link
Owner

Which version of the plugin you use ?

Because we have an if statement just before this code :

if (!node) {
            return;
        }

@dpatti
Copy link
Author

dpatti commented Dec 24, 2016

I'm using the latest. The check there returns false because the node does exist, but node.elements is [], so when it runs

node = node.elements[node.elements.length - 1];

node becomes undefined (i.e., node = [][0]). It looks like the code expects all arrays in this position to be injections, but either that isn't true for empty arrays, or that isn't true for values.

@EmmanuelDemey
Copy link
Owner

You are right. Do you know which rules throw the error ?

@dpatti
Copy link
Author

dpatti commented Dec 31, 2016

When I was originally setting up the plugin, it felt like it was happening in about a third of the rules, but it was hard to tell because if any one of them threw everything would fail. The stack trace didn't help narrow it down either. After running into several, I just changed the one empty array value in our codebase to .value('emptyArray', new Array()) so that I didn't have to deal with it.

Taking a look at the source, I want to say it is any rule that requires angularRule.

@EmmanuelDemey EmmanuelDemey self-assigned this Jan 1, 2017
@EmmanuelDemey
Copy link
Owner

I can reproduce it. Will solve it this evening maybe ;) Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants