Skip to content

Commit 2693be0

Browse files
committed
fix(regexp): organize
1 parent 7442b80 commit 2693be0

File tree

14 files changed

+290
-208
lines changed

14 files changed

+290
-208
lines changed

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,
@@ -210,17 +214,17 @@ export const processEntry = (entry, remark) => {
210214
const content = structuredClone(entry.content);
211215

212216
// Visit and transform stability nodes
213-
visit(content, createQueries.UNIST.isStabilityNode, transformStabilityNode);
217+
visit(content, isStabilityNode, transformStabilityNode);
214218

215219
// Visit and transform headings with metadata and links
216-
visit(content, createQueries.UNIST.isHeading, (...args) =>
220+
visit(content, isHeading, (...args) =>
217221
transformHeadingNode(entry, remark, ...args)
218222
);
219223

220224
// Transform typed lists into property tables
221225
visit(
222226
content,
223-
createQueries.UNIST.isTypedList,
227+
isTypedList,
224228
(node, idx, parent) => (parent.children[idx] = createPropertyTable(node))
225229
);
226230

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/utils/parseList.mjs

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

1112
/**
@@ -46,7 +47,7 @@ export const extractPattern = (text, pattern, key, current) => {
4647
export function parseListItem(child) {
4748
const current = {};
4849

49-
const subList = child.children.find(createQueries.UNIST.isTypedList);
50+
const subList = child.children.find(isTypedList);
5051

5152
// Extract and clean raw text from the node, excluding nested lists
5253
current.textRaw = transformTypeReferences(
@@ -58,7 +59,7 @@ export function parseListItem(child) {
5859
let text = current.textRaw;
5960

6061
// Identify return items or extract key properties (name, type, default) from the text
61-
const starter = text.match(createQueries.QUERIES.typedListStarters);
62+
const starter = text.match(TYPED_LIST_STARTERS);
6263
if (starter) {
6364
current.name =
6465
starter[1] === 'Returns' ? 'return' : starter[1].toLowerCase();

src/generators/metadata/utils/parse.mjs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,29 @@ import { SKIP, visit } from 'unist-util-visit';
99
import { createNodeSlugger } from './slugger.mjs';
1010
import createMetadata from '../../../metadata.mjs';
1111
import createQueries from '../../../utils/queries/index.mjs';
12+
import {
13+
isLinkReference,
14+
isMarkdownUrl,
15+
isHeading,
16+
isStabilityNode,
17+
isYamlNode,
18+
isTextWithType,
19+
isTextWithUnixManual,
20+
} from '../../../utils/queries/unist.mjs';
1221
import { getRemark } from '../../../utils/remark.mjs';
1322

14-
/**
23+
/**x
1524
* This generator generates a flattened list of metadata entries from a API doc
1625
*
1726
* @param {ParserOutput<import('mdast').Root>} input
1827
* @returns {Promise<Array<ApiDocMetadataEntry>>}
1928
*/
29+
/**
30+
*
31+
* @param root0
32+
* @param root0.file
33+
* @param root0.tree
34+
*/
2035
export const parseApiDoc = ({ file, tree }) => {
2136
/**
2237
* This holds references to all the Metadata entries for a given file
@@ -54,7 +69,7 @@ export const parseApiDoc = ({ file, tree }) => {
5469
const headingNodes = selectAll('heading', tree);
5570

5671
// Handles Markdown link references and updates them to be plain links
57-
visit(tree, createQueries.UNIST.isLinkReference, node =>
72+
visit(tree, isLinkReference, node =>
5873
updateLinkReference(node, markdownDefinitions)
5974
);
6075

@@ -64,9 +79,7 @@ export const parseApiDoc = ({ file, tree }) => {
6479

6580
// Handles the normalisation URLs that reference to API doc files with .md extension
6681
// to replace the .md into .html, since the API doc files get eventually compiled as HTML
67-
visit(tree, createQueries.UNIST.isMarkdownUrl, node =>
68-
updateMarkdownLink(node)
69-
);
82+
visit(tree, isMarkdownUrl, node => updateMarkdownLink(node));
7083

7184
// If the document has no headings but it has content, we add a fake heading to the top
7285
// so that our parsing logic can work correctly, and generate content for the whole file
@@ -79,7 +92,7 @@ export const parseApiDoc = ({ file, tree }) => {
7992
// (so all elements after a Heading until the next Heading)
8093
// and then it creates and updates a Metadata entry for each API doc entry
8194
// and then generates the final content for each API doc entry and pushes it to the collection
82-
visit(tree, createQueries.UNIST.isHeading, (headingNode, index) => {
95+
visit(tree, isHeading, (headingNode, index) => {
8396
// Creates a new Metadata entry for the current API doc file
8497
const apiEntryMetadata = createMetadata(nodeSlugger);
8598

@@ -90,8 +103,7 @@ export const parseApiDoc = ({ file, tree }) => {
90103
// This is used for ensuring that we don't include items that would
91104
// belong only to the next heading to the current Heading metadata
92105
// Note that if there is no next heading, we use the current node as the next one
93-
const nextHeadingNode =
94-
findAfter(tree, index, createQueries.UNIST.isHeading) ?? headingNode;
106+
const nextHeadingNode = findAfter(tree, index, isHeading) ?? headingNode;
95107

96108
// This is the cutover index of the subtree that we should get
97109
// of all the Nodes within the AST tree that belong to this section
@@ -109,34 +121,32 @@ export const parseApiDoc = ({ file, tree }) => {
109121

110122
// Visits all Stability Index nodes from the current subtree if there's any
111123
// and then apply the Stability Index metadata to the current metadata entry
112-
visit(subTree, createQueries.UNIST.isStabilityNode, node =>
124+
visit(subTree, isStabilityNode, node =>
113125
addStabilityMetadata(node, apiEntryMetadata)
114126
);
115127

116128
// Visits all HTML nodes from the current subtree and if there's any that matches
117129
// our YAML metadata structure, it transforms into YAML metadata
118130
// and then apply the YAML Metadata to the current Metadata entry
119-
visit(subTree, createQueries.UNIST.isYamlNode, node => {
131+
visit(subTree, isYamlNode, node => {
120132
// TODO: Is there always only one YAML node?
121133
apiEntryMetadata.setYamlPosition(node.position);
122134
addYAMLMetadata(node, apiEntryMetadata);
123135
});
124136

125137
// Visits all Text nodes from the current subtree and if there's any that matches
126138
// any API doc type reference and then updates the type reference to be a Markdown link
127-
visit(subTree, createQueries.UNIST.isTextWithType, (node, _, parent) =>
139+
visit(subTree, isTextWithType, (node, _, parent) =>
128140
updateTypeReference(node, parent)
129141
);
130142

131143
// Visits all Unix manual references, and replaces them with links
132-
visit(
133-
subTree,
134-
createQueries.UNIST.isTextWithUnixManual,
135-
(node, _, parent) => updateUnixManualReference(node, parent)
144+
visit(subTree, isTextWithUnixManual, (node, _, parent) =>
145+
updateUnixManualReference(node, parent)
136146
);
137147

138148
// Removes already parsed items from the subtree so that they aren't included in the final content
139-
remove(subTree, [createQueries.UNIST.isYamlNode]);
149+
remove(subTree, [isYamlNode]);
140150

141151
// Applies the AST transformations to the subtree based on the API doc entry Metadata
142152
// Note that running the transformation on the subtree isn't costly as it is a reduced tree

src/linter/rules/duplicate-stability-nodes.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
import createQueries from '../../utils/queries/index.mjs';
5+
import { STABILITY_INDEX } from '../../utils/queries/regex.mjs';
66
import { LINT_MESSAGES } from '../constants.mjs';
77

88
/**
@@ -33,9 +33,7 @@ export const duplicateStabilityNodes = context => {
3333
) {
3434
const text = paragraph.children[0];
3535
if (text.type === 'text') {
36-
const match = text.value.match(
37-
createQueries.QUERIES.stabilityIndex
38-
);
36+
const match = text.value.match(STABILITY_INDEX);
3937
if (match) {
4038
const stability = parseFloat(match[1]);
4139

src/linter/rules/invalid-change-version.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
extractYamlContent,
99
normalizeYamlSyntax,
1010
} from '../../utils/parser/index.mjs';
11-
import createQueries from '../../utils/queries/index.mjs';
11+
import { isYamlNode } from '../../utils/queries/unist.mjs';
1212
import { LINT_MESSAGES } from '../constants.mjs';
1313
import {
1414
createYamlIssueReporter,
@@ -94,7 +94,7 @@ export const extractVersions = ({ context, node, createYamlIssue }) => {
9494
* @returns {void}
9595
*/
9696
export const invalidChangeVersion = context => {
97-
visit(context.tree, createQueries.UNIST.isYamlNode, node => {
97+
visit(context.tree, isYamlNode, node => {
9898
const yamlContent = extractYamlContent(node);
9999

100100
const normalizedYaml = normalizeYamlSyntax(yamlContent);

src/utils/parser/constants.mjs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
'use strict';
22

3+
import {
4+
CLASS_HEADING,
5+
CLASS_METHOD_HEADING,
6+
CTOR_HEADING,
7+
EVENT_HEADING,
8+
METHOD_HEADING,
9+
PROPERTY_HEADING,
10+
} from '../queries/regex.mjs';
11+
312
// This is the base URL of the MDN Web documentation
413
export const DOC_MDN_BASE_URL = 'https://developer.mozilla.org/en-US/docs/Web/';
514

@@ -19,34 +28,12 @@ export const DOC_MDN_BASE_URL_JS_GLOBALS = `${DOC_MDN_BASE_URL_JS}Reference/Glob
1928
// is a specific type of API Doc entry (e.g., Event, Class, Method, etc)
2029
// and to extract the inner content of said Heading to be used as the API doc entry name
2130
export const DOC_API_HEADING_TYPES = [
22-
{
23-
type: 'method',
24-
regex:
25-
// Group 1: foo[bar]()
26-
// Group 2: foo.bar()
27-
// Group 3: foobar()
28-
/^`?(?:\w*(?:(\[[^\]]+\])|(?:\.(\w+)))|(\w+))\([^)]*\)`?$/i,
29-
},
30-
{ type: 'event', regex: /^Event: +`?['"]?([^'"]+)['"]?`?$/i },
31-
{
32-
type: 'class',
33-
regex:
34-
/^Class: +`?([A-Z]\w+(?:\.[A-Z]\w+)*(?: +extends +[A-Z]\w+(?:\.[A-Z]\w+)*)?)`?$/i,
35-
},
36-
{
37-
type: 'ctor',
38-
regex: /^(?:Constructor: +)?`?new +([A-Z]\w+(?:\.[A-Z]\w+)*)\([^)]*\)`?$/i,
39-
},
40-
{
41-
type: 'classMethod',
42-
regex:
43-
/^Static method: +`?[A-Z]\w+(?:\.[A-Z]\w+)*(?:(\[\w+\.\w+\])|\.(\w+))\([^)]*\)`?$/i,
44-
},
45-
{
46-
type: 'property',
47-
regex:
48-
/^(?:Class property: +)?`?[A-Z]\w+(?:\.[A-Z]\w+)*(?:(\[\w+\.\w+\])|\.(\w+))`?$/i,
49-
},
31+
{ type: 'method', regex: METHOD_HEADING },
32+
{ type: 'event', regex: EVENT_HEADING },
33+
{ type: 'class', regex: CLASS_HEADING },
34+
{ type: 'ctor', regex: CTOR_HEADING },
35+
{ type: 'classMethod', regex: CLASS_METHOD_HEADING },
36+
{ type: 'property', regex: PROPERTY_HEADING },
5037
];
5138

5239
// This is a mapping for types within the Markdown content and their respective

0 commit comments

Comments
 (0)