Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($inject): enhance annotate function to work with inheritance #16344

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ function annotate(fn, strictDi, name) {
last;

if (typeof fn === 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
$inject = fn.$inject;
if (!$inject || !fn.hasOwnProperty('$inject')) {
var $computedInject = [];
if (fn.length) {
if (strictDi) {
if (!isString(name) || !name) {
Expand All @@ -109,10 +110,17 @@ function annotate(fn, strictDi, name) {
argDecl = extractArgs(fn);
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg) {
arg.replace(FN_ARG, function(all, underscore, name) {
$inject.push(name);
$computedInject.push(name);
});
});
}

var proto = fn.prototype && Object.getPrototypeOf(fn.prototype);
if (!$computedInject.length && !$inject && proto && proto.constructor) {
$inject = annotate(proto.constructor, strictDi, name);
} else {
$inject = $computedInject.length ? $computedInject : ($inject || []);
}
fn.$inject = $inject;
}
} else if (isArray(fn)) {
Expand Down
71 changes: 71 additions & 0 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,45 @@ describe('injector', function() {
});


it('should handle inheritance without own args', function() {
function Base(a) {
}
function Sub() {
Base.apply(this, arguments);
}
Sub.prototype = Object.create(Base.prototype);
Sub.prototype.constructor = Sub;

expect(annotate(Base)).toEqual(['a']);
expect(annotate(Sub)).toEqual(['a']);

delete Base.$inject;
delete Sub.$inject;

expect(annotate(Sub)).toEqual(['a']);
expect(annotate(Base)).toEqual(['a']);
});

it('should handle inheritance with different args', function() {
function Base(a) {
}
function Sub(x, a) {
Base.call(this, a);
}
Sub.prototype = Object.create(Base.prototype);
Sub.prototype.constructor = Sub;

expect(annotate(Base)).toEqual(['a']);
expect(annotate(Sub)).toEqual(['x', 'a']);

delete Base.$inject;
delete Sub.$inject;

expect(annotate(Sub)).toEqual(['x', 'a']);
expect(annotate(Base)).toEqual(['a']);
});


describe('es6', function() {
if (support.shorthandMethods) {
// The functions are generated using `eval` as just having the ES6 syntax can break some browsers.
Expand Down Expand Up @@ -501,6 +540,38 @@ describe('injector', function() {

expect(instance).toEqual(jasmine.any(Clazz));
});

it('should annotate correctly when inheriting with different constructor args', function() {
// eslint-disable-next-line no-eval
var clazzes = eval('(() => { class Base { constructor(a) {} }; class Sub extends Base { constructor(x, a) {super(a);} }; return {Base,Sub}})()');
var Base = clazzes.Base;
var Sub = clazzes.Sub;

expect(annotate(Base)).toEqual(['a']);
expect(annotate(Sub)).toEqual(['x', 'a']);

delete Base.$inject;
delete Sub.$inject;

expect(annotate(Sub)).toEqual(['x', 'a']);
expect(annotate(Base)).toEqual(['a']);
});

it('should annotate correctly when inheriting without subclass constructor', function() {
// eslint-disable-next-line no-eval
var clazzes = eval('(() => { class Base { constructor(a) {} }; class Sub extends Base {}; return {Base,Sub}})()');
var Base = clazzes.Base;
var Sub = clazzes.Sub;

expect(annotate(Base)).toEqual(['a']);
expect(annotate(Sub)).toEqual(['a']);

delete Base.$inject;
delete Sub.$inject;

expect(annotate(Sub)).toEqual(['a']);
expect(annotate(Base)).toEqual(['a']);
});
}
});

Expand Down