esindent

ECMAScript code indenter based on Esprima AST and Rocambole

use esformatter instead, since this project is not maintained anymore
Last updated 12 years ago by millermedeiros .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install esindent 
SYNC missed versions from official npm registry.

esindent

ECMAScript code indenter based on Esprima AST

work in progress


Why?

This project started as an experiment while trying to decouple the indent logic from the other token manipulations on esformatter. It's an attempt at finding a simpler solution to the problem while still maintaining the flexibility.

By building it as a separate tool it is also simpler to reuse it for other projects and will make some of the tests simpler to write (since we can test the indentation separately).

How?

This tool uses a rocambole generated AST to traverse tokens inside each node and add/remove indent based on the node type.

The algorithm is very straightforward, it simple loops through all nodes (starting from the leaf) up to the program root, scanning each line start for WhiteSpace and adding/removing/editing Indent tokens as needed.

Goals

  • Indent based on syntax (not tokens).
  • Options to toggle behavior.
  • Be able to indent any JavaScript program!

API

esindent.transform(ast[, opts]):AST

Transforms AST in-place, adding Indent tokens at the beginning of each line that needs indentation.

var esi = require('esindent');
esi.transform(ast, {
  value: '  ',
  ArrayExpression: 1,
  BlockStatement: 1,
  ChainedMemberExpression: 1,
  MultipleVariableDeclaration: 1,
  ObjectExpression: 1,
  SwitchCase: 1,
  SwitchStatement: 1,
  EmptyStatement: 0
});

// to get the result as a string simply call ast.toString()
console.log( ast.toString() );

License

Released under the MIT License

Current Tags

  • 0.4.3                                ...           latest (12 years ago)

8 Versions

  • 0.4.3 [deprecated]           ...           12 years ago
  • 0.4.2 [deprecated]           ...           12 years ago
  • 0.4.1 [deprecated]           ...           12 years ago
  • 0.4.0 [deprecated]           ...           12 years ago
  • 0.3.1 [deprecated]           ...           12 years ago
  • 0.3.0 [deprecated]           ...           12 years ago
  • 0.2.0 [deprecated]           ...           12 years ago
  • 0.1.0 [deprecated]           ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (4)
Dev Dependencies (4)
  • mocha https://github.com/millermedeiros/mocha/tarball/latest
  • chai ~1.8.1
  • glob ~3.2.7
  • jshint ~2.3.0
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |