rocambole-indent
Helpers to manipulate rocambole
Indent
tokens.
Used mainly by esformatter and its plugins.
API
var indent = require('rocambole-indent');
setOptions(opts)
setOptions
used to set the indent value.
setOptions({
value: ' ',
CommentInsideEmptyBlock: 1
});
inBetween(startToken, endToken, level)
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.
inBetween(node.startToken, node.endToken, 1);
inBetween(node.startToken, node.endToken, -1);
inBetween(node.endToken, 0);
Important: negative values only work if original Indent
token contains
a level
property since there is no reliable way to infer this value (probably
will only work if indent was added by this lib).
addLevel(token, level)
Increases/decreases the indent level at the beginning of the line that includes
the given token
.
addLevel(node.startToken, 2);
addLevel(node.endToken, -1);
addLevel(node.endToken, 0);
Important: negative values only work if original Indent
token contains
a level
property since there is no reliable way to infer this value (probably
will only work if indent was added by this lib).
sanitize(astOrNode)
Removes any Indent
tokens that don't have a level
property (this is
usually the original indentation of the program parsed by rocambole) or that
are not at the beginning of the line. Also removing WhiteSpace
tokens that
are at the beginning of the line to avoid mistakes.
sanitize(node);
sanitize(ast);
Updates BlockComment
raw
value to make sure all the lines have the same
Indent
level.
This is called internally by the addLevel
and indentInBetween
methods (if
first token of line is a BlockComment
), 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
or
node
.
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:
switch (foo) {
case bar:
baz();
case biz:
what();
}
function noop() {
}
whiteSpaceToIndent(token, [indentValue])
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 by
setOptions()
to calculate the indent level
(basically count how many times
this string repeats inside the token.value
).
var token = {
type: 'WhiteSpace',
value: '\t\t\t',
prev: { type: 'LineBreak', value: '\n' }
};
whiteSpaceToIndent(token, '\t');
console.log(token.type);
console.log(token.level);
This is useful in case you want to make sure sanitize
won't remove the
original indents.
Debug
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:
DEBUG=rocambole:indent npm test
License
Released under the MIT License