ng-test-utils
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -6,4 +6,5 @@ var istanbul = require('istanbul'), | ||
collector.add(require('./coverage/nohack/coverage.json')); | ||
collector.add(require('./coverage/hack/coverage.json')); | ||
collector.add(require('./coverage/recast/coverage.json')); | ||
collector.add(require('./coverage/esprima/coverage.json')); | ||
collector.add(require('./coverage/acorn/coverage.json')); | ||
@@ -10,0 +11,0 @@ reporter.add('text'); |
{ | ||
"name": "ng-test-utils", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "angular test utilities", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "jshint src/** test/**", | ||
"test": "npm run lint && npm run cover", | ||
"test-nohack": "mocha mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"test-hack": "RECAST_COMMENT_HACK=true mocha mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"watch": "mocha -w --growl mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"cover": "rm -rf coverage && npm run cover-nohack && npm run cover-hack && node combine-coverage", | ||
"cover-nohack": "istanbul cover --report none --dir ./coverage/nohack _mocha -- mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"cover-hack": "RECAST_COMMENT_HACK=true istanbul cover --report none --dir ./coverage/hack _mocha -- mocha-globals.js test/*-test.js test/**/*-test.js" | ||
}, | ||
"repository": { | ||
@@ -31,7 +21,8 @@ "type": "git", | ||
"dependencies": { | ||
"ast-types": "^0.6.15", | ||
"recast": "^0.10.1", | ||
"convert-source-map": "git://github.com/jamestalmage/convert-source-map#4397e24b9a41b19b964ea0a4b6eb0858d6a3450f" | ||
"convert-source-map": "git://github.com/jamestalmage/convert-source-map#4397e24b9a41b19b964ea0a4b6eb0858d6a3450f", | ||
"merge": "^1.2.0", | ||
"recast": "^0.10.1" | ||
}, | ||
"devDependencies": { | ||
"acorn": "^0.12.0", | ||
"chai": "^2.1.0", | ||
@@ -42,2 +33,3 @@ "coveralls": "^2.11.2", | ||
"istanbul": "^0.3.6", | ||
"jscs": "^1.11.3", | ||
"jshint": "^2.6.3", | ||
@@ -48,3 +40,18 @@ "mocha": "^2.1.0", | ||
"sinon-chai": "^2.7.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint && npm run cover && npm run check-style", | ||
"cover": "rm -rf coverage && npm run cover-recast && npm run cover-esprima && npm run cover-acorn && node combine-coverage", | ||
"watch": "mocha -w --growl mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"lint": "jshint src/** test/**", | ||
"check-style": "jscs test/** src/**", | ||
"test-run": "mocha mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"cover-run": "istanbul cover --report none --root ./src --dir ./coverage/$NG_UTILS_PARSER _mocha -- mocha-globals.js test/*-test.js test/**/*-test.js", | ||
"test-recast": "NG_UTILS_PARSER=recast npm run test-run", | ||
"test-esprima": "NG_UTILS_PARSER=esprima npm run test-run", | ||
"test-acorn": "NG_UTILS_PARSER=acorn npm run test-run", | ||
"cover-recast": "NG_UTILS_PARSER=recast npm run cover-run", | ||
"cover-esprima": "NG_UTILS_PARSER=esprima npm run cover-run", | ||
"cover-acorn": "NG_UTILS_PARSER=acorn npm run cover-run" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
module.exports = transform; | ||
@@ -7,31 +9,39 @@ | ||
var convert = require('convert-source-map'); | ||
var merge = require('merge'); | ||
function transform(src, suppliedOptions){ | ||
var options = {}; | ||
suppliedOptions = suppliedOptions || {}; | ||
if(suppliedOptions.sourceMap){ | ||
if(suppliedOptions.sourceFileName) options.sourceFileName = suppliedOptions.sourceFileName; | ||
if(suppliedOptions.sourceFileName && !suppliedOptions.sourceMapName){ | ||
options.sourceMapName = suppliedOptions.sourceFileName + '.map'; | ||
function transform(src, suppliedOptions) { | ||
var options = merge({ | ||
readSourceMapComments:true, | ||
ngInject:true, | ||
ngProvide:true | ||
}, suppliedOptions); | ||
if (options.sourceMap) { | ||
if (options.sourceFileName && !options.sourceMapName) { | ||
options.sourceMapName = options.sourceFileName + '.map'; | ||
} | ||
if(suppliedOptions.inputSourceMap){ | ||
options.inputSourceMap = suppliedOptions.inputSourceMap; | ||
} else if(suppliedOptions.readSourceMapComments !== false) { | ||
if (!options.inputSourceMap && options.readSourceMapComments) { | ||
var inputMap = convert.fromSource(src); | ||
if(inputMap) options.inputSourceMap = inputMap.toObject(); | ||
if (inputMap) { | ||
options.inputSourceMap = inputMap.toObject(); | ||
} | ||
} | ||
} else { | ||
delete options.sourceFileName; | ||
} | ||
var doNgInject = suppliedOptions.hasOwnProperty('ngInject') ? suppliedOptions.ngInject : true; | ||
var doNgProvide = suppliedOptions.hasOwnProperty('ngProvide') ? suppliedOptions.ngProvide : true; | ||
var ast = recast.parse(src, options); | ||
if(doNgProvide) ngProvide(ast); | ||
if(doNgInject) ngInject(ast); | ||
if (options.ngProvide) { | ||
ngProvide(ast); | ||
} | ||
if (options.ngInject) { | ||
ngInject(ast); | ||
} | ||
var result = recast.print(ast, options); | ||
var transformedCode = result.code; | ||
var map = null; | ||
if(suppliedOptions.sourceMap){ | ||
if (options.sourceMap) { | ||
map = result.map; | ||
if(result.map && suppliedOptions.appendSourceMapComment){ | ||
if (result.map && options.appendSourceMapComment) { | ||
transformedCode = convert.removeComments(transformedCode); | ||
transformedCode += '\n' + convert.fromObject(result.map).toComment() + '\n'; | ||
map = convert.fromObject(result.map); | ||
transformedCode += '\n' + map.toComment() + '\n'; | ||
} | ||
@@ -43,2 +53,2 @@ } | ||
}; | ||
} | ||
} |
@@ -0,16 +1,44 @@ | ||
'use strict'; | ||
module.exports = collectVariableIds; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
var b = types.builders; | ||
var requiredInjection = require('../utils/requiredInjection'); | ||
var s = require('../utils/builders'); | ||
function collectVariableIds(node){ | ||
function collectVariableIds(node) { | ||
n.VariableDeclaration.assert(node); | ||
var ids=[]; | ||
types.visit(node,{ | ||
visitIdentifier:function(path){ | ||
ids.push(path.node.name); | ||
var injectedOrSet = {}; | ||
var ids = []; | ||
var inject = []; | ||
var assign = []; | ||
types.visit(node, { | ||
visitVariableDeclarator:function(path) { | ||
var node = path.node; | ||
if (node.init) { | ||
var req = requiredInjection(node.init); | ||
var reqId = b.identifier(req); | ||
if (injectedOrSet[req] !== true) { | ||
injectedOrSet[req] = true; | ||
inject.push(reqId); | ||
} | ||
ids.push(node.id); | ||
injectedOrSet[node.id.name] = true; | ||
assign.push(s.assignmentStatement(node.id, node.init)); | ||
} else { | ||
ids.push(node.id); | ||
var name = node.id.name; | ||
var injectionName = '_' + name + '_'; | ||
injectedOrSet[name] = true; | ||
var injectId = b.identifier(injectionName); | ||
inject.push(injectId); | ||
assign.push(s.assignmentStatement(node.id, injectId)); | ||
} | ||
return false; | ||
} | ||
}); | ||
return ids; | ||
} | ||
return {ids:ids, inject:inject, assign:assign}; | ||
} |
@@ -0,11 +1,12 @@ | ||
'use strict'; | ||
module.exports = createInjector(); | ||
module.exports.create = createInjector; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
var s = require('../utils/builders'); | ||
function createInjector (regexp, logger) { | ||
function createInjector(regexp, logger) { | ||
var needsInjection = require('./needsInjection').create(regexp, logger); | ||
var buildInjectionCode = require('./buildInjectionCode'); | ||
var collectVariableIds = require('./collectVariableIds'); | ||
@@ -15,9 +16,19 @@ | ||
function addVariableInjections (ast) { | ||
function addVariableInjections(ast) { | ||
types.visit(ast, { | ||
visitVariableDeclaration: function (path) { | ||
visitVariableDeclaration: function(path) { | ||
var node = path.node; | ||
if (needsInjection(node)) { | ||
var ids = collectVariableIds(node); | ||
path.insertAfter(buildInjectionCode(ids)); | ||
var obj = collectVariableIds(node); | ||
// variable declaration | ||
var decl = s.variableDeclaration(obj.ids); | ||
decl.comments = node.comments; | ||
// beforeEach call that injects required variables and assigns them. | ||
var beforeEach = s.beforeEachStmt([ | ||
s.injectCall(obj.inject, obj.assign) | ||
]); | ||
path.replace(decl, beforeEach); | ||
} | ||
@@ -29,2 +40,2 @@ return false; | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var hasAnnotation = require('../utils/hasAnnotation'); | ||
@@ -5,8 +7,8 @@ module.exports = create(); | ||
module.exports.create = create; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
var requiredInjection = require('../utils/requiredInjection'); | ||
function create(regexp, logger) { | ||
function create(regexp, logger){ | ||
regexp = regexp || /^\s*@ngInject\s*$/; | ||
@@ -20,13 +22,12 @@ | ||
function getsInjection(node){ | ||
if(!n.VariableDeclaration.check(node)){ | ||
function getsInjection(node) { | ||
if (!n.VariableDeclaration.check(node)) { | ||
logger.logRejectedNode('not a VariableDeclaration', node); | ||
return false; | ||
} | ||
if(!containsNgInjectAnnotation(node)){ | ||
if (!containsNgInjectAnnotation(node)) { | ||
logger.logRejectedNode('does not contain an NgInit comment', node); | ||
return false; | ||
} | ||
if(hasInit(node)){ | ||
if (hasNonInjectableInit(node)) { | ||
logger.logRejectedNode('contains a variable initialization', node); | ||
@@ -39,8 +40,11 @@ return false; | ||
function hasInit(node){ | ||
function hasNonInjectableInit(node) { | ||
n.VariableDeclaration.assert(node); | ||
var found = false; | ||
types.visit(node,{ | ||
visitVariableDeclarator:function(path){ | ||
if(path.node.init !== null) found = true; | ||
types.visit(node, { | ||
visitVariableDeclarator:function(path) { | ||
var init = path.node.init; | ||
if (init !== null) { | ||
found = found || (requiredInjection(init) === null); | ||
} | ||
return false; | ||
@@ -51,2 +55,2 @@ } | ||
} | ||
} | ||
} |
@@ -0,10 +1,12 @@ | ||
'use strict'; | ||
module.exports = buildNgProvide; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
var b = types.builders; | ||
var s = require('../utils/shared'); | ||
var s = require('../utils/builders'); | ||
var assert = require('assert'); | ||
function buildNgProvide(ids, inits){ | ||
function buildNgProvide(ids, inits) { | ||
assert.equal(ids.length, inits.length, 'ids and inits must be same length'); | ||
@@ -16,23 +18,18 @@ | ||
for (var i = 0; i < ids.length; i++) { | ||
assignments.push( | ||
s.assignmentStatement(ids[i],inits[i]) | ||
); | ||
provides.push( | ||
s.provideValue(b.literal(ids[i].name),ids[i]) | ||
); | ||
assignments.push(s.assignmentStatement(ids[i], inits[i])); | ||
provides.push(s.provideValue(b.literal(ids[i].name), ids[i])); | ||
} | ||
var func= b.functionExpression( | ||
var func = b.functionExpression( | ||
null, | ||
[b.identifier('$provide')], | ||
b.blockStatement( | ||
assignments.concat(provides) | ||
) | ||
[b.identifier('$provide')], | ||
b.blockStatement(assignments.concat(provides)) | ||
); | ||
var moduleStmt = s.moduleStmt(func ); | ||
var moduleStmt = s.moduleStmt(func); | ||
return s.beforeEachStmt( | ||
[ b.functionExpression(null,[], b.blockStatement([ moduleStmt]))] | ||
[b.functionExpression(null, [], b.blockStatement([moduleStmt]))] | ||
); | ||
} |
@@ -0,12 +1,14 @@ | ||
'use strict'; | ||
module.exports = collectVariableIds; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
function collectVariableIds(node){ | ||
function collectVariableIds(node) { | ||
n.VariableDeclaration.assert(node); | ||
var ids=[]; | ||
var ids = []; | ||
var inits = []; | ||
types.visit(node,{ | ||
visitVariableDeclarator:function(path){ | ||
types.visit(node, { | ||
visitVariableDeclarator:function(path) { | ||
ids.push(path.node.id); | ||
@@ -17,3 +19,3 @@ inits.push(path.node.init); | ||
}); | ||
return {ids:ids,inits:inits}; | ||
} | ||
return {ids:ids, inits:inits}; | ||
} |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var hasAnnotation = require('../utils/hasAnnotation'); | ||
@@ -6,3 +8,3 @@ | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
@@ -24,12 +26,11 @@ var b = types.builders; | ||
types.visit(ast, { | ||
visitVariableDeclaration: function (path) { | ||
visitVariableDeclaration: function(path) { | ||
var node = path.node; | ||
if (needsInjection(node)) { | ||
var idInits = collectVariableIdsAndInits(node); | ||
var ids = idInits.ids, inits = idInits.inits; | ||
var obj = collectVariableIdsAndInits(node); | ||
var decl = b.variableDeclaration( | ||
'var', | ||
ids.map(function(id){ | ||
return b.variableDeclarator(id,null); | ||
obj.ids.map(function(id) { | ||
return b.variableDeclarator(id, null); | ||
}) | ||
@@ -40,4 +41,3 @@ ); | ||
path.replace(decl, buildInjection(ids, inits)); | ||
path.replace(decl, buildInjection(obj.ids, obj.inits)); | ||
} | ||
@@ -48,8 +48,9 @@ return false; | ||
var node = path.node; | ||
var parent = path.parent, parentNode = parent && parent.node; | ||
if( | ||
var parent = path.parent; | ||
var parentNode = parent && parent.node; | ||
if ( | ||
parentNode && | ||
n.ExpressionStatement.check(parentNode) && | ||
hasNgProvideAnnotation(parentNode) | ||
){ | ||
) { | ||
var injection = buildInjection([node.left], [node.right]); | ||
@@ -64,2 +65,2 @@ injection.comments = parentNode.comments; | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var hasAnnotation = require('../utils/hasAnnotation'); | ||
@@ -5,8 +7,7 @@ module.exports = create(); | ||
module.exports.create = create; | ||
var types = require('ast-types'); | ||
var types = require('recast').types; | ||
var n = types.namedTypes; | ||
function create(regexp, logger) { | ||
function create(regexp, logger){ | ||
regexp = regexp || /^\s*@ngProvide\s*$/; | ||
@@ -20,14 +21,13 @@ | ||
function getsInjection(node){ | ||
if(!n.VariableDeclaration.check(node)){ | ||
function getsInjection(node) { | ||
if (!n.VariableDeclaration.check(node)) { | ||
logger.logRejectedNode('not a VariableDeclaration', node); | ||
return false; | ||
} | ||
if(!containsNgInjectAnnotation(node)){ | ||
if (!containsNgInjectAnnotation(node)) { | ||
logger.logRejectedNode('does not contain an NgProvide comment', node); | ||
return false; | ||
} | ||
if(missingInit(node)){ | ||
logger.logRejectedNode('at least one variable is missing an initialization', node); | ||
if (missingInit(node)) { | ||
logger.logRejectedNode('variable missing initialization', node); | ||
return false; | ||
@@ -39,8 +39,11 @@ } | ||
function missingInit(node){ | ||
function missingInit(node) { | ||
n.VariableDeclaration.assert(node); | ||
var missing = false; | ||
types.visit(node,{ | ||
visitVariableDeclarator:function(path){ | ||
if(path.node.init === null || path.node.init === undefined) missing = true; | ||
types.visit(node, { | ||
visitVariableDeclarator:function(path) { | ||
var node = path.node; | ||
if (node.init === null || node.init === undefined) { | ||
missing = true; | ||
} | ||
return false; | ||
@@ -51,2 +54,2 @@ } | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
module.exports = { | ||
@@ -7,2 +9,2 @@ logRejectedNode:noop, | ||
function noop(){} | ||
function noop() {} |
@@ -0,13 +1,15 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
module.exports = function (regexp){ | ||
assert(regexp instanceof RegExp, 'expected regular expression got: ' + regexp); | ||
module.exports = function(regexp) { | ||
assert(regexp instanceof RegExp, 'expected regular expression: ' + regexp); | ||
return containsNgInjectAnnotation; | ||
function containsNgInjectAnnotation(node){ | ||
if(node.leadingComments){ // esprima ast with `options.attachComments == true` | ||
function containsNgInjectAnnotation(node) { | ||
if (node.leadingComments) { // esprima ast with `options.attachComments == true` | ||
return node.leadingComments.some(isNgInjectAnnotation); | ||
} else if (node.comments) { | ||
return node.comments.some(function(comment){ //recast style ast | ||
return node.comments.some(function(comment) { //recast style ast | ||
return comment.leading && isNgInjectAnnotation(comment); | ||
@@ -19,5 +21,5 @@ }); | ||
function isNgInjectAnnotation(comment){ | ||
function isNgInjectAnnotation(comment) { | ||
return regexp.test(comment.value); | ||
} | ||
}; | ||
}; |
@@ -0,11 +1,15 @@ | ||
'use strict'; | ||
module.exports = { | ||
logRejectedNode:function(reasons, node){ | ||
if(Array.isArray(reasons)) reasons = reasons.join(', '); | ||
logRejectedNode:function(reasons, node) { | ||
if (Array.isArray(reasons)) { | ||
reasons = reasons.join(', '); | ||
} | ||
reasons = reasons || ''; | ||
logNode('rejected - ' + reasons, node); | ||
}, | ||
logAcceptedNode:function(node){ | ||
logAcceptedNode:function(node) { | ||
logNode('accepted ', node); | ||
}, | ||
logCode:function(code){ | ||
logCode:function(code) { | ||
logNode('', code); | ||
@@ -15,3 +19,3 @@ } | ||
function logNode(message, node){ | ||
function logNode(message, node) { | ||
prefix(message); | ||
@@ -23,3 +27,3 @@ var code = require('recast').print(node).code; | ||
function logCode(message, code){ | ||
function logCode(message, code) { | ||
prefix(message); | ||
@@ -30,10 +34,9 @@ console.log(code); | ||
function prefix(message){ | ||
console.log('---------------'+ message + '------------------------------'); | ||
function prefix(message) { | ||
console.log('---------------' + message + '----------------------------'); | ||
} | ||
function suffix(){ | ||
function suffix() { | ||
console.log('--------------------------------------------------------'); | ||
console.log('\n\n'); | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16495
21
482
12
1
+ Addedmerge@^1.2.0
+ Addedmerge@1.2.1(transitive)
- Removedast-types@^0.6.15
- Removedast-types@0.6.16(transitive)