Skip to content

Commit d0d5873

Browse files
Merge pull request #937 from BitGo/support-computed-properties
Support simple computed properties
2 parents c29f1e1 + bbf1c75 commit d0d5873

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

packages/openapi-generator/src/codec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ function parseObjectExpression(
223223
} else if (
224224
property.key.type !== 'Identifier' &&
225225
property.key.type !== 'StringLiteral' &&
226-
property.key.type !== 'NumericLiteral'
226+
property.key.type !== 'NumericLiteral' &&
227+
property.key.type !== 'Computed'
227228
) {
228229
return errorLeft(`Unimplemented property key type ${property.key.type}`);
229230
}
@@ -235,7 +236,41 @@ function parseObjectExpression(
235236
commentEndIdx,
236237
);
237238
commentStartIdx = (property.value as swc.HasSpan).span.end;
238-
const name = property.key.value;
239+
240+
let name: string = '';
241+
if (property.key.type === 'Computed') {
242+
if (property.key.expression.type !== 'Identifier') {
243+
return errorLeft(
244+
`Unimplemented computed property value type ${property.value.type}`,
245+
);
246+
}
247+
248+
const initE = findSymbolInitializer(
249+
project,
250+
source,
251+
property.key.expression.value,
252+
);
253+
if (E.isLeft(initE)) {
254+
return initE;
255+
}
256+
const [newSourceFile, init] = initE.right;
257+
const valueE = parsePlainInitializer(project, newSourceFile, init);
258+
if (E.isLeft(valueE)) {
259+
return valueE;
260+
}
261+
const schema = valueE.right;
262+
if (
263+
(schema.type === 'string' || schema.type === 'number') &&
264+
schema.enum !== undefined
265+
) {
266+
name = String(schema.enum[0]);
267+
} else {
268+
return errorLeft('Computed property must be string or number literal');
269+
}
270+
} else {
271+
name = String(property.key.value);
272+
}
273+
239274
const valueE = parsePlainInitializer(project, source, property.value);
240275
if (E.isLeft(valueE)) {
241276
return valueE;

packages/openapi-generator/test/codec.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ testCase(
554554
DECLARATION_COMMENT_WITHOUT_LINE_BREAK,
555555
{
556556
FOO: {
557-
type: 'number',
557+
type: 'number',
558558
primitive: true,
559559
comment: {
560560
description: 'Test codec',
@@ -682,7 +682,7 @@ testCase('second property comment is parsed', SECOND_PROPERTY_COMMENT, {
682682
properties: {
683683
foo: { type: 'number', primitive: true },
684684
bar: {
685-
type: 'string',
685+
type: 'string',
686686
primitive: true,
687687
comment: {
688688
description: 'this is a comment',
@@ -848,3 +848,25 @@ testCase('object assign is parsed', OBJECT_ASSIGN, {
848848
required: ['foo', 'bar'],
849849
},
850850
});
851+
852+
const COMPUTED_PROPERTY = `
853+
import * as t from 'io-ts';
854+
const key = 'foo';
855+
export const FOO = t.type({
856+
[key]: t.number,
857+
});
858+
`;
859+
860+
testCase('computed property is parsed', COMPUTED_PROPERTY, {
861+
FOO: {
862+
type: 'object',
863+
properties: {
864+
foo: { type: 'number', primitive: true },
865+
},
866+
required: ['foo'],
867+
},
868+
key: {
869+
type: 'string',
870+
enum: ['foo'],
871+
}
872+
});

0 commit comments

Comments
 (0)