📄 addESLintHashToCodeBlocksMeta¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 4 |
| 📦 Imports | 4 |
| 📊 Variables & Constants | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/website/plugins/generated-rule-docs/addESLintHashToCodeBlocksMeta.ts
📦 Imports¶
| Name | Source |
|---|---|
MdxJsxFlowElement |
mdast-util-mdx |
RuleDocsPage |
./RuleDocsPage |
nodeIsCode |
../utils/nodes |
convertToPlaygroundHash |
../utils/rules |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
optionRegex |
RegExp |
const | /option='(?<option>.*?)'/ |
✗ |
Functions¶
addESLintHashToCodeBlocksMeta(page: RuleDocsPage, eslintrc: string): void¶
Parameters:
pageRuleDocsPageeslintrcstring
Returns: void
Calls:
nodeIsJsxTabsaddHashesToChildrenTabsaddHashToNodeIfCodenodeIsCode (from ../utils/nodes)node.meta?.includesnode.meta?.matchplaygroundEslintrc.replaceJSON.stringifyJSON.parseconsole.error[ node.meta,eslintrcHash="${convertToPlaygroundHash(playgroundEslintrc)}", ] .filter(Boolean) .join
Internal Comments:
Code
export function addESLintHashToCodeBlocksMeta(
page: RuleDocsPage,
eslintrc: string,
): void {
for (const node of page.children) {
if (nodeIsJsxTabs(node)) {
addHashesToChildrenTabs(node);
} else {
addHashToNodeIfCode(node);
}
}
function addHashesToChildrenTabs(node: MdxJsxFlowElement): void {
for (const tabItem of node.children) {
if ('children' in tabItem) {
for (const child of tabItem.children) {
addHashToNodeIfCode(child, true);
}
}
}
}
function addHashToNodeIfCode(node: unist.Node, insideTab?: boolean): void {
if (
nodeIsCode(node) &&
(insideTab || node.meta?.includes('showPlaygroundButton')) &&
!node.meta?.includes('title="eslint.config.mjs"') &&
!node.meta?.includes('title=".eslintrc.cjs"') &&
!node.meta?.includes('eslintrcHash=')
) {
let playgroundEslintrc = eslintrc;
const option = node.meta?.match(optionRegex)?.groups?.option;
if (option) {
playgroundEslintrc = playgroundEslintrc.replace(
'"error"',
`["error", ${option}]`,
);
try {
playgroundEslintrc = JSON.stringify(
JSON.parse(playgroundEslintrc),
null,
2,
);
} catch (err) {
// eslint-disable-next-line no-console
console.error(
`Invalid JSON detected in ${page.file.basename}. Check the \`option\` in the meta strings of code blocks.`,
);
throw err;
}
}
node.meta = [
node.meta,
`eslintrcHash="${convertToPlaygroundHash(playgroundEslintrc)}"`,
]
.filter(Boolean)
.join(' ');
}
}
}
nodeIsJsxTabs(node: unist.Node): node is MdxJsxFlowElement¶
Parameters:
nodeunist.Node
Returns: node is MdxJsxFlowElement
Code
Internal helpers¶
Declared inside another function in this file.
addHashesToChildrenTabs(node: MdxJsxFlowElement): void¶
Parameters:
nodeMdxJsxFlowElement
Returns: void
Calls:
addHashToNodeIfCode
Code
addHashToNodeIfCode(node: unist.Node, insideTab: boolean): void¶
Parameters:
nodeunist.NodeinsideTabboolean
Returns: void
Calls:
nodeIsCode (from ../utils/nodes)node.meta?.includesnode.meta?.matchplaygroundEslintrc.replaceJSON.stringifyJSON.parseconsole.error[ node.meta,eslintrcHash="${convertToPlaygroundHash(playgroundEslintrc)}", ] .filter(Boolean) .join
Internal Comments:
Code
function addHashToNodeIfCode(node: unist.Node, insideTab?: boolean): void {
if (
nodeIsCode(node) &&
(insideTab || node.meta?.includes('showPlaygroundButton')) &&
!node.meta?.includes('title="eslint.config.mjs"') &&
!node.meta?.includes('title=".eslintrc.cjs"') &&
!node.meta?.includes('eslintrcHash=')
) {
let playgroundEslintrc = eslintrc;
const option = node.meta?.match(optionRegex)?.groups?.option;
if (option) {
playgroundEslintrc = playgroundEslintrc.replace(
'"error"',
`["error", ${option}]`,
);
try {
playgroundEslintrc = JSON.stringify(
JSON.parse(playgroundEslintrc),
null,
2,
);
} catch (err) {
// eslint-disable-next-line no-console
console.error(
`Invalid JSON detected in ${page.file.basename}. Check the \`option\` in the meta strings of code blocks.`,
);
throw err;
}
}
node.meta = [
node.meta,
`eslintrcHash="${convertToPlaygroundHash(playgroundEslintrc)}"`,
]
.filter(Boolean)
.join(' ');
}
}
Generated by Syntax Scribe