Giter Site home page Giter Site logo

babel-plugin-react-add-property's Introduction

Babel Plugin React Add Property

Overview

This is a small Babel plugin for adding data attributes to React JSX elements. I created this plugin to add meaningful identifiers for styled components. This allows our components to have a designated attribute for end-to-end integration tests (without having to add them manually).

Before

<div class="Page-jLerck lhFHrB">
  <div class="Header-dJBcYZ dqmObD">...</div>
  <div class="Body-MnRsT gzvZiS">...</div>
</div>

After

<div class="Page-jLerck lhFHrB" data-test="Page">
  <div class="Header-dJBcYZ dqmObD" data-test="Header">
    ...
  </div>
  <div class="Body-MnRsT gzvZiS" data-test="Body">
    ...
  </div>
</div>

Installation

This package is available on npm as babel-plugin-react-add-property, and you can find it here.

To install the latest stable version with Yarn:

$ yarn add --dev babel-plugin-react-add-property

...or with npm:

$ npm install babel-plugin-react-add-property

Usage

Via .babelrc (Recommended)

DEFAULT CONFIG

If you don't provide a property name, the attribute name will default to data-test.

NOTE: As these attributes are intended only for testing purposes, we're telling Babel to only use the plugin in our development environment. If you'd like to use this plugin in other environments, you'll need to specify them as well.

// .babelrc

{
  "env": {
    "development": {
      "plugins": ["react-add-property"]
    }
  }
}

CUSTOM CONFIG

However, if you'd like to have a custom attribute name, you can pass it in with your .bablerc.

// .babelrc

{
  "env": {
    "development": {
      "plugins": [["react-add-property", { "property": "data-qa" }]]
    }
  }
}

This custom config would transform this div:

<div class="Header-dJBcYZ dqmObD">...</div>

to look like this:

<div class="Header-dJBcYZ dqmObD" data-qa="Header">
  ...
</div>

Via CLI

babel --plugins react-add-property script.js

Via Node API

without options:

require('babel-core').transform('code', {
  plugins: ['react-add-property'],
});

with options:

require('babel-core').transform('code', {
  plugins: [['react-add-property', { property: 'data-qa' }]],
});

Contributing

I am thankful for any contributions made by the community. By contributing you agree to abide by the Code of Conduct in the Contributing Guidelines.

License

MIT

babel-plugin-react-add-property's People

Contributors

alanbsmith avatar tinynumbers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

babel-plugin-react-add-property's Issues

TypeError: foo.js: Property value expected type of string but got null

xxx/node_modules/.registry.npmjs.org/babel-types/6.26.0/node_modules/babel-types/lib/definitions/index.js:161
      throw new TypeError("Property " + key + " expected type of " + type + " but got " + getType(val));
            ^
TypeError: foo.js: Property value expected type of string but got null
    at Object.validate (xxx/node_modules/.registry.npmjs.org/babel-types/6.26.0/node_modules/babel-types/lib/definitions/index.js:161:13)
    at validate (xxx/node_modules/.registry.npmjs.org/babel-types/6.26.0/node_modules/babel-types/lib/index.js:505:9)
    at Object.builder (xxx/node_modules/.registry.npmjs.org/babel-types/6.26.0/node_modules/babel-types/lib/index.js:466:7)
    at JSXElement (xxx/node_modules/.registry.npmjs.org/babel-plugin-react-add-property/0.1.2/node_modules/babel-plugin-react-add-property/lib/index.js:24:99)
    at NodePath._call (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/index.js:114:17)
    at NodePath.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:115:19)
    at TraversalContext.visitQueue (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitMultiple (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:103:17)
    at TraversalContext.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:190:19)
    at Function.traverse.node (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/index.js:114:17)
    at NodePath.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:115:19)
    at TraversalContext.visitQueue (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitSingle (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:108:19)
    at TraversalContext.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/context.js:192:19)
    at Function.traverse.node (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/index.js:114:17)
    at NodePath.visit (xxx/node_modules/.registry.npmjs.org/babel-traverse/6.26.0/node_modules/babel-traverse/lib/path/context.js:115:19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.