This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-trailing-whitespace.js
137 lines (126 loc) · 3.76 KB
/
disallow-trailing-whitespace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Requires all lines to end on a non-whitespace character
*
* Types: `Boolean` or `String`
*
* Values:
* - `true`
* - `"ignoreEmptyLines"`: (default: `false`) allow whitespace on empty lines
*
* JSHint: [`trailing`](http://jshint.com/docs/options/#trailing)
*
* #### Example
*
* ```js
* "disallowTrailingWhitespace": true
* ```
*
* ##### Valid
*
* ```js
* var foo = "blah blah";
* ```
*
* ##### Invalid
*
* ```js
* var foo = "blah blah"; //<-- whitespace character here
* ```
*
* ##### Valid for `true`
*
* ```js
* foo = 'bar';
*
* foo = 'baz';
* ```
*
* ##### Invalid for `true` but Valid for `ignoreEmptyLines`
*
* ```js
* foo = 'bar';
* \t
* foo = 'baz';
* ```
*/
var assert = require('assert');
var Token = require('cst').Token;
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true || options === 'ignoreEmptyLines',
this.getOptionName() + ' option requires a true value or "ignoreEmptyLines"'
);
this._ignoreEmptyLines = options === 'ignoreEmptyLines';
},
getOptionName: function() {
return 'disallowTrailingWhitespace';
},
check: function(file, errors) {
var program = file.getProgram();
if (!program) {
return;
}
var lastToken = program.getLastToken();
if (lastToken && lastToken.type === 'EOF') {
lastToken = lastToken.getPreviousToken();
}
program.selectTokensByType('Whitespace').forEach(function(whitespace) {
whitespace.getValueLineInfo().some(function(line, i) {
if (this._ignoreEmptyLines && i > 0) {
return true;
}
if (line.text && (line.lineBreak || whitespace === lastToken)) {
errors.cast({
message: 'Illegal trailing whitespace',
element: whitespace,
offset: line.offset,
additional: {
lineNumber: i
}
});
}
}, this);
}, this);
program.selectTokensByType('CommentBlock').concat(program.selectTokensByType('CommentLine'))
.forEach(function(comment) {
var lines = comment.getValueLineInfo();
lines.forEach(function(line, i) {
if (i > 0 && this._ignoreEmptyLines && line.text.trim() === '') {
return;
}
if (comment.type === 'CommentBlock' && i === lines.length - 1) {
return;
}
if (line.text.match(/\s$/)) {
errors.cast({
message: 'Illegal trailing comment',
element: comment,
offset: line.offset,
additional: {
lineNumber: i
}
});
}
}, this);
}, this);
},
_fix: function(file, error) {
var element = error.element;
var newValue;
var lines = element.getValueLineInfo();
var line = lines[error.additional.lineNumber];
if (element.isWhitespace) {
line.text = '';
}
if (element.isComment) {
line.text = line.text.replace(/\s+$/, '');
}
newValue = lines.map(function(line) {
return line.text + (line.lineBreak || '');
}).join('');
var newElement = new Token(element.type, newValue);
element.parentElement.replaceChild(newElement, element);
}
};