9428bbc3d564ab2970a80287bd6e2443734ed1b0
[platform/framework/web/crosswalk-tizen.git] /
1 # rocambole-linebreak
2
3 Helpers to manipulate [rocambole](https://github.com/millermedeiros/rocambole)
4 `LineBreak` tokens.
5
6 Used mainly by [esformatter](https://github.com/millermedeiros/esformatter/) and its plugins.
7
8 ## Allowed values
9
10  - positive integer (`1` till `99`): "add or keep `[n]` line breaks".
11  - `-1`: keep original line breaks.
12  - `">2"`: add linebreaks until it's over `2`.
13  - `">=1"`: add line breaks until it's equal or greater than `1`.
14  - `"<2"`: remove linebreaks until it's smaller than `2`.
15  - `"<=1"`: remove/add line breaks until it's smaller or equal to `1`.
16
17 ## API
18
19 ```js
20 var br = require('rocambole-linebreak');
21 ```
22
23 ### setOptions(opts)
24
25 `setOptions` is just a way to store some constants so later on the
26 `limit`/`limitBefore`/`limitAfter` you can reference the values by Id.
27
28 ```js
29 setOptions({
30   // sets "value" used by `LineBreak` tokens (defaults to `"\n"`)
31   value: '\n',
32
33   // values inside "before" are used by `limitBefore`
34   before: {
35     // setting to `0` will remove all line breaks before the token
36     parenthesis: 0
37   },
38
39   // values inside "after" are used by `limitAfter`
40   after: {
41     // setting to `1` will add/keep a single `LineBreak` after the token
42     parenthesis: 1
43   }
44 });
45 ```
46
47 **Important:** calling this method will override **all** the options.
48
49 ### limitBefore(token, typeOrValue)
50
51 limits the amount of `LineBreak` before a given token.
52
53 ```js
54 // remove all line breaks before `node.startToken`
55 limitBefore(node.startToken, 0);
56 // add/keep 2 line breaks before `node.startToken`
57 limitBefore(node.startToken, 2);
58 // add/keep more than 1 line break
59 limitBefore(node.startToken, '>1');
60 // keep 2 line breaks or more
61 limitBefore(node.startToken, '>=2');
62 // keep less than 3 line breaks
63 limitBefore(node.startToken, '<3');
64 // will use value stored on `setOptions` for `before.parenthesis`
65 limitBefore(node.startToken, 'parenthesis');
66 // values smaller than zero are ignored (this won't change anything)
67 limitBefore(node.startToken, -1);
68 ```
69
70 ### limitAfter(token, typeOrValue)
71
72 limits the amount of `LineBreak` after a given token.
73
74 ```js
75 // remove all line breaks after `node.startToken`
76 limitAfter(node.startToken, 0);
77 // add/keep 1 line break after `node.startToken`
78 limitAfter(node.startToken, 1);
79 // add/keep more than 1 line break
80 limitAfter(node.startToken, '>1');
81 // keep 2 line breaks or more
82 limitAfter(node.startToken, '>=2');
83 // keep less than 3 line breaks
84 limitAfter(node.startToken, '<3');
85 // will use value stored on `setOptions` for `after.parenthesis`
86 limitAfter(node.startToken, 'parenthesis');
87 // values smaller than zero are ignored (this won't change anything)
88 limitAfter(node.startToken, -1);
89 ```
90
91 ### limit(token, typeOrValue)
92
93 limits the amount of `LineBreak` around a given token.
94
95 ```js
96 // add/keep 1 line break before and after `node.startToken`
97 limit(node.startToken, 1);
98
99 // it's just an alias to
100 limitBefore(node.startToken, 1);
101 limitAfter(node.startToken, 1);
102 ```
103
104 ### limitBeforeEndOfFile(ast[, typeOrValue])
105
106 limits the amount of line breaks at the end of the AST.
107
108 ```js
109 // at least one line break at the end of the file
110 limitBeforeEndOfFile(ast, 1);
111 // if you don't pass the `typeOrValue` it will use "EndOfFile" as the type
112 limitBeforeEndOfFile(ast);
113 ```
114
115 ### expectedBefore(type)
116
117 reads value stored during `setOptions` for a given `type`, or returns `-1` if
118 not found.
119
120 ```js
121 assert( expectedBefore('parenthesis') === 0 );
122 ```
123
124 ### expectedAfter(type)
125
126 reads value stored during `setOptions` for a given `type`, or returns `-1` if
127 not found.
128
129 ```js
130 assert( expectedAfter('parenthesis') === 1 );
131 ```
132
133 ## Debug
134
135 This module uses [debug](https://www.npmjs.com/package/debug) internally. To
136 make it easier to identify what is wrong we sometimes run the esformatter tests
137 with a `DEBUG` flag, like:
138
139 ```sh
140 DEBUG=rocambole:br:* npm test
141 ```
142
143 ## License
144
145 Released under the MIT License
146