{"id":13400334,"url":"https://github.com/jsx-eslint/jsx-ast-utils","last_synced_at":"2025-05-14T13:05:59.483Z","repository":{"id":39640916,"uuid":"60607078","full_name":"jsx-eslint/jsx-ast-utils","owner":"jsx-eslint","description":"AST utility module for statically analyzing JSX","archived":false,"fork":false,"pushed_at":"2025-02-20T16:51:12.000Z","size":436,"stargazers_count":162,"open_issues_count":17,"forks_count":32,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-01T02:41:02.734Z","etag":null,"topics":["ast","ast-parser","eslint","jsx","utility"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsx-eslint.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jsx-eslint","ljharb"],"patreon":null,"open_collective":"jsx-eslint","ko_fi":null,"tidelift":"npm/eslint-plugin-jsx-a11y","community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-06-07T11:11:13.000Z","updated_at":"2025-02-20T16:51:16.000Z","dependencies_parsed_at":"2025-03-18T02:07:59.647Z","dependency_job_id":"6767c259-b139-4c9e-b13d-e7eb3a0dc5b2","html_url":"https://github.com/jsx-eslint/jsx-ast-utils","commit_stats":{"total_commits":229,"total_committers":16,"mean_commits":14.3125,"dds":0.6200873362445415,"last_synced_commit":"5943318eaf23764eec3ff397ebb969613d728a95"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsx-eslint%2Fjsx-ast-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsx-eslint%2Fjsx-ast-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsx-eslint%2Fjsx-ast-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsx-eslint%2Fjsx-ast-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsx-eslint","download_url":"https://codeload.github.com/jsx-eslint/jsx-ast-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252646443,"owners_count":21781970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","ast-parser","eslint","jsx","utility"],"created_at":"2024-07-30T19:00:50.780Z","updated_at":"2025-05-14T13:05:59.452Z","avatar_url":"https://github.com/jsx-eslint.png","language":"JavaScript","funding_links":["https://github.com/sponsors/jsx-eslint","https://github.com/sponsors/ljharb","https://opencollective.com/jsx-eslint","https://tidelift.com/funding/github/npm/eslint-plugin-jsx-a11y"],"categories":["Uncategorized","Developer Tools"],"sub_categories":["Uncategorized"],"readme":"# jsx-ast-utils \u003csup\u003e[![Version Badge][npm-version-svg]][package-url]\u003c/sup\u003e\n\n[![github actions][actions-image]][actions-url]\n[![coverage][codecov-image]][codecov-url]\n[![dependency status][deps-svg]][deps-url]\n[![dev dependency status][dev-deps-svg]][dev-deps-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\n[![npm badge][npm-badge-png]][package-url]\n\nAST utility module for statically analyzing JSX.\n\n## Installation\n```sh\n$ npm i jsx-ast-utils --save\n```\n\n## Usage\nThis 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.\n\n### ESLint example\n```js\nimport { hasProp } from 'jsx-ast-utils';\n// OR: var hasProp = require('jsx-ast-utils').hasProp;\n// OR: const hasProp = require('jsx-ast-utils/hasProp');\n// OR: import hasProp from 'jsx-ast-utils/hasProp';\n\nmodule.exports = context =\u003e ({\n  JSXOpeningElement: node =\u003e {\n    const onChange = hasProp(node.attributes, 'onChange');\n\n    if (onChange) {\n      context.report({\n        node,\n        message: `No onChange!`\n      });\n    }\n  }\n});\n```\n\n## API\n### AST Resources\n1. [JSX spec](https://github.com/facebook/jsx/blob/master/AST.md)\n2. [JS spec](https://github.com/estree/estree/blob/master/spec.md)\n\n### hasProp\n```js\nhasProp(props, prop, options);\n```\nReturns boolean indicating whether an prop exists as an attribute on a JSX element node.\n\n#### Props\nObject - The attributes on the visited node. (Usually `node.attributes`).\n#### Prop\nString - A string representation of the prop you want to check for existence.\n#### Options\nObject - An object representing options for existence checking\n  1. `ignoreCase` - automatically set to `true`.\n  2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in\n     props, it will assume the prop you are looking for is not in the spread.\n     Example: `\u003cdiv {...props} /\u003e` looking for specific prop here will return false if `spreadStrict` is `true`.\n\n\u003chr /\u003e\n\n### hasAnyProp\n\n```js\nhasAnyProp(props, prop, options);\n```\nReturns a boolean indicating if **any** of props in `prop` argument exist on the node.\n\n#### Props\nObject - The attributes on the visited node. (Usually `node.attributes`).\n#### Prop\nArray\u003cString\u003e - An array of strings representing the props you want to check for existence.\n#### Options\nObject - An object representing options for existence checking\n  1. `ignoreCase` - automatically set to `true`.\n  2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in\n     props, it will assume the prop you are looking for is not in the spread.\n     Example: `\u003cdiv {...props} /\u003e` looking for specific prop here will return false if `spreadStrict` is `true`.\n\n\u003chr /\u003e\n\n### hasEveryProp\n\n```js\nhasEveryProp(props, prop, options);\n```\nReturns a boolean indicating if **all** of props in `prop` argument exist on the node.\n\n#### Props\nObject - The attributes on the visited node. (Usually `node.attributes`).\n#### Prop\nArray\u003cString\u003e - An array of strings representing the props you want to check for existence.\n#### Options\nObject - An object representing options for existence checking\n 1. `ignoreCase` - automatically set to `true`.\n 2. `spreadStrict` - automatically set to `true`. This means if spread operator exists in\n    props, it will assume the prop you are looking for is not in the spread.\n    Example: `\u003cdiv {...props} /\u003e` looking for specific prop here will return false if `spreadStrict` is `true`.\n\n\u003chr /\u003e\n\n### getProp\n\n```js\ngetProp(props, prop, options);\n```\nReturns the JSXAttribute itself or undefined, indicating the prop is not present on the JSXOpeningElement.\n\n#### Props\nObject - The attributes on the visited node. (Usually `node.attributes`).\n#### Prop\nString - A string representation of the prop you want to check for existence.\n#### Options\nObject - An object representing options for existence checking\n  1. `ignoreCase` - automatically set to `true`.\n\n\u003chr /\u003e\n\n### elementType\n```js\nelementType(node)\n```\nReturns the tagName associated with a JSXElement.\n\n#### Node\nObject - The visited JSXElement node object.\n\n\u003chr /\u003e\n\n### getPropValue\n\n```js\ngetPropValue(prop);\n```\nReturns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.\n\nThis function should return the most *closely* associated value with the intention of the JSX.\n\n#### Prop\nObject - The JSXAttribute collected by AST parser.\n\n\u003chr /\u003e\n\n### getLiteralPropValue\n\n```js\ngetLiteralPropValue(prop);\n```\nReturns the value of a given attribute. Different types of attributes have their associated values in different properties on the object.\n\nThis 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.)\n\n#### Prop\nObject - The JSXAttribute collected by AST parser.\n\n\u003chr /\u003e\n\n### propName\n\n```js\npropName(prop);\n```\nReturns the name associated with a JSXAttribute. For example, given `\u003cdiv foo=\"bar\" /\u003e` and the JSXAttribute for `foo`, this will return the string `\"foo\"`.\n\n#### Prop\nObject - The JSXAttribute collected by AST parser.\n\n\u003chr /\u003e\n\n### eventHandlers\n\n```js\nconsole.log(eventHandlers);\n/*\n[\n  'onCopy',\n  'onCut',\n  'onPaste',\n  'onCompositionEnd',\n  'onCompositionStart',\n  'onCompositionUpdate',\n  'onKeyDown',\n  'onKeyPress',\n  'onKeyUp',\n  'onFocus',\n  'onBlur',\n  'onChange',\n  'onInput',\n  'onSubmit',\n  'onClick',\n  'onContextMenu',\n  'onDblClick',\n  'onDoubleClick',\n  'onDrag',\n  'onDragEnd',\n  'onDragEnter',\n  'onDragExit',\n  'onDragLeave',\n  'onDragOver',\n  'onDragStart',\n  'onDrop',\n  'onMouseDown',\n  'onMouseEnter',\n  'onMouseLeave',\n  'onMouseMove',\n  'onMouseOut',\n  'onMouseOver',\n  'onMouseUp',\n  'onSelect',\n  'onTouchCancel',\n  'onTouchEnd',\n  'onTouchMove',\n  'onTouchStart',\n  'onScroll',\n  'onWheel',\n  'onAbort',\n  'onCanPlay',\n  'onCanPlayThrough',\n  'onDurationChange',\n  'onEmptied',\n  'onEncrypted',\n  'onEnded',\n  'onError',\n  'onLoadedData',\n  'onLoadedMetadata',\n  'onLoadStart',\n  'onPause',\n  'onPlay',\n  'onPlaying',\n  'onProgress',\n  'onRateChange',\n  'onSeeked',\n  'onSeeking',\n  'onStalled',\n  'onSuspend',\n  'onTimeUpdate',\n  'onVolumeChange',\n  'onWaiting',\n  'onLoad',\n  'onError',\n  'onAnimationStart',\n  'onAnimationEnd',\n  'onAnimationIteration',\n  'onTransitionEnd',\n]\n*/\n```\n\nContains a flat list of common event handler props used in JSX to attach behaviors\nto DOM events.\n\n#### eventHandlersByType\n\nThe same list as `eventHandlers`, grouped into types.\n\n```js\nconsole.log(eventHandlersByType);\n/*\n{\n  clipboard: [ 'onCopy', 'onCut', 'onPaste' ],\n  composition: [ 'onCompositionEnd', 'onCompositionStart', 'onCompositionUpdate' ],\n  keyboard: [ 'onKeyDown', 'onKeyPress', 'onKeyUp' ],\n  focus: [ 'onFocus', 'onBlur' ],\n  form: [ 'onChange', 'onInput', 'onSubmit' ],\n  mouse: [ 'onClick', 'onContextMenu', 'onDblClick', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave', 'onDragOver', 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver', 'onMouseUp' ],\n  selection: [ 'onSelect' ],\n  touch: [ 'onTouchCancel', 'onTouchEnd', 'onTouchMove', 'onTouchStart' ],\n  ui: [ 'onScroll' ],\n  wheel: [ 'onWheel' ],\n  media: [ 'onAbort', 'onCanPlay', 'onCanPlayThrough', 'onDurationChange', 'onEmptied', 'onEncrypted', 'onEnded', 'onError', 'onLoadedData', 'onLoadedMetadata', 'onLoadStart', 'onPause', 'onPlay', 'onPlaying', 'onProgress', 'onRateChange', 'onSeeked', 'onSeeking', 'onStalled', 'onSuspend', 'onTimeUpdate', 'onVolumeChange', 'onWaiting' ],\n  image: [ 'onLoad', 'onError' ],\n  animation: [ 'onAnimationStart', 'onAnimationEnd', 'onAnimationIteration' ],\n  transition: [ 'onTransitionEnd' ],\n}\n*/\n```\n\n[package-url]: https://npmjs.org/package/jsx-ast-utils\n[npm-version-svg]: https://versionbadg.es/jsx-eslint/jsx-ast-utils.svg\n[deps-svg]: https://david-dm.org/jsx-eslint/jsx-ast-utils.svg\n[deps-url]: https://david-dm.org/jsx-eslint/jsx-ast-utils\n[dev-deps-svg]: https://david-dm.org/jsx-eslint/jsx-ast-utils/dev-status.svg\n[dev-deps-url]: https://david-dm.org/jsx-eslint/jsx-ast-utils#info=devDependencies\n[npm-badge-png]: https://nodei.co/npm/jsx-ast-utils.png?downloads=true\u0026stars=true\n[license-image]: https://img.shields.io/npm/l/jsx-ast-utils.svg\n[license-url]: LICENSE\n[downloads-image]: https://img.shields.io/npm/dm/jsx-ast-utils.svg\n[downloads-url]: https://npm-stat.com/charts.html?package=jsx-ast-utils\n[codecov-image]: https://codecov.io/gh/jsx-eslint/jsx-ast-utils/branch/main/graphs/badge.svg\n[codecov-url]: https://app.codecov.io/gh/jsx-eslint/jsx-ast-utils/\n[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/jsx-eslint/jsx-ast-utils\n[actions-url]: https://github.com/jsx-eslint/jsx-ast-utils/actions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsx-eslint%2Fjsx-ast-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsx-eslint%2Fjsx-ast-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsx-eslint%2Fjsx-ast-utils/lists"}