Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ The arguments (e.g. for an operation) look like this:
```JS
{
"arguments": [{
"type": "argument",
"optional": false,
"variadic": true
"extAttrs": []
Expand Down
2 changes: 1 addition & 1 deletion dist/webidl2.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/webidl2.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions lib/productions/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Default } from "./default.js";
import { ExtendedAttributes } from "./extended-attributes.js";
import { unescape, type_with_extended_attributes } from "./helpers.js";
import { argumentNameKeywords } from "../tokeniser.js";
import { validationError } from "../error.js";
import { idlTypeIncludesDictionary } from "../validators/helpers.js";

export class Argument extends Base {
/**
Expand All @@ -29,6 +31,9 @@ export class Argument extends Base {
return ret;
}

get type() {
return "argument";
}
get optional() {
return !!this.tokens.optional;
}
Expand All @@ -38,4 +43,13 @@ export class Argument extends Base {
get name() {
return unescape(this.tokens.name.value);
}

*validate(defs) {
if (idlTypeIncludesDictionary(this.idlType, defs)) {
if (this.optional && !this.default) {
const message = `Optional dictionary arguments must have a default value of \`{}\`.`;
yield validationError(this.source, this.tokens.name, this, message);
}
}
}
}
9 changes: 1 addition & 8 deletions lib/productions/operation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Base } from "./base.js";
import { return_type, argument_list, unescape } from "./helpers.js";
import { validationError } from "../error.js";
import { idlTypeIncludesDictionary } from "../validators/helpers.js";

export class Operation extends Base {
/**
Expand Down Expand Up @@ -48,12 +46,7 @@ export class Operation extends Base {

*validate(defs) {
for (const argument of this.arguments) {
if (idlTypeIncludesDictionary(argument.idlType, defs)) {
if (!argument.default) {
const message = `Optional dictionary arguments must have a default value of \`{}\`.`;
yield validationError(this.source, argument.tokens.name, this, message);
}
}
yield* argument.validate(defs);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Validation error at line 9 in operation-dict-default.webidl, inside `operation x`:
Validation error at line 13 in argument-dict-default.webidl, inside `argument dict`:
void x(optional Dict dict);
^ Optional dictionary arguments must have a default value of `{}`.
Validation error at line 11 in operation-dict-default.webidl, inside `operation y`:
Validation error at line 15 in argument-dict-default.webidl, inside `argument union`:
(boolean or Dict) union);
^ Optional dictionary arguments must have a default value of `{}`.
Validation error at line 13 in operation-dict-default.webidl, inside `operation z`:
Validation error at line 17 in argument-dict-default.webidl, inside `argument union`:
void z(optional Union union);
^ Optional dictionary arguments must have a default value of `{}`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ dictionary Dict {
short x = 0;
};

dictionary Required {
required short x;
};

typedef (short or Dict) Union;

[Exposed=Window]
Expand All @@ -12,4 +16,5 @@ interface X {
void y2(optional (boolean or Dict) union = {});
void z(optional Union union);
void z2(optional Union union = {});
void r(Required req);
};
2 changes: 2 additions & 0 deletions test/syntax/baseline/allowany.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"arguments": [
{
"type": "argument",
"name": "b",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"arguments": [
{
"type": "argument",
"name": "s",
"extAttrs": [
{
Expand Down
1 change: 1 addition & 0 deletions test/syntax/baseline/argument-extattrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"arguments": [
{
"type": "argument",
"name": "argname",
"extAttrs": [
{
Expand Down
4 changes: 4 additions & 0 deletions test/syntax/baseline/callback.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"arguments": [
{
"type": "argument",
"name": "status",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -47,6 +48,7 @@
},
"arguments": [
{
"type": "argument",
"name": "details",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -82,6 +84,7 @@
},
"arguments": [
{
"type": "argument",
"name": "a",
"extAttrs": [],
"idlType": {
Expand All @@ -97,6 +100,7 @@
"variadic": false
},
{
"type": "argument",
"name": "b",
"extAttrs": [],
"idlType": {
Expand Down
1 change: 1 addition & 0 deletions test/syntax/baseline/constructor.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"rhs": null,
"arguments": [
{
"type": "argument",
"name": "radius",
"extAttrs": [],
"idlType": {
Expand Down
2 changes: 2 additions & 0 deletions test/syntax/baseline/enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
},
"arguments": [
{
"type": "argument",
"name": "type",
"extAttrs": [],
"idlType": {
Expand All @@ -81,6 +82,7 @@
"variadic": false
},
{
"type": "argument",
"name": "size",
"extAttrs": [],
"idlType": {
Expand Down
9 changes: 9 additions & 0 deletions test/syntax/baseline/equivalent-decl.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand All @@ -78,6 +80,7 @@
"variadic": false
},
{
"type": "argument",
"name": "propertyValue",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -133,6 +136,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -164,6 +168,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand All @@ -179,6 +184,7 @@
"variadic": false
},
{
"type": "argument",
"name": "propertyValue",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -210,6 +216,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -241,6 +248,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand All @@ -256,6 +264,7 @@
"variadic": false
},
{
"type": "argument",
"name": "propertyValue",
"extAttrs": [],
"idlType": {
Expand Down
1 change: 1 addition & 0 deletions test/syntax/baseline/extended-attributes.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"rhs": null,
"arguments": [
{
"type": "argument",
"name": "radius",
"extAttrs": [],
"idlType": {
Expand Down
3 changes: 3 additions & 0 deletions test/syntax/baseline/getter-setter.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"arguments": [
{
"type": "argument",
"name": "propertyName",
"extAttrs": [],
"idlType": {
Expand All @@ -78,6 +80,7 @@
"variadic": false
},
{
"type": "argument",
"name": "propertyValue",
"extAttrs": [],
"idlType": {
Expand Down
3 changes: 3 additions & 0 deletions test/syntax/baseline/identifier-qualified-names.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"arguments": [
{
"type": "argument",
"name": "interface",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -61,6 +62,7 @@
},
"arguments": [
{
"type": "argument",
"name": "keyName",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -140,6 +142,7 @@
},
"arguments": [
{
"type": "argument",
"name": "callback",
"extAttrs": [],
"idlType": {
Expand Down
8 changes: 8 additions & 0 deletions test/syntax/baseline/indexed-properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"arguments": [
{
"type": "argument",
"name": "index",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -63,6 +64,7 @@
},
"arguments": [
{
"type": "argument",
"name": "index",
"extAttrs": [],
"idlType": {
Expand All @@ -78,6 +80,7 @@
"variadic": false
},
{
"type": "argument",
"name": "value",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -109,6 +112,7 @@
},
"arguments": [
{
"type": "argument",
"name": "index",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -140,6 +144,7 @@
},
"arguments": [
{
"type": "argument",
"name": "name",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -171,6 +176,7 @@
},
"arguments": [
{
"type": "argument",
"name": "name",
"extAttrs": [],
"idlType": {
Expand All @@ -186,6 +192,7 @@
"variadic": false
},
{
"type": "argument",
"name": "value",
"extAttrs": [],
"idlType": {
Expand Down Expand Up @@ -217,6 +224,7 @@
},
"arguments": [
{
"type": "argument",
"name": "name",
"extAttrs": [],
"idlType": {
Expand Down
1 change: 1 addition & 0 deletions test/syntax/baseline/namedconstructor.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"arguments": [
{
"type": "argument",
"name": "src",
"extAttrs": [],
"idlType": {
Expand Down
Loading