Skip to content

Commit

Permalink
fix: make enums behave properly
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Aug 19, 2022
1 parent 9586242 commit 20d7030
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 75 deletions.
29 changes: 12 additions & 17 deletions lib/typed.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export function safeFieldReference (name) {
return safeNameRe.test(name) ? `${name}` : `'${name}'`
}

/**
* @param {string[]|number[]} list
* @returns {string}
*/
function fromArray (list) {
return JSON.stringify(list).replace(/"/g, '\'').replace(/,/g, ', ')
}

const KindsDefn =
`const Kinds = {
Null: /** @returns {undefined|null} */ (/** @type {any} */ obj) => obj === null ? obj : undefined,
Expand Down Expand Up @@ -678,26 +670,26 @@ ${typeTransformers.join('\n')}
if (!Array.isArray(typeDef.enum.members)) {
throw new Error('Enum needs a "members" list')
}
/** @type {string[]|number[]}} */
/** @type {[string,string][]}} */
let values
let representation = 'string'
if (typeof typeDef.enum.representation === 'object') {
if ('string' in typeDef.enum.representation && typeof typeDef.enum.representation.string === 'object') {
const renames = typeDef.enum.representation.string
values = typeDef.enum.members.map((v) => {
v = renames[v] !== undefined ? renames[v] : v
if (typeof v !== 'string') {
values = typeDef.enum.members.map((to) => {
const from = renames[to] !== undefined ? renames[to] : to
if (typeof from !== 'string') {
throw new Error('Enum members must be strings')
}
return v
return [`'${from}'`, `'${to}'`]
})
} else if ('int' in typeDef.enum.representation && typeof typeDef.enum.representation.int === 'object') {
const renames = typeDef.enum.representation.int
values = typeDef.enum.members.map((v) => {
if (renames[v] === undefined || typeof renames[v] !== 'number' || !Number.isInteger(renames[v])) {
values = typeDef.enum.members.map((to) => {
if (renames[to] === undefined || typeof renames[to] !== 'number' || !Number.isInteger(renames[to])) {
throw new Error('Enum members must be ints')
}
return renames[v]
return [String(renames[to]), `'${to}'`]
})
representation = 'int'
} else {
Expand All @@ -710,7 +702,10 @@ ${typeTransformers.join('\n')}
if (Kinds.${tc(representation)}(obj) === undefined) {
return undefined
}
return ${fromArray(values)}.includes(obj) ? obj : undefined
${values.map(([from, to]) => ` if (obj === ${from}) {
return ${to}
}`).join('\n')}
return undefined
}`

return
Expand Down
10 changes: 5 additions & 5 deletions test/test-typed-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ describe('Enums', () => {

await lint(typed.toTyped)

assert.strictEqual(typed.toTyped('f'), 'f')
assert.strictEqual(typed.toTyped('f'), 'Foo')
assert.isUndefined(typed.toTyped('Foo'))
assert.strictEqual(typed.toTyped('Bar'), 'Bar')
assert.strictEqual(typed.toTyped('b'), 'b')
assert.strictEqual(typed.toTyped('b'), 'Baz')
assert.isUndefined(typed.toTyped('Baz'))
assert.isUndefined(typed.toTyped('Blip'))
assert.isUndefined(typed.toTyped(''))
Expand Down Expand Up @@ -97,9 +97,9 @@ describe('Enums', () => {

await lint(typed.toTyped)

assert.strictEqual(typed.toTyped(0), 0)
assert.strictEqual(typed.toTyped(1), 1)
assert.strictEqual(typed.toTyped(100), 100)
assert.strictEqual(typed.toTyped(0), 'Foo')
assert.strictEqual(typed.toTyped(1), 'Bar')
assert.strictEqual(typed.toTyped(100), 'Baz')
assert.isUndefined(typed.toTyped(-1))
assert.isUndefined(typed.toTyped(-100))
assert.isUndefined(typed.toTyped(10))
Expand Down
45 changes: 0 additions & 45 deletions types/lib/typed-gen.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/lib/typed-gen.d.ts.map

This file was deleted.

2 changes: 1 addition & 1 deletion types/lib/typed.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions types/typed.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/typed.d.ts.map

This file was deleted.

0 comments on commit 20d7030

Please sign in to comment.