EmmanuelDemey / eslint-plugin-angular

ESLint plugin for AngularJS applications
https://www.npmjs.com/package/eslint-plugin-angular
618 stars 131 forks source link

di rule crashes on correct file #469

Open LionC opened 7 years ago

LionC commented 7 years ago

Configuring the di rule to array made eslint crash on me. The file is fine (and runs without problems), the error message looks like whatever it assumes to be an injection array is actually undefined. Setup:

Versions:

"eslint": "^3.19.0",
"eslint-config-angular": "^0.5.0",
"eslint-plugin-angular": "^2.2.1"

.eslintrc.json:

{
    "extends": "angular",

    "settings": {
        "angular": 1
    },

    "rules": {
        "angular/log": "warn",

        "angular/controller-as-vm": [ "error", "self" ],
        "angular/function-type": [ "error", "named" ],
        "angular/di": [ "error", "array" ],

        "angular/no-service-method": "off"
    }
}

the angular file:

angular.module('myapp')
    .provider('loadingInterceptor', [
        '$httpProvider',
        loadingInterceptorProvider
    ])

function loadingInterceptorProvider($httpProvider) {
    var self = this

    $httpProvider.interceptors.push('loadingInterceptor')

    self.$get = [
        'Loading',
        $get
    ]

    function $get(Loading) {
        function startLoading(config) {
            Loading.startLoading()

            return config
        }

        function stopLoading(response) {
            Loading.stopLoading()

            return response
        }

        return {
            request: startLoading,
            requestError: stopLoading,
            response: stopLoading,
            responseError: stopLoading
        }
    }

}

error:

$ ./node_modules/.bin/eslint -c ./.eslintrc.json ./scripts/components/loading/loading.interceptor.js

The rule `angular/service-name` will be split up to different rules in the next version. Please read the docs for more information
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at checkDi (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint-plugin-angular/rules/di.js:63:68)
    at Object.angular?provider (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint-plugin-angular/rules/di.js:131:17)
    at /Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:234:16
    at Array.forEach (native)
    at callAngularRules (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:228:27)
    at EventEmitter.callBoth (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:86:13)
    at emitOne (events.js:101:20)
    at EventEmitter.emit (events.js:188:7)
    at NodeEventGenerator.applySelector (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/Users/LionC/Projects/acomodeo/onboarding-webapp/node_modules/eslint/lib/util/node-event-generator.js:294:22)
EmmanuelDemey commented 7 years ago

Can you send a PR with unit test ?