Skip to content

Commit 68b3b7e

Browse files
committed
Support negatives in default params
1 parent 30a7940 commit 68b3b7e

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

lib/parser/nodejs/exported_function_parser.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class ExportedFunctionParser {
2323
BooleanLiteral: 'boolean',
2424
NullLiteral: 'any',
2525
ObjectExpression: 'object',
26-
ArrayExpression: 'array'
26+
ArrayExpression: 'array',
27+
UnaryExpression: 'number'
2728
};
2829
this.validateExpressions = {
2930
ObjectExpression: (node, stack) => {
@@ -41,6 +42,19 @@ class ExportedFunctionParser {
4142
},
4243
ArrayExpression: (node, stack) => {
4344
return node.elements.map((el, i) => this.validateDefaultParameterExpression(i, el, stack));
45+
},
46+
UnaryExpression: (node, stack) => {
47+
if (node.argument.type === 'NumericLiteral') {
48+
if (node.operator === '-') {
49+
return -node.argument.value;
50+
} else if (node.operator === '+') {
51+
return node.argument.value;
52+
} else {
53+
throw new Error(`Invalid UnaryExpression`);
54+
}
55+
} else {
56+
throw new Error(`Invalid UnaryExpression`);
57+
}
4458
}
4559
};
4660
}
@@ -50,8 +64,11 @@ class ExportedFunctionParser {
5064
stack = (stack || []).slice(0);
5165
stack.push(name);
5266

53-
if (!this.literals[node.type]) {
54-
throw new Error(`(${stack.join('.')}) Expected ${Object.keys(this.literals).join(', ')} in Right-Hand of AssignmentPattern`);
67+
let type = node.type;
68+
69+
if (!this.literals[type]) {
70+
console.log(node);
71+
throw new Error(`(${stack.join('.')}) Expected ${Object.keys(this.literals).join(', ')} in Right-Hand of AssignmentPattern, got ${type}.`);
5572
}
5673

5774
if (this.validateExpressions[node.type]) {
@@ -107,7 +124,7 @@ class ExportedFunctionParser {
107124
return expression.right;
108125
}
109126

110-
parseParamsFromFunctionExpression(functionExpression) {
127+
parseParamsFromFunctionExpression(functionExpression, fileString) {
111128

112129
if (!functionExpression) {
113130
return {
@@ -169,7 +186,15 @@ class ExportedFunctionParser {
169186
if (!this.validateFunctionParamName(param.left.name)) {
170187
throw new Error(`Invalid parameter name "${param.left.name}"`);
171188
}
172-
let defaultValue = this.validateDefaultParameterExpression(param.left.name, param.right);
189+
let defaultValue;
190+
try {
191+
defaultValue = this.validateDefaultParameterExpression(param.left.name, param.right);
192+
} catch (e) {
193+
throw new Error([
194+
`Invalid default parameter: ${param.left.name} = ${fileString.slice(param.right.start, param.right.end)}`,
195+
`(${e.message})`
196+
].join(' '));
197+
}
173198
formattedParam = {
174199
name: param.left.name,
175200
type: this.literals[param.right.type],
@@ -287,7 +312,7 @@ class ExportedFunctionParser {
287312
}
288313

289314
let commentDefinition = this.commentDefinitionParser.parse(name, comment);
290-
let functionDefinition = this.parseParamsFromFunctionExpression(functionExpression);
315+
let functionDefinition = this.parseParamsFromFunctionExpression(functionExpression, fileString);
291316

292317
let description = commentDefinition.description || '';
293318
let origins = commentDefinition.origins || null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functionscript",
3-
"version": "2.9.1",
3+
"version": "2.9.2",
44
"description": "An API gateway and framework for turning functions into web services",
55
"author": "Keith Horwood <keithwhor@gmail.com>",
66
"main": "index.js",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* My function
3+
* @param {string} a alpha
4+
* @param {number} b beta
5+
* @returns {string}
6+
*/
7+
module.exports = async (a = 'hi', b = !7, context) => {
8+
// valid, negative number
9+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* My function
3+
* @param {string} a alpha
4+
* @param {number} b beta
5+
* @returns {string}
6+
*/
7+
module.exports = async (a = 'hi', b = !!7, context) => {
8+
// valid, negative number
9+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* My function
3+
* @param {string} a alpha
4+
* @param {number} b beta
5+
* @returns {string}
6+
*/
7+
module.exports = async (a = 'hi', b = -7, context) => {
8+
// valid, negative number
9+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* My function
3+
* @param {string} a alpha
4+
* @param {number} b beta
5+
* @returns {string}
6+
*/
7+
module.exports = async (a = 'hi', b = +7, context) => {
8+
// valid, negative number
9+
};

0 commit comments

Comments
 (0)