Skip to content

Commit

Permalink
Fix: Handle string literals being used as keys in enums (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShukantPal authored Jun 6, 2022
1 parent 118cac2 commit 1e5c4dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/webdoc-parser/src/symbols-babel/extract-symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
isObjectMethod,
isObjectProperty,
isReturnStatement,
isStringLiteral,
isTSAsExpression,
isTSDeclareFunction,
isTSDeclareMethod,
Expand All @@ -39,8 +40,7 @@ import {
isTSParameterProperty,
isTSPropertySignature,
isTSTypeElement,
isThisExpression,
isVariableDeclarator,
isThisExpression, isVariableDeclarator,
} from "@babel/types";

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

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

Expand Down
2 changes: 1 addition & 1 deletion packages/webdoc-parser/src/transformer/symbol-to-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const TAG_PARSERS: { [id: string]: TagParser } = {
const TAG_OVERRIDES: { [id: string]: string | any } = { // replace any, no lazy
"class": "ClassDoc",
"interface": "InterfaceDoc",
// "enum": "PropertyDoc",
"enum": "EnumDoc",
"member": "PropertyDoc",
"method": "MethodDoc",
"mixin": "MixinDoc",
Expand Down
25 changes: 25 additions & 0 deletions packages/webdoc-parser/test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,29 @@ describe("@webdoc/parser.parse", function() {
expect(engineClass.description).to.include("Good evening!");
expect(engineClass.description).to.include("Hello world!");
});

it("should resolve objects properties using string literals as keys correctly", async function() {
const documentTree = await parse(`
/** @namespace input */
/**
* standard keyboard constants
* @public
* @enum {number}
* @namespace KEY
* @memberof input
*/
const KEY = {
/** @memberof input.KEY */
"BACKSPACE" : 8,
/** @memberof input.KEY */
"TAB" : 9,
/** @memberof input.KEY */
"ENTER" : 13,
};
`);

const docKeyEnum = findDoc("input.KEY", documentTree);

expect(docKeyEnum.members.length).to.equal(3);
});
});

0 comments on commit 1e5c4dd

Please sign in to comment.