Skip to content

Commit 4668e58

Browse files
author
Andrew Schmadel
committed
follow references on component properties
also add support for providing a template/templateUrl via an injectable function fixes #7
1 parent 63275e0 commit 4668e58

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

ng-annotate-main.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,8 @@ function matchRegular(path, ctx) {
390390
args.length === 2 && t.isLiteral(args[0]) && is.string(args[0].value) && argPaths[1]);
391391

392392
if (method.name === "component") {
393-
const controllers = target.get("properties").filter(prop => prop.node.key.name == "controller");
394-
if(controllers.length === 1) {
395-
target = controllers[0].get("value");
396-
} else {
397-
return false;
398-
}
393+
target.node.$chained = chainedRegular;
394+
return matchComponent(target);
399395
}
400396

401397
if (target) {
@@ -453,6 +449,34 @@ function matchResolve(props) {
453449
return [];
454450
}
455451

452+
function matchComponent(path){
453+
let chained = path.node.$chained;
454+
if(t.isIdentifier(path)) {
455+
path = followReference(path);
456+
if(t.isVariableDeclarator(path)){
457+
path = path.get('init');
458+
}
459+
}
460+
if(t.isObjectExpression(path)){
461+
path.node.chained = chained;
462+
const props = path.get("properties");
463+
464+
const ctrl = matchProp("controller", props);
465+
const tmpl = matchProp("template", props);
466+
const tmplUrl = matchProp("templateUrl", props);
467+
468+
let res = [];
469+
ctrl && res.push(ctrl);
470+
tmpl && res.push(tmpl);
471+
tmplUrl && res.push(tmplUrl);
472+
473+
res.forEach(t => t.node.$chained = chained);
474+
return res;
475+
} else {
476+
return false;
477+
}
478+
}
479+
456480
function renamedString(ctx, originalString) {
457481
if (ctx.rename) {
458482
return ctx.rename.get(originalString) || originalString;

tests/references.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ module.exports = {
180180
});
181181
}
182182
}
183+
},
184+
{
185+
name: "chained component controller",
186+
implicit: true,
187+
input: function() {
188+
var tmplProvider = function(bar){};
189+
var testFeedback = {
190+
controller: testFeedbackController,
191+
template: tmplProvider
192+
};
193+
function testFeedbackController(foo) {}
194+
195+
angular.module("test.feedback.pkg", [])
196+
.component("testFeedback", testFeedback);
197+
},
198+
expected: function() {
199+
testFeedbackController.$inject = ["foo"];
200+
var tmplProvider = function(bar){};
201+
tmplProvider.$inject = ["bar"];
202+
var testFeedback = {
203+
controller: testFeedbackController,
204+
template: tmplProvider
205+
};
206+
function testFeedbackController(foo) {
207+
}
208+
209+
angular.module("test.feedback.pkg", [])
210+
.component("testFeedback", testFeedback);
211+
}
183212
}
184213
]
185214
}

tests/simple.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,32 @@ module.exports = {
563563
})();
564564
angular.module("MyMod").controller("MyCtrl", myCtrl10);
565565
}
566+
},
567+
{
568+
name: "injectable component templates/controller/templateurl",
569+
implicit: true,
570+
input: function(){
571+
angular.module("mod").component("cmp", {
572+
controller: function(a){},
573+
template: function(b){},
574+
templateUrl: function(c){},
575+
}).component("cmp2", {
576+
controller: "myCtrl",
577+
template: "tmpl",
578+
templateUrl: "template.html"
579+
});
580+
},
581+
expected: function(){
582+
angular.module("mod").component("cmp", {
583+
controller: ["a", function(a){}],
584+
template: ["b", function(b){}],
585+
templateUrl: ["c", function(c){}],
586+
}).component("cmp2", {
587+
controller: "myCtrl",
588+
template: "tmpl",
589+
templateUrl: "template.html"
590+
});
591+
}
566592
}
567593
]
568594
};

0 commit comments

Comments
 (0)