From 95fd64ec152a0568734c7748d9c95992408e8d09 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 14 Dec 2013 21:46:47 +0100 Subject: [PATCH] .jshintrc: Add the "forin" option & fix fallout Also added few missing |hasOwnProperty| calls that JSHint didn't detect because it only looks whether there is an |if| statement wrapping the loop body. --- .jshintrc | 1 + bin/pegjs | 4 +++- lib/compiler.js | 4 +++- lib/peg.js | 4 +++- lib/utils.js | 18 +++++++++++++----- spec/generated-parser.spec.js | 12 ++++++++---- spec/helpers.js | 6 ++++-- spec/parser.spec.js | 8 +++++--- 8 files changed, 40 insertions(+), 17 deletions(-) diff --git a/.jshintrc b/.jshintrc index 587d66f..1b15e51 100644 --- a/.jshintrc +++ b/.jshintrc @@ -2,6 +2,7 @@ "curly": true, "eqeqeq": true, "evil": true, + "forin": true, "laxbreak": true, "noempty": true, "nonew": true, diff --git a/bin/pegjs b/bin/pegjs index 3275d22..4c7eb8f 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -70,7 +70,9 @@ function addExtraOptions(options, json) { } for (var key in extraOptions) { - options[key] = extraOptions[key]; + if (extraOptions.hasOwnProperty(key)) { + options[key] = extraOptions[key]; + } } } diff --git a/lib/compiler.js b/lib/compiler.js index ab03a37..ffdff9b 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -48,7 +48,9 @@ module.exports = { }); for (stage in passes) { - utils.each(passes[stage], runPass); + if (passes.hasOwnProperty(stage)) { + utils.each(passes[stage], runPass); + } } switch (options.output) { diff --git a/lib/peg.js b/lib/peg.js index 55c6095..aea3022 100644 --- a/lib/peg.js +++ b/lib/peg.js @@ -24,7 +24,9 @@ module.exports = { var converted = {}, stage; for (stage in passes) { - converted[stage] = utils.values(passes[stage]); + if (passes.hasOwnProperty(stage)) { + converted[stage] = utils.values(passes[stage]); + } } return converted; diff --git a/lib/utils.js b/lib/utils.js index 8ca3ce1..3cdeca4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -69,7 +69,9 @@ var utils = { keys: function(object) { var result = []; for (var key in object) { - result.push(key); + if (object.hasOwnProperty(key)) { + result.push(key); + } } return result; }, @@ -77,7 +79,9 @@ var utils = { values: function(object) { var result = []; for (var key in object) { - result.push(object[key]); + if (object.hasOwnProperty(key)) { + result.push(object[key]); + } } return result; }, @@ -85,15 +89,19 @@ var utils = { clone: function(object) { var result = {}; for (var key in object) { - result[key] = object[key]; + if (object.hasOwnProperty(key)) { + result[key] = object[key]; + } } return result; }, defaults: function(object, defaults) { for (var key in defaults) { - if (!(key in object)) { - object[key] = defaults[key]; + if (defaults.hasOwnProperty(key)) { + if (!(key in object)) { + object[key] = defaults[key]; + } } } }, diff --git a/spec/generated-parser.spec.js b/spec/generated-parser.spec.js index 61a3a4c..4695474 100644 --- a/spec/generated-parser.spec.js +++ b/spec/generated-parser.spec.js @@ -15,7 +15,9 @@ describe("generated parser", function() { * changed later. */ for (key in options) { - clonedOptions[key] = options[key]; + if (options.hasOwnProperty(key)) { + clonedOptions[key] = options[key]; + } } describe( @@ -123,10 +125,12 @@ describe("generated parser", function() { } else { if (details) { for (key in details) { - if (!this.env.equals_(e[key], details[key])) { - this.message = buildKeyMessage(key, e[key]); + if (details.hasOwnProperty(key)) { + if (!this.env.equals_(e[key], details[key])) { + this.message = buildKeyMessage(key, e[key]); - return false; + return false; + } } } } diff --git a/spec/helpers.js b/spec/helpers.js index 64970ce..41b7954 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -29,9 +29,11 @@ beforeEach(function() { if (!isObject(value)) { return false; } for (key in details) { - if (!(key in value)) { return false; } + if (details.hasOwnProperty(key)) { + if (!(key in value)) { return false; } - if (!matchDetails(value[key], details[key])) { return false; } + if (!matchDetails(value[key], details[key])) { return false; } + } } return true; diff --git a/spec/parser.spec.js b/spec/parser.spec.js index cb4322c..cd30efe 100644 --- a/spec/parser.spec.js +++ b/spec/parser.spec.js @@ -145,10 +145,12 @@ describe("PEG.js grammar parser", function() { } else { if (details) { for (key in details) { - if (!this.env.equals_(e[key], details[key])) { - this.message = buildKeyMessage(key, e[key]); + if (details.hasOwnProperty(key)) { + if (!this.env.equals_(e[key], details[key])) { + this.message = buildKeyMessage(key, e[key]); - return false; + return false; + } } } }