Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/utils/queries/__tests__/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,114 @@ describe('createQueries', () => {
};
queries.addStabilityMetadata(node, apiEntryMetadata);
});

describe('UNIST', () => {
describe('isTypedList', () => {
it('returns false for non-list nodes', () => {
strictEqual(
createQueries.UNIST.isTypedList({ type: 'paragraph', children: [] }),
false
);
});

it('returns false for empty lists', () => {
strictEqual(
createQueries.UNIST.isTypedList({ type: 'list', children: [] }),
false
);
});

const cases = [
{
name: 'typedListStarters pattern match',
node: {
type: 'list',
children: [
{
children: [
{
children: [{ type: 'text', value: 'Returns: foo' }],
},
],
},
],
},
expected: true,
},
{
name: 'direct type link pattern',
node: {
type: 'list',
children: [
{
children: [
{
children: [
{
type: 'link',
children: [{ type: 'inlineCode', value: '<Type>' }],
},
],
},
],
},
],
},
expected: true,
},
{
name: 'inlineCode + space + type link pattern',
node: {
type: 'list',
children: [
{
children: [
{
children: [
{ type: 'inlineCode', value: 'foo' },
{ type: 'text', value: ' ' },
{
type: 'link',
children: [{ type: 'text', value: '<Bar>' }],
},
],
},
],
},
],
},
expected: true,
},
{
name: 'non-matching content',
node: {
type: 'list',
children: [
{
children: [
{
children: [
{ type: 'inlineCode', value: 'not a valid prop' },
{ type: 'text', value: ' ' },
{
type: 'link',
children: [{ type: 'text', value: '<Bar>' }],
},
],
},
],
},
],
},
expected: false,
},
];

cases.forEach(({ name, node, expected }) => {
it(`returns ${expected} for ${name}`, () => {
strictEqual(createQueries.UNIST.isTypedList(node), expected);
});
});
});
});
});
2 changes: 2 additions & 0 deletions src/utils/queries/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
// This is the perma-link within the API docs that reference the Stability Index
export const DOC_API_STABILITY_SECTION_REF_URL =
'documentation.html#stability-index';

export const VALID_JAVASCRIPT_PROPERTY = /^[.a-z0-9$_]+$/i;
16 changes: 10 additions & 6 deletions src/utils/queries/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import { u as createTree } from 'unist-builder';
import { SKIP } from 'unist-util-visit';

import { DOC_API_STABILITY_SECTION_REF_URL } from './constants.mjs';
import {
DOC_API_STABILITY_SECTION_REF_URL,
VALID_JAVASCRIPT_PROPERTY,
} from './constants.mjs';
import {
extractYamlContent,
parseHeadingIntoMetadata,
Expand Down Expand Up @@ -278,15 +281,15 @@ createQueries.UNIST = {
const [node, ...contentNodes] =
list?.children?.[0]?.children?.[0]?.children ?? [];

// Exit if no content nodes
// Exit if no node
if (!node) {
return false;
}

const possibleProperty = node?.value?.trimStart();

// Check for other starters
if (
node.value?.trimStart().match(createQueries.QUERIES.typedListStarters)
) {
if (possibleProperty?.match(createQueries.QUERIES.typedListStarters)) {
return true;
}

Expand All @@ -298,7 +301,8 @@ createQueries.UNIST = {
// Check for inline code + space + type link pattern
if (
node.type === 'inlineCode' &&
contentNodes[0]?.value.trim() === '' &&
possibleProperty?.match(VALID_JAVASCRIPT_PROPERTY) &&
contentNodes[0]?.value?.trim() === '' &&
contentNodes[1]?.type === 'link' &&
contentNodes[1]?.children?.[0]?.value?.[0] === '<'
) {
Expand Down
Loading