UNPKG

1.24 kBJavaScriptView Raw
1/**
2 * Disallows the test, consequent and alternate to be on separate lines when using the ternary operator.
3 *
4 * Types: `Boolean`
5 *
6 * #### Example
7 *
8 * ```js
9 * "disallowMultiLineTernary": true
10 * ```
11 *
12 * ##### Valid
13 *
14 * ```js
15 * var foo = (a === b) ? 1 : 2;
16 * ```
17 *
18 * ##### Invalid
19 *
20 * ```js
21 * var foo = (a === b)
22 * ? 1
23 * : 2;
24 * ```
25 */
26
27var assert = require('assert');
28
29module.exports = function() {};
30
31module.exports.prototype = {
32 configure: function(options) {
33 assert(
34 options === true,
35 this.getOptionName() + ' option requires a true value or should be removed'
36 );
37 },
38
39 getOptionName: function() {
40 return 'disallowMultiLineTernary';
41 },
42
43 check: function(file, errors) {
44 file.iterateNodesByType('ConditionalExpression', function(node) {
45
46 errors.assert.sameLine({
47 token: node.test,
48 nextToken: node.consequent,
49 message: 'Illegal new line after test'
50 });
51
52 errors.assert.sameLine({
53 token: node.consequent,
54 nextToken: node.alternate,
55 message: 'Illegal new line after consequent'
56 });
57 });
58 }
59
60};