Skip to content

Commit 2c75bcf

Browse files
committed
chore(queries): organize and test
1 parent a04ad2d commit 2c75bcf

File tree

26 files changed

+1216
-271
lines changed

26 files changed

+1216
-271
lines changed

src/generators/addon-verify/constants.mjs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/generators/addon-verify/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { join } from 'node:path';
55

66
import { visit } from 'unist-util-visit';
77

8-
import { EXTRACT_CODE_FILENAME_COMMENT } from './constants.mjs';
98
import { generateFileList } from './utils/generateFileList.mjs';
109
import {
1110
generateSectionFolderName,
1211
isBuildableSection,
1312
normalizeSectionName,
1413
} from './utils/section.mjs';
14+
import { EXTRACT_CODE_FILENAME_COMMENT } from '../../utils/queries/regex.mjs';
1515

1616
/**
1717
* This generator generates a file list from code blocks extracted from

src/generators/api-links/constants.mjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/generators/api-links/utils/extractExports.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import { visit } from 'estree-util-visit';
44

5-
import { CONSTRUCTOR_EXPRESSION } from '../constants.mjs';
6-
75
/**
86
* @see https://github.com/estree/estree/blob/master/es5.md#assignmentexpression
97
*
@@ -99,7 +97,7 @@ function handleExpression(node, basename, nameToLineNumberMap) {
9997
case 'Identifier': {
10098
exports.identifiers.push(value.name);
10199

102-
if (CONSTRUCTOR_EXPRESSION.test(value.name[0])) {
100+
if (/^[A-Z]/.test(value.name)) {
103101
exports.ctors.push(value.name);
104102
}
105103

src/generators/json-simple/index.mjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from 'node:path';
55

66
import { remove } from 'unist-util-remove';
77

8-
import createQueries from '../../utils/queries/index.mjs';
8+
import { isHeading, isStabilityNode } from '../../utils/queries/unist.mjs';
99

1010
/**
1111
* This generator generates a simplified JSON version of the API docs and returns it as a string
@@ -41,10 +41,7 @@ export default {
4141

4242
// Removes numerous nodes from the content that should not be on the "body"
4343
// of the JSON version of the API docs as they are already represented in the metadata
44-
remove(content, [
45-
createQueries.UNIST.isStabilityNode,
46-
createQueries.UNIST.isHeading,
47-
]);
44+
remove(content, [isStabilityNode, isHeading]);
4845

4946
return { ...node, content };
5047
});

src/generators/jsx-ast/utils/buildContent.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import createPropertyTable from './buildPropertyTable.mjs';
99
import { DOC_NODE_BLOB_BASE_URL } from '../../../constants.mjs';
1010
import { enforceArray } from '../../../utils/array.mjs';
1111
import { sortChanges } from '../../../utils/generators.mjs';
12-
import createQueries from '../../../utils/queries/index.mjs';
12+
import {
13+
isStabilityNode,
14+
isHeading,
15+
isTypedList,
16+
} from '../../../utils/queries/unist.mjs';
1317
import { JSX_IMPORTS } from '../../web/constants.mjs';
1418
import {
1519
STABILITY_LEVELS,
@@ -219,17 +223,17 @@ export const processEntry = (entry, remark) => {
219223
const content = structuredClone(entry.content);
220224

221225
// Visit and transform stability nodes
222-
visit(content, createQueries.UNIST.isStabilityNode, transformStabilityNode);
226+
visit(content, isStabilityNode, transformStabilityNode);
223227

224228
// Visit and transform headings with metadata and links
225-
visit(content, createQueries.UNIST.isHeading, (...args) =>
229+
visit(content, isHeading, (...args) =>
226230
transformHeadingNode(entry, remark, ...args)
227231
);
228232

229233
// Transform typed lists into property tables
230234
visit(
231235
content,
232-
createQueries.UNIST.isTypedList,
236+
isTypedList,
233237
(node, idx, parent) => (parent.children[idx] = createPropertyTable(node))
234238
);
235239

src/generators/jsx-ast/utils/buildPropertyTable.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { h as createElement } from 'hastscript';
22

3-
import createQueries from '../../../utils/queries/index.mjs';
3+
import { TYPED_LIST_STARTERS } from '../../../utils/queries/regex.mjs';
4+
import { isTypedList } from '../../../utils/queries/unist.mjs';
45

56
/**
67
* Determines if a node looks like part of a type annotation.
@@ -51,9 +52,7 @@ export const extractPropertyName = children => {
5152

5253
// Text with a prefix like "Type:", "Param:", etc.
5354
if (first.type === 'text') {
54-
const starterMatch = first.value.match(
55-
createQueries.QUERIES.typedListStarters
56-
);
55+
const starterMatch = first.value.match(TYPED_LIST_STARTERS);
5756
if (starterMatch) {
5857
// If the starter is 'Type', we don't have a property.
5958
const label = starterMatch[1] !== 'Type' && starterMatch[1];
@@ -133,7 +132,7 @@ export const parseListIntoProperties = node => {
133132
// The remaining children are the description
134133
desc: children,
135134
// Is there a list within this list?
136-
sublist: sublists.find(createQueries.UNIST.isTypedList),
135+
sublist: sublists.find(isTypedList),
137136
});
138137
}
139138

src/generators/jsx-ast/utils/buildSignature.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { highlightToHast } from '@node-core/rehype-shiki';
22
import { h as createElement } from 'hastscript';
33

4-
import createQueries from '../../../utils/queries/index.mjs';
4+
import { isTypedList } from '../../../utils/queries/unist.mjs';
55
import { parseListItem } from '../../legacy-json/utils/parseList.mjs';
66
import parseSignature from '../../legacy-json/utils/parseSignature.mjs';
77

@@ -89,7 +89,7 @@ export const getFullName = ({ name, text }, fallback = name) => {
8989
*/
9090
export default ({ children }, { data }, idx) => {
9191
// Try to locate the parameter list immediately following the heading
92-
const listIdx = children.findIndex(createQueries.UNIST.isTypedList);
92+
const listIdx = children.findIndex(isTypedList);
9393

9494
// Parse parameters from the list, if found
9595
const params =

src/generators/legacy-html/utils/buildContent.mjs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { SKIP, visit } from 'unist-util-visit';
66

77
import buildExtraContent from './buildExtraContent.mjs';
88
import { DOC_NODE_BLOB_BASE_URL } from '../../../constants.mjs';
9-
import createQueries from '../../../utils/queries/index.mjs';
9+
import { LINKS_WITH_TYPES } from '../../../utils/queries/regex.mjs';
10+
import {
11+
isHeading,
12+
isHtmlWithType,
13+
isStabilityNode,
14+
} from '../../../utils/queries/unist.mjs';
1015

1116
/**
1217
* Builds a Markdown heading for a given node
@@ -71,7 +76,7 @@ const buildStability = ({ children, data }, index, parent) => {
7176
*/
7277
const buildHtmlTypeLink = node => {
7378
node.value = node.value.replace(
74-
createQueries.QUERIES.linksWithTypes,
79+
LINKS_WITH_TYPES,
7580
(_, type, link) => `<a href="${link}" class="type">&lt;${type}&gt;</a>`
7681
);
7782
};
@@ -223,16 +228,16 @@ export default (headNodes, metadataEntries, remark) => {
223228
const content = structuredClone(entry.content);
224229

225230
// Parses the Heading nodes into Heading elements
226-
visit(content, createQueries.UNIST.isHeading, buildHeading);
231+
visit(content, isHeading, buildHeading);
227232

228233
// Parses the Blockquotes into Stability elements
229234
// This is treated differently as we want to preserve the position of a Stability Index
230235
// within the content, so we can't just remove it and append it to the metadata
231-
visit(content, createQueries.UNIST.isStabilityNode, buildStability);
236+
visit(content, isStabilityNode, buildStability);
232237

233238
// Parses the type references that got replaced into Markdown links (raw)
234239
// into actual HTML links, these then get parsed into HAST nodes on `runSync`
235-
visit(content, createQueries.UNIST.isHtmlWithType, buildHtmlTypeLink);
240+
visit(content, isHtmlWithType, buildHtmlTypeLink);
236241

237242
// Splits the content into the Heading node and the rest of the content
238243
const [headingNode, ...restNodes] = content.children;

src/generators/legacy-json/constants.mjs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
// Grabs a method's name
2-
export const NAME_EXPRESSION = /^['`"]?([^'`": {]+)['`"]?\s*:?\s*/;
3-
4-
// Denotes a method's type
5-
export const TYPE_EXPRESSION = /^\{([^}]+)\}\s*/;
6-
7-
// Checks if there's a leading hyphen
8-
export const LEADING_HYPHEN = /^-\s*/;
9-
10-
// Grabs the default value if present
11-
export const DEFAULT_EXPRESSION = /\s*\*\*Default:\*\*\s*([^]+)$/i;
12-
13-
// Grabs the parameters from a method's signature
14-
// ex/ 'new buffer.Blob([sources[, options]])'.match(PARAM_EXPRESSION) === ['([sources[, options]])', '[sources[, options]]']
15-
export const PARAM_EXPRESSION = /\((.+)\);?$/;
16-
171
// The plurals associated with each section type.
182
export const SECTION_TYPE_PLURALS = {
193
module: 'modules',

0 commit comments

Comments
 (0)