|
| 1 | +/** |
| 2 | + * @author Toru Nagashima <https://github.com/mysticatea> |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +const path = require('path'); |
| 12 | +const util = require('../utils'); |
| 13 | + |
| 14 | +// ----------------------------------------------------------------------------- |
| 15 | +// Rule Definition |
| 16 | +// ----------------------------------------------------------------------------- |
| 17 | + |
| 18 | +module.exports = { |
| 19 | + meta: { |
| 20 | + docs: { |
| 21 | + description: 'require rules to implement a meta.docs.url property', |
| 22 | + category: 'Rules', |
| 23 | + recommended: false, |
| 24 | + }, |
| 25 | + fixable: 'code', |
| 26 | + schema: [{ |
| 27 | + type: 'object', |
| 28 | + properties: { |
| 29 | + pattern: { type: 'string' }, |
| 30 | + }, |
| 31 | + additionalProperties: false, |
| 32 | + }], |
| 33 | + }, |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates AST event handlers for require-meta-docs-url. |
| 37 | + * @param {RuleContext} context - The rule context. |
| 38 | + * @returns {Object} AST event handlers. |
| 39 | + */ |
| 40 | + create (context) { |
| 41 | + const options = context.options[0] || {}; |
| 42 | + const sourceCode = context.getSourceCode(); |
| 43 | + const filename = context.getFilename(); |
| 44 | + const ruleName = filename === '<input>' ? undefined : path.basename(filename, '.js'); |
| 45 | + const expectedUrl = !options.pattern || !ruleName |
| 46 | + ? undefined |
| 47 | + : options.pattern.replace(/{{\s*name\s*}}/g, ruleName); |
| 48 | + |
| 49 | + /** |
| 50 | + * Check whether a given node is the expected URL. |
| 51 | + * @param {Node} node The node of property value to check. |
| 52 | + * @returns {boolean} `true` if the node is the expected URL. |
| 53 | + */ |
| 54 | + function isExpectedUrl (node) { |
| 55 | + return Boolean( |
| 56 | + node && |
| 57 | + node.type === 'Literal' && |
| 58 | + typeof node.value === 'string' && |
| 59 | + ( |
| 60 | + expectedUrl === undefined || |
| 61 | + node.value === expectedUrl |
| 62 | + ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Insert a given property into a given object literal. |
| 68 | + * @param {SourceCodeFixer} fixer The fixer. |
| 69 | + * @param {Node} node The ObjectExpression node to insert a property. |
| 70 | + * @param {string} propertyText The property code to insert. |
| 71 | + * @returns {void} |
| 72 | + */ |
| 73 | + function insertProperty (fixer, node, propertyText) { |
| 74 | + if (node.properties.length === 0) { |
| 75 | + return fixer.replaceText(node, `{\n${propertyText}\n}`); |
| 76 | + } |
| 77 | + return fixer.insertTextAfter( |
| 78 | + sourceCode.getLastToken(node.properties[node.properties.length - 1]), |
| 79 | + `,\n${propertyText}` |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + return { |
| 84 | + Program (node) { |
| 85 | + const info = util.getRuleInfo(node); |
| 86 | + const metaNode = info && info.meta; |
| 87 | + const docsPropNode = |
| 88 | + metaNode && |
| 89 | + metaNode.properties && |
| 90 | + metaNode.properties.find(p => p.type === 'Property' && util.getKeyName(p) === 'docs'); |
| 91 | + const urlPropNode = |
| 92 | + docsPropNode && |
| 93 | + docsPropNode.value.properties && |
| 94 | + docsPropNode.value.properties.find(p => p.type === 'Property' && util.getKeyName(p) === 'url'); |
| 95 | + |
| 96 | + if (isExpectedUrl(urlPropNode && urlPropNode.value)) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + context.report({ |
| 101 | + loc: |
| 102 | + (urlPropNode && urlPropNode.value.loc) || |
| 103 | + (docsPropNode && docsPropNode.value.loc) || |
| 104 | + (metaNode && metaNode.loc) || |
| 105 | + node.loc.start, |
| 106 | + |
| 107 | + message: |
| 108 | + !urlPropNode ? 'Rules should export a `meta.docs.url` property.' : |
| 109 | + !expectedUrl ? '`meta.docs.url` property must be a string.' : |
| 110 | + /* otherwise */ '`meta.docs.url` property must be `{{expectedUrl}}`.', |
| 111 | + |
| 112 | + data: { |
| 113 | + expectedUrl, |
| 114 | + }, |
| 115 | + |
| 116 | + fix (fixer) { |
| 117 | + if (expectedUrl) { |
| 118 | + const urlString = JSON.stringify(expectedUrl); |
| 119 | + if (urlPropNode) { |
| 120 | + return fixer.replaceText(urlPropNode.value, urlString); |
| 121 | + } |
| 122 | + if (docsPropNode && docsPropNode.value.type === 'ObjectExpression') { |
| 123 | + return insertProperty(fixer, docsPropNode.value, `url: ${urlString}`); |
| 124 | + } |
| 125 | + if (!docsPropNode && metaNode && metaNode.type === 'ObjectExpression') { |
| 126 | + return insertProperty(fixer, metaNode, `docs: {\nurl: ${urlString}\n}`); |
| 127 | + } |
| 128 | + } |
| 129 | + return null; |
| 130 | + }, |
| 131 | + }); |
| 132 | + }, |
| 133 | + }; |
| 134 | + }, |
| 135 | +}; |
0 commit comments