Skip to content

Commit

Permalink
Type check optional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Van Ginkel committed Oct 10, 2020
1 parent 878fd72 commit a939617
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ export class Runtime {
let currentSpec: InputArgument[];
let actualType: InputArgument;
let typeMatched: boolean;
let isOptional: boolean;
for (let i = 0; i < signature.length; i += 1) {
typeMatched = false;
currentSpec = signature[i].types;
isOptional = signature[i].optional ?? false;
actualType = this.getTypeName(args[i]) as InputArgument;
let j: number;
for (j = 0; j < currentSpec.length; j += 1) {
Expand All @@ -115,7 +113,7 @@ export class Runtime {
break;
}
}
if (!typeMatched && !isOptional) {
if (!typeMatched && actualType !== undefined) {
const expected = currentSpec
.map((typeIdentifier): string => {
return this.TYPE_NAME_TABLE[typeIdentifier];
Expand Down
22 changes: 20 additions & 2 deletions test/jmespath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('registerFunction', () => {
() => {
/* EMPTY FUNCTION */
},
[{ types: [jmespath.TYPE_ANY] }],
[{ types: [jmespath.TYPE_ANY] }, { types: [jmespath.TYPE_NUMBER], optional: true }],
);
expect(() =>
search(
Expand All @@ -250,7 +250,25 @@ describe('registerFunction', () => {
},
'tooFewArgs()',
),
).toThrow('ArgumentError: tooFewArgs() takes 1 argument but received 0');
).toThrow('ArgumentError: tooFewArgs() takes 1 arguments but received 0');
expect(() =>
search(
{
foo: 60,
bar: 10,
},
'tooFewArgs(foo, @)',
),
).toThrow('TypeError: tooFewArgs() expected argument 2 to be type (number) but received type object instead.');
expect(() =>
search(
{
foo: 60,
bar: 10,
},
'tooFewArgs(foo, `2`, @)',
),
).toThrow('ArgumentError: tooFewArgs() takes 1 arguments but received 3');
});
it('alerts too many arguments', () => {
registerFunction(
Expand Down

0 comments on commit a939617

Please sign in to comment.