選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

no-unknown-property.js 8.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * @fileoverview Prevent usage of unknown DOM property
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Constants
  9. // ------------------------------------------------------------------------------
  10. const DEFAULTS = {
  11. ignore: []
  12. };
  13. const UNKNOWN_MESSAGE = 'Unknown property \'{{name}}\' found, use \'{{standardName}}\' instead';
  14. const WRONG_TAG_MESSAGE = 'Invalid property \'{{name}}\' found on tag \'{{tagName}}\', but it is only allowed on: {{allowedTags}}';
  15. const DOM_ATTRIBUTE_NAMES = {
  16. 'accept-charset': 'acceptCharset',
  17. class: 'className',
  18. for: 'htmlFor',
  19. 'http-equiv': 'httpEquiv',
  20. crossorigin: 'crossOrigin'
  21. };
  22. const ATTRIBUTE_TAGS_MAP = {
  23. crossOrigin: ['script', 'img', 'video', 'audio', 'link']
  24. };
  25. const SVGDOM_ATTRIBUTE_NAMES = {
  26. 'accent-height': 'accentHeight',
  27. 'alignment-baseline': 'alignmentBaseline',
  28. 'arabic-form': 'arabicForm',
  29. 'baseline-shift': 'baselineShift',
  30. 'cap-height': 'capHeight',
  31. 'clip-path': 'clipPath',
  32. 'clip-rule': 'clipRule',
  33. 'color-interpolation': 'colorInterpolation',
  34. 'color-interpolation-filters': 'colorInterpolationFilters',
  35. 'color-profile': 'colorProfile',
  36. 'color-rendering': 'colorRendering',
  37. 'dominant-baseline': 'dominantBaseline',
  38. 'enable-background': 'enableBackground',
  39. 'fill-opacity': 'fillOpacity',
  40. 'fill-rule': 'fillRule',
  41. 'flood-color': 'floodColor',
  42. 'flood-opacity': 'floodOpacity',
  43. 'font-family': 'fontFamily',
  44. 'font-size': 'fontSize',
  45. 'font-size-adjust': 'fontSizeAdjust',
  46. 'font-stretch': 'fontStretch',
  47. 'font-style': 'fontStyle',
  48. 'font-variant': 'fontVariant',
  49. 'font-weight': 'fontWeight',
  50. 'glyph-name': 'glyphName',
  51. 'glyph-orientation-horizontal': 'glyphOrientationHorizontal',
  52. 'glyph-orientation-vertical': 'glyphOrientationVertical',
  53. 'horiz-adv-x': 'horizAdvX',
  54. 'horiz-origin-x': 'horizOriginX',
  55. 'image-rendering': 'imageRendering',
  56. 'letter-spacing': 'letterSpacing',
  57. 'lighting-color': 'lightingColor',
  58. 'marker-end': 'markerEnd',
  59. 'marker-mid': 'markerMid',
  60. 'marker-start': 'markerStart',
  61. 'overline-position': 'overlinePosition',
  62. 'overline-thickness': 'overlineThickness',
  63. 'paint-order': 'paintOrder',
  64. 'panose-1': 'panose1',
  65. 'pointer-events': 'pointerEvents',
  66. 'rendering-intent': 'renderingIntent',
  67. 'shape-rendering': 'shapeRendering',
  68. 'stop-color': 'stopColor',
  69. 'stop-opacity': 'stopOpacity',
  70. 'strikethrough-position': 'strikethroughPosition',
  71. 'strikethrough-thickness': 'strikethroughThickness',
  72. 'stroke-dasharray': 'strokeDasharray',
  73. 'stroke-dashoffset': 'strokeDashoffset',
  74. 'stroke-linecap': 'strokeLinecap',
  75. 'stroke-linejoin': 'strokeLinejoin',
  76. 'stroke-miterlimit': 'strokeMiterlimit',
  77. 'stroke-opacity': 'strokeOpacity',
  78. 'stroke-width': 'strokeWidth',
  79. 'text-anchor': 'textAnchor',
  80. 'text-decoration': 'textDecoration',
  81. 'text-rendering': 'textRendering',
  82. 'underline-position': 'underlinePosition',
  83. 'underline-thickness': 'underlineThickness',
  84. 'unicode-bidi': 'unicodeBidi',
  85. 'unicode-range': 'unicodeRange',
  86. 'units-per-em': 'unitsPerEm',
  87. 'v-alphabetic': 'vAlphabetic',
  88. 'v-hanging': 'vHanging',
  89. 'v-ideographic': 'vIdeographic',
  90. 'v-mathematical': 'vMathematical',
  91. 'vector-effect': 'vectorEffect',
  92. 'vert-adv-y': 'vertAdvY',
  93. 'vert-origin-x': 'vertOriginX',
  94. 'vert-origin-y': 'vertOriginY',
  95. 'word-spacing': 'wordSpacing',
  96. 'writing-mode': 'writingMode',
  97. 'x-height': 'xHeight',
  98. 'xlink:actuate': 'xlinkActuate',
  99. 'xlink:arcrole': 'xlinkArcrole',
  100. 'xlink:href': 'xlinkHref',
  101. 'xlink:role': 'xlinkRole',
  102. 'xlink:show': 'xlinkShow',
  103. 'xlink:title': 'xlinkTitle',
  104. 'xlink:type': 'xlinkType',
  105. 'xml:base': 'xmlBase',
  106. 'xml:lang': 'xmlLang',
  107. 'xml:space': 'xmlSpace'
  108. };
  109. const DOM_PROPERTY_NAMES = [
  110. // Standard
  111. 'acceptCharset', 'accessKey', 'allowFullScreen', 'allowTransparency', 'autoComplete', 'autoFocus', 'autoPlay',
  112. 'cellPadding', 'cellSpacing', 'classID', 'className', 'colSpan', 'contentEditable', 'contextMenu',
  113. 'dateTime', 'encType', 'formAction', 'formEncType', 'formMethod', 'formNoValidate', 'formTarget',
  114. 'frameBorder', 'hrefLang', 'htmlFor', 'httpEquiv', 'inputMode', 'keyParams', 'keyType', 'marginHeight', 'marginWidth',
  115. 'maxLength', 'mediaGroup', 'minLength', 'noValidate', 'onAnimationEnd', 'onAnimationIteration', 'onAnimationStart',
  116. 'onBlur', 'onChange', 'onClick', 'onContextMenu', 'onCopy', 'onCompositionEnd', 'onCompositionStart',
  117. 'onCompositionUpdate', 'onCut', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave',
  118. 'onError', 'onFocus', 'onInput', 'onKeyDown', 'onKeyPress', 'onKeyUp', 'onLoad', 'onWheel', 'onDragOver',
  119. 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver',
  120. 'onMouseUp', 'onPaste', 'onScroll', 'onSelect', 'onSubmit', 'onTransitionEnd', 'radioGroup', 'readOnly', 'rowSpan',
  121. 'spellCheck', 'srcDoc', 'srcLang', 'srcSet', 'tabIndex', 'useMap',
  122. // Non standard
  123. 'autoCapitalize', 'autoCorrect',
  124. 'autoSave',
  125. 'itemProp', 'itemScope', 'itemType', 'itemRef', 'itemID'
  126. ];
  127. // ------------------------------------------------------------------------------
  128. // Helpers
  129. // ------------------------------------------------------------------------------
  130. /**
  131. * Checks if a node matches the JSX tag convention.
  132. * @param {Object} node - JSX element being tested.
  133. * @returns {boolean} Whether or not the node name match the JSX tag convention.
  134. */
  135. const tagConvention = /^[a-z][^-]*$/;
  136. function isTagName(node) {
  137. if (tagConvention.test(node.parent.name.name)) {
  138. // http://www.w3.org/TR/custom-elements/#type-extension-semantics
  139. return !node.parent.attributes.some(attrNode => (
  140. attrNode.type === 'JSXAttribute' &&
  141. attrNode.name.type === 'JSXIdentifier' &&
  142. attrNode.name.name === 'is'
  143. ));
  144. }
  145. return false;
  146. }
  147. /**
  148. * Extracts the tag name for the JSXAttribute
  149. * @param {Object} node - JSXAttribute being tested.
  150. * @returns {String} tag name
  151. */
  152. function getTagName(node) {
  153. if (node && node.parent && node.parent.name && node.parent.name) {
  154. return node.parent.name.name;
  155. }
  156. return null;
  157. }
  158. /**
  159. * Get the standard name of the attribute.
  160. * @param {String} name - Name of the attribute.
  161. * @returns {String} The standard name of the attribute.
  162. */
  163. function getStandardName(name) {
  164. if (DOM_ATTRIBUTE_NAMES[name]) {
  165. return DOM_ATTRIBUTE_NAMES[name];
  166. }
  167. if (SVGDOM_ATTRIBUTE_NAMES[name]) {
  168. return SVGDOM_ATTRIBUTE_NAMES[name];
  169. }
  170. let i;
  171. const found = DOM_PROPERTY_NAMES.some((element, index) => {
  172. i = index;
  173. return element.toLowerCase() === name;
  174. });
  175. return found ? DOM_PROPERTY_NAMES[i] : null;
  176. }
  177. // ------------------------------------------------------------------------------
  178. // Rule Definition
  179. // ------------------------------------------------------------------------------
  180. module.exports = {
  181. meta: {
  182. docs: {
  183. description: 'Prevent usage of unknown DOM property',
  184. category: 'Possible Errors',
  185. recommended: true,
  186. url: docsUrl('no-unknown-property')
  187. },
  188. fixable: 'code',
  189. schema: [{
  190. type: 'object',
  191. properties: {
  192. ignore: {
  193. type: 'array',
  194. items: {
  195. type: 'string'
  196. }
  197. }
  198. },
  199. additionalProperties: false
  200. }]
  201. },
  202. create: function(context) {
  203. function getIgnoreConfig() {
  204. return context.options[0] && context.options[0].ignore || DEFAULTS.ignore;
  205. }
  206. const sourceCode = context.getSourceCode();
  207. return {
  208. JSXAttribute: function(node) {
  209. const ignoreNames = getIgnoreConfig();
  210. const name = sourceCode.getText(node.name);
  211. if (ignoreNames.indexOf(name) >= 0) {
  212. return;
  213. }
  214. const tagName = getTagName(node);
  215. const allowedTags = ATTRIBUTE_TAGS_MAP[name];
  216. if (tagName && allowedTags && /[^A-Z]/.test(tagName.charAt(0)) && allowedTags.indexOf(tagName) === -1) {
  217. context.report({
  218. node: node,
  219. message: WRONG_TAG_MESSAGE,
  220. data: {
  221. name: name,
  222. tagName: tagName,
  223. allowedTags: allowedTags.join(', ')
  224. }
  225. });
  226. }
  227. const standardName = getStandardName(name);
  228. if (!isTagName(node) || !standardName) {
  229. return;
  230. }
  231. context.report({
  232. node: node,
  233. message: UNKNOWN_MESSAGE,
  234. data: {
  235. name: name,
  236. standardName: standardName
  237. },
  238. fix: function(fixer) {
  239. return fixer.replaceText(node.name, standardName);
  240. }
  241. });
  242. }
  243. };
  244. }
  245. };