Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <p align="center">
  2. <a href="https://travis-ci.org/evcohen/jsx-ast-utils">
  3. <img src="https://api.travis-ci.org/evcohen/jsx-ast-utils.svg?branch=master"
  4. alt="build status">
  5. </a>
  6. <a href="https://npmjs.org/package/jsx-ast-utils">
  7. <img src="https://img.shields.io/npm/v/jsx-ast-utils.svg"
  8. alt="npm version">
  9. </a>
  10. <a href="https://github.com/evcohen/jsx-ast-utils/blob/master/LICENSE.md">
  11. <img src="https://img.shields.io/npm/l/jsx-ast-utils.svg"
  12. alt="license">
  13. </a>
  14. <a href='https://coveralls.io/github/evcohen/jsx-ast-utils?branch=master'>
  15. <img src='https://coveralls.io/repos/github/evcohen/jsx-ast-utils/badge.svg?branch=master' alt='Coverage Status' />
  16. </a>
  17. <a href='https://npmjs.org/package/jsx-ast-utils'>
  18. <img src='https://img.shields.io/npm/dt/jsx-ast-utils.svg'
  19. alt='Total npm downloads' />
  20. </a>
  21. </p>
  22. # jsx-ast-utils
  23. AST utility module for statically analyzing JSX.
  24. ## Installation
  25. ```sh
  26. $ npm i jsx-ast-utils --save
  27. ```
  28. ## Usage
  29. This is a utility module to evaluate AST objects for JSX syntax. This can be super useful when writing linting rules for JSX code. It was originally in the code for [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y), however I thought it could be useful to be extracted and maintained separately so **you** could write new interesting rules to statically analyze JSX.
  30. ### ESLint example
  31. ```js
  32. import { hasProp } from 'jsx-ast-utils';
  33. // OR: var hasProp = require('jsx-ast-utils').hasProp;
  34. // OR: const hasProp = require('jsx-ast-utils/hasProp');
  35. // OR: import hasProp from 'jsx-ast-utils/hasProp';
  36. module.exports = context => ({
  37. JSXOpeningElement: node => {
  38. const onChange = hasProp(node.attributes, 'onChange');
  39. if (onChange) {
  40. context.report({
  41. node,
  42. message: `No onChange!`
  43. });
  44. }
  45. }
  46. });
  47. ```
  48. ## API
  49. ### AST Resources
  50. 1. [JSX spec](https://github.com/facebook/jsx/blob/master/AST.md)
  51. 2. [JS spec](https://github.com/estree/estree/blob/master/spec.md)
  52. ### hasProp
  53. ```js
  54. hasProp(props, prop, options);
  55. ```
  56. Returns boolean indicating whether an prop exists as an attribute on a JSX element node.
  57. #### Props
  58. Object - The attributes on the visited node. (Usually `node.attributes`).
  59. #### Prop
  60. String - A string representation of the prop you want to check for existence.
  61. #### Options
  62. Object - An object representing options for existence checking
  63. 1. `ignoreCase` - automatically set to `true`.
  64. 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
  65. props, it will assume the prop you are looking for is not in the spread.
  66. Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
  67. <hr />
  68. ### hasAnyProp
  69. ```js
  70. hasAnyProp(props, prop, options);
  71. ```
  72. Returns a boolean indicating if **any** of props in `prop` argument exist on the node.
  73. #### Props
  74. Object - The attributes on the visited node. (Usually `node.attributes`).
  75. #### Prop
  76. Array<String> - An array of strings representing the props you want to check for existence.
  77. #### Options
  78. Object - An object representing options for existence checking
  79. 1. `ignoreCase` - automatically set to `true`.
  80. 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
  81. props, it will assume the prop you are looking for is not in the spread.
  82. Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
  83. <hr />
  84. ### hasEveryProp
  85. ```js
  86. hasEveryProp(props, prop, options);
  87. ```
  88. Returns a boolean indicating if **all** of props in `prop` argument exist on the node.
  89. #### Props
  90. Object - The attributes on the visited node. (Usually `node.attributes`).
  91. #### Prop
  92. Array<String> - An array of strings representing the props you want to check for existence.
  93. #### Options
  94. Object - An object representing options for existence checking
  95. 1. `ignoreCase` - automatically set to `true`.
  96. 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
  97. props, it will assume the prop you are looking for is not in the spread.
  98. Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
  99. <hr />
  100. ### getProp
  101. ```js
  102. getProp(props, prop, options);
  103. ```
  104. Returns the JSXAttribute itself or undefined, indicating the prop is not present on the JSXOpeningElement.
  105. #### Props
  106. Object - The attributes on the visited node. (Usually `node.attributes`).
  107. #### Prop
  108. String - A string representation of the prop you want to check for existence.
  109. #### Options
  110. Object - An object representing options for existence checking
  111. 1. `ignoreCase` - automatically set to `true`.
  112. <hr />
  113. ### elementType
  114. ```js
  115. elementType(node)
  116. ```
  117. Returns the tagName associated with a JSXElement.
  118. #### Node
  119. Object - The visited JSXElement node object.
  120. <hr />
  121. ### getPropValue
  122. ```js
  123. getPropValue(prop);
  124. ```
  125. Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
  126. This function should return the most *closely* associated value with the intention of the JSX.
  127. #### Prop
  128. Object - The JSXAttribute collected by AST parser.
  129. <hr />
  130. ### getLiteralPropValue
  131. ```js
  132. getLiteralPropValue(prop);
  133. ```
  134. Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
  135. This function should return a value only if we can extract a literal value from its attribute (i.e. values that have generic types in JavaScript - strings, numbers, booleans, etc.)
  136. #### Prop
  137. Object - The JSXAttribute collected by AST parser.
  138. <hr />
  139. ### propName
  140. ```js
  141. propName(prop);
  142. ```
  143. Returns the name associated with a JSXAttribute. For example, given `<div foo="bar" />` and the JSXAttribute for `foo`, this will return the string `"foo"`.
  144. #### Prop
  145. Object - The JSXAttribute collected by AST parser.
  146. <hr />
  147. ### eventHandlers
  148. ```js
  149. console.log(eventHandlers);
  150. /*
  151. [
  152. 'onCopy',
  153. 'onCut',
  154. 'onPaste',
  155. 'onCompositionEnd',
  156. 'onCompositionStart',
  157. 'onCompositionUpdate',
  158. 'onKeyDown',
  159. 'onKeyPress',
  160. 'onKeyUp',
  161. 'onFocus',
  162. 'onBlur',
  163. 'onChange',
  164. 'onInput',
  165. 'onSubmit',
  166. 'onClick',
  167. 'onContextMenu',
  168. 'onDblClick',
  169. 'onDoubleClick',
  170. 'onDrag',
  171. 'onDragEnd',
  172. 'onDragEnter',
  173. 'onDragExit',
  174. 'onDragLeave',
  175. 'onDragOver',
  176. 'onDragStart',
  177. 'onDrop',
  178. 'onMouseDown',
  179. 'onMouseEnter',
  180. 'onMouseLeave',
  181. 'onMouseMove',
  182. 'onMouseOut',
  183. 'onMouseOver',
  184. 'onMouseUp',
  185. 'onSelect',
  186. 'onTouchCancel',
  187. 'onTouchEnd',
  188. 'onTouchMove',
  189. 'onTouchStart',
  190. 'onScroll',
  191. 'onWheel',
  192. 'onAbort',
  193. 'onCanPlay',
  194. 'onCanPlayThrough',
  195. 'onDurationChange',
  196. 'onEmptied',
  197. 'onEncrypted',
  198. 'onEnded',
  199. 'onError',
  200. 'onLoadedData',
  201. 'onLoadedMetadata',
  202. 'onLoadStart',
  203. 'onPause',
  204. 'onPlay',
  205. 'onPlaying',
  206. 'onProgress',
  207. 'onRateChange',
  208. 'onSeeked',
  209. 'onSeeking',
  210. 'onStalled',
  211. 'onSuspend',
  212. 'onTimeUpdate',
  213. 'onVolumeChange',
  214. 'onWaiting',
  215. 'onLoad',
  216. 'onError',
  217. 'onAnimationStart',
  218. 'onAnimationEnd',
  219. 'onAnimationIteration',
  220. 'onTransitionEnd',
  221. ]
  222. */
  223. ```
  224. Contains a flat list of common event handler props used in JSX to attach behaviors
  225. to DOM events.
  226. #### eventHandlersByType
  227. The same list as `eventHandlers`, grouped into types.
  228. ```js
  229. console.log(eventHandlersByType);
  230. /*
  231. {
  232. clipboard: [ 'onCopy', 'onCut', 'onPaste' ],
  233. composition: [ 'onCompositionEnd', 'onCompositionStart', 'onCompositionUpdate' ],
  234. keyboard: [ 'onKeyDown', 'onKeyPress', 'onKeyUp' ],
  235. focus: [ 'onFocus', 'onBlur' ],
  236. form: [ 'onChange', 'onInput', 'onSubmit' ],
  237. mouse: [ 'onClick', 'onContextMenu', 'onDblClick', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave', 'onDragOver', 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver', 'onMouseUp' ],
  238. selection: [ 'onSelect' ],
  239. touch: [ 'onTouchCancel', 'onTouchEnd', 'onTouchMove', 'onTouchStart' ],
  240. ui: [ 'onScroll' ],
  241. wheel: [ 'onWheel' ],
  242. media: [ 'onAbort', 'onCanPlay', 'onCanPlayThrough', 'onDurationChange', 'onEmptied', 'onEncrypted', 'onEnded', 'onError', 'onLoadedData', 'onLoadedMetadata', 'onLoadStart', 'onPause', 'onPlay', 'onPlaying', 'onProgress', 'onRateChange', 'onSeeked', 'onSeeking', 'onStalled', 'onSuspend', 'onTimeUpdate', 'onVolumeChange', 'onWaiting' ],
  243. image: [ 'onLoad', 'onError' ],
  244. animation: [ 'onAnimationStart', 'onAnimationEnd', 'onAnimationIteration' ],
  245. transition: [ 'onTransitionEnd' ],
  246. }
  247. */
  248. ```