|
| 1 | +/** |
| 2 | + * @fileoverview Warn for common attribute typos. |
| 3 | + * @author Koen Punt |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +var Components = require('../util/Components'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +var PATTERN_REGEXP = new RegExp('^/(.+)/(.+)?$'); |
| 14 | + |
| 15 | +/** |
| 16 | + * Convert pattern string to a regular expression. |
| 17 | + * @param {String|RegExp} string regex |
| 18 | + * @return {RegExp} |
| 19 | + */ |
| 20 | +function parsePattern(pattern) { |
| 21 | + if (pattern instanceof RegExp) { |
| 22 | + return pattern; |
| 23 | + } |
| 24 | + if (PATTERN_REGEXP.exec(pattern)) { |
| 25 | + return new RegExp(RegExp.$1, RegExp.$2); |
| 26 | + } |
| 27 | + return new RegExp('^' + pattern + '$'); |
| 28 | +} |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + meta: { |
| 32 | + docs: { |
| 33 | + description: 'Warn for common attribute typos.', |
| 34 | + category: 'Possible Errors', |
| 35 | + recommended: false |
| 36 | + }, |
| 37 | + schema: [{ |
| 38 | + type: 'object', |
| 39 | + rules: { |
| 40 | + type: 'array', |
| 41 | + items: { |
| 42 | + type: 'object', |
| 43 | + properties: { |
| 44 | + pattern: { |
| 45 | + type: 'string' |
| 46 | + }, |
| 47 | + correct: { |
| 48 | + type: 'string' |
| 49 | + } |
| 50 | + }, |
| 51 | + additionalProperties: false |
| 52 | + } |
| 53 | + } |
| 54 | + }], |
| 55 | + fixable: 'code' |
| 56 | + }, |
| 57 | + |
| 58 | + create: Components.detect(function(context) { |
| 59 | + |
| 60 | + var rules = context.options || []; |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks if we are using an incorrect cased dangerouslySetInnerHTML attribute. |
| 64 | + * @param {ASTNode} node The AST node being checked. |
| 65 | + * @returns {Boolean} True if we are using a ref attribute, false if not. |
| 66 | + */ |
| 67 | + function isIncorrectAttributeName(regex, correct, node) { |
| 68 | + return Boolean( |
| 69 | + node.type === 'JSXAttribute' && |
| 70 | + node.name && |
| 71 | + node.name.name !== correct && |
| 72 | + regex.test(node.name.name) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + JSXAttribute: function(node) { |
| 78 | + rules.forEach(function(rule) { |
| 79 | + var regex = parsePattern(rule.pattern); |
| 80 | + if (isIncorrectAttributeName(regex, rule.correct, node)) { |
| 81 | + var attribute = node.name.name; |
| 82 | + context.report({ |
| 83 | + node: node, |
| 84 | + message: 'Using possible incorrect attribute `' + attribute + '`, did you mean `' + rule.correct + '`?', |
| 85 | + fix: function(fixer) { |
| 86 | + return fixer.replaceText(attribute, rule.correct); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + }; |
| 93 | + }) |
| 94 | +}; |
0 commit comments