1 | # jsx-ast-utils <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![dependency status][deps-svg]][deps-url]
|
---|
6 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
7 | [![License][license-image]][license-url]
|
---|
8 | [![Downloads][downloads-image]][downloads-url]
|
---|
9 |
|
---|
10 | [![npm badge][npm-badge-png]][package-url]
|
---|
11 |
|
---|
12 | AST utility module for statically analyzing JSX.
|
---|
13 |
|
---|
14 | ## Installation
|
---|
15 | ```sh
|
---|
16 | $ npm i jsx-ast-utils --save
|
---|
17 | ```
|
---|
18 |
|
---|
19 | ## Usage
|
---|
20 | 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/jsx-eslint/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.
|
---|
21 |
|
---|
22 | ### ESLint example
|
---|
23 | ```js
|
---|
24 | import { hasProp } from 'jsx-ast-utils';
|
---|
25 | // OR: var hasProp = require('jsx-ast-utils').hasProp;
|
---|
26 | // OR: const hasProp = require('jsx-ast-utils/hasProp');
|
---|
27 | // OR: import hasProp from 'jsx-ast-utils/hasProp';
|
---|
28 |
|
---|
29 | module.exports = context => ({
|
---|
30 | JSXOpeningElement: node => {
|
---|
31 | const onChange = hasProp(node.attributes, 'onChange');
|
---|
32 |
|
---|
33 | if (onChange) {
|
---|
34 | context.report({
|
---|
35 | node,
|
---|
36 | message: `No onChange!`
|
---|
37 | });
|
---|
38 | }
|
---|
39 | }
|
---|
40 | });
|
---|
41 | ```
|
---|
42 |
|
---|
43 | ## API
|
---|
44 | ### AST Resources
|
---|
45 | 1. [JSX spec](https://github.com/facebook/jsx/blob/master/AST.md)
|
---|
46 | 2. [JS spec](https://github.com/estree/estree/blob/master/spec.md)
|
---|
47 |
|
---|
48 | ### hasProp
|
---|
49 | ```js
|
---|
50 | hasProp(props, prop, options);
|
---|
51 | ```
|
---|
52 | Returns boolean indicating whether an prop exists as an attribute on a JSX element node.
|
---|
53 |
|
---|
54 | #### Props
|
---|
55 | Object - The attributes on the visited node. (Usually `node.attributes`).
|
---|
56 | #### Prop
|
---|
57 | String - A string representation of the prop you want to check for existence.
|
---|
58 | #### Options
|
---|
59 | Object - An object representing options for existence checking
|
---|
60 | 1. `ignoreCase` - automatically set to `true`.
|
---|
61 | 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
|
---|
62 | props, it will assume the prop you are looking for is not in the spread.
|
---|
63 | Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
|
---|
64 |
|
---|
65 | <hr />
|
---|
66 |
|
---|
67 | ### hasAnyProp
|
---|
68 |
|
---|
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 |
|
---|
74 | #### Props
|
---|
75 | Object - The attributes on the visited node. (Usually `node.attributes`).
|
---|
76 | #### Prop
|
---|
77 | Array<String> - An array of strings representing the props you want to check for existence.
|
---|
78 | #### Options
|
---|
79 | Object - An object representing options for existence checking
|
---|
80 | 1. `ignoreCase` - automatically set to `true`.
|
---|
81 | 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
|
---|
82 | props, it will assume the prop you are looking for is not in the spread.
|
---|
83 | Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
|
---|
84 |
|
---|
85 | <hr />
|
---|
86 |
|
---|
87 | ### hasEveryProp
|
---|
88 |
|
---|
89 | ```js
|
---|
90 | hasEveryProp(props, prop, options);
|
---|
91 | ```
|
---|
92 | Returns a boolean indicating if **all** of props in `prop` argument exist on the node.
|
---|
93 |
|
---|
94 | #### Props
|
---|
95 | Object - The attributes on the visited node. (Usually `node.attributes`).
|
---|
96 | #### Prop
|
---|
97 | Array<String> - An array of strings representing the props you want to check for existence.
|
---|
98 | #### Options
|
---|
99 | Object - An object representing options for existence checking
|
---|
100 | 1. `ignoreCase` - automatically set to `true`.
|
---|
101 | 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in
|
---|
102 | props, it will assume the prop you are looking for is not in the spread.
|
---|
103 | Example: `<div {...props} />` looking for specific prop here will return false if `spreadStrict` is `true`.
|
---|
104 |
|
---|
105 | <hr />
|
---|
106 |
|
---|
107 | ### getProp
|
---|
108 |
|
---|
109 | ```js
|
---|
110 | getProp(props, prop, options);
|
---|
111 | ```
|
---|
112 | Returns the JSXAttribute itself or undefined, indicating the prop is not present on the JSXOpeningElement.
|
---|
113 |
|
---|
114 | #### Props
|
---|
115 | Object - The attributes on the visited node. (Usually `node.attributes`).
|
---|
116 | #### Prop
|
---|
117 | String - A string representation of the prop you want to check for existence.
|
---|
118 | #### Options
|
---|
119 | Object - An object representing options for existence checking
|
---|
120 | 1. `ignoreCase` - automatically set to `true`.
|
---|
121 |
|
---|
122 | <hr />
|
---|
123 |
|
---|
124 | ### elementType
|
---|
125 | ```js
|
---|
126 | elementType(node)
|
---|
127 | ```
|
---|
128 | Returns the tagName associated with a JSXElement.
|
---|
129 |
|
---|
130 | #### Node
|
---|
131 | Object - The visited JSXElement node object.
|
---|
132 |
|
---|
133 | <hr />
|
---|
134 |
|
---|
135 | ### getPropValue
|
---|
136 |
|
---|
137 | ```js
|
---|
138 | getPropValue(prop);
|
---|
139 | ```
|
---|
140 | Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
|
---|
141 |
|
---|
142 | This function should return the most *closely* associated value with the intention of the JSX.
|
---|
143 |
|
---|
144 | #### Prop
|
---|
145 | Object - The JSXAttribute collected by AST parser.
|
---|
146 |
|
---|
147 | <hr />
|
---|
148 |
|
---|
149 | ### getLiteralPropValue
|
---|
150 |
|
---|
151 | ```js
|
---|
152 | getLiteralPropValue(prop);
|
---|
153 | ```
|
---|
154 | Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
|
---|
155 |
|
---|
156 | 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.)
|
---|
157 |
|
---|
158 | #### Prop
|
---|
159 | Object - The JSXAttribute collected by AST parser.
|
---|
160 |
|
---|
161 | <hr />
|
---|
162 |
|
---|
163 | ### propName
|
---|
164 |
|
---|
165 | ```js
|
---|
166 | propName(prop);
|
---|
167 | ```
|
---|
168 | Returns the name associated with a JSXAttribute. For example, given `<div foo="bar" />` and the JSXAttribute for `foo`, this will return the string `"foo"`.
|
---|
169 |
|
---|
170 | #### Prop
|
---|
171 | Object - The JSXAttribute collected by AST parser.
|
---|
172 |
|
---|
173 | <hr />
|
---|
174 |
|
---|
175 | ### eventHandlers
|
---|
176 |
|
---|
177 | ```js
|
---|
178 | console.log(eventHandlers);
|
---|
179 | /*
|
---|
180 | [
|
---|
181 | 'onCopy',
|
---|
182 | 'onCut',
|
---|
183 | 'onPaste',
|
---|
184 | 'onCompositionEnd',
|
---|
185 | 'onCompositionStart',
|
---|
186 | 'onCompositionUpdate',
|
---|
187 | 'onKeyDown',
|
---|
188 | 'onKeyPress',
|
---|
189 | 'onKeyUp',
|
---|
190 | 'onFocus',
|
---|
191 | 'onBlur',
|
---|
192 | 'onChange',
|
---|
193 | 'onInput',
|
---|
194 | 'onSubmit',
|
---|
195 | 'onClick',
|
---|
196 | 'onContextMenu',
|
---|
197 | 'onDblClick',
|
---|
198 | 'onDoubleClick',
|
---|
199 | 'onDrag',
|
---|
200 | 'onDragEnd',
|
---|
201 | 'onDragEnter',
|
---|
202 | 'onDragExit',
|
---|
203 | 'onDragLeave',
|
---|
204 | 'onDragOver',
|
---|
205 | 'onDragStart',
|
---|
206 | 'onDrop',
|
---|
207 | 'onMouseDown',
|
---|
208 | 'onMouseEnter',
|
---|
209 | 'onMouseLeave',
|
---|
210 | 'onMouseMove',
|
---|
211 | 'onMouseOut',
|
---|
212 | 'onMouseOver',
|
---|
213 | 'onMouseUp',
|
---|
214 | 'onSelect',
|
---|
215 | 'onTouchCancel',
|
---|
216 | 'onTouchEnd',
|
---|
217 | 'onTouchMove',
|
---|
218 | 'onTouchStart',
|
---|
219 | 'onScroll',
|
---|
220 | 'onWheel',
|
---|
221 | 'onAbort',
|
---|
222 | 'onCanPlay',
|
---|
223 | 'onCanPlayThrough',
|
---|
224 | 'onDurationChange',
|
---|
225 | 'onEmptied',
|
---|
226 | 'onEncrypted',
|
---|
227 | 'onEnded',
|
---|
228 | 'onError',
|
---|
229 | 'onLoadedData',
|
---|
230 | 'onLoadedMetadata',
|
---|
231 | 'onLoadStart',
|
---|
232 | 'onPause',
|
---|
233 | 'onPlay',
|
---|
234 | 'onPlaying',
|
---|
235 | 'onProgress',
|
---|
236 | 'onRateChange',
|
---|
237 | 'onSeeked',
|
---|
238 | 'onSeeking',
|
---|
239 | 'onStalled',
|
---|
240 | 'onSuspend',
|
---|
241 | 'onTimeUpdate',
|
---|
242 | 'onVolumeChange',
|
---|
243 | 'onWaiting',
|
---|
244 | 'onLoad',
|
---|
245 | 'onError',
|
---|
246 | 'onAnimationStart',
|
---|
247 | 'onAnimationEnd',
|
---|
248 | 'onAnimationIteration',
|
---|
249 | 'onTransitionEnd',
|
---|
250 | ]
|
---|
251 | */
|
---|
252 | ```
|
---|
253 |
|
---|
254 | Contains a flat list of common event handler props used in JSX to attach behaviors
|
---|
255 | to DOM events.
|
---|
256 |
|
---|
257 | #### eventHandlersByType
|
---|
258 |
|
---|
259 | The same list as `eventHandlers`, grouped into types.
|
---|
260 |
|
---|
261 | ```js
|
---|
262 | console.log(eventHandlersByType);
|
---|
263 | /*
|
---|
264 | {
|
---|
265 | clipboard: [ 'onCopy', 'onCut', 'onPaste' ],
|
---|
266 | composition: [ 'onCompositionEnd', 'onCompositionStart', 'onCompositionUpdate' ],
|
---|
267 | keyboard: [ 'onKeyDown', 'onKeyPress', 'onKeyUp' ],
|
---|
268 | focus: [ 'onFocus', 'onBlur' ],
|
---|
269 | form: [ 'onChange', 'onInput', 'onSubmit' ],
|
---|
270 | mouse: [ 'onClick', 'onContextMenu', 'onDblClick', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave', 'onDragOver', 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver', 'onMouseUp' ],
|
---|
271 | selection: [ 'onSelect' ],
|
---|
272 | touch: [ 'onTouchCancel', 'onTouchEnd', 'onTouchMove', 'onTouchStart' ],
|
---|
273 | ui: [ 'onScroll' ],
|
---|
274 | wheel: [ 'onWheel' ],
|
---|
275 | media: [ 'onAbort', 'onCanPlay', 'onCanPlayThrough', 'onDurationChange', 'onEmptied', 'onEncrypted', 'onEnded', 'onError', 'onLoadedData', 'onLoadedMetadata', 'onLoadStart', 'onPause', 'onPlay', 'onPlaying', 'onProgress', 'onRateChange', 'onSeeked', 'onSeeking', 'onStalled', 'onSuspend', 'onTimeUpdate', 'onVolumeChange', 'onWaiting' ],
|
---|
276 | image: [ 'onLoad', 'onError' ],
|
---|
277 | animation: [ 'onAnimationStart', 'onAnimationEnd', 'onAnimationIteration' ],
|
---|
278 | transition: [ 'onTransitionEnd' ],
|
---|
279 | }
|
---|
280 | */
|
---|
281 | ```
|
---|
282 |
|
---|
283 |
|
---|
284 | [1]: https://npmjs.org/package/jsx-ast-utils
|
---|
285 | [2]: https://versionbadg.es/jsx-eslint/jsx-ast-utils.svg
|
---|
286 | [5]: https://david-dm.org/jsx-eslint/jsx-ast-utils.svg
|
---|
287 | [6]: https://david-dm.org/jsx-eslint/jsx-ast-utils
|
---|
288 | [7]: https://david-dm.org/jsx-eslint/jsx-ast-utils/dev-status.svg
|
---|
289 | [8]: https://david-dm.org/jsx-eslint/jsx-ast-utils#info=devDependencies
|
---|
290 | [11]: https://nodei.co/npm/jsx-ast-utils.png?downloads=true&stars=true
|
---|
291 | [license-image]: https://img.shields.io/npm/l/jsx-ast-utils.svg
|
---|
292 | [license-url]: LICENSE
|
---|
293 | [downloads-image]: https://img.shields.io/npm/dm/jsx-ast-utils.svg
|
---|
294 | [downloads-url]: https://npm-stat.com/charts.html?package=jsx-ast-utils
|
---|
295 | [codecov-image]: https://codecov.io/gh/jsx-eslint/jsx-ast-utils/branch/main/graphs/badge.svg
|
---|
296 | [codecov-url]: https://app.codecov.io/gh/jsx-eslint/jsx-ast-utils/
|
---|
297 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/jsx-eslint/jsx-ast-utils
|
---|
298 | [actions-url]: https://github.com/jsx-eslint/jsx-ast-utils/actions
|
---|