Skip to content

Commit 3db7a7a

Browse files
committed
add tests, fix edge case
1 parent fcef3a5 commit 3db7a7a

File tree

2 files changed

+116
-6
lines changed

2 files changed

+116
-6
lines changed

src/utils/queries/__tests__/index.test.mjs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,114 @@ describe('createQueries', () => {
110110
};
111111
queries.addStabilityMetadata(node, apiEntryMetadata);
112112
});
113+
114+
describe('UNIST', () => {
115+
describe('isTypedList', () => {
116+
it('returns false for non-list nodes', () => {
117+
strictEqual(
118+
createQueries.UNIST.isTypedList({ type: 'paragraph', children: [] }),
119+
false
120+
);
121+
});
122+
123+
it('returns false for empty lists', () => {
124+
strictEqual(
125+
createQueries.UNIST.isTypedList({ type: 'list', children: [] }),
126+
false
127+
);
128+
});
129+
130+
const cases = [
131+
{
132+
name: 'typedListStarters pattern match',
133+
node: {
134+
type: 'list',
135+
children: [
136+
{
137+
children: [
138+
{
139+
children: [{ type: 'text', value: 'Returns: foo' }],
140+
},
141+
],
142+
},
143+
],
144+
},
145+
expected: true,
146+
},
147+
{
148+
name: 'direct type link pattern',
149+
node: {
150+
type: 'list',
151+
children: [
152+
{
153+
children: [
154+
{
155+
children: [
156+
{
157+
type: 'link',
158+
children: [{ type: 'inlineCode', value: '<Type>' }],
159+
},
160+
],
161+
},
162+
],
163+
},
164+
],
165+
},
166+
expected: true,
167+
},
168+
{
169+
name: 'inlineCode + space + type link pattern',
170+
node: {
171+
type: 'list',
172+
children: [
173+
{
174+
children: [
175+
{
176+
children: [
177+
{ type: 'inlineCode', value: 'foo' },
178+
{ type: 'text', value: ' ' },
179+
{
180+
type: 'link',
181+
children: [{ type: 'text', value: '<Bar>' }],
182+
},
183+
],
184+
},
185+
],
186+
},
187+
],
188+
},
189+
expected: true,
190+
},
191+
{
192+
name: 'non-matching content',
193+
node: {
194+
type: 'list',
195+
children: [
196+
{
197+
children: [
198+
{
199+
children: [
200+
{ type: 'inlineCode', value: 'not a valid prop' },
201+
{ type: 'text', value: ' ' },
202+
{
203+
type: 'link',
204+
children: [{ type: 'text', value: '<Bar>' }],
205+
},
206+
],
207+
},
208+
],
209+
},
210+
],
211+
},
212+
expected: false,
213+
},
214+
];
215+
216+
cases.forEach(({ name, node, expected }) => {
217+
it(`returns ${expected} for ${name}`, () => {
218+
strictEqual(createQueries.UNIST.isTypedList(node), expected);
219+
});
220+
});
221+
});
222+
});
113223
});

src/utils/queries/index.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ createQueries.UNIST = {
281281
const [node, ...contentNodes] =
282282
list?.children?.[0]?.children?.[0]?.children ?? [];
283283

284-
const possibleProperty = node?.value?.trimStart();
285-
286-
// Exit if no content in node (or if no node exists)
287-
if (!possibleProperty) {
284+
// Exit if no node
285+
if (!node) {
288286
return false;
289287
}
290288

289+
const possibleProperty = node?.value?.trimStart();
290+
291291
// Check for other starters
292-
if (possibleProperty.match(createQueries.QUERIES.typedListStarters)) {
292+
if (possibleProperty?.match(createQueries.QUERIES.typedListStarters)) {
293293
return true;
294294
}
295295

@@ -301,7 +301,7 @@ createQueries.UNIST = {
301301
// Check for inline code + space + type link pattern
302302
if (
303303
node.type === 'inlineCode' &&
304-
possibleProperty.match(VALID_JAVASCRIPT_PROPERTY) &&
304+
possibleProperty?.match(VALID_JAVASCRIPT_PROPERTY) &&
305305
contentNodes[0]?.value?.trim() === '' &&
306306
contentNodes[1]?.type === 'link' &&
307307
contentNodes[1]?.children?.[0]?.value?.[0] === '<'

0 commit comments

Comments
 (0)