|
| 1 | +import type { TSESTree as AST } from "@typescript-eslint/types"; |
| 2 | +import { ESLintUtils } from "@typescript-eslint/utils"; |
| 3 | +import type { SourceCode } from "@typescript-eslint/utils/ts-eslint"; |
| 4 | + |
| 5 | +import references from "../docs/public/canonical-references.json" with { type: "json" }; |
| 6 | + |
| 7 | +const referenceSet = new Set(references); |
| 8 | + |
| 9 | +export const validInheritDoc = ESLintUtils.RuleCreator.withoutDocs({ |
| 10 | + create(context) { |
| 11 | + const source = context.sourceCode; |
| 12 | + let handled = new Set(); |
| 13 | + return { |
| 14 | + "*"(node) { |
| 15 | + for (const comment of source.getCommentsBefore(node)) { |
| 16 | + if (handled.has(comment)) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + handled.add(comment); |
| 20 | + if (comment.type === "Block") { |
| 21 | + const text = source.getText(comment); |
| 22 | + let match: RegExpMatchArray | null; |
| 23 | + if ((match = text.match(/@inheritDoc\s+([^\s}]+)/d))) { |
| 24 | + const canonicalReference = match[1]; |
| 25 | + if (!referenceSet.has(canonicalReference)) { |
| 26 | + context.report({ |
| 27 | + node: comment, |
| 28 | + loc: locForMatch(source, comment, match, 1), |
| 29 | + messageId: "invalidCanonicalReference", |
| 30 | + }); |
| 31 | + } |
| 32 | + } |
| 33 | + if ( |
| 34 | + (match = text.match(/@inheritdoc/di)) && |
| 35 | + match[0] !== "@inheritDoc" |
| 36 | + ) { |
| 37 | + const loc = locForMatch(source, comment, match, 0); |
| 38 | + context.report({ |
| 39 | + node: comment, |
| 40 | + loc, |
| 41 | + messageId: "invalidSpelling", |
| 42 | + fix(fixer) { |
| 43 | + return fixer.replaceTextRange( |
| 44 | + [ |
| 45 | + source.getIndexFromLoc(loc.start), |
| 46 | + source.getIndexFromLoc(loc.end), |
| 47 | + ], |
| 48 | + "@inheritDoc" |
| 49 | + ); |
| 50 | + }, |
| 51 | + }); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }, |
| 56 | + }; |
| 57 | + }, |
| 58 | + meta: { |
| 59 | + messages: { |
| 60 | + invalidCanonicalReference: "Unknown canonical reference.", |
| 61 | + invalidSpelling: "Invalid spelling of @inheritDoc.", |
| 62 | + }, |
| 63 | + type: "problem", |
| 64 | + schema: [], |
| 65 | + fixable: "code", |
| 66 | + }, |
| 67 | + defaultOptions: [], |
| 68 | +}); |
| 69 | + |
| 70 | +export const validMdxCanonicalReferences = ESLintUtils.RuleCreator.withoutDocs({ |
| 71 | + create(context, optionsWithDefault) { |
| 72 | + return { |
| 73 | + JSXAttribute(node: AST.JSXAttribute) { |
| 74 | + if (node.name.name == "canonicalReference") { |
| 75 | + const ref = |
| 76 | + node.value.type === "JSXExpressionContainer" ? |
| 77 | + node.value.expression |
| 78 | + : node.value; |
| 79 | + if (!ref || ref.type !== "Literal" || typeof ref.value !== "string") { |
| 80 | + context.report({ |
| 81 | + node: ref || node, |
| 82 | + messageId: "shouldBeString", |
| 83 | + }); |
| 84 | + } else if (!referenceSet.has(ref.value)) { |
| 85 | + context.report({ |
| 86 | + node: ref, |
| 87 | + messageId: "invalidCanonicalReference", |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + }, |
| 92 | + }; |
| 93 | + }, |
| 94 | + meta: { |
| 95 | + messages: { |
| 96 | + shouldBeString: "The canonicalReference value should be a string.", |
| 97 | + invalidCanonicalReference: "Unknown canonical reference.", |
| 98 | + }, |
| 99 | + type: "problem", |
| 100 | + schema: [], |
| 101 | + }, |
| 102 | + defaultOptions: [], |
| 103 | +}); |
| 104 | + |
| 105 | +function locForMatch( |
| 106 | + source: SourceCode, |
| 107 | + node: AST.NodeOrTokenData, |
| 108 | + match: RegExpMatchArray, |
| 109 | + index: number |
| 110 | +) { |
| 111 | + return { |
| 112 | + start: source.getLocFromIndex( |
| 113 | + source.getIndexFromLoc(node.loc.start) + match.indices[index][0] |
| 114 | + ), |
| 115 | + end: source.getLocFromIndex( |
| 116 | + source.getIndexFromLoc(node.loc.start) + match.indices[index][1] |
| 117 | + ), |
| 118 | + }; |
| 119 | +} |
0 commit comments