1 | 'use strict';
|
2 |
|
3 | const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
|
4 | const blockString = require('../../utils/blockString');
|
5 | const getPreviousNonSharedLineCommentNode = require('../../utils/getPreviousNonSharedLineCommentNode');
|
6 | const hasEmptyLine = require('../../utils/hasEmptyLine');
|
7 | const isAfterComment = require('../../utils/isAfterComment');
|
8 | const isCustomProperty = require('../../utils/isCustomProperty');
|
9 | const isFirstNested = require('../../utils/isFirstNested');
|
10 | const isSingleLineString = require('../../utils/isSingleLineString');
|
11 | const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
|
12 | const optionsMatches = require('../../utils/optionsMatches');
|
13 | const removeEmptyLinesBefore = require('../../utils/removeEmptyLinesBefore');
|
14 | const report = require('../../utils/report');
|
15 | const ruleMessages = require('../../utils/ruleMessages');
|
16 | const validateOptions = require('../../utils/validateOptions');
|
17 |
|
18 | const ruleName = 'custom-property-empty-line-before';
|
19 |
|
20 | const messages = ruleMessages(ruleName, {
|
21 | expected: 'Expected empty line before custom property',
|
22 | rejected: 'Unexpected empty line before custom property',
|
23 | });
|
24 |
|
25 | function 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 |
|
61 | if (optionsMatches(options, 'ignore', 'after-comment') && isAfterComment(decl)) {
|
62 | return;
|
63 | }
|
64 |
|
65 |
|
66 | if (optionsMatches(options, 'ignore', 'first-nested') && isFirstNested(decl)) {
|
67 | return;
|
68 | }
|
69 |
|
70 |
|
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 |
|
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 |
|
92 | if (expectEmptyLineBefore === hasEmptyLineBefore) {
|
93 | return;
|
94 | }
|
95 |
|
96 |
|
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 |
|
119 | function isAfterCustomProperty(decl) {
|
120 | const prevNode = getPreviousNonSharedLineCommentNode(decl);
|
121 |
|
122 | return prevNode && prevNode.prop && isCustomProperty(prevNode.prop);
|
123 | }
|
124 |
|
125 | rule.ruleName = ruleName;
|
126 | rule.messages = messages;
|
127 | module.exports = rule;
|