UNPKG

1.25 kBJavaScriptView Raw
1/**
2 * Requires 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 * "requireMultiLineTernary": true
10 * ```
11 *
12 * ##### Valid
13 *
14 * ```js
15 * var foo = (a === b)
16 * ? 1
17 * : 2;
18 * ```
19 *
20 * ##### Invalid
21 *
22 * ```js
23 * var foo = (a === b) ? 1 : 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 'requireMultiLineTernary';
41 },
42
43 check: function(file, errors) {
44 file.iterateNodesByType('ConditionalExpression', function(node) {
45
46 errors.assert.differentLine({
47 token: node.test,
48 nextToken: node.consequent,
49 message: 'Missing new line after test'
50 });
51
52 errors.assert.differentLine({
53 token: node.consequent,
54 nextToken: node.alternate,
55 message: 'Missing new line after consequent'
56 });
57
58 });
59 }
60
61};