StructuredJS

structured.js is a Javascript library that provides a simple interface for checking the structure of Javascript code, backed by the abstract syntax tree generated by Esprima. Structured.js works in-browser or as a standalone npm module. It is used particularly for checking beginner code to provide useful feedback as part of Khan Academy's CS curriculum.

A structure is valid Javascript code that contains blanks ( _ characters) and stand-ins ($str) that match values. The structure is sensitive to nesting and ordering. The matcher only requires that code contain the structure -- extra code has no effect.

Structures can contain any Javascript -- control structures (for, while, if, else, try, etc), function calls, variable declarations, parameters, numbers, strings, arrays, objects, and even operators are all fair game. Use _ characters to indicate what can be filled by anything. Use variables starting with $ to indicate what static values should be the same (i.e. $foo to indicate some number, string, or name). You can also check out the pretty display of structures.

Try it out!

Required structure Code to check
if (_) { _ += _; for (var $a = $start; $a < $check; $a += _) { rect($a, $bigNum, 30, 30); _(); } }
if (y > 30 && x > 13) { x += y; for (var i = 0; i < 99; i += 1) { rect(i, 300, 30, 30); bar(); x = i + 2; } }
Variable callbacks (optional) show/hide
// Optional extra functionality for variables. // Uncomment this code -- select it then cmd-/ or ctrl-/ { // "$start, $check": function(start, check) { // if (check.value < start.value) { // return {"failure": "Double-check your for loop!"} // } // return true; // }, // "$bigNum": function(bigNum) { // if (bigNum.value < 1000) { // return {"failure": "Your number is not big enough! " + bigNum.value} // } // return true; // } }
(Match result here)
(Fail message here)
generate qunit test
              QUnit test code generated here.