1 | /**
|
2 | * Requires newline before line comments
|
3 | *
|
4 | * Types: `Boolean` or `Object`
|
5 | *
|
6 | * Values:
|
7 | * - `true`: always require a newline before line comments
|
8 | * - `Object`:
|
9 | * - `"allExcept"`: `"firstAfterCurly"` Comments may be first line of block without extra padding
|
10 | *
|
11 | * #### Examples
|
12 | * ```js
|
13 | * "requirePaddingNewLinesBeforeLineComments": true
|
14 | * "requirePaddingNewLinesBeforeLineComments": { "allExcept": "firstAfterCurly" }
|
15 | * ```
|
16 | *
|
17 | * ##### Valid for `true`
|
18 | *
|
19 | * ```js
|
20 | * var a = 2;
|
21 | * var b = 3; // comment
|
22 | *
|
23 | * // comment
|
24 | * return a;
|
25 | *
|
26 | * function() {
|
27 | *
|
28 | * // comment
|
29 | * }
|
30 | * ```
|
31 | *
|
32 | * ##### Valid for `{ "allExcept": "firstAfterCurly" }`
|
33 | *
|
34 | * ```js
|
35 | * var a = 2;
|
36 | *
|
37 | * // comment
|
38 | * return a;
|
39 | *
|
40 | * function() {
|
41 | * // comment
|
42 | * }
|
43 | * ```
|
44 | *
|
45 | * ##### Invalid
|
46 | *
|
47 | * ```js
|
48 | * var a = 2;
|
49 | * //comment
|
50 | * return a;
|
51 | *
|
52 | * function() {
|
53 | * // comment
|
54 | * }
|
55 | * ```
|
56 | */
|
57 |
|
58 | var assert = require('assert');
|
59 |
|
60 | module.exports = function() {};
|
61 |
|
62 | module.exports.prototype = {
|
63 |
|
64 | configure: function(value) {
|
65 | this._allowFirstAfterCurly = false;
|
66 |
|
67 | if (typeof value === 'object') {
|
68 | assert(typeof value.allExcept === 'string' && value.allExcept === 'firstAfterCurly',
|
69 | this.getOptionName() + ' option requires the "allExcept" ' +
|
70 | 'property to equal "firstAfterCurly"');
|
71 | this._allowFirstAfterCurly = true;
|
72 | } else {
|
73 | assert(value === true,
|
74 | this.getOptionName() + ' option requires true value or object'
|
75 | );
|
76 | }
|
77 | },
|
78 |
|
79 | getOptionName: function() {
|
80 | return 'requirePaddingNewLinesBeforeLineComments';
|
81 | },
|
82 |
|
83 | check: function(file, errors) {
|
84 | var allowFirstAfterCurly = this._allowFirstAfterCurly;
|
85 |
|
86 | file.iterateTokensByType('CommentLine', function(comment) {
|
87 | if (comment.getLoc().start.line === 1) {
|
88 | return;
|
89 | }
|
90 |
|
91 | var firstToken = file.getFirstTokenOnLineWith(comment);
|
92 |
|
93 | // Should not consider code and comment on the same line (#1194)
|
94 | if (firstToken !== null && firstToken.type !== 'EOF') {
|
95 | return;
|
96 | }
|
97 |
|
98 | var prevToken = file.getPrevToken(comment, {includeComments: true});
|
99 |
|
100 | if (!prevToken || prevToken.type === 'CommentLine') {
|
101 | return;
|
102 | }
|
103 |
|
104 | if (allowFirstAfterCurly && prevToken.type === 'Punctuator' && prevToken.value === '{') {
|
105 | return;
|
106 | }
|
107 |
|
108 | errors.assert.linesBetween({
|
109 | token: prevToken,
|
110 | nextToken: comment,
|
111 | atLeast: 2,
|
112 | message: 'Line comments must be preceded with a blank line'
|
113 | });
|
114 | });
|
115 | }
|
116 | };
|