From da8e7f36e109bfd5a1fd2806662d148f475856ca Mon Sep 17 00:00:00 2001 From: HoLyVieR Date: Thu, 30 May 2019 17:21:38 -0400 Subject: [PATCH] Added additional type check to classes. Fixed an issue with wrong type assumption in equals function. --- classes/concatenation.js | 10 +++++++++- classes/constant.js | 2 +- classes/function-argument.js | 12 ++++++++++++ classes/function-invocation.js | 2 +- classes/global-function-call.js | 10 +++++++++- classes/local-function-call.js | 12 ++++++++++-- classes/member-expression.js | 6 +++++- classes/object-function-call.js | 4 ++++ classes/reference.js | 6 +++++- 9 files changed, 56 insertions(+), 8 deletions(-) diff --git a/classes/concatenation.js b/classes/concatenation.js index df5fbae..bbf47c7 100644 --- a/classes/concatenation.js +++ b/classes/concatenation.js @@ -1,6 +1,14 @@ /// class Concatenation function Concatenation(value1, value2) { + if (value1 === null) { + throw new Error("value1 can't be null"); + } + + if (value2 === null) { + throw new Error("value1 can't be null"); + } + this.values = []; this.values.push(value1); this.values.push(value2); @@ -22,4 +30,4 @@ Concatenation.prototype.equals = function (val) { return val.values[0].equals(this.values[0]) && val.values[1].equals(this.values[1]); } -module.exports = Concatenation; \ No newline at end of file +module.exports = Concatenation; diff --git a/classes/constant.js b/classes/constant.js index 68eff41..0af5149 100644 --- a/classes/constant.js +++ b/classes/constant.js @@ -16,4 +16,4 @@ Constant.prototype.equals = function (val) { return this.value === val.value; } -module.exports = Constant; \ No newline at end of file +module.exports = Constant; diff --git a/classes/function-argument.js b/classes/function-argument.js index 0ec66fd..fe7069d 100644 --- a/classes/function-argument.js +++ b/classes/function-argument.js @@ -1,6 +1,18 @@ /// class FunctionArgument function FunctionArgument(fnct, variableName, index) { + if (!fnct) { + throw new Error("Type of fnct must be string or object."); + } + + if (typeof variableName !== "string") { + throw new Error("Type of variableName must be string."); + } + + if (typeof index !== "number") { + throw new Error("Type of index must be number."); + } + this.name = variableName; this.fnct = fnct; this.index = index; diff --git a/classes/function-invocation.js b/classes/function-invocation.js index fee2714..16db42c 100644 --- a/classes/function-invocation.js +++ b/classes/function-invocation.js @@ -29,4 +29,4 @@ FunctionInvocation.prototype.equals = function (val) { return good; } -module.exports = FunctionInvocation; \ No newline at end of file +module.exports = FunctionInvocation; diff --git a/classes/global-function-call.js b/classes/global-function-call.js index b0b637d..8dd9c89 100644 --- a/classes/global-function-call.js +++ b/classes/global-function-call.js @@ -1,6 +1,14 @@ /// class GlobalFunctionCall function GlobalFunctionCall(name, args) { + if (typeof name !== "string") { + throw new Error("Type of name must be string."); + } + + if (typeof args !== "object" || typeof args.length !== "number") { + throw new Error("Type of args must be array."); + } + this.name = name; this.arguments = args; } @@ -41,4 +49,4 @@ GlobalFunctionCall.prototype.equals = function (val) { return good; } -module.exports = GlobalFunctionCall; \ No newline at end of file +module.exports = GlobalFunctionCall; diff --git a/classes/local-function-call.js b/classes/local-function-call.js index 23e93a9..54c5b03 100644 --- a/classes/local-function-call.js +++ b/classes/local-function-call.js @@ -1,6 +1,14 @@ /// class LocalFunctionCall function LocalFunctionCall(reference, args) { + if (reference == null) { + throw new Exception("Reference is null."); + } + + if (typeof args !== "object" || typeof args.length !== "number") { + throw new Error("Type of args must be array."); + } + this.arguments = args; this.reference = reference; } @@ -19,7 +27,7 @@ LocalFunctionCall.prototype.equals = function (val) { return false; } - if (!this.reference.equals(val.reference)) { + if (this.reference !== val.reference) { return false; } @@ -38,4 +46,4 @@ LocalFunctionCall.prototype.equals = function (val) { return good; } -module.exports = LocalFunctionCall; \ No newline at end of file +module.exports = LocalFunctionCall; diff --git a/classes/member-expression.js b/classes/member-expression.js index e041144..55a9860 100644 --- a/classes/member-expression.js +++ b/classes/member-expression.js @@ -1,6 +1,10 @@ /// class MemberExpression function MemberExpression(parts) { + if (typeof parts !== "object" || typeof parts.length !== "number") { + throw new Error("Type of parts must be array."); + } + this.parts = parts; } @@ -33,4 +37,4 @@ MemberExpression.prototype.equals = function (val) { return good; } -module.exports = MemberExpression; \ No newline at end of file +module.exports = MemberExpression; diff --git a/classes/object-function-call.js b/classes/object-function-call.js index 018fcf3..bd956af 100644 --- a/classes/object-function-call.js +++ b/classes/object-function-call.js @@ -3,6 +3,10 @@ var MemberExpression = require("./member-expression"); function ObjectFunctionCall(members, args) { + if (typeof args !== "object" || typeof args.length !== "number") { + throw new Error("Type of args must be array."); + } + this.members = new MemberExpression(members); this.arguments = args; } diff --git a/classes/reference.js b/classes/reference.js index f7f6995..8ed5c4c 100644 --- a/classes/reference.js +++ b/classes/reference.js @@ -3,6 +3,10 @@ var CONST_SEPARATOR_ID = "&&&"; function Reference(name) { + if (typeof name !== "string") { + throw new Error("Type of name must be string."); + } + this.name = name; } @@ -18,4 +22,4 @@ Reference.prototype.equals = function (val) { return this.name === val.name; } -module.exports = Reference; \ No newline at end of file +module.exports = Reference;