-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathfinal-newline.js
65 lines (58 loc) · 1.84 KB
/
final-newline.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
'use strict';
var helpers = require('../helpers');
/**
* Get the 'last' node of the tree to test for an EOL
*
* @param {Object} node - The node whose last child we want to return
* @returns {Object} The last node
*/
var getLastNode = function (node) {
var last = node.last();
return last ? getLastNode(last) : node;
};
module.exports = {
'name': 'final-newline',
'defaults': {
'include': true
},
'detect': function (ast, parser) {
var result = [],
last,
error = {
'ruleId': parser.rule.name,
'severity': parser.severity
};
// If the syntax is Sass we must recursively loop to determine the last node.
// This is not required for SCSS which will always use the last node in the
// content of the parent stylesheet node
if (ast.syntax === 'sass') {
last = getLastNode(ast);
}
else {
last = ast.content[ast.content.length - 1];
}
if (!last.is('space') && !last.is('declarationDelimiter')) {
if (parser.options.include) {
error.line = last.end.line;
error.column = last.end.column;
error.message = 'Files must end with a new line';
result = helpers.addUnique(result, error);
}
}
else if ((last.is('space') || last.is('declarationDelimiter'))) {
if (!helpers.hasEOL(last.content) && parser.options.include) {
error.line = last.start.line;
error.column = last.start.column;
error.message = 'Files must end with a new line';
result = helpers.addUnique(result, error);
}
else if (helpers.hasEOL(last.content) && !parser.options.include) {
error.line = last.start.line;
error.column = last.start.column;
error.message = 'Files must not end with a new line';
result = helpers.addUnique(result, error);
}
}
return result;
}
};