npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anshckr/fix-js

v1.6.5

Published

Fix JS code

Downloads

33

Readme

Build Status code style: prettier GitHub license GitHub stars

This repository contains a collection of codemod scripts for use with JSCodeshift + it can also be added as a package to use the other transformers it exposes

Setup & Run When Using 'jscodeshift'

npm install -g jscodeshift
git clone https://github.com/anshckr/fix-js.git
jscodeshift -t <codemod-script> <file>

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

Recast Options

Options to recast's printer can be provided through the printOptions command line argument

jscodeshift -t transform.js <file> --printOptions='{"quote":"double"}'

Included Scripts

no-lonely-if

Fixes eslint no-lonely-if rule

jscodeshift -t ./transforms/no-lonely-if.js <file>
else {
  if (someCondition) {
    ...
  } else {
    ...
  }
}

The above will get converted to

else if (someCondition) {
  ...
} else {
  ...
}

no-nested-ternary

Fixes eslint no-nested-ternary rule by converting the nested ConditionalExpressions into an IIFE block with If/Else Statements

jscodeshift -t ./transforms/no-nested-ternary.js <file>
a ? 'a' : b ? 'b' : 'c'

The above will get converted to

(function() {
  if (a) {
    return 'a';
  }

  if (b) {
    return 'b';
  }

  return 'c';
})()

no-unused-vars

Fixes eslint no-unused-vars rule. Adds disable comment/block wherever the function/variable getting fixed is globally exposed

jscodeshift -t ./transforms/no-unused-vars.js <file>
function someFunc(index) {}
var someUsedVar = 0, someUnUsedVar = false;
var someVar = (function(){
  var someInternalVar;

  function someInternalFunc() {};

  someUsedVar = 1;
  return someUsedVar;
})();

The above will get converted to

// eslint-disable-next-line no-unused-vars
function someFunc() {}
var someUsedVar = 0;
window.someUnUsedVar = false;

window.someVar = (function(){
  someUsedVar = 1;
  return someUsedVar;
})();
Options:

--skip-disable-comments=true: Doesn't add eslint disable comment/block to globals exposed

react-action-as

Transforms all named export actions use 'as' while importing. Also converts the 'bindActionCreators' objectExpressionNode to use the as imported action

jscodeshift -t ./transforms/react-action-as.js <file>
import { someAction } from '../actions';

const mapDispatchToProps = (dispatch) =>
  bindActionCreators(
    {
      someAction
    },
    dispatch
  );

The above will get converted to

import { someAction as someActionAction } from '../actions';

const mapDispatchToProps = (dispatch) =>
  bindActionCreators(
    {
      someAction: someActionAction
    },
    dispatch
  );

react-destruct-assign

Transformer to fix react/destructuring-assignment rule

jscodeshift -t ./transforms/react-destruct-assign.js <file>
render() {
    if (this.props.someProp) {
        return this.state.someState;
    }
}

The above will get converted to

render() {
    const { someProp } = this.props;
    const { someState } = this.state;
    if (someProp) {
        return someState;
    }
}

block-scoped-var

Transformer that moves all the variable declarators to their scope level

jscodeshift -t ./transforms/block-scoped-var.js <file>
function someFunc() {
  if (someCondition) {
    var i = 1;
  } else {
    var i = 2;
  }

  for (var j = 0; j < i; j++) {
    ...
  }

  for (var k in someObj) {
    ...
  }
}

The above will get converted to

function someFunc() {
  var i, j, k;
  if (someCondition) {
    i = 1;
  } else {
    i = 2;
  }

  for (j = 0; j < i; j++) {
    ...
  }

  for (k in someObj) {
    ...
  }
}

no-camelcase

Transformer to fix all the non camel cased variables and function names in a JS file

jscodeshift -t ./transforms/no-camelcase.js <file>
Options:

--fix-exposed-functions=true: Fixes non camel-cased functions that are exposed from the file

--fix-dependencies=true: Finds all the dependencies needed by the file and fixes them if they are not camel-cased

var _some_var, $some_var;
function some_func() {}
some_func();

The above will get converted to (with no options passed)

var someVar, $someVar;
function some_func() {}
some_func();

Setup & Run When Using As A Package

$ npm i @anshckr/fix-js

In Node.js:


var {
  fixJSsAtPath,
  transformLeakingGlobalsVars,
  transformUnusedAssignedVars,
  transformNoUnderscoreDangle
} = require('@anshckr/fix-js');

API

1. fixJSsAtPath (Transforms all the JS files at the dirPath)

| Parameter | Type | Description | | :--- | :--- | :--- | | dirPath | String | Required. The directory path where you want to run the transform at | | transformer | Function | Required. The transformer which will modify the JS files | | paramsIgnoreFilesRegex | Regex | Optional. Regular expression to match file names to ignore during transform. Default: /$^/ | | paramsIgnoreFoldersRegex | Regex | Optional. Regular expression to match folder names to ignore during transform. Default: /$^/ | | paramsIgnoreableExternalDeps | Array | Optional. Array of dependencies to ignore during transform. Default: [] |

2. transformLeakingGlobalsVars (Transformer to fix all the leaking globals from a JS file)

| Parameter | Type | Description | | :--- | :--- | :--- | | filePath | String | Required. The file path you want to fix | | dependencies | Function | Optional. Array of dependencies you want to fix for the file at filePath. Default: All the global dependencies for the file | | updateInplace | Boolean | Optional. Whether to update the file or not. Default: false |

Returns

| Type | Description | | :--- | :--- | | String | Transformed file content |

Example

for (i = 0; i < 10; i++) {....}

In the above code if we don't declare i in the upper scope like var i then i becomes a global leak

The utility will declare these types leaking variables

3. transformUnusedAssignedVars (Transformer to fix all the unused assigned variables from a JS file)

| Parameter | Type | Description | | :--- | :--- | :--- | | filePath | String | Required. The file path you want to fix | | updateInplace | Boolean | Optional. Whether to update the file or not. Default: false |

Returns

| Type | Description | | :--- | :--- | | String | Transformed file content |

4. transformNoUnderscoreDangle (Transformer to fix leading '__' in function names to "_", removes "_" from function params)

| Parameter | Type | Description | | :--- | :--- | :--- | | filePath | String | Required. The file path you want to fix | | updateInplace | Boolean | Optional. Whether to update the file or not. Default: false | | collectedGlobals | Object | Optional. Contains two keys globalsExposed, dependencies for the file. Default: {} |

Returns

| Type | Description | | :--- | :--- | | String | Transformed file content |

Example

function __someFunc(_someParam) {
  ..._someParam
}
__someFunc();

The above will get converted to

function _someFunc(someParam) {
  ...someParam
}
_someFunc();

Usage

Refer the example folder

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Give a ⭐️ if this project helped you!

License

@anshckr/fix-js is freely distributable under the terms of the MIT license