3 Helpers to manipulate [rocambole](https://github.com/millermedeiros/rocambole)
6 Used mainly by [esformatter](https://github.com/millermedeiros/esformatter/) and its plugins.
12 var indent = require('rocambole-indent');
17 `setOptions` used to set the indent value.
21 // sets "value" used by `Indent` tokens (defaults to two spaces)
23 // amount of indents added on `alignComments` if comment is inside an empty
24 // block (surrounded by `{}`, `[]` or `()`) - defaults to `1`
25 CommentInsideEmptyBlock: 1
29 ### inBetween(startToken, endToken, level)
31 Increase/Decrease the indent level in between the `startToken` and `endToken`.
33 It will not include the start and end tokens on the indentation range, only the
34 tokens in between them.
37 // increase the indent level by 1
38 inBetween(node.startToken, node.endToken, 1);
39 // decrease the indent level by 1
40 inBetween(node.startToken, node.endToken, -1);
42 inBetween(node.endToken, 0);
45 **Important:** negative values only work if original `Indent` token contains
46 a `level` property since there is no reliable way to infer this value (probably
47 will only work if indent was added by this lib).
49 ### addLevel(token, level)
51 Increases/decreases the indent level at the beginning of the line that includes
56 addLevel(node.startToken, 2);
57 // decrease indent level in 1 step
58 addLevel(node.endToken, -1);
60 addLevel(node.endToken, 0);
63 **Important:** negative values only work if original `Indent` token contains
64 a `level` property since there is no reliable way to infer this value (probably
65 will only work if indent was added by this lib).
67 ### sanitize(astOrNode)
69 Removes any `Indent` tokens that don't have a `level` property (this is
70 usually the original indentation of the program parsed by rocambole) or that
71 are not at the beginning of the line. Also removing `WhiteSpace` tokens that
72 are at the beginning of the line to avoid mistakes.
75 // sanitize a single node
81 ### updateBlockComment(token)
83 Updates `BlockComment` `raw` value to make sure all the lines have the same
86 This is called internally by the `addLevel` and `indentInBetween` methods (if
87 first token of line is a `BlockComment`), so as long as you only use those
88 methods to edit the indent level you shouldn't need to call this.
90 ### alignComments(astOrNode)
92 Align all the comments based on the next/previous lines inside a given `ast` or
95 It will align the comments with the next line unless the comment block is
96 followed by an empty line, in that case it will use the previous non-empty line
102 // aligned with next line
104 // aligned with next non-empty line
107 // aligned with next line
109 // this should be aligned with previous line since comment block is
110 // followed by an empty line
112 // aligned with next line
114 // aligned with next line
116 // aligned with next line
120 // indented since it's inside an empty block
123 // aligned with previous line since it's at the end of program
126 ### whiteSpaceToIndent(token, [indentValue])
128 Convert `WhiteSpace` token into `Indent` if it's the first token of the line.
130 You can pass a custom `indentValue` or it will use the value set by
131 `setOptions()` to calculate the indent `level` (basically count how many times
132 this string repeats inside the `token.value`).
138 prev: { type: 'LineBreak', value: '\n' }
140 whiteSpaceToIndent(token, '\t');
141 // edits properties in place
142 console.log(token.type); // > "Indent"
143 console.log(token.level); // > 3
146 This is useful in case you want to make sure `sanitize` won't remove the
151 This module uses [debug](https://www.npmjs.com/package/debug) internally. To
152 make it easier to identify what is wrong we sometimes run the esformatter tests
153 with a `DEBUG` flag, like:
156 DEBUG=rocambole:indent npm test
161 Released under the MIT License