Skip to content

Commit 927328d

Browse files
authored
fix(lib/webidl2): disallow null on constants (#284)
* fix(lib/webidl2): disallow null on constants * proper error message
1 parent 45287bf commit 927328d

File tree

7 files changed

+19
-94
lines changed

7 files changed

+19
-94
lines changed

lib/webidl2.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@
436436
}
437437

438438
function const_value() {
439-
return consume("true", "false", "null", "Infinity", "-Infinity", "NaN", FLOAT, INT);
439+
return consume("true", "false", "Infinity", "-Infinity", "NaN", FLOAT, INT);
440440
}
441441

442442
function const_data(token) {
@@ -753,7 +753,7 @@
753753
if (!assign) {
754754
return null;
755755
}
756-
const def = const_value() || consume(STR, "[") || error("No value for default");
756+
const def = const_value() || consume(STR, "null", "[") || error("No value for default");
757757
const expression = [def];
758758
if (def.type === "[") {
759759
const close = consume("]") || error("Default sequence value must be empty");
@@ -804,7 +804,9 @@
804804
};
805805
}
806806
idlType = Object.assign({ type: "const-type" }, EMPTY_IDLTYPE, idlType);
807-
type_suffix(idlType);
807+
if (probe("?")) {
808+
error("Unexpected nullable constant type");
809+
}
808810
tokens.name = consume(ID) || error("No name for const");
809811
tokens.assign = consume("=") || error("No value assignment for const");
810812
tokens.value = const_value() || error("No value for const");

test/invalid/baseline/const-null.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Syntax error at line 2, since `interface MyConstants`:
2+
const MyPrimitive MY_NULL = null;
3+
^ No value for const
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Syntax error at line 3, since `interface MyConstants`:
2+
const boolean? ARE_WE_THERE_YET = false;
3+
^ Unexpected nullable constant type

test/invalid/idl/const-null.widl

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface MyConstants {
2+
const MyPrimitive MY_NULL = null;
3+
};

test/invalid/idl/const-nullable.widl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06
2+
interface MyConstants {
3+
const boolean? ARE_WE_THERE_YET = false;
4+
};

test/syntax/baseline/nullable.json

+1-85
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,4 @@
11
[
2-
{
3-
"type": "interface",
4-
"name": "MyConstants",
5-
"escapedName": "MyConstants",
6-
"inheritance": null,
7-
"members": [
8-
{
9-
"type": "const",
10-
"name": "ARE_WE_THERE_YET",
11-
"idlType": {
12-
"type": "const-type",
13-
"generic": null,
14-
"nullable": {
15-
"trivia": ""
16-
},
17-
"union": false,
18-
"idlType": "boolean",
19-
"baseName": "boolean",
20-
"escapedBaseName": "boolean",
21-
"prefix": null,
22-
"postfix": null,
23-
"separator": null,
24-
"extAttrs": null,
25-
"trivia": {
26-
"base": " "
27-
}
28-
},
29-
"extAttrs": null,
30-
"value": {
31-
"type": "boolean",
32-
"value": false
33-
},
34-
"trivia": {
35-
"base": "\n ",
36-
"name": " ",
37-
"assign": " ",
38-
"value": " ",
39-
"termination": ""
40-
}
41-
},
42-
{
43-
"type": "const",
44-
"name": "MY_NULL",
45-
"idlType": {
46-
"type": "const-type",
47-
"generic": null,
48-
"nullable": {
49-
"trivia": ""
50-
},
51-
"union": false,
52-
"idlType": "MyPrimitive",
53-
"baseName": "MyPrimitive",
54-
"escapedBaseName": "MyPrimitive",
55-
"prefix": null,
56-
"postfix": null,
57-
"separator": null,
58-
"extAttrs": null,
59-
"trivia": {
60-
"base": " "
61-
}
62-
},
63-
"extAttrs": null,
64-
"value": {
65-
"type": "null"
66-
},
67-
"trivia": {
68-
"base": "\n ",
69-
"name": " ",
70-
"assign": " ",
71-
"value": " ",
72-
"termination": ""
73-
}
74-
}
75-
],
76-
"extAttrs": null,
77-
"partial": null,
78-
"trivia": {
79-
"base": "// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06\n",
80-
"name": " ",
81-
"open": " ",
82-
"close": "\n",
83-
"termination": ""
84-
}
85-
},
862
{
873
"type": "interface",
884
"name": "Node",
@@ -126,7 +42,7 @@
12642
"extAttrs": null,
12743
"partial": null,
12844
"trivia": {
129-
"base": "\n\n",
45+
"base": "",
13046
"name": " ",
13147
"open": " ",
13248
"close": "\n // ...\n",

test/syntax/idl/nullable.widl

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06
2-
interface MyConstants {
3-
const boolean? ARE_WE_THERE_YET = false;
4-
const MyPrimitive? MY_NULL = null;
5-
};
6-
71
interface Node {
82
readonly attribute DOMString? namespaceURI;
93
// ...

0 commit comments

Comments
 (0)