Comparing version 0.0.10 to 0.0.11
'use strict'; | ||
/** | ||
* This file is heavily inspired from the annotate function in AngularJS. | ||
* Thanks a lot to all the AngularJS team. | ||
*/ | ||
var _ = require('lodash'); | ||
@@ -11,2 +16,4 @@ | ||
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | ||
var STRIP_NORMAL_COMMENTS = /((\/\/.*$)|(\/\*([^!][\s\S]*?)?\*\/))/mg; | ||
var MATCH_ARG_COMMENT = /\/\*!\s*?([\S]+)\s*?\*\// | ||
@@ -18,6 +25,6 @@ module.exports = function annotate(fn) { | ||
var $inject, | ||
fnText, | ||
argDecl, | ||
last; | ||
var $inject; | ||
var fnText; | ||
var argDecl; | ||
var last; | ||
@@ -27,6 +34,14 @@ if (typeof fn == 'function') { | ||
$inject = []; | ||
fnText = fn.toString().replace(STRIP_COMMENTS, ''); | ||
fnText = fn.toString().replace(STRIP_NORMAL_COMMENTS, ''); | ||
argDecl = fnText.match(FN_ARGS); | ||
_.each(argDecl[1].split(FN_ARG_SPLIT), function(arg){ | ||
arg.replace(FN_ARG, function(all, underscore, name){ | ||
_.each(argDecl[1].split(FN_ARG_SPLIT), function(arg) { | ||
arg.replace(FN_ARG, function(all, underscore, name) { | ||
var match = name.match(MATCH_ARG_COMMENT); | ||
if (match) { | ||
name = match[1]; | ||
} | ||
else { | ||
name = name.replace(STRIP_COMMENTS, '').trim(); | ||
} | ||
$inject.push(name); | ||
@@ -37,4 +52,4 @@ }); | ||
} | ||
} | ||
} | ||
return $inject; | ||
}; |
{ | ||
"name": "r42", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Dependency injection done right.", | ||
@@ -5,0 +5,0 @@ "author": "Quentin Raynaud <npm@qraynaud.eu>", |
@@ -83,2 +83,13 @@ ## r42 | ||
You can also use special r42 comments (looking like `/*! module */`) before your argument | ||
names: | ||
```js | ||
define(function (/*! sub/folder/test */ test) { | ||
// here test will be resolved to module 'sub/folder/test' | ||
}); | ||
``` | ||
*Note* : spaces are optional in r42 comments. | ||
##### Modules in the same folder | ||
@@ -96,5 +107,6 @@ | ||
superSub: '$super/sub', | ||
}, function (superSub, $sideFn) { | ||
}, function (superSub, $sideFn, /*! $sub/sideValue */ sideVal) { | ||
// superSub refers to 'module/super/sub' | ||
// $sideFn refers to module/sideFn | ||
// sideVal refers to $sub/sideValue | ||
}); | ||
@@ -101,0 +113,0 @@ ``` |
@@ -33,2 +33,23 @@ 'use strict'; | ||
}); | ||
it('ignore normal comments in the function arguments', function () { | ||
function testFn(/*comment*/ arg1, arg2) {} | ||
var res = annotate(testFn); | ||
expect(res).to.be.an('Array').and.to.be.deep.eql(['arg1', 'arg2']); | ||
}); | ||
it('ignore malformed r42 comments in arguments', function () { | ||
function testFn(/*!*/ arg1, /*! */ arg2) {} | ||
var res = annotate(testFn); | ||
expect(res).to.be.an('Array').and.to.be.deep.eql(['arg1', 'arg2']); | ||
}); | ||
it('reoplace argument names by r42 comments', function () { | ||
function testFn(/*! c1 */ arg1, /*!c2*/ arg2) {} | ||
var res = annotate(testFn); | ||
expect(res).to.be.an('Array').and.to.be.deep.eql(['c1', 'c2']); | ||
}); | ||
}); |
27726
23
742
172