Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

esformatter

millermedeiros116.4kMIT0.11.3

ECMAScript code beautifier/formatter

babel, beautifier, beautify, ecmascript, esprima, format, formatter, javascript, jscs, source, style, syntax

readme

esformatter

Build Status

ECMAScript code beautifier/formatter.

Important

This tool is still missing support for many important features. Please report any bugs you find, the code is only as good as the test cases. Feature requests are very welcome.

We are looking for contributors!!

Why?

jsbeautifier.org doesn't have enough options and not all IDEs/Editors have a good JavaScript code formatter. I would like to have a command line tool (and standalone lib) at least as powerful/flexible as the WebStorm and FDT code formatters so that it can be plugged into any editor and reused by other tools like escodegen.

For more reasoning behind it and history of the project see: esformatter & rocambole

How?

This tool uses rocambole and babylon to recursively parse the tokens and transform it in place.

Goals

  • granular control about white spaces, indent and line breaks.
  • command line interface (cli).
  • be non-destructive.
  • support for local/global config file so settings can be shared between team members.
  • be extensive (plugins and other cli tools).
  • support most popular style guides through plugins (Google, jQuery, Idiomatic.js).
  • be the best JavaScript code formatter!

API

var esformatter = require('esformatter');
var fs = require('fs');
var codeStr = fs.readFileSync('path/to/js/file.js').toString();

// for a list of available options check "lib/preset/default.js"
var options = {
  indent : {
    value : '  '
  },
  lineBreak : {
    before : {
      // at least one line break before BlockStatement
      BlockStatement : '>=1',
      // only one line break before DoWhileStatementOpeningBrace
      DoWhileStatementOpeningBrace : 1,
      // ...
    }
  },
  whiteSpace : {
    // ...
  }
};

// return a string with the formatted code
var formattedCode = esformatter.format(codeStr, options);

See the doc/api.md file for a list of all the public methods and detailed documentation about each one.

See doc/config.md for more info about the configuration options.

CLI

You can also use the simple command line interface to process the stdin or read from a file.

npm install [-g] esformatter

Usage:

Pass the --help flag to see the available options or see doc/cli.txt.

esformatter --help

Examples:

# Format
# ======

# format "test.js" and output result to stdout
esformatter test.js
# you can also pipe other shell commands (read file from stdin)
cat test.js | esformatter
# format "test.js" using options in "options.json" and output result to stdout
esformatter --config options.json test.js
# process "test.js" and writes to "test.out.js"
esformatter test.js > test.out.js
# you can override the default settings, see lib/preset/default.js for
# a list of available options
esformatter test.js --indent.value="\t" --lineBreak.before.IfStatementOpeningBrace=0
# format "test.js" and output result to "test.js"
esformatter -i test.js
# format and overwrite all the ".js" files inside the "lib/" folder
esformatter -i 'lib/*.js'
# format and overwrite all the ".js" files inside "lib/" and it's subfolders
esformatter -i 'lib/**/*.js'

# **important:** surround the glob with single quotes to avoid expansion; [glob
# syntax reference](https://github.com/isaacs/node-glob/#glob-primer)

# Diff
# ======

# check if "test.js" matches style and output diff to stdout
esformatter --diff test.js
# check if "test.js" matches style and output unified diff to stdout
esformatter --diff-unified test.js
# check if "test.js" matches "options.json" style and output diff to stdout
esformatter --diff --config options.json test.js
# check all files inside "lib/" and it's subfolders
esformatter --diff 'lib/**/*.js'

Local version

If a locally installed esformatter is found, the CLI uses that instead of the global executable (this means you can have multiple projects depending on different versions of esformatter).

protip: add esformatter and all the plugins that you need on your project to the package.json devDependencies that way you can use locally installed plugins and also make sure everyone on your team is using the same version/settings.

{
  "devDependencies": {
    "esformatter": "~0.6.0",
    "esformatter-quotes": "^1.0.1"
  },
  "esformatter": {
    "plugins": ["esformatter-quotes"],
    "quotes": {
      "type": "single"
    }
  }
}

IDE / Editor integration

Since esformatter is available as a command-line tool, it can be used in any editor that supports external shell commands.

Configuration

See doc/config.md.

Presets

Presets are reusable config files that can require other presets/plugins and override configs.

{
  // presets are used as "base settings"
  "extends": [
    "preset:foobar", // load "esformatter-preset-foobar" from "./node_modules"
    "./lorem_ipsum.json" // load relative config file
  ],

  // you can still override any setting from the preset if needed
  "indent": {
    "value": "  "
  }
}

For more info see presets.md

Pipe other CLI tools

Since we don't expect everyone to write plugins that only works with esformatter we decided to encourage the usage of standalone CLI tools.

{
  // pipe is a simple way to "pipe" multiple binaries input/output
  "pipe": {
    // scripts listed as "before" will be executed before esformatter
    // and will forward output to next command in the queue
    "before": [
      "strip-debug",
      "./bin/my-custom-script.sh --foo true -zx"
    ],
    // scripts listed as "after" will be executed after esformatter
    "after": [
      "baz --keepLineBreaks"
    ]
  }
}

Plugins

Plugins are automatically loaded from node_modules if you pass the module id in the config file:

{
  "plugins": [ "esformatter-sample-plugin", "foobar" ]
}

List of plugins and plugins wish list: https://github.com/millermedeiros/esformatter/wiki/Plugins

List of plugins with easy filterable search: http://pgilad.github.io/esformatter-plugins/

For detailed information about plugins structure and API see doc/plugins.md

IRC

We have an IRC channel #esformatter on irc.freenode.net for quick discussions about the project development/structure.

Wiki

See project Wiki for more info: https://github.com/millermedeiros/esformatter/wiki

Project structure / Contributing

See CONTRIBUTING.md

License

Released under the MIT license

changelog

esformatter changelog

v0.11.2 (2019-12-06)

  • Updated dependencies
  • Fixes vulnerability issues with transitive dependencies

v0.11.1 (2019-09-30)

  • no longer publishes test folder to npm

v0.11.0 (2019-09-30)

  • BREAKING: requires node >= 8
  • Updated dependencies

v0.10.0 (2016-10-30)

  • add "extends" support, to make it easier to share presets. see #328
  • add --ignore support to cli. closes #329
  • add MethodDefinition{Closing,Opening}Brace support. closes #417
  • add configs for ModuleSpecifier{Opening,Closing}Brace and ModuleSpecifierComma. closes #402
  • add support for ClassKeyword and ClassExtendsKeyword settings. closes
  • add support for MethodName setting. closes #368
  • convert default presets to JS instead of JSON. closes #457
  • fix ParameterComma + flow. closes #458
  • fix expressionParentheses edge case related to ArrowFunctionExpression.
  • limit amount of spaces between async function. closes #404

v0.9.6 (2016-07-13)

  • remove line break between VariableName and equal sign
  • handle object expression method properties. fixes #456
  • fix IfStatement line break if consequent is not a BlockStatement #446

v0.9.5 (2016-06-05)

  • skip ReturnStatement indentation if argument is an UnaryExpression. fixes #443
  • handle ArrayPattern with null elements. fixes #444
  • use esformatter-parser. closes #440

v0.9.4 (2016-05-25)

  • add CalleeOpeningParentheses and CalleeClosingParentheses support. closes #434
  • add UpdateExpressionOperator support. see #435
  • don't add line break around Property if inside ObjectPattern. fixes #441
  • fix indent and line break on NewExpression with comments. closes #439
  • fix line break before "else if". closes #426. closes #438
  • handle BlockStatement as SwitchCase.consequent; closes #432
  • handle standalone BlockStatement. closes #430
  • improve UnaryExpressionOperator behavior. closes #435
  • safe guard against no startToken on expressionParentheses. fixes #427

v0.9.3 (2016-03-27)

  • correct ClassOpeningBrace spelling #411
  • Preserve indent for Literals on a newline on the right side of a LogicalExpression #416 and #421

v0.9.2 (2016-02-27)

  • fix export not followed by semicolon. fixes #408
  • add stack trace to a few error logs to make it easier to debug.

v0.9.1 (2016-02-13)

  • safeguard for userHome. fixes #362
  • re-enable VariableDeclarationWithoutInit. fixes #382
  • fix indent edges of arrow function expression. fixes #399
  • fix do while without semicolon indentation. fixes #401
  • Fix incorrect removal of space around async functions

v0.9.0 (2016-01-29)

  • use babylon as the default parser to allow experimental features support.

v0.8.2 (2016-01-28)

  • avoid indenting body of arrow functions twice. fixes #357
  • add test for arrow function indentation. closes #393
  • avoid double indent for nodes during assignment. fixes #380
  • change default value for MemberExpression line breaks. fixes #378
  • add test for returning class expression. closes #358
  • add option to set space after MethodDefinitionName. closes #384
  • fix ReturnStatement indentation. fixes #377
  • add support for ClassExpression. fixes #359
  • fix SwitchCaseColon when not immediately followed by another case. closes #392
  • update npm-run to v2.0, fixes unit tests on node v4.2; closes #376
  • fix ENOENT error tests. see #376. closes #383.

v0.8.1 (2015-10-28)

  • ops, forgot to lint.

v0.8.0 (2015-10-28)

  • pass esformatter instance to plugins during setOptions. closes #348
  • remove semver dep (not needed anymore)
  • fix line breaks around ParameterComma not enforced for FunctionExpression #373
  • fixes generator functions
  • fixes delete p['t']
  • add support for "for...of" statements
  • add support for experimental object spread operator. closes #353
  • avoid applying FunctionReservedWord whitespace to class method definition
  • migrate to espree 2.x
  • added option for SwitchCaseColon for normalizing spacing around switch cases

v0.7.3 (2015-08-03)

  • limit the amount of white spaces and line breaks on MemberExpression. fixes #343
  • add support for indent.SingleVariableDeclaration. closes #339
  • limit the amount of line breaks and white spaces after VariableValue and around VariableDeclarationSemiColon. closes #334
  • fix ClassDeclaration extends expressions. closes #337

v0.7.2 (2015-07-17)

  • Fix the CLI --diff-unified flag
  • make sure we load the global config if findAndMergeConfigs returns an empty object. fixes #336
  • add ModuleSpecifierOpeningBrace and ModuleSpecifierClosingBrace settings

v0.7.1 (2015-06-26)

  • limit line break and white space after new keyword. fixes #321
  • use user-home dependency to locate the global config file. fixes #323

v0.7.0 (2015-05-13)

  • handle all the ES6 import/export formats. closes #311
  • add support for ArrayPattern (array destructuring). closes #302
  • better support for ObjectPattern (object detructuring). closes #301
  • fix error when constructor call don't use parentheses. fixes #318
  • handle ES6 Object literal Property value shorthand properly
  • add lineBreak[before|after].CommentGroup support. closes #310
  • add support for computed property names in ObjectExpression and change default line break rules for PropertyName and PropertyValue. fixes #287
  • use espree to parse the programs by default (more ES6 features and JSX support) and allow user to replace parser if needed
  • better defaults for ObjectExpressionOpeningBrace and ObjectExpressionClosingBrace
  • rename indent.AlignComments to indent.alignComments for consistency
  • explain that the config property names are derived from the esprima AST node types. closes #307
  • show a descriptive error message when we can't find a plugin. fixes #309
  • add --diff flags and replace optimist with minimist. closes #292
  • fix VariableDeclaration.init indentation when value is surounded by parenthesis. see #306
  • indent UnaryExpression by default if inside AssignmentExpression, CallExpression, ObjectExpression and VariableDeclaration. fixes #306
  • better error messages and avoids terminating the process on first error. fixes #303
  • add glob expansion support. closes #291

v0.6.1 (2015-03-27)

  • fix comment alignment if surrounded by whitespaces. closes #297
  • improve ObjectExpression hook logic to handle values surrounded by parenthesis. fixes #295
  • fix setOptions behavior to allow user to override plugin default settings. fixes #293

v0.6.0 (2015-03-26)

  • add support for the pipe option on the config file, allowing user to pipe other CLI tools. closes #168
  • add support for ES6 classes. closes #286
  • add support for ES6 default params. closes #285
  • add support for ES6 arrow functions. closes #255
  • change comment indentation behavior to match next/prev code block based on context. fixes #209. fixes #270.
  • extract helpers from lib/indent.js into rocambole-indent. closes #277
  • fixes SwitchCase indentation range. closes #290
  • ignore shebang by default. closes #284
  • add setting to limit line breaks and white spaces around BreakKeyword. fixes #213
  • limit spaces before function name. closes #283
  • make sure plugins.setOptions is called before plugins.stringBefore. fixes #288
  • replace lib/lineBreak.js with rocambole-linebreak. closes #279
  • replace lib/whiteSpace.js with rocambole-whitespace. closes #278
  • support in place edits. fixes #275
  • use locally installed esformatter version if available. closes #190

v0.5.1 (2015-03-17)

  • make it work with esprima@2.1 AST!
  • ConditionalExpression: indent even if consequent is on the same line as the test
  • Fix indent for logical expression as argument of return statement
  • Fix indent for multi-var declaration with logical expression
  • ForStatement improvements
  • IfStatement: Fix multi-line test with comments
  • ObjectExpression: Indent multi-line binary and logical expression values
  • Update jQuery preset to fix single-line object expression spacing
  • Update jQuery preset to make use of new IIFE settings
  • fix DoWhileStatement indent. closes #256
  • improve CallExpression hook to handle a few edge cases and add option to limit br/ws around opening/closing parentheses. closes #267
  • improve getter/setter behavior on ObjectExpression. fixes #265
  • indent NewExpression arguments. fixes #254
  • make sure we don't loop over same CatchClause twice. fixes #264

v0.5.0 (2015-02-26)

  • add support for br/ws around IIFEOpeningParentheses and IIFEClosingParentheses and keep spaces around CallExpression to avoid conflicts. closes #223. closes #250.
  • don't remove line breaks before FunctionExpression by default and handle an edge case for array indentation if it is a chained member expression argument. fixes #202
  • drop ArgumentList exceptions (used previously by jQuery preset)
  • fix FunctionExpression: Handle two special cases for TopLevelFunctionBlock
  • fix SwitchCase indent if missing semicolon after break. fixes #225
  • fix array indentation for cases where closing bracket is on the same line. simplify ObjectExpression indentation logic while also fixing how it works inside arrays. fixes #224. fixes #239.
  • fix indent edges for chained MemberExpression. fixes #240
  • improve IfStatement.test indentation. closes #222
  • indent BinaryExpression & LogicalExpression if inside VariableDeclaration, CallExpression or AssignmentExpression. fixes #212
  • indent support for ParameterList and enable it by default. closes #231
  • make sure plugins are loaded/registered before plugins.stringBefore is called and expose unregisterAll. fixes #245. closes #246.
  • only indent if there is a line break before/between arguments. fixes #238
  • support getters/setters in ObjectExpression
  • update jQuery preset to use CatchParameterList whiteSpace setting
  • update jQuery preset to tolerate single line objects
  • update rocambole to v0.5 and change the BinaryExpression test to match esprima@2.0 behavior (no EmptyStatement between ExpressionStatements). see #192

v0.4.3 (2014-10-13)

  • fix error related to multiple plugin.register calls. (#218)

v0.4.2 (2014-09-08)

  • fix else if consequent with no braces. closes #196
  • fix indent of nested if braces before comments. closes #197
  • fix CatchClause indent and add settings for FinallyKeyword and TryKeyword. closes #203
  • indent LogicalExpression if it's the VariableDeclaration.init.

v0.4.1 (2014-07-09)

  • fix comma and white spaces around object property values (#191).
  • fix ObjectExpression indent edges (#193).

v0.4.0 (2014-07-06)

  • add transformBefore hook for plugins and transformAfter to keep API consistent. closes #187
  • add option to add/remove trailing empty lines in the file (EOF). closes #111.
  • add options to control line breaks inside ArrayExpressions. closes #129
  • add support for multiple indent levels (closes #179, closes #106).
  • add double indent for IfStatementConditional on the jQuery preset (closes #150)
  • add support for line breaks before/after PropertyName and PropertyValue (closes #184)
  • support plugins option on the CLI. closes #182
  • fix MemberExpression indent behavior. (meta bug #181)
  • fix indent end edge for FunctionExpression inside MemberExpression. closes #152
  • change indent rules for AssignmentExpression and ConditionalExpression to match jQuery preset needs. closes #149
  • convert comma-first objects to comma-last (closes #175)
  • improve CatchClause and TryStatement line break and white space handling. closes #128
  • improve ReturnStatement and CallExpression indent behavior.
  • rename ChainedMemberExpression to MemberExpression on the config file.

v0.3.2 (2014-06-23)

  • fix issue with sparse arrays. see millermedeiros/rocambole#15
  • fix Params comma handling and update rocambole to 0.3.4 to fix BlockComment. closes #139
  • fix indent for objects inside arrays. closes #142
  • fix white space inside expression statement with parens. closes #155
  • change IfStatement indent edges to avoid indenting comments that are just before } else. closes #123
  • fix comments inside ObjectExpression. closes #166

v0.3.1 (2014-06-23)

  • avoid merging undefined config on esformatter.rc.
  • make sure esformatter.rc doesn't load config file if user provides a 'preset' or if 'root == true' to match CLI behavior.

v0.3.0 (2014-06-20)

  • expose ALL the things!! exposed a few methods related to line break, white space and indentation; also flattened the directory structure to make it easier for plugin authors to reuse esformatter internal methods when needed.

v0.2.0 (2014-06-16)

  • add plugin support.
  • refactored the way indentation is handled (and changed default settings related to indentation).
  • expose the rc method.
  • fix rc merge/search logic to avoid problems on windows.
  • fix void 0
  • proper indent edges for AssignmentExpression
  • indent CallExpression by default
  • make sure _br.limit doesn't remove LineBreak if previous token is a comment (fixes a few bugs).
  • fix comments inside empty catch block (avoid removing line breaks).

v0.1.1 (2014-05-12)

  • fix error when input is an empty file.
  • fix typeof
  • add spaces inside ForStatement and WhileStatement parenthesis on jQuery preset.

v0.1.0 (2014-04-15)

  • major refactor on the code structure and major changes to the default tool behavior.
  • changed some rules so the tool is less opinionated.
  • change formatter logic to support ranges on the configuration file.
  • avoid removing line breaks during the formatting process, increasing the flexibility of the formatting rules.

notes about v0.0.1 (2012-12-06) till v0.0.16 (2014-02-24)

The formatter had stricter rules and was way less flexible before v0.1.0;

Lots of small improvements between each version. Behavior was still in flux and each version was breaking backwards compatibility.

We considered v0.0.15 (2013-12-18) to be "stable" for most common cases, most of bugs found in the following months after release was on edge-cases. We decided to make a big refactor to increase the formatter flexibility and to be less aggressive on the changes.