eslint-plugin-angular icon indicating copy to clipboard operation
eslint-plugin-angular copied to clipboard

TypeError: Cannot read property 'forEach' of undefined (no-directive-replace)

Open miwillhite opened this issue 8 years ago • 3 comments

I'm using directive for the first time in a very long time (it is an appropriate use case) and running into the following error:

TypeError: Cannot read property 'forEach' of undefined
    at Object.angular?directive (/aaa/node_modules/eslint-plugin-angular/rules/no-directive-replace.js:56:33)
    at /aaa/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:234:16
    at Array.forEach (native)
    at callAngularRules (/aaa/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:228:27)
    at EventEmitter.callBoth (/aaa/node_modules/eslint-plugin-angular/rules/utils/angular-rule.js:86:13)
    at emitOne (events.js:120:20)
    at EventEmitter.emit (events.js:210:7)
    at NodeEventGenerator.applySelector (/aaa/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/aaa/node_modules/eslint/lib/util/node-event-generator.js:294:22)
    at NodeEventGenerator.leaveNode (/aaa/node_modules/eslint/lib/util/node-event-generator.js:317:14)
    at CodePathAnalyzer.leaveNode (/aaa/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:626:23)
    at Traverser.leave (/aaa/node_modules/eslint/lib/linter.js:965:32)

The failing line:

fnNode.body.body.forEach(function(statement) {
  ...
}

An inspection of fnNode.body: https://gist.github.com/miwillhite/04ac6ccf9abf9bac3ec398e7512adf20

miwillhite avatar Mar 12 '18 15:03 miwillhite

When working around I find this is a problem in other rules as well (e.g. prefer-component)…

miwillhite avatar Mar 12 '18 15:03 miwillhite

Code example to reproduce this error?

xdjinnx avatar Mar 12 '18 17:03 xdjinnx

Sure! Here is the offender:

angular
.module('contact.contactShow.dashboardSite')
.directive('siteDashboard', () =>
  ({
    template,
    restrict: 'E',
    scope: true,
  })
)
.name;

It looks like when I use function () {} instead of the fat-arrow it works!

// This works fine
.directive('siteDashboard', function SiteDashboardDirective () {
  return {
    template,
    restrict: 'E',
    scope: true,
  };
})

miwillhite avatar Mar 12 '18 17:03 miwillhite