UNPKG

3.64 kBJavaScriptView Raw
1'use strict';
2
3const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
4const blockString = require('../../utils/blockString');
5const getPreviousNonSharedLineCommentNode = require('../../utils/getPreviousNonSharedLineCommentNode');
6const hasEmptyLine = require('../../utils/hasEmptyLine');
7const isAfterComment = require('../../utils/isAfterComment');
8const isCustomProperty = require('../../utils/isCustomProperty');
9const isFirstNested = require('../../utils/isFirstNested');
10const isSingleLineString = require('../../utils/isSingleLineString');
11const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
12const optionsMatches = require('../../utils/optionsMatches');
13const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
14const report = require('../../utils/report');
15const ruleMessages = require('../../utils/ruleMessages');
16const validateOptions = require('../../utils/validateOptions');
17
18const ruleName = 'custom-property-empty-line-before';
19
20const messages = ruleMessages(ruleName, {
21 expected: 'Expected empty line before custom property',
22 rejected: 'Unexpected empty line before custom property',
23});
24
25function rule(expectation, options, context) {
26 return (root, result) => {
27 const validOptions = validateOptions(
28 result,
29 ruleName,
30 {
31 actual: expectation,
32 possible: ['always', 'never'],
33 },
34 {
35 actual: options,
36 possible: {
37 except: ['first-nested', 'after-comment', 'after-custom-property'],
38 ignore: ['after-comment', 'first-nested', 'inside-single-line-block'],
39 },
40 optional: true,
41 },
42 );
43
44 if (!validOptions) {
45 return;
46 }
47
48 root.walkDecls((decl) => {
49 const prop = decl.prop;
50 const parent = decl.parent;
51
52 if (!isStandardSyntaxDeclaration(decl)) {
53 return;
54 }
55
56 if (!isCustomProperty(prop)) {
57 return;
58 }
59
60 // Optionally ignore the node if a comment precedes it
61 if (optionsMatches(options, 'ignore', 'after-comment') && isAfterComment(decl)) {
62 return;
63 }
64
65 // Optionally ignore the node if it is the first nested
66 if (optionsMatches(options, 'ignore', 'first-nested') && isFirstNested(decl)) {
67 return;
68 }
69
70 // Optionally ignore nodes inside single-line blocks
71 if (
72 optionsMatches(options, 'ignore', 'inside-single-line-block') &&
73 isSingleLineString(blockString(parent))
74 ) {
75 return;
76 }
77
78 let expectEmptyLineBefore = expectation === 'always';
79
80 // Optionally reverse the expectation if any exceptions apply
81 if (
82 (optionsMatches(options, 'except', 'first-nested') && isFirstNested(decl)) ||
83 (optionsMatches(options, 'except', 'after-comment') && isAfterComment(decl)) ||
84 (optionsMatches(options, 'except', 'after-custom-property') && isAfterCustomProperty(decl))
85 ) {
86 expectEmptyLineBefore = !expectEmptyLineBefore;
87 }
88
89 const hasEmptyLineBefore = hasEmptyLine(decl.raws.before);
90
91 // Return if the expectation is met
92 if (expectEmptyLineBefore === hasEmptyLineBefore) {
93 return;
94 }
95
96 // Fix
97 if (context.fix) {
98 if (expectEmptyLineBefore) {
99 addEmptyLineBefore(decl, context.newline);
100 } else {
101 removeEmptyLinesBefore(decl, context.newline);
102 }
103
104 return;
105 }
106
107 const message = expectEmptyLineBefore ? messages.expected : messages.rejected;
108
109 report({
110 message,
111 node: decl,
112 result,
113 ruleName,
114 });
115 });
116 };
117}
118
119function isAfterCustomProperty(decl) {
120 const prevNode = getPreviousNonSharedLineCommentNode(decl);
121
122 return prevNode && prevNode.prop && isCustomProperty(prevNode.prop);
123}
124
125rule.ruleName = ruleName;
126rule.messages = messages;
127module.exports = rule;