@@ -72,7 +72,7 @@ export function scanEnums(options: ScanOptions): EnumData {
7272 const lang = getLang ( file )
7373 if ( ! isTs ( lang ) ) continue
7474
75- const content = readFileSync ( file , 'utf-8 ' )
75+ const content = readFileSync ( file , 'utf8 ' )
7676 const ast = babelParse ( content , lang )
7777
7878 const enumIds : Set < string > = new Set ( )
@@ -113,59 +113,66 @@ export function scanEnums(options: ScanOptions): EnumData {
113113 const init = e . initializer
114114 if ( init ) {
115115 let value : string | number
116- if (
117- init . type === 'StringLiteral' ||
118- init . type === 'NumericLiteral'
119- ) {
120- value = init . value
121- }
122- // e.g. 1 << 2
123- else if ( init . type === 'BinaryExpression' ) {
124- const resolveValue = ( node : Expression | PrivateName ) => {
125- assert . ok ( typeof node . start === 'number' )
126- assert . ok ( typeof node . end === 'number' )
127- if (
128- node . type === 'NumericLiteral' ||
129- node . type === 'StringLiteral'
130- ) {
131- return node . value
132- } else if ( node . type === 'MemberExpression' ) {
133- const exp = content . slice (
134- node . start ,
135- node . end ,
136- ) as `${string } .${string } `
137- if ( ! ( exp in defines ) ) {
116+ switch ( init . type ) {
117+ case 'StringLiteral' :
118+ case 'NumericLiteral' : {
119+ value = init . value
120+
121+ break
122+ }
123+ case 'BinaryExpression' : {
124+ const resolveValue = ( node : Expression | PrivateName ) => {
125+ assert . ok ( typeof node . start === 'number' )
126+ assert . ok ( typeof node . end === 'number' )
127+ if (
128+ node . type === 'NumericLiteral' ||
129+ node . type === 'StringLiteral'
130+ ) {
131+ return node . value
132+ } else if ( node . type === 'MemberExpression' ) {
133+ const exp = content . slice (
134+ node . start ,
135+ node . end ,
136+ ) as `${string } .${string } `
137+ if ( ! ( exp in defines ) ) {
138+ throw new Error (
139+ `unhandled enum initialization expression ${ exp } in ${ file } ` ,
140+ )
141+ }
142+ return defines [ exp ]
143+ } else {
138144 throw new Error (
139- `unhandled enum initialization expression ${ exp } in ${ file } ` ,
145+ `unhandled BinaryExpression operand type ${ node . type } in ${ file } ` ,
140146 )
141147 }
142- return defines [ exp ]
148+ }
149+ const exp = `${ resolveValue ( init . left ) } ${
150+ init . operator
151+ } ${ resolveValue ( init . right ) } `
152+ value = evaluate ( exp )
153+
154+ break
155+ }
156+ case 'UnaryExpression' : {
157+ if (
158+ init . argument . type === 'StringLiteral' ||
159+ init . argument . type === 'NumericLiteral'
160+ ) {
161+ const exp = `${ init . operator } ${ init . argument . value } `
162+ value = evaluate ( exp )
143163 } else {
144164 throw new Error (
145- `unhandled BinaryExpression operand type ${ node . type } in ${ file } ` ,
165+ `unhandled UnaryExpression argument type ${ init . argument . type } in ${ file } ` ,
146166 )
147167 }
168+
169+ break
148170 }
149- const exp = `${ resolveValue ( init . left ) } ${
150- init . operator
151- } ${ resolveValue ( init . right ) } `
152- value = evaluate ( exp )
153- } else if ( init . type === 'UnaryExpression' ) {
154- if (
155- init . argument . type === 'StringLiteral' ||
156- init . argument . type === 'NumericLiteral'
157- ) {
158- const exp = `${ init . operator } ${ init . argument . value } `
159- value = evaluate ( exp )
160- } else {
171+ default : {
161172 throw new Error (
162- `unhandled UnaryExpression argument type ${ init . argument . type } in ${ file } ` ,
173+ `unhandled initializer type ${ init . type } for ${ fullKey } in ${ file } ` ,
163174 )
164175 }
165- } else {
166- throw new Error (
167- `unhandled initializer type ${ init . type } for ${ fullKey } in ${ file } ` ,
168- )
169176 }
170177 lastInitialized = value
171178 saveValue ( lastInitialized )
0 commit comments