helpers for rocambole AST indentation
npm install rocambole-indentHelpers to manipulate rocamboleIndent tokens.
Used mainly by esformatter and its plugins.
``js`
var indent = require('rocambole-indent');
setOptions used to set the indent value.
`jsIndent
setOptions({
// sets "value" used by tokens (defaults to two spaces)alignComments
value: ' ',
// amount of indents added on if comment is inside an empty{}
// block (surrounded by , [] or ()) - defaults to 1`
CommentInsideEmptyBlock: 1
});
Increase/Decrease the indent level in between the startToken and endToken.
It will not include the start and end tokens on the indentation range, only the
tokens in between them.
`js`
// increase the indent level by 1
inBetween(node.startToken, node.endToken, 1);
// decrease the indent level by 1
inBetween(node.startToken, node.endToken, -1);
// zero does nothing
inBetween(node.endToken, 0);
Important: negative values only work if original Indent token containslevel
a property since there is no reliable way to infer this value (probably
will only work if indent was added by this lib).
Increases/decreases the indent level at the beginning of the line that includes
the given token.
`js`
// adds 2 indents
addLevel(node.startToken, 2);
// decrease indent level in 1 step
addLevel(node.endToken, -1);
// zero does nothing
addLevel(node.endToken, 0);
Important: negative values only work if original Indent token containslevel
a property since there is no reliable way to infer this value (probably
will only work if indent was added by this lib).
Removes any Indent tokens that don't have a level property (this isWhiteSpace
usually the original indentation of the program parsed by rocambole) or that
are not at the beginning of the line. Also removing tokens that
are at the beginning of the line to avoid mistakes.
`js`
// sanitize a single node
sanitize(node);
// sanitize whole AST
sanitize(ast);
Updates BlockComment raw value to make sure all the lines have the sameIndent level.
This is called internally by the addLevel and indentInBetween methods (ifBlockComment
first token of line is a ), so as long as you only use those
methods to edit the indent level you shouldn't need to call this.
Align all the comments based on the next/previous lines inside a given ast ornode.
It will align the comments with the next line unless the comment block is
followed by an empty line, in that case it will use the previous non-empty line
as a reference.
Example output:
`js
// aligned with next line
switch (foo) {
// aligned with next non-empty line
case bar:
// aligned with next line
baz();
// this should be aligned with previous line since comment block is
// followed by an empty line
// aligned with next line
case biz:
// aligned with next line
what();
// aligned with next line
}
function noop() {
// indented since it's inside an empty block
}
// aligned with previous line since it's at the end of program
`
Convert WhiteSpace token into Indent if it's the first token of the line.
You can pass a custom indentValue or it will use the value set bysetOptions() to calculate the indent level (basically count how many timestoken.value
this string repeats inside the ).
`js`
var token = {
type: 'WhiteSpace',
value: '\t\t\t',
prev: { type: 'LineBreak', value: '\n' }
};
whiteSpaceToIndent(token, '\t');
// edits properties in place
console.log(token.type); // > "Indent"
console.log(token.level); // > 3
This is useful in case you want to make sure sanitize won't remove the
original indents.
This module uses debug internally. To
make it easier to identify what is wrong we sometimes run the esformatter tests
with a DEBUG flag, like:
`sh``
DEBUG=rocambole:indent npm test
Released under the MIT License