Skip to content

Commit 8bfba2c

Browse files
authored
fix(legacy-json): use leftHandAssign in more cases (#525)
1 parent 053ab86 commit 8bfba2c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mock.module('../../../../utils/generators.mjs', {
1313
{ version: '18.0.0', isLts: true, isCurrent: false },
1414
{ version: '19.0.0', isLts: false, isCurrent: true },
1515
],
16+
leftHandAssign: Object.assign,
1617
getVersionFromSemVer: version => version.split('.')[0],
1718
getVersionURL: (version, api) => `/api/${version}/${api}`,
1819
},

src/generators/legacy-json/utils/parseList.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
TYPE_EXPRESSION,
66
} from '../constants.mjs';
77
import parseSignature from './parseSignature.mjs';
8+
import { leftHandAssign } from '../../../utils/generators.mjs';
89
import createQueries from '../../../utils/queries/index.mjs';
910
import { transformNodesToString } from '../../../utils/unist.mjs';
1011

@@ -95,6 +96,10 @@ export function parseList(section, nodes) {
9596
// Update the section based on its type and parsed values
9697
switch (section.type) {
9798
case 'ctor':
99+
// Constructors are their own signatures
100+
leftHandAssign(section, parseSignature(section.textRaw, values));
101+
break;
102+
98103
case 'classMethod':
99104
case 'method':
100105
// For methods and constructors, parse and attach signatures
@@ -104,10 +109,7 @@ export function parseList(section, nodes) {
104109
case 'property':
105110
// For properties, update type and other details if values exist
106111
if (values.length) {
107-
const { type, ...rest } = values[0];
108-
section.type = type;
109-
Object.assign(section, rest);
110-
section.textRaw = `\`${section.name}\` ${section.textRaw}`;
112+
leftHandAssign(section, values[0]);
111113
}
112114
break;
113115

src/utils/generators.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,16 @@ export const sortChanges = (changes, key = 'version') => {
9696
return compare(coerceSemVer(bVersion), coerceSemVer(aVersion));
9797
});
9898
};
99+
100+
/**
101+
* Assigns properties from one or more source objects to the target object
102+
* **without overwriting existing keys** in the target.
103+
*
104+
* Similar to `Object.assign`, but preserves the target's existing keys.
105+
* The target object is mutated in place.
106+
*
107+
* @param {Object} target - The object to assign properties to.
108+
* @param {Object} source - The source object
109+
*/
110+
export const leftHandAssign = (target, source) =>
111+
Object.keys(source).forEach(k => k in target || (target[k] = source[k]));

0 commit comments

Comments
 (0)