Giter Club home page Giter Club logo

codemod-imports-sort's Introduction

Build Status

CodeMod to sort ES6 imports by type

Use this codemod to sort ES6 imports by type in this order:

  • internal Node.js modules before
  • external module imports before
  • local imports from parent folders before
  • local imports from sibling folders.

Imports of the same type are sorted alphabetically.

Install

yarn global add codemod-imports-sort

Use

codemod-imports-sort path/to/file.js

Example

Before:

import './index.css';
import Beta from 'Beta';
import fs from 'fs';
import bar from '../bar';
import './';
import baz from './baz';
import Alpha from 'alpha';
import foo from '../../foo';
import App from './App';

After:

import fs from 'fs';
import Alpha from 'alpha';
import Beta from 'Beta';
import foo from '../../foo';
import bar from '../bar';
import './';
import App from './App';
import baz from './baz';
import './index.css';

Options

--sortConfig FILE.json

Optionally you can pass the path to a JSON file with a config to define the desired order of imports. The config should resemble the config for the import/order plugin of eslint. groups must be an array of string or [string]. The only allowed strings are: "builtin", "external", "scoped-external", "internal", "parent", "sibling", "index".

For example to define to sort index imports first, then internal and external modules in a alphabetically sorted group and then sibling, parent and builtin modules together in a group, use this configuration:

{
  "groups": [
    "index",
    ["internal", "external"],
    ["sibling", "parent", "builtin"]
  ]
}

Omitted types are implicitly grouped together as the last element

Built with jscodeshift.

codemod-imports-sort's People

Contributors

bfncs avatar namefilip avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

codemod-imports-sort's Issues

Error: Cannot find module '../utils/requirePackage'

โžœ  project git:(sort-imports) codemod-imports-sort --version
module.js:471
    throw err;
    ^

Error: Cannot find module '../utils/requirePackage'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.callback (/Users/fspiridonov/.nvm/versions/node/v6.10.0/lib/node_modules/codemod-imports-sort/bin/codemod-imports-sort:82:32)
    at Object.ArgParser.setOption (/Users/fspiridonov/.nvm/versions/node/v6.10.0/lib/node_modules/codemod-imports-sort/node_modules/nomnom/nomnom.js:447:26)
    at /Users/fspiridonov/.nvm/versions/node/v6.10.0/lib/node_modules/codemod-imports-sort/node_modules/nomnom/nomnom.js:274:14
    at Array.reduce (native)
    at Object.parse (/Users/fspiridonov/.nvm/versions/node/v6.10.0/lib/node_modules/codemod-imports-sort/node_modules/nomnom/nomnom.js:223:10)
    at Object.<anonymous> (/Users/fspiridonov/.nvm/versions/node/v6.10.0/lib/node_modules/codemod-imports-sort/bin/codemod-imports-sort:93:4)

In here: https://github.com/bfncs/codemod-imports-sort/blob/master/bin/codemod-imports-sort#L82

Should it be

const requirePackage = require('jscodeshift/utils/requirePackage');

?

Errors on running

Hi,
First, when I installed it I had this error on running

ERROR Transform file /home/julien/Sites/theRepo/dist/index.js does not exist

I created the missing file, and when rerun ;

ERR app/components/Nav/components/LeftMenuList/LeftMenuList.js Transformation error
TypeError: transform is not a function
All done. 
Results: 
1 errors
0 unmodified
0 skipped
0 ok

Could you help me please on this issue ?

Thank you !

Moves comment line

Nice tool! One issue: I use flow, so there's a /* @flow */ comment in the 1st line of source files.

Before:

/* @flow */
import calcDropTargetClass from "../common/calcDropTargetClass";
import classNames from "classnames";
import dropSourceInTarget from "../StandardShow/dropSourceInTarget";
import { DropTarget } from "react-dnd";
import React from "react";
import SearchTypes from "../../constants/SearchTypes";

After:

import classNames from "classnames";
import React from "react";
import { DropTarget } from "react-dnd";
import SearchTypes from "../../constants/SearchTypes";
/* @flow */
import calcDropTargetClass from "../common/calcDropTargetClass";
import dropSourceInTarget from "../StandardShow/dropSourceInTarget";

Looks like it's keeping comments above their following import, but moving the comment messes things up. Maybe special-case and keep /* flow */ at top?

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.