UNPKG

@sourcegraph/eslint-plugin-sourcegraph

Version:
87 lines 3.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkHelpLinks = exports.messages = void 0; const experimental_utils_1 = require("@typescript-eslint/experimental-utils"); const utils_1 = require("../../utils"); exports.messages = { invalidHelpLink: 'Help link to non-existent page: {{ destination }}', }; exports.checkHelpLinks = (0, utils_1.createRule)({ name: 'check-help-links', meta: { docs: { description: 'Check that /help links point to real, non-redirected pages', recommended: false, }, messages: exports.messages, schema: [ { type: 'object', properties: { docsiteList: { type: 'array', items: { type: 'string', }, }, }, additionalProperties: false, }, ], type: 'problem', }, defaultOptions: [], create(context) { const pages = new Set(); if (process.env.DOCSITE_LIST) { process.env.DOCSITE_LIST.split('\n').forEach(page => { return pages.add(page); }); } else if (context.options.length > 0) { context.options[0].docsiteList.forEach(page => { return pages.add(page); }); } if (pages.size === 0) { return {}; } return { JSXOpeningElement: node => { let attributeName; if (node.name.type === experimental_utils_1.AST_NODE_TYPES.JSXIdentifier) { if (node.name.name === 'Link') { attributeName = 'to'; } else if (node.name.name === 'a') { attributeName = 'href'; } } else { return; } const target = node.attributes.reduce((target, attribute) => { return (target || (attribute.type === experimental_utils_1.AST_NODE_TYPES.JSXAttribute && attribute.name && attribute.name.name === attributeName && attribute.value?.type === experimental_utils_1.AST_NODE_TYPES.Literal ? attribute.value.value : null)); }, null); if (typeof target !== 'string' || !target.startsWith('/help/')) { return; } const destination = target.slice(6).split('#')[0].replace(/\/+$/, ''); if (!pages.has(destination + '.md') && !pages.has(destination + '/index.md')) { context.report({ node, messageId: 'invalidHelpLink', data: { destination }, }); } }, }; }, }); //# sourceMappingURL=check-help-links.js.map