@@ -5,7 +5,8 @@ module.exports = Enum;
5
5
var ReflectionObject = require ( "./object" ) ;
6
6
( ( Enum . prototype = Object . create ( ReflectionObject . prototype ) ) . constructor = Enum ) . className = "Enum" ;
7
7
8
- var util = require ( "./util" ) ;
8
+ var Namespace = require ( "./namespace" ) ,
9
+ util = require ( "./util" ) ;
9
10
10
11
/**
11
12
* Constructs a new enum instance.
@@ -84,7 +85,7 @@ Enum.prototype.toJSON = function toJSON() {
84
85
return util . toObject ( [
85
86
"options" , this . options ,
86
87
"values" , this . values ,
87
- "reserved" , this . reserved
88
+ "reserved" , this . reserved && this . reserved . length ? this . reserved : undefined
88
89
] ) ;
89
90
} ;
90
91
@@ -97,7 +98,7 @@ Enum.prototype.toJSON = function toJSON() {
97
98
* @throws {TypeError } If arguments are invalid
98
99
* @throws {Error } If there is already a value with this name or id
99
100
*/
100
- Enum . prototype . add = function ( name , id , comment ) {
101
+ Enum . prototype . add = function add ( name , id , comment ) {
101
102
// utilized by the parser but not by .fromJSON
102
103
103
104
if ( ! util . isString ( name ) )
@@ -107,11 +108,17 @@ Enum.prototype.add = function(name, id, comment) {
107
108
throw TypeError ( "id must be an integer" ) ;
108
109
109
110
if ( this . values [ name ] !== undefined )
110
- throw Error ( "duplicate name" ) ;
111
+ throw Error ( "duplicate name '" + name + "' in " + this ) ;
112
+
113
+ if ( this . isReservedId ( id ) )
114
+ throw Error ( "id " + id + " is reserved in " + this ) ;
115
+
116
+ if ( this . isReservedName ( name ) )
117
+ throw Error ( "name '" + name + "' is reserved in " + this ) ;
111
118
112
119
if ( this . valuesById [ id ] !== undefined ) {
113
120
if ( ! ( this . options && this . options . allow_alias ) )
114
- throw Error ( "duplicate id" ) ;
121
+ throw Error ( "duplicate id " + id + " in " + this ) ;
115
122
this . values [ name ] = id ;
116
123
} else
117
124
this . valuesById [ this . values [ name ] = id ] = name ;
@@ -127,18 +134,36 @@ Enum.prototype.add = function(name, id, comment) {
127
134
* @throws {TypeError } If arguments are invalid
128
135
* @throws {Error } If `name` is not a name of this enum
129
136
*/
130
- Enum . prototype . remove = function ( name ) {
137
+ Enum . prototype . remove = function remove ( name ) {
131
138
132
139
if ( ! util . isString ( name ) )
133
140
throw TypeError ( "name must be a string" ) ;
134
141
135
142
var val = this . values [ name ] ;
136
- if ( val === undefined )
137
- throw Error ( "name does not exist" ) ;
143
+ if ( val == null )
144
+ throw Error ( "name '" + name + "' does not exist in " + this ) ;
138
145
139
146
delete this . valuesById [ val ] ;
140
147
delete this . values [ name ] ;
141
148
delete this . comments [ name ] ;
142
149
143
150
return this ;
144
151
} ;
152
+
153
+ /**
154
+ * Tests if the specified id is reserved.
155
+ * @param {number } id Id to test
156
+ * @returns {boolean } `true` if reserved, otherwise `false`
157
+ */
158
+ Enum . prototype . isReservedId = function isReservedId ( id ) {
159
+ return Namespace . isReservedId ( this . reserved , id ) ;
160
+ } ;
161
+
162
+ /**
163
+ * Tests if the specified name is reserved.
164
+ * @param {string } name Name to test
165
+ * @returns {boolean } `true` if reserved, otherwise `false`
166
+ */
167
+ Enum . prototype . isReservedName = function isReservedName ( name ) {
168
+ return Namespace . isReservedName ( this . reserved , name ) ;
169
+ } ;
0 commit comments