jsx-ast-utils
jsx-ast-utils
AST utility module for statically analyzing JSX.
Installation
Usage
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, however I thought it could be useful to be extracted and maintained separately so you could write new interesting rules to statically analyze JSX.
ESLint example
API
AST Resources
hasProp
Returns boolean indicating whether an prop exists as an attribute on a JSX element node.
Props
Object - The attributes on the visited node. (Usually node.attributes
).
Prop
String - A string representation of the prop you want to check for existence.
Options
Object - An object representing options for existence checking
ignoreCase
- automatically set totrue
.spreadStrict
- automatically set totrue
. This means if spread operator exists in props, it will assume the prop you are looking for is not in the spread. Example:<div {...props} />
looking for specific prop here will return false ifspreadStrict
istrue
.
hasAnyProp
Returns a boolean indicating if any of props in prop
argument exist on the node.
Props
Object - The attributes on the visited node. (Usually node.attributes
).
Prop
Array - An array of strings representing the props you want to check for existence.
Options
Object - An object representing options for existence checking
ignoreCase
- automatically set totrue
.spreadStrict
- automatically set totrue
. This means if spread operator exists in props, it will assume the prop you are looking for is not in the spread. Example:<div {...props} />
looking for specific prop here will return false ifspreadStrict
istrue
.
hasEveryProp
Returns a boolean indicating if all of props in prop
argument exist on the node.
Props
Object - The attributes on the visited node. (Usually node.attributes
).
Prop
Array - An array of strings representing the props you want to check for existence.
Options
Object - An object representing options for existence checking
ignoreCase
- automatically set totrue
.spreadStrict
- automatically set totrue
. This means if spread operator exists in props, it will assume the prop you are looking for is not in the spread. Example:<div {...props} />
looking for specific prop here will return false ifspreadStrict
istrue
.
getProp
Returns the JSXAttribute itself or undefined, indicating the prop is not present on the JSXOpeningElement.
Props
Object - The attributes on the visited node. (Usually node.attributes
).
Prop
String - A string representation of the prop you want to check for existence.
Options
Object - An object representing options for existence checking
ignoreCase
- automatically set totrue
.
elementType
Returns the tagName associated with a JSXElement.
Node
Object - The visited JSXElement node object.
getPropValue
Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
This function should return the most closely associated value with the intention of the JSX.
Prop
Object - The JSXAttribute collected by AST parser.
getLiteralPropValue
Returns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.
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.)
Prop
Object - The JSXAttribute collected by AST parser.
propName
Returns the name associated with a JSXAttribute. For example, given <div foo="bar" />
and the JSXAttribute for foo
, this will return the string "foo"
.
Prop
Object - The JSXAttribute collected by AST parser.
eventHandlers
Contains a flat list of common event handler props used in JSX to attach behaviors to DOM events.
eventHandlersByType
The same list as eventHandlers
, grouped into types.
Last updated