Gathering detailed insights and metrics for jsx-ast-utils
Gathering detailed insights and metrics for jsx-ast-utils
Gathering detailed insights and metrics for jsx-ast-utils
Gathering detailed insights and metrics for jsx-ast-utils
npm install jsx-ast-utils
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
161 Stars
244 Commits
32 Forks
6 Watching
2 Branches
14 Contributors
Updated on 11 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.5%
3,892,269
Compared to previous day
Last week
3.4%
20,894,110
Compared to previous week
Last month
13.4%
85,527,759
Compared to previous month
Last year
15.7%
905,609,373
Compared to previous year
25
[![dependency status][deps-svg]][deps-url] [![dev dependency status][dev-deps-svg]][dev-deps-url]
[![npm badge][npm-badge-png]][package-url]
AST utility module for statically analyzing JSX.
1$ npm i jsx-ast-utils --save
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.
1import { hasProp } from 'jsx-ast-utils'; 2// OR: var hasProp = require('jsx-ast-utils').hasProp; 3// OR: const hasProp = require('jsx-ast-utils/hasProp'); 4// OR: import hasProp from 'jsx-ast-utils/hasProp'; 5 6module.exports = context => ({ 7 JSXOpeningElement: node => { 8 const onChange = hasProp(node.attributes, 'onChange'); 9 10 if (onChange) { 11 context.report({ 12 node, 13 message: `No onChange!` 14 }); 15 } 16 } 17});
1hasProp(props, prop, options);
Returns boolean indicating whether an prop exists as an attribute on a JSX element node.
Object - The attributes on the visited node. (Usually node.attributes
).
String - A string representation of the prop you want to check for existence.
Object - An object representing options for existence checking
ignoreCase
- automatically set to true
.spreadStrict
- automatically set to true
. 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 if spreadStrict
is true
.1hasAnyProp(props, prop, options);
Returns a boolean indicating if any of props in prop
argument exist on the node.
Object - The attributes on the visited node. (Usually node.attributes
).
Array
Object - An object representing options for existence checking
ignoreCase
- automatically set to true
.spreadStrict
- automatically set to true
. 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 if spreadStrict
is true
.1hasEveryProp(props, prop, options);
Returns a boolean indicating if all of props in prop
argument exist on the node.
Object - The attributes on the visited node. (Usually node.attributes
).
Array
Object - An object representing options for existence checking
ignoreCase
- automatically set to true
.spreadStrict
- automatically set to true
. 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 if spreadStrict
is true
.1getProp(props, prop, options);
Returns the JSXAttribute itself or undefined, indicating the prop is not present on the JSXOpeningElement.
Object - The attributes on the visited node. (Usually node.attributes
).
String - A string representation of the prop you want to check for existence.
Object - An object representing options for existence checking
ignoreCase
- automatically set to true
.1elementType(node)
Returns the tagName associated with a JSXElement.
Object - The visited JSXElement node object.
1getPropValue(prop);
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.
Object - The JSXAttribute collected by AST parser.
1getLiteralPropValue(prop);
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.)
Object - The JSXAttribute collected by AST parser.
1propName(prop);
Returns the name associated with a JSXAttribute. For example, given <div foo="bar" />
and the JSXAttribute for foo
, this will return the string "foo"
.
Object - The JSXAttribute collected by AST parser.
1console.log(eventHandlers); 2/* 3[ 4 'onCopy', 5 'onCut', 6 'onPaste', 7 'onCompositionEnd', 8 'onCompositionStart', 9 'onCompositionUpdate', 10 'onKeyDown', 11 'onKeyPress', 12 'onKeyUp', 13 'onFocus', 14 'onBlur', 15 'onChange', 16 'onInput', 17 'onSubmit', 18 'onClick', 19 'onContextMenu', 20 'onDblClick', 21 'onDoubleClick', 22 'onDrag', 23 'onDragEnd', 24 'onDragEnter', 25 'onDragExit', 26 'onDragLeave', 27 'onDragOver', 28 'onDragStart', 29 'onDrop', 30 'onMouseDown', 31 'onMouseEnter', 32 'onMouseLeave', 33 'onMouseMove', 34 'onMouseOut', 35 'onMouseOver', 36 'onMouseUp', 37 'onSelect', 38 'onTouchCancel', 39 'onTouchEnd', 40 'onTouchMove', 41 'onTouchStart', 42 'onScroll', 43 'onWheel', 44 'onAbort', 45 'onCanPlay', 46 'onCanPlayThrough', 47 'onDurationChange', 48 'onEmptied', 49 'onEncrypted', 50 'onEnded', 51 'onError', 52 'onLoadedData', 53 'onLoadedMetadata', 54 'onLoadStart', 55 'onPause', 56 'onPlay', 57 'onPlaying', 58 'onProgress', 59 'onRateChange', 60 'onSeeked', 61 'onSeeking', 62 'onStalled', 63 'onSuspend', 64 'onTimeUpdate', 65 'onVolumeChange', 66 'onWaiting', 67 'onLoad', 68 'onError', 69 'onAnimationStart', 70 'onAnimationEnd', 71 'onAnimationIteration', 72 'onTransitionEnd', 73] 74*/
Contains a flat list of common event handler props used in JSX to attach behaviors to DOM events.
The same list as eventHandlers
, grouped into types.
1console.log(eventHandlersByType); 2/* 3{ 4 clipboard: [ 'onCopy', 'onCut', 'onPaste' ], 5 composition: [ 'onCompositionEnd', 'onCompositionStart', 'onCompositionUpdate' ], 6 keyboard: [ 'onKeyDown', 'onKeyPress', 'onKeyUp' ], 7 focus: [ 'onFocus', 'onBlur' ], 8 form: [ 'onChange', 'onInput', 'onSubmit' ], 9 mouse: [ 'onClick', 'onContextMenu', 'onDblClick', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave', 'onDragOver', 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver', 'onMouseUp' ], 10 selection: [ 'onSelect' ], 11 touch: [ 'onTouchCancel', 'onTouchEnd', 'onTouchMove', 'onTouchStart' ], 12 ui: [ 'onScroll' ], 13 wheel: [ 'onWheel' ], 14 media: [ 'onAbort', 'onCanPlay', 'onCanPlayThrough', 'onDurationChange', 'onEmptied', 'onEncrypted', 'onEnded', 'onError', 'onLoadedData', 'onLoadedMetadata', 'onLoadStart', 'onPause', 'onPlay', 'onPlaying', 'onProgress', 'onRateChange', 'onSeeked', 'onSeeking', 'onStalled', 'onSuspend', 'onTimeUpdate', 'onVolumeChange', 'onWaiting' ], 15 image: [ 'onLoad', 'onError' ], 16 animation: [ 'onAnimationStart', 'onAnimationEnd', 'onAnimationIteration' ], 17 transition: [ 'onTransitionEnd' ], 18} 19*/
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
0 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More