Skip to content

Commit 1e5c4dd

Browse files
authored
Fix: Handle string literals being used as keys in enums (#164)
1 parent 118cac2 commit 1e5c4dd

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/webdoc-parser/src/symbols-babel/extract-symbol.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
isObjectMethod,
3030
isObjectProperty,
3131
isReturnStatement,
32+
isStringLiteral,
3233
isTSAsExpression,
3334
isTSDeclareFunction,
3435
isTSDeclareMethod,
@@ -39,8 +40,7 @@ import {
3940
isTSParameterProperty,
4041
isTSPropertySignature,
4142
isTSTypeElement,
42-
isThisExpression,
43-
isVariableDeclarator,
43+
isThisExpression, isVariableDeclarator,
4444
} from "@babel/types";
4545

4646
import {OBLIGATE_LEAF, PASS_THROUGH, type Symbol, VIRTUAL, isVirtual} from "../types/Symbol";
@@ -184,6 +184,7 @@ export default function extractSymbol(
184184
// let symbolName = (() => <ClassExpression> | <FunctionExpression)();
185185
// let symbolName = (function() { class symbolName {}; return symbolName; })();
186186
// propertyName: <Literal> | <ClassExpression> | <FunctionExpression>,
187+
// "propertyName": <Literal> | <ClassExpression> | <FunctionExpression>,
187188

188189
// NOTE: If this type of symbol is initialized to a <ClassExpression> or <FunctionExpression>,
189190
// it treated as a virtual symbol so that the assigned class/function (i.e. initially a child
@@ -214,7 +215,7 @@ export default function extractSymbol(
214215
nodeSymbol.meta.readonly = true;
215216
}
216217
} else {// ObjectProperty
217-
name = node.key.name;
218+
name = isStringLiteral(node.key) ? node.key.value : node.key.name;
218219
init = node.value;
219220
}
220221

packages/webdoc-parser/src/transformer/symbol-to-doc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const TAG_PARSERS: { [id: string]: TagParser } = {
115115
const TAG_OVERRIDES: { [id: string]: string | any } = { // replace any, no lazy
116116
"class": "ClassDoc",
117117
"interface": "InterfaceDoc",
118-
// "enum": "PropertyDoc",
118+
"enum": "EnumDoc",
119119
"member": "PropertyDoc",
120120
"method": "MethodDoc",
121121
"mixin": "MixinDoc",

packages/webdoc-parser/test/parse.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,29 @@ describe("@webdoc/parser.parse", function() {
185185
expect(engineClass.description).to.include("Good evening!");
186186
expect(engineClass.description).to.include("Hello world!");
187187
});
188+
189+
it("should resolve objects properties using string literals as keys correctly", async function() {
190+
const documentTree = await parse(`
191+
/** @namespace input */
192+
/**
193+
* standard keyboard constants
194+
* @public
195+
* @enum {number}
196+
* @namespace KEY
197+
* @memberof input
198+
*/
199+
const KEY = {
200+
/** @memberof input.KEY */
201+
"BACKSPACE" : 8,
202+
/** @memberof input.KEY */
203+
"TAB" : 9,
204+
/** @memberof input.KEY */
205+
"ENTER" : 13,
206+
};
207+
`);
208+
209+
const docKeyEnum = findDoc("input.KEY", documentTree);
210+
211+
expect(docKeyEnum.members.length).to.equal(3);
212+
});
188213
});

0 commit comments

Comments
 (0)