|
| 1 | +import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils' |
| 2 | +import { Literal } from '@typescript-eslint/types/dist/ast-spec' |
| 3 | + |
| 4 | +import { createRule } from '../../utils' |
| 5 | + |
| 6 | +export const messages = { |
| 7 | + invalidHelpLink: 'Help link to non-existent page: {{ destination }}', |
| 8 | +} |
| 9 | + |
| 10 | +export interface Option { |
| 11 | + docsiteList: { |
| 12 | + type: string |
| 13 | + }[] |
| 14 | +} |
| 15 | + |
| 16 | +export const checkHelpLinks = createRule<Option[], keyof typeof messages>({ |
| 17 | + name: 'check-help-links', |
| 18 | + meta: { |
| 19 | + docs: { |
| 20 | + description: 'Check that /help links point to real, non-redirected pages', |
| 21 | + recommended: false, |
| 22 | + }, |
| 23 | + messages, |
| 24 | + schema: [ |
| 25 | + { |
| 26 | + type: 'object', |
| 27 | + properties: { |
| 28 | + docsiteList: { |
| 29 | + type: 'array', |
| 30 | + items: { |
| 31 | + type: 'string', |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + additionalProperties: false, |
| 36 | + }, |
| 37 | + ], |
| 38 | + type: 'problem', |
| 39 | + }, |
| 40 | + defaultOptions: [], |
| 41 | + create(context) { |
| 42 | + // Build the set of valid pages. In order, we'll try to get this from: |
| 43 | + // |
| 44 | + // 1. The DOCSITE_LIST environment variable, which should be a newline |
| 45 | + // separated list of pages, as outputted by `docsite ls`. |
| 46 | + // 2. The docsiteList rule option, which should be an array of pages. |
| 47 | + // |
| 48 | + // If neither of these are set, this rule will silently pass, so as not to |
| 49 | + // require docsite to be run when a user wants to run eslint in general. |
| 50 | + const pages = new Set() |
| 51 | + if (process.env.DOCSITE_LIST) { |
| 52 | + process.env.DOCSITE_LIST.split('\n').forEach(page => { |
| 53 | + return pages.add(page) |
| 54 | + }) |
| 55 | + } else if (context.options.length > 0) { |
| 56 | + context.options[0].docsiteList.forEach(page => { |
| 57 | + return pages.add(page) |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + // No pages were provided, so we'll return an empty object and do nothing. |
| 62 | + if (pages.size === 0) { |
| 63 | + return {} |
| 64 | + } |
| 65 | + |
| 66 | + // Return the object that will install the listeners we want. In this case, |
| 67 | + // we only need to look at JSX opening elements. |
| 68 | + // |
| 69 | + // Note that we could use AST selectors below, but the structure of the AST |
| 70 | + // makes that tricky: the identifer (Link or a) and attribute (to or href) |
| 71 | + // we use to identify an element of interest are siblings, so we'd probably |
| 72 | + // have to select on the identifier and have some ugly traversal code below |
| 73 | + // to check the attribute. It feels cleaner to do it this way with the |
| 74 | + // opening element as the context. |
| 75 | + return { |
| 76 | + JSXOpeningElement: node => { |
| 77 | + // Figure out what kind of element we have and therefore what attribute |
| 78 | + // we'd want to look for. |
| 79 | + let attributeName: string |
| 80 | + |
| 81 | + if (node.name.type === AST_NODE_TYPES.JSXIdentifier) { |
| 82 | + if (node.name.name === 'Link') { |
| 83 | + attributeName = 'to' |
| 84 | + } else if (node.name.name === 'a') { |
| 85 | + attributeName = 'href' |
| 86 | + } |
| 87 | + } else { |
| 88 | + // Anything that's not a link is uninteresting. |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + // Go find the link target in the attribute array. |
| 93 | + const target = node.attributes.reduce<Literal['value']>((target, attribute) => { |
| 94 | + return ( |
| 95 | + target || |
| 96 | + (attribute.type === AST_NODE_TYPES.JSXAttribute && |
| 97 | + attribute.name && |
| 98 | + attribute.name.name === attributeName && |
| 99 | + attribute.value?.type === AST_NODE_TYPES.Literal |
| 100 | + ? attribute.value.value |
| 101 | + : null) |
| 102 | + ) |
| 103 | + }, null) |
| 104 | + |
| 105 | + // Make sure the target points to a help link; if not, we don't need to |
| 106 | + // go any further. |
| 107 | + if (typeof target !== 'string' || !target.startsWith('/help/')) { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + // Strip off the /help/ prefix, any anchor, and any trailing slash, then |
| 112 | + // look up the resultant page in the pages set, bearing in mind that it |
| 113 | + // might point to a directory and we also need to look for any index |
| 114 | + // page that might exist. |
| 115 | + const destination = target.slice(6).split('#')[0].replace(/\/+$/, '') |
| 116 | + |
| 117 | + if (!pages.has(destination + '.md') && !pages.has(destination + '/index.md')) { |
| 118 | + context.report({ |
| 119 | + node, |
| 120 | + messageId: 'invalidHelpLink', |
| 121 | + data: { destination }, |
| 122 | + }) |
| 123 | + } |
| 124 | + }, |
| 125 | + } |
| 126 | + }, |
| 127 | +}) |
0 commit comments