|
| 1 | +'use strict'; |
| 2 | +const getDocumentationUrl = require('./utils/get-documentation-url'); |
| 3 | +const quoteString = require('./utils/quote-string'); |
| 4 | +const replaceTemplateElement = require('./utils/replace-template-element'); |
| 5 | +const escapeTemplateElementRaw = require('./utils/escape-template-element-raw'); |
| 6 | + |
| 7 | +const defaultPatterns = { |
| 8 | + '\'': '’' |
| 9 | +}; |
| 10 | + |
| 11 | +const defaultMessage = 'Prefer `{{suggest}}` over `{{match}}`.'; |
| 12 | + |
| 13 | +function getReplacements(patterns) { |
| 14 | + return Object.entries({ |
| 15 | + ...defaultPatterns, |
| 16 | + ...patterns |
| 17 | + }) |
| 18 | + .filter(([, options]) => options !== false) |
| 19 | + .map(([match, options]) => { |
| 20 | + if (typeof options === 'string') { |
| 21 | + options = { |
| 22 | + suggest: options |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + match, |
| 28 | + regex: new RegExp(match, 'gu'), |
| 29 | + fix: true, |
| 30 | + ...options |
| 31 | + }; |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +const create = context => { |
| 36 | + const {patterns} = { |
| 37 | + patterns: {}, |
| 38 | + ...context.options[0] |
| 39 | + }; |
| 40 | + const replacements = getReplacements(patterns); |
| 41 | + |
| 42 | + if (replacements.length === 0) { |
| 43 | + return {}; |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + 'Literal, TemplateElement': node => { |
| 48 | + const {type} = node; |
| 49 | + |
| 50 | + let string; |
| 51 | + if (type === 'Literal') { |
| 52 | + string = node.value; |
| 53 | + if (typeof string !== 'string') { |
| 54 | + return; |
| 55 | + } |
| 56 | + } else { |
| 57 | + string = node.value.raw; |
| 58 | + } |
| 59 | + |
| 60 | + if (!string) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const replacement = replacements.find(({regex}) => regex.test(string)); |
| 65 | + |
| 66 | + if (!replacement) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const {fix, message = defaultMessage, match, suggest} = replacement; |
| 71 | + const problem = { |
| 72 | + node, |
| 73 | + message, |
| 74 | + data: { |
| 75 | + match, |
| 76 | + suggest |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + if (!fix) { |
| 81 | + context.report(problem); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const fixed = string.replace(replacement.regex, suggest); |
| 86 | + if (type === 'Literal') { |
| 87 | + problem.fix = fixer => fixer.replaceText( |
| 88 | + node, |
| 89 | + quoteString(fixed, node.raw[0]) |
| 90 | + ); |
| 91 | + } else { |
| 92 | + problem.fix = fixer => replaceTemplateElement( |
| 93 | + fixer, |
| 94 | + node, |
| 95 | + escapeTemplateElementRaw(fixed) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + context.report(problem); |
| 100 | + } |
| 101 | + }; |
| 102 | +}; |
| 103 | + |
| 104 | +const schema = [ |
| 105 | + { |
| 106 | + type: 'object', |
| 107 | + properties: { |
| 108 | + patterns: { |
| 109 | + type: 'object', |
| 110 | + additionalProperties: { |
| 111 | + anyOf: [ |
| 112 | + { |
| 113 | + enum: [ |
| 114 | + false |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + type: 'string' |
| 119 | + }, |
| 120 | + { |
| 121 | + type: 'object', |
| 122 | + required: [ |
| 123 | + 'suggest' |
| 124 | + ], |
| 125 | + properties: { |
| 126 | + suggest: { |
| 127 | + type: 'string' |
| 128 | + }, |
| 129 | + fix: { |
| 130 | + type: 'boolean' |
| 131 | + // Default: true |
| 132 | + }, |
| 133 | + message: { |
| 134 | + type: 'string' |
| 135 | + // Default: '' |
| 136 | + } |
| 137 | + }, |
| 138 | + additionalProperties: false |
| 139 | + } |
| 140 | + ] |
| 141 | + }} |
| 142 | + }, |
| 143 | + additionalProperties: false |
| 144 | + } |
| 145 | +]; |
| 146 | + |
| 147 | +module.exports = { |
| 148 | + create, |
| 149 | + meta: { |
| 150 | + type: 'suggestion', |
| 151 | + docs: { |
| 152 | + url: getDocumentationUrl(__filename) |
| 153 | + }, |
| 154 | + fixable: 'code', |
| 155 | + schema |
| 156 | + } |
| 157 | +}; |
0 commit comments