diff options
author | Trevor Elliott <trevor@galois.com> | 2013-06-03 17:11:35 -0700 |
---|---|---|
committer | Trevor Elliott <trevor@galois.com> | 2013-06-03 17:11:35 -0700 |
commit | 998e6546b172850a7f51d0e4e1d50017b655a80e (patch) | |
tree | 61f294daec641b22968dde12370649ca825c9b20 | |
parent | 8c0e0a63d3e91977e71bdceaf8caed5a60f4f6d0 (diff) |
Switch to a CommonJS format for rule definitions
Rules are now defined as CommonJS modules, using the exports object as
the external facing API for the rule.
-rw-r--r-- | contexts/data/fiveui/injected/fiveui-injected-compute.js | 59 | ||||
-rw-r--r-- | contexts/data/fiveui/rules.js | 28 | ||||
-rw-r--r-- | exampleData/ruleSets/colorRules.json | 20 | ||||
-rw-r--r-- | exampleData/ruleSets/colorRules/backgroundCheck.js (renamed from exampleData/ruleSets/backgroundCheck.js) | 12 | ||||
-rw-r--r-- | exampleData/ruleSets/colorRules/foregroundCheck.js (renamed from exampleData/ruleSets/foregroundCheck.js) | 11 |
5 files changed, 69 insertions, 61 deletions
diff --git a/contexts/data/fiveui/injected/fiveui-injected-compute.js b/contexts/data/fiveui/injected/fiveui-injected-compute.js index 3221a87..7ae9848 100644 --- a/contexts/data/fiveui/injected/fiveui-injected-compute.js +++ b/contexts/data/fiveui/injected/fiveui-injected-compute.js @@ -196,10 +196,14 @@ /* END of BSD licensed code */ core.evaluate = function(rs) { - var i; var theRule = null; - var date = new Date(); - var stats = { start: date.getTime(), end: null, numRules: 0, numElts: 0 }; + var date = new Date(); + var stats = + { start: date.getTime() + , end: null + , numRules: 0 + , numElts: 0 + }; fiveui.stats.numElts = 0; // reset stats element counter var report = function(name, node) { @@ -211,34 +215,28 @@ } }; - for (i = 0; i < rs.length; i += 1) { - theRule = rs[i]; - try { - var fn = eval('('+rs[i].ruleStr+')'); - } catch (e) { - console.log('could not load ruleStr for rule: '+theRule.name); - console.log(e.toString()); - } - + for(var i=0; i<rs.length; ++i) { + var theRule = rs[i]; var scope = { - name : theRule.name, - description : theRule.description, - ruleSet : core.rules + name: theRule.name, + description: theRule.description, + ruleSet: core.rules }; - if (fn) { + if (theRule.rule) { try { // note: fiveui.stats.numElts is updated as a side effect here - fn.apply(scope); + theRule.rule.apply(scope); } catch (e) { - console.log('exception running rule: '+theRule.name); + console.log('exception running rule: ' + theRule.name); console.log(e.toString()); } stats.numRules += 1; } } - date = new Date(); - stats.end = date.getTime(); + + date = new Date(); + stats.end = date.getTime(); stats.numElts = fiveui.stats.numElts; core.reportStats(stats); }; @@ -289,8 +287,25 @@ }; var registerBackendListeners = function(port) { - port.on('SetRules', function(payload){ - core.rules = payload; + port.on('SetRules', function(payload) { + + var rules = payload.rules; + + core.rules = payload; + core.rules.rules = []; + + for(var i=0; i<rules.length; ++i) { + var moduleStr = + [ '(function(){' + , 'var exports = {};' + , rules[i].module + , 'return exports;' + , '})()' + ].join('\n'); + + core.rules.rules.push(eval(moduleStr)); + } + if (null == core.rules) { debugger; } diff --git a/contexts/data/fiveui/rules.js b/contexts/data/fiveui/rules.js index 03dbc96..e45d0d8 100644 --- a/contexts/data/fiveui/rules.js +++ b/contexts/data/fiveui/rules.js @@ -25,36 +25,26 @@ var fiveui = fiveui || {}; /** * @constructor - * @param {!number} id The unique Rule identifier. - * @param {!string} name A human-readable name for this Rule. - * @param {!string} desc A description of the Rule. - * @param {!string} ruleStr A Javascript expression implementing the rule. + * @param {!string} module A Javascript module that defines the rule. */ -fiveui.Rule = function(id, name, desc, ruleStr) { - this.id = id; - this.name = name; - this.description = desc; - this.ruleStr = ruleStr; +fiveui.Rule = function(module) { + this.module = module; }; fiveui.Rule.defaults = function(obj) { return _.defaults(obj, { - id: null, - name: '', - description: '', - ruleStr: '' + module: '', }); }; /** * Create a Rule from a JSON object. * - * // param {!number} id A unique id for the rehydrated Rule. * @param {!Object} obj The object to take settings from. * @return {!fiveui.Rule} A populated Rule object. */ fiveui.Rule.fromJSON = function(obj) { - return new fiveui.Rule(obj.id, obj.name, obj.description, obj.ruleStr); + return new fiveui.Rule(obj.module); }; /** @@ -125,15 +115,13 @@ fiveui.RuleSet.load = function(manifest_url, options) { // XXX there's likely problems here, how should we make sure that the // url is what we expect? - var rule = fiveui.Rule.defaults(rules.pop()); - var rule_url = base_url + '/' + rule.file; + var rule_file = fiveui.Rule.defaults(rules.pop()); + var rule_url = base_url + '/' + rule_file; fiveui.ajax.get(rule_url, { success: function(text) { - rule.ruleStr = text; - manifest.rules.push(rule); - + manifest.rules.push(new fiveui.Rule(text)); loadRules(manifest, rules); }, diff --git a/exampleData/ruleSets/colorRules.json b/exampleData/ruleSets/colorRules.json index 54fb549..bcfce72 100644 --- a/exampleData/ruleSets/colorRules.json +++ b/exampleData/ruleSets/colorRules.json @@ -6,20 +6,10 @@ { "name": "Color Guidelines" , "description": "Foreground/background color guidelines" -, "rules": [ - //---------------------------------------------------------------- - { "name": "Foreground check" - , "description": ["Foreground colors should be in the set: \n", - "#00 #FF #3D #F7 #C2 #B4 #4E #FFCB05 #7B8738"] - , "file": "foregroundCheck.js" - }, - //---------------------------------------------------------------- - { "name": "Background check" - , "description": ["Backgrounds colors should be in the set:", - "#00 #FF #3D #F7 #C2 #B4 #4E"] - , "file": "backgroundCheck.js" - } -] +, "rules": + [ "colorRules/foregroundCheck.js" + , "colorRules/backgroundCheck.js" + ] } //---------------------------------------------------------------- /* { "id": 2141103 @@ -72,4 +62,4 @@ //---------------------------------------------------------------- ] } - */
\ No newline at end of file + */ diff --git a/exampleData/ruleSets/backgroundCheck.js b/exampleData/ruleSets/colorRules/backgroundCheck.js index 7da59aa..4ce7043 100644 --- a/exampleData/ruleSets/backgroundCheck.js +++ b/exampleData/ruleSets/colorRules/backgroundCheck.js @@ -1,4 +1,12 @@ -function() { + +exports.name = 'Foreground Check'; + +exports.description = + [ 'Foreground colors should be in the set:' + , '#00 #FF #3D #F7 #C2 #B4 #4E #FFCB05 #7B8738' + ].join('\n'); + +exports.rule = function() { var allow = '#00 #FF #3D #F7 #C2 #B4 #4E'.split(' '); report("broken"); $5(':visible') @@ -7,4 +15,4 @@ function() { var color = fiveui.color.colorToHex($(elt).css('background-color')); report('non-standard background color: ' + color, $(elt)); }); -} +}; diff --git a/exampleData/ruleSets/foregroundCheck.js b/exampleData/ruleSets/colorRules/foregroundCheck.js index 32a8ee5..5b1e04b 100644 --- a/exampleData/ruleSets/foregroundCheck.js +++ b/exampleData/ruleSets/colorRules/foregroundCheck.js @@ -1,4 +1,11 @@ -function() { +exports.name = 'Background Check'; + +exports.description = + [ 'Backgrounds colors should be in the set:' + , '#00 #FF #3D #F7 #C2 #B4 #4E' + ].join('\n'); + +exports.rule = function() { var allow = '#00 #FF #3D #F7 #C2 #B4 #4E #FFCB05 #7B8738'.split(' '); $5(':visible') .cssIsNot('color', allow, fiveui.color.colorToHex) @@ -6,4 +13,4 @@ function() { var color = fiveui.color.colorToHex($(elt).css('color')); report('foreground color: ' + color, elt); }); -} +}; |