|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of unknown DOM property |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Constants |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var UNKNOWN_MESSAGE = 'Unknown property \'{{name}}\' found, use \'{{standardName}}\' instead'; |
| 12 | + |
| 13 | +var DOM_ATTRIBUTE_NAMES = { |
| 14 | + 'accept-charset': 'acceptCharset', |
| 15 | + class: 'className', |
| 16 | + for: 'htmlFor', |
| 17 | + 'http-equiv': 'httpEquiv' |
| 18 | +}; |
| 19 | + |
| 20 | +var DOM_PROPERTY_NAMES = [ |
| 21 | + 'acceptCharset', 'accessKey', 'allowFullScreen', 'allowTransparency', 'autoComplete', 'autoFocus', 'autoPlay', |
| 22 | + 'cellPadding', 'cellSpacing', 'charSet', 'classID', 'className', 'colSpan', 'contentEditable', 'contextMenu', |
| 23 | + 'crossOrigin', 'dateTime', 'encType', 'formAction', 'formEncType', 'formMethod', 'formNoValidate', 'formTarget', |
| 24 | + 'frameBorder', 'hrefLang', 'htmlFor', 'httpEquiv', 'marginHeight', 'marginWidth', 'maxLength', 'mediaGroup', |
| 25 | + 'noValidate', 'radioGroup', 'readOnly', 'rowSpan', 'spellCheck', 'srcDoc', 'srcSet', 'tabIndex', 'useMap', |
| 26 | + 'itemProp', 'itemScope', 'itemType', 'itemRef', 'itemId' |
| 27 | +]; |
| 28 | + |
| 29 | +// ------------------------------------------------------------------------------ |
| 30 | +// Helpers |
| 31 | +// ------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +/** |
| 34 | + * Checks if a node name match the JSX tag convention. |
| 35 | + * @param {String} name - Name of the node to check. |
| 36 | + * @returns {boolean} Whether or not the node name match the JSX tag convention. |
| 37 | + */ |
| 38 | +var tagConvention = /^[a-z]|\-/; |
| 39 | +function isTagName(name) { |
| 40 | + return tagConvention.test(name); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Get the standard name of the attribute. |
| 45 | + * @param {String} name - Name of the attribute. |
| 46 | + * @returns {String} The standard name of the attribute. |
| 47 | + */ |
| 48 | +function getStandardName(name) { |
| 49 | + if (DOM_ATTRIBUTE_NAMES[name]) { |
| 50 | + return DOM_ATTRIBUTE_NAMES[name]; |
| 51 | + } |
| 52 | + var i; |
| 53 | + var found = DOM_PROPERTY_NAMES.some(function(element, index) { |
| 54 | + i = index; |
| 55 | + return element.toLowerCase() === name; |
| 56 | + }); |
| 57 | + return found ? DOM_PROPERTY_NAMES[i] : null; |
| 58 | +} |
| 59 | + |
| 60 | +// ------------------------------------------------------------------------------ |
| 61 | +// Rule Definition |
| 62 | +// ------------------------------------------------------------------------------ |
| 63 | + |
| 64 | +module.exports = function(context) { |
| 65 | + |
| 66 | + return { |
| 67 | + |
| 68 | + JSXAttribute: function(node) { |
| 69 | + var standardName = getStandardName(node.name.name); |
| 70 | + if (!isTagName(node.parent.name.name) || !standardName) { |
| 71 | + return; |
| 72 | + } |
| 73 | + context.report(node, UNKNOWN_MESSAGE, { |
| 74 | + name: node.name.name, |
| 75 | + standardName: standardName |
| 76 | + }); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | +}; |
0 commit comments