UNPKG

1.49 kBJavaScriptView Raw
1/**
2 * Requires putting certain operators on the next line rather than on the current line before a line break.
3 *
4 * Types: `Array` or `Boolean`
5 *
6 * Values: Array of operators to apply to or `true`
7 *
8 * #### Example
9 *
10 * ```js
11 * "disallowOperatorBeforeLineBreak": ["+", "."]
12 * ```
13 *
14 * ##### Valid
15 *
16 * ```js
17 * $el.on( 'click', fn )
18 * .appendTo( 'body' );
19 *
20 * var x = 4 + 5
21 * + 12 + 13;
22 * ```
23 *
24 * ##### Invalid
25 *
26 * ```js
27 * $el.on( 'click', fn ).
28 * appendTo( 'body' );
29 *
30 * var x = 4 + 5 +
31 * 12 + 13;
32 * ```
33 */
34
35var assert = require('assert');
36var defaultOperators = require('../utils').binaryOperators.slice().concat(['.']);
37
38module.exports = function() {};
39
40module.exports.prototype = {
41 configure: function(operators) {
42 assert(Array.isArray(operators) || operators === true,
43 this.getOptionName() + ' option requires array or true value');
44
45 if (operators === true) {
46 operators = defaultOperators;
47 }
48 this._operators = operators;
49 },
50
51 getOptionName: function() {
52 return 'disallowOperatorBeforeLineBreak';
53 },
54
55 check: function(file, errors) {
56 file.iterateTokensByTypeAndValue('Punctuator', this._operators, function(token) {
57 errors.assert.sameLine({
58 token: token,
59 nextToken: file.getNextToken(token),
60 message: 'Operator needs to either be on the same line or after a line break.'
61 });
62 });
63 }
64};