Skip to content

Commit c642aaf

Browse files
committed
--plugin support
1 parent 2b03aca commit c642aaf

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Use the `--single_quotes` option to output `'$scope'` instead of `"$scope"`.
3232
Use the `--regexp` option in case you want to restrict matching further (rarely used). See
3333
description further down.
3434

35+
Use the `--plugin` option to load user plugins (experimental, 0.9.x may change API). See
36+
[plugin-example.js](plugin-example.js) for more info.
37+
3538

3639
## Tools support
3740
* [Grunt](http://gruntjs.com/): [grunt-ng-annotate](https://npmjs.org/package/grunt-ng-annotate)
@@ -161,6 +164,7 @@ ng-annotate can be used as a library. See [ng-annotate.js](ng-annotate.js) for f
161164
options and return value.
162165

163166
var ngAnnotate = require("ng-annotate");
164-
var res = ngAnnotate(src, {add: true})
167+
var somePlugin = require("./some/path/some-plugin");
168+
var res = ngAnnotate(src, {add: true, plugin: [somePlugin]})
165169
var errorstringArray = res.errors;
166170
var transformedSource = res.src;

Diff for: ng-annotate-main.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const chainedUrlRouterProvider = 2;
1515
const chainedStateProvider = 3;
1616
const chainedRegular = 4;
1717

18-
function match(node, re) {
18+
function match(node, re, matchPlugins) {
1919
const isMethodCall = (
2020
node.type === "CallExpression" &&
2121
node.callee.type === "MemberExpression" &&
@@ -26,6 +26,7 @@ function match(node, re) {
2626
(matchRegular(node, re) || matchNgRoute(node) || matchUiRouter(node) || matchHttpProvider(node)));
2727

2828
return matchMethodCalls ||
29+
(matchPlugins && matchPlugins(node)) ||
2930
matchDirectiveReturnObject(node) ||
3031
matchProviderGet(node);
3132
}
@@ -391,7 +392,22 @@ module.exports = function ngAnnotate(src, options) {
391392
stringify: stringify,
392393
};
393394

395+
const plugins = options.plugin || [];
396+
function matchPlugins(node, isMethodCall) {
397+
for (let i = 0; i < plugins.length; i++) {
398+
const res = plugins[i].match(node, isMethodCall);
399+
if (res) {
400+
return res;
401+
}
402+
}
403+
return false;
404+
}
405+
const matchPluginsOrNull = (plugins.length === 0 ? null : matchPlugins);
406+
394407
ngInjectComments.init(ctx);
408+
plugins.forEach(function(plugin) {
409+
plugin.init(ctx);
410+
});
395411

396412
traverse(ast, {pre: function(node) {
397413
const pos = node.range[0];
@@ -400,7 +416,7 @@ module.exports = function ngAnnotate(src, options) {
400416
trigger.fn.call(null, node, trigger.ctx);
401417
}
402418
}, post: function(node) {
403-
let targets = match(node, re);
419+
let targets = match(node, re, matchPluginsOrNull);
404420
if (!targets) {
405421
return;
406422
}

Diff for: ng-annotate.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const optimist = require("optimist")
2626
})
2727
.options("regexp", {
2828
describe: "detect short form myMod.controller(...) iff myMod matches regexp",
29+
})
30+
.options("plugin", {
31+
describe: "use plugin with path (experimental)",
2932
});
3033

3134
const argv = optimist.argv;
@@ -68,7 +71,22 @@ function addOption(opt) {
6871
}
6972
}
7073

71-
["add", "remove", "regexp", "single_quotes"].forEach(addOption);
74+
["add", "remove", "regexp", "single_quotes", "plugin"].forEach(addOption);
75+
76+
if (config.plugin) {
77+
if (!Array.isArray(config.plugin)) {
78+
config.plugin = [config.plugin];
79+
}
80+
config.plugin = config.plugin.map(function(path) {
81+
const absPath = tryor(fs.realpathSync.bind(fs, path), null);
82+
if (!absPath) {
83+
exit(fmt('error: plugin file not found {0}', path));
84+
}
85+
// the require below may throw an exception on parse-error
86+
// that is fine because it gives the user the line info
87+
return require(absPath);
88+
});
89+
}
7290

7391
const ret = ngAnnotate(src, config);
7492

Diff for: plugin-example.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var ctx;
2+
module.exports = {
3+
init: function(_ctx) {
4+
console.error("plugin init called, got ctx with keys " + Object.keys(_ctx));
5+
6+
// ctx contains a bunch of helpers and data
7+
// stash it away so you can use it inside match
8+
ctx = _ctx;
9+
10+
// if you want to setup position triggers now, checkout nginject-comments.js
11+
},
12+
match: function(node) {
13+
console.error("plugin match called, node with type " + node.type);
14+
15+
// if you think you have a match, return the found target node
16+
// (may or may not be the passed in argument node)
17+
// you may also return an array of target nodes
18+
19+
// ng-annotate will then execute replaceRemoveOrInsertArrayForTarget
20+
// on every target, i.e. it may remove an array (if --remove) and it may
21+
// add an array (if --add)
22+
23+
// please consider filing an issue if you need to workaround a defect or
24+
// an obviously missing feature in ng-annotate. we'll try to fix it!
25+
26+
// you know about /* @ngInject */, don't you? (you may not need a plugin)
27+
28+
// please consider sending a pull request if your plugin is of general use
29+
},
30+
};

0 commit comments

Comments
 (0)