schmod / babel-plugin-angularjs-annotate

Add Angular 1.x dependency injection annotations to ES6 code
http://schmod.github.io/babel-plugin-angularjs-annotate
MIT License
241 stars 26 forks source link

Problem with function hoisting #29

Closed lucassus closed 6 years ago

lucassus commented 6 years ago

Input code:

module.exports = function($filterProvider) {
  'ngInject';

  function ordinalSuffixFilter(ordinalSuffix) {
    'ngInject';

    return function(day) {
      if (angular.isNumber(day) && isFinite(day)) {
        return ordinalSuffix(Number(day));
      }
    };
  }

  $filterProvider.register('sqOrdinalSuffix', ordinalSuffixFilter);

};

Output code:

'use strict';

ordinalSuffixFilter.$inject = ['ordinalSuffix'];
module.exports = ['$filterProvider', function ($filterProvider) {
  'ngInject';

  function ordinalSuffixFilter(ordinalSuffix) {
    'ngInject';

    return function (day) {
      if (angular.isNumber(day) && isFinite(day)) {
        return ordinalSuffix(Number(day));
      }
    };
  }

  $filterProvider.register('sqOrdinalSuffix', ordinalSuffixFilter);
}];

In the given example ordinalSuffixFilter.$inject = ['ordinalSuffix']; is defined on the wrong scope therefore it leads to "undefined variable orfinalSuffixFilter" error. I believe that in order to avoid such errors annotations should be added directly above the annotated function.

lucassus commented 6 years ago

It could be related to https://github.com/schmod/babel-plugin-angularjs-annotate/issues/22

schmod commented 6 years ago

I'll take a look at this. For now, you can work around this by declaring the function as a variable.

schmod commented 6 years ago

Fixed in v0.8.2