Created
April 7, 2014 14:16
-
-
Save rblalock/10021131 to your computer and use it in GitHub Desktop.
Sample type tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require('fs'), | |
esprima = require('esprima'), | |
estraverse = require('estraverse'), | |
doctrine = require('doctrine'); | |
function analyze(node) { | |
var comment, data, params, missing; | |
comment = node.leadingComments[0]; | |
if (comment.type !== 'Block') { | |
return; | |
} | |
data = doctrine.parse(comment.value, {unwrap: true}); | |
data.tags.forEach(function (tag) { | |
if (tag.title === 'param') { | |
node.body.body.forEach(function(_item) { | |
if(_item.expression.type === "AssignmentExpression") { | |
if(tag.name === _item.expression.left.name && | |
typeof _item.expression.right.value !== tag.type.name.toLowerCase()) { | |
console.error( | |
"Type mismatch: " + _item.expression.left.name, | |
"Expected " + tag.type.name.toLowerCase() + " but got " + typeof _item.expression.right.value | |
); | |
} | |
} | |
}); | |
} | |
}); | |
} | |
function verify(node) { | |
switch (node.type) { | |
case esprima.Syntax.FunctionDeclaration: | |
if (node.leadingComments.length === 1) { | |
analyze(node); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
function check(filename) { | |
var content, tree; | |
try { | |
content = fs.readFileSync(filename, 'utf-8'); | |
tree = esprima.parse(content, { attachComment: true, loc: true }); | |
estraverse.traverse(tree, { enter: verify }); | |
} catch (e) { | |
console.error(e.toString()); | |
process.exit(1); | |
} | |
} | |
if (process.argv.length === 2) { | |
console.error('Usage: missing file yo'); | |
process.exit(1); | |
} | |
check(process.argv[2]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test Function | |
* @param {String} _arg1 | |
* @param {Boolean} _arg2 | |
**/ | |
function test(_arg1, _arg2) { | |
_arg1 = false; | |
_arg2 = "Now it's a string! Argh."; | |
} | |
test("a string", false, 123); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment