Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 1.3 KB

README.md

File metadata and controls

63 lines (44 loc) · 1.3 KB

js-codemods

This repository contains a collection of codemod scripts for use with JSCodeshift.

Setup & Run

npm install -g jscodeshift
git clone https://github.com/yangshun/js-codemods.git
jscodeshift -t path/to/codemod-script.js <file>

Use the -d option for a dry-run and use -p to print the output for comparison.

Included Scripts

jsx-conditional-rendering-operator

Transforms JSX inline If-Else with conditional operator rendering into inline if with logical && operator rendering.

jscodeshift -t js-codemods/transforms/jsx-conditional-rendering-operator.js <file>

Before:

<div>
  {this.state.showAlert ? <Alert /> : null}
  {this.state.hideAlert ? null : <Alert />}
</div>

After:

<div>
  {this.state.showAlert && <Alert />}
  {!this.state.hideAlert && <Alert />}
</div>

jsxattribute-expression-to-literal

Transforms JSX attribute values which are string literals wrapped within JSXExpressionContainers to just the string literal itself.

jscodeshift -t js-codemods/transforms/jsxattribute-expression-to-literal.js <file>

Before:

<Foo header={"Lorem Ipsum"} message={'dolor sit amet'} />

After:

<Foo header="Lorem Ipsum" message="dolor sit amet" />