diff --git a/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs b/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs index 0c1d8f89..145777b3 100644 --- a/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs +++ b/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs @@ -13,6 +13,7 @@ mock.module('../../../../utils/generators.mjs', { { version: '18.0.0', isLts: true, isCurrent: false }, { version: '19.0.0', isLts: false, isCurrent: true }, ], + leftHandAssign: Object.assign, getVersionFromSemVer: version => version.split('.')[0], getVersionURL: (version, api) => `/api/${version}/${api}`, }, diff --git a/src/generators/legacy-json/utils/parseList.mjs b/src/generators/legacy-json/utils/parseList.mjs index 73c36237..6f62bf66 100644 --- a/src/generators/legacy-json/utils/parseList.mjs +++ b/src/generators/legacy-json/utils/parseList.mjs @@ -5,6 +5,7 @@ import { TYPE_EXPRESSION, } from '../constants.mjs'; import parseSignature from './parseSignature.mjs'; +import { leftHandAssign } from '../../../utils/generators.mjs'; import createQueries from '../../../utils/queries/index.mjs'; import { transformNodesToString } from '../../../utils/unist.mjs'; @@ -95,6 +96,10 @@ export function parseList(section, nodes) { // Update the section based on its type and parsed values switch (section.type) { case 'ctor': + // Constructors are their own signatures + leftHandAssign(section, parseSignature(section.textRaw, values)); + break; + case 'classMethod': case 'method': // For methods and constructors, parse and attach signatures @@ -104,10 +109,7 @@ export function parseList(section, nodes) { case 'property': // For properties, update type and other details if values exist if (values.length) { - const { type, ...rest } = values[0]; - section.type = type; - Object.assign(section, rest); - section.textRaw = `\`${section.name}\` ${section.textRaw}`; + leftHandAssign(section, values[0]); } break; diff --git a/src/utils/generators.mjs b/src/utils/generators.mjs index f169a9fc..ddaae088 100644 --- a/src/utils/generators.mjs +++ b/src/utils/generators.mjs @@ -96,3 +96,16 @@ export const sortChanges = (changes, key = 'version') => { return compare(coerceSemVer(aVersion), coerceSemVer(bVersion)); }); }; + +/** + * Assigns properties from one or more source objects to the target object + * **without overwriting existing keys** in the target. + * + * Similar to `Object.assign`, but preserves the target's existing keys. + * The target object is mutated in place. + * + * @param {Object} target - The object to assign properties to. + * @param {Object} source - The source object + */ +export const leftHandAssign = (target, source) => + Object.keys(source).forEach(k => k in target || (target[k] = source[k]));