Skip to content

Commit 8cfd3e5

Browse files
authored
fix(isTypedList): validate property syntax (#414)
1 parent d4b2833 commit 8cfd3e5

File tree

3 files changed

+122
-6
lines changed

3 files changed

+122
-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/constants.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
// This is the perma-link within the API docs that reference the Stability Index
44
export const DOC_API_STABILITY_SECTION_REF_URL =
55
'documentation.html#stability-index';
6+
7+
export const VALID_JAVASCRIPT_PROPERTY = /^[.a-z0-9$_]+$/i;

src/utils/queries/index.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import { u as createTree } from 'unist-builder';
44
import { SKIP } from 'unist-util-visit';
55

6-
import { DOC_API_STABILITY_SECTION_REF_URL } from './constants.mjs';
6+
import {
7+
DOC_API_STABILITY_SECTION_REF_URL,
8+
VALID_JAVASCRIPT_PROPERTY,
9+
} from './constants.mjs';
710
import {
811
extractYamlContent,
912
parseHeadingIntoMetadata,
@@ -278,15 +281,15 @@ createQueries.UNIST = {
278281
const [node, ...contentNodes] =
279282
list?.children?.[0]?.children?.[0]?.children ?? [];
280283

281-
// Exit if no content nodes
284+
// Exit if no node
282285
if (!node) {
283286
return false;
284287
}
285288

289+
const possibleProperty = node?.value?.trimStart();
290+
286291
// Check for other starters
287-
if (
288-
node.value?.trimStart().match(createQueries.QUERIES.typedListStarters)
289-
) {
292+
if (possibleProperty?.match(createQueries.QUERIES.typedListStarters)) {
290293
return true;
291294
}
292295

@@ -298,7 +301,8 @@ createQueries.UNIST = {
298301
// Check for inline code + space + type link pattern
299302
if (
300303
node.type === 'inlineCode' &&
301-
contentNodes[0]?.value.trim() === '' &&
304+
possibleProperty?.match(VALID_JAVASCRIPT_PROPERTY) &&
305+
contentNodes[0]?.value?.trim() === '' &&
302306
contentNodes[1]?.type === 'link' &&
303307
contentNodes[1]?.children?.[0]?.value?.[0] === '<'
304308
) {

0 commit comments

Comments
 (0)