Skip to content

Commit 5122de9

Browse files
author
Andrew Schmadel
committed
feature: throw an error if we try to incorrectly re-annotate an existing array
1 parent 9d2844b commit 5122de9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

nginject.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const is = require("simple-is");
88
const t = require('babel-types');
9+
const codeFrame = require("babel-code-frame");
910

1011
module.exports = {
1112
inspectComment: inspectComment,
@@ -99,9 +100,9 @@ function inspectFunction(path, ctx) {
99100
// added/rebuilt/removed => g(["a", function(a) {
100101
// "ngInject"
101102
// }]);
102-
const maybeArrayExpression = path.parent;
103+
const maybeArrayExpression = path.parentPath;
103104
if (isAnnotatedArray(maybeArrayExpression)) {
104-
addSuspect(path.parentPath, ctx, !annotate);
105+
addSuspect(maybeArrayExpression, ctx, !annotate);
105106
} else {
106107
addSuspect(path, ctx, !annotate);
107108
}
@@ -365,24 +366,36 @@ function nestedObjectValues(path, res) {
365366
return res;
366367
}
367368

368-
function isAnnotatedArray(node) {
369-
if (!t.isArrayExpression(node)) {
369+
function isAnnotatedArray(path) {
370+
if (!t.isArrayExpression(path)) {
370371
return false;
371372
}
372-
const elements = node.elements;
373+
const elements = path.get('elements');
373374

374375
// last should be a function expression
375376
let fn = elements.slice(-1)[0];
376377
if (elements.length === 0 || !isFunctionExpressionOrArrow(fn)) {
377378
return false;
378379
}
379380

381+
var fnParams = fn.node.params.map(param => param.name);
382+
if(fnParams.length > elements.length - 1){
383+
throw path.buildCodeFrameError("[angularjs-annotate] ERROR: Function parameters do not match existing annotations.");
384+
}
385+
386+
var warnedOnce = false;
380387
// all but last should be string literals
381388
for (let i = 0; i < elements.length - 1; i++) {
382389
const n = elements[i];
383-
if (!t.isLiteral(n) || !is.string(n.value)) {
390+
if (!t.isLiteral(n) || !is.string(n.node.value)) {
384391
return false;
385392
}
393+
394+
if (!warnedOnce && fnParams[i] && n.node.value !== fnParams[i]) {
395+
warnedOnce = true;
396+
var frame = codeFrame(n.hub.file.code, n.node.loc.start.line, n.node.loc.start.column + 2);
397+
console.warn("[angularjs-annotate] WARN: Function parameters do not match existing annotations.\n" + frame);
398+
}
386399
}
387400

388401
return true;

0 commit comments

Comments
 (0)