All Projects → jsx-eslint → Jsx Ast Utils

jsx-eslint / Jsx Ast Utils

Licence: mit
AST utility module for statically analyzing JSX

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jsx Ast Utils

Nativejsx
JSX to native DOM API transpilation. 💛 <div> ⟹ document.createElement('div')!
Stars: ✭ 145 (+62.92%)
Mutual labels:  ast, jsx
ts-transform-react-jsx-source
TypeScript AST Transformer that adds source file and line number to JSX elements
Stars: ✭ 12 (-86.52%)
Mutual labels:  jsx, ast
Hyperapp One
Hyperapp One is a Parcel boilerplate for quickstarting a web application with Hyperapp (V1), JSX, and Prettier.
Stars: ✭ 129 (+44.94%)
Mutual labels:  eslint, jsx
Redux React Starter
DEPRECATED use the new https://github.com/didierfranc/react-webpack-4
Stars: ✭ 137 (+53.93%)
Mutual labels:  eslint, jsx
lowcode
React Lowcode - prototype, develop and maintain internal apps easier
Stars: ✭ 32 (-64.04%)
Mutual labels:  jsx, ast
Prettier
Prettier is an opinionated code formatter.
Stars: ✭ 41,411 (+46429.21%)
Mutual labels:  ast, jsx
Eslint Plugin Jsx A11y
Static AST checker for a11y rules on JSX elements.
Stars: ✭ 2,609 (+2831.46%)
Mutual labels:  eslint, jsx
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (+2.25%)
Mutual labels:  ast, jsx
react-enterprise-starter-kit
Highly Scalable Awesome React Starter Kit for an enterprise application with a very easy maintainable codebase. 🔥
Stars: ✭ 55 (-38.2%)
Mutual labels:  eslint, jsx
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.
Stars: ✭ 19 (-78.65%)
Mutual labels:  eslint, jsx
eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (-83.15%)
Mutual labels:  eslint, jsx
Sowing Machine
🌱A React UI toolchain & JSX alternative
Stars: ✭ 64 (-28.09%)
Mutual labels:  eslint, jsx
Meriyah
A 100% compliant, self-hosted javascript parser - https://meriyah.github.io/meriyah
Stars: ✭ 690 (+675.28%)
Mutual labels:  ast, jsx
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (-26.97%)
Mutual labels:  eslint, ast
Sapper Typescript Graphql Template
A template that includes Sapper for Svelte, TypeScript preprocessing, and a GraphQL server through TypeGraphQL
Stars: ✭ 84 (-5.62%)
Mutual labels:  eslint
Eslint Plugin Github
An opinionated collection of ESLint rules used by GitHub.
Stars: ✭ 86 (-3.37%)
Mutual labels:  eslint
Gdash
A functional utility library for GML/GameMaker: Studio - partials, map/reduce and more
Stars: ✭ 83 (-6.74%)
Mutual labels:  utility
Tkframework
react + relay + redux + saga + graphql + webpack
Stars: ✭ 83 (-6.74%)
Mutual labels:  jsx
Snoopwpf
Snoop - The WPF Spy Utility
Stars: ✭ 1,286 (+1344.94%)
Mutual labels:  utility
Uaiso
A multi-language parsing infrastructure with an unified AST
Stars: ✭ 86 (-3.37%)
Mutual labels:  ast

build status npm version license Coverage Status Total npm downloads

jsx-ast-utils

AST utility module for statically analyzing JSX.

Installation

$ npm i jsx-ast-utils --save

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

import { hasProp } from 'jsx-ast-utils';
// OR: var hasProp = require('jsx-ast-utils').hasProp;
// OR: const hasProp = require('jsx-ast-utils/hasProp');
// OR: import hasProp from 'jsx-ast-utils/hasProp';

module.exports = context => ({
  JSXOpeningElement: node => {
    const onChange = hasProp(node.attributes, 'onChange');

    if (onChange) {
      context.report({
        node,
        message: `No onChange!`
      });
    }
  }
});

API

AST Resources

  1. JSX spec
  2. JS spec

hasProp

hasProp(props, prop, options);

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

  1. ignoreCase - automatically set to true.
  2. 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.

hasAnyProp

hasAnyProp(props, prop, options);

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

  1. ignoreCase - automatically set to true.
  2. 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.

hasEveryProp

hasEveryProp(props, prop, options);

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

  1. ignoreCase - automatically set to true.
  2. 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.

getProp

getProp(props, prop, options);

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

  1. ignoreCase - automatically set to true.

elementType

elementType(node)

Returns the tagName associated with a JSXElement.

Node

Object - The visited JSXElement node object.


getPropValue

getPropValue(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.

Prop

Object - The JSXAttribute collected by AST parser.


getLiteralPropValue

getLiteralPropValue(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.)

Prop

Object - The JSXAttribute collected by AST parser.


propName

propName(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".

Prop

Object - The JSXAttribute collected by AST parser.


eventHandlers

console.log(eventHandlers);
/*
[
  'onCopy',
  'onCut',
  'onPaste',
  'onCompositionEnd',
  'onCompositionStart',
  'onCompositionUpdate',
  'onKeyDown',
  'onKeyPress',
  'onKeyUp',
  'onFocus',
  'onBlur',
  'onChange',
  'onInput',
  'onSubmit',
  'onClick',
  'onContextMenu',
  'onDblClick',
  'onDoubleClick',
  'onDrag',
  'onDragEnd',
  'onDragEnter',
  'onDragExit',
  'onDragLeave',
  'onDragOver',
  'onDragStart',
  'onDrop',
  'onMouseDown',
  'onMouseEnter',
  'onMouseLeave',
  'onMouseMove',
  'onMouseOut',
  'onMouseOver',
  'onMouseUp',
  'onSelect',
  'onTouchCancel',
  'onTouchEnd',
  'onTouchMove',
  'onTouchStart',
  'onScroll',
  'onWheel',
  'onAbort',
  'onCanPlay',
  'onCanPlayThrough',
  'onDurationChange',
  'onEmptied',
  'onEncrypted',
  'onEnded',
  'onError',
  'onLoadedData',
  'onLoadedMetadata',
  'onLoadStart',
  'onPause',
  'onPlay',
  'onPlaying',
  'onProgress',
  'onRateChange',
  'onSeeked',
  'onSeeking',
  'onStalled',
  'onSuspend',
  'onTimeUpdate',
  'onVolumeChange',
  'onWaiting',
  'onLoad',
  'onError',
  'onAnimationStart',
  'onAnimationEnd',
  'onAnimationIteration',
  'onTransitionEnd',
]
*/

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.

console.log(eventHandlersByType);
/*
{
  clipboard: [ 'onCopy', 'onCut', 'onPaste' ],
  composition: [ 'onCompositionEnd', 'onCompositionStart', 'onCompositionUpdate' ],
  keyboard: [ 'onKeyDown', 'onKeyPress', 'onKeyUp' ],
  focus: [ 'onFocus', 'onBlur' ],
  form: [ 'onChange', 'onInput', 'onSubmit' ],
  mouse: [ 'onClick', 'onContextMenu', 'onDblClick', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave', 'onDragOver', 'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver', 'onMouseUp' ],
  selection: [ 'onSelect' ],
  touch: [ 'onTouchCancel', 'onTouchEnd', 'onTouchMove', 'onTouchStart' ],
  ui: [ 'onScroll' ],
  wheel: [ 'onWheel' ],
  media: [ 'onAbort', 'onCanPlay', 'onCanPlayThrough', 'onDurationChange', 'onEmptied', 'onEncrypted', 'onEnded', 'onError', 'onLoadedData', 'onLoadedMetadata', 'onLoadStart', 'onPause', 'onPlay', 'onPlaying', 'onProgress', 'onRateChange', 'onSeeked', 'onSeeking', 'onStalled', 'onSuspend', 'onTimeUpdate', 'onVolumeChange', 'onWaiting' ],
  image: [ 'onLoad', 'onError' ],
  animation: [ 'onAnimationStart', 'onAnimationEnd', 'onAnimationIteration' ],
  transition: [ 'onTransitionEnd' ],
}
*/
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].