Coder Social home page Coder Social logo

esformatter-collapse-objects's Introduction

esformatter-collapse-objects

esformatter plugin for conditionally collapsing object and array literals.

An Automattic fork of wbinnssmith's original

Usage

install it:

npm install esformatter-collapse-objects-a8c

and something like this to your esformatter config file:

{
  "plugins": [
    "esformatter-collapse-objects"
  ],
  "collapseObjects": {
    "ObjectExpression": {
      "maxLineLength": 80,
      "maxKeys": 3,
      "forbidden": [
        "FunctionExpression"
      ]
    },
    "ArrayExpression": {
      "maxLineLength": 80,
      "maxKeys": 5,
      "forbidden": [
        "FunctionExpression"
      ]
    }
  }
}

Important

In order to collapse Array literals, you need to have esformatter expand them in the first place. Add the following to your esformatter config when collapsing Arrays:

"lineBreak": {
  "before": {
    "ArrayExpressionClosing": 1
  },
  "after": {
    "ArrayExpressionOpening": 1,
    "ArrayExpressionComma": 1
  }
},

Options

Options map esprima AST Node types (in this case both ObjectExpression and ArrayExpression) to their respective options, just like indentation in esformatter.

You can also avoid collapsing literals under certain conditions like a maximum number of keys, or when they contain other nodes like FunctionExpression.

[function foo() { return 'bar' }]

for example, could never occur since FunctionExpression is forbidden when trying to collapse a literal if this is set.

The following is the default configuration for the plugin:

{
  ObjectExpression: {
    maxLineLength: 80,
    maxKeys: 3,
    forbidden: [
      'FunctionExpression'
    ]
  },
  ArrayExpression: {
    maxLineLength: 80,
    maxKeys: 3,
    forbidden: [
      'FunctionExpression'
    ]
  }
}

JavaScript API

Register the plugin and call esformatter like so:

// register plugin
esformatter.register(require('esformatter-collapse-objects'));
// pass options as second argument
var output = esformatter.format(str, {
  "collapseObjects": {
    "ObjectExpression": {
      maxLineLength: 80,
      maxKeys: 3,
      forbidden: [
        'FunctionExpression'
      ]
    },
    "ArrayExpression": {
      maxLineLength: 80,
      maxKeys: 5,
      forbidden: [
        'FunctionExpression'
      ]
    }
  }
});

License

Released under the MIT License.

Credits

Huge thanks to Jörn Zaefferer, who published an MIT-licensed gist which serves as the foundation for this module.

esformatter-collapse-objects's People

Contributors

blowery avatar pgilad avatar wbinnssmith avatar

Stargazers

 avatar

Watchers

 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.