olov / ng-annotate

Add, remove and rebuild AngularJS dependency injection annotations
MIT License
2.03k stars 150 forks source link

ngInject comment does not appear to work on export arrow functions with TypeScript. #235

Closed punmechanic closed 8 years ago

punmechanic commented 8 years ago

Using TypeScript 1.7.5. Uncompiled output:

// @ngInject
export default (SettingsSvc: SettingsSvc) =>
  function dateFilter(input: string, format: string) {
    if (!format) {
      format = SettingsSvc.DateFormat
    }

    const momentDate = moment(input, format.replace('dd', 'DD'))
    return momentDate.format('dddd, MMMM, Do YYYY')
  }

Compiled output:

Object.defineProperty(exports, "__esModule", { value: true });
exports.default = function (SettingsSvc) {
    return function dateFilter(input, format) {
        if (!format) {
            format = SettingsSvc.DateFormat;
        }
        var momentDate = moment(input, format.replace('dd', 'DD'));
        return momentDate.format('dddd, MMMM, Do YYYY');
    }
};

All is fine if we use 'ngInject' inside the arrow function:

export default (SettingsSvc: SettingsSvc) => {
  'ngInject'
  return function dateFilter(input: string, format: string) {
    if (!format) {
      format = SettingsSvc.DateFormat
    }

    const momentDate = moment(input, format.replace('dd', 'DD'))
    return momentDate.format('dddd, MMMM, Do YYYY')
  }
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ["SettingsSvc", function (SettingsSvc) {
    'ngInject';
    return function dateFilter(input, format) {
        if (!format) {
            format = SettingsSvc.DateFormat;
        }
        var momentDate = moment(input, format.replace('dd', 'DD'));
        return momentDate.format('dddd, MMMM, Do YYYY');
    }
}];

I prefer the comment syntax as it can be removed after ngAnnotate has done its job. Any thoughts? Comment syntax works in other instances (export default class, for example)

olov commented 8 years ago

"ngInject" is the way to go!