|
| 1 | +/** |
| 2 | + * @typedef { import("../types").RuleFixer } RuleFixer |
| 3 | + * @typedef { import("../types").RuleModule } RuleModule |
| 4 | + * @typedef { import("../types").TagNode } TagNode |
| 5 | + * @typedef {Object} MessageId |
| 6 | + * @property {"closeStyleWrong"} CLOSE_STYLE_WRONG |
| 7 | + * @property {"newlineMissing"} NEWLINE_MISSING |
| 8 | + * @property {"newlineUnexpected"} NEWLINE_UNEXPECTED |
| 9 | + */ |
| 10 | + |
| 11 | +const { RULE_CATEGORY } = require("../constants"); |
| 12 | + |
| 13 | +/** |
| 14 | + * @type {MessageId} |
| 15 | + */ |
| 16 | + |
| 17 | +const MESSAGE_ID = { |
| 18 | + CLOSE_STYLE_WRONG: "closeStyleWrong", |
| 19 | + NEWLINE_MISSING: "newlineMissing", |
| 20 | + NEWLINE_UNEXPECTED: "newlineUnexpected", |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * @type {RuleModule} |
| 25 | + */ |
| 26 | +module.exports = { |
| 27 | + meta: { |
| 28 | + type: "code", |
| 29 | + |
| 30 | + docs: { |
| 31 | + description: "Enforce newline between attributes", |
| 32 | + category: RULE_CATEGORY.STYLE, |
| 33 | + recommended: true, |
| 34 | + }, |
| 35 | + |
| 36 | + fixable: true, |
| 37 | + schema: [ |
| 38 | + { |
| 39 | + type: "object", |
| 40 | + properties: { |
| 41 | + closeStyle: { |
| 42 | + enum: ["newline", "sameline"], |
| 43 | + }, |
| 44 | + ifAttrsMoreThan: { |
| 45 | + type: "integer", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + messages: { |
| 51 | + [MESSAGE_ID.CLOSE_STYLE_WRONG]: |
| 52 | + "Closing bracket was on {{actual}}; expected {{expected}}", |
| 53 | + [MESSAGE_ID.NEWLINE_MISSING]: "Newline expected before {{attrName}}", |
| 54 | + [MESSAGE_ID.NEWLINE_UNEXPECTED]: |
| 55 | + "Newlines not expected between attributes, since this tag has fewer than {{attrMin}} attributes", |
| 56 | + }, |
| 57 | + }, |
| 58 | + |
| 59 | + create(context) { |
| 60 | + const options = context.options[0] || {}; |
| 61 | + const attrMin = isNaN(options.ifAttrsMoreThan) |
| 62 | + ? 2 |
| 63 | + : options.ifAttrsMoreThan; |
| 64 | + const closeStyle = options.closeStyle || "newline"; |
| 65 | + |
| 66 | + return { |
| 67 | + /** |
| 68 | + * @param {TagNode} node |
| 69 | + */ |
| 70 | + Tag(node) { |
| 71 | + const shouldBeMultiline = node.attributes.length > attrMin; |
| 72 | + |
| 73 | + /** |
| 74 | + * This doesn't do any indentation, so the result will look silly. Indentation should be covered by the `indent` rule |
| 75 | + * @param {RuleFixer} fixer |
| 76 | + */ |
| 77 | + function fix(fixer) { |
| 78 | + const spacer = shouldBeMultiline ? "\n" : " "; |
| 79 | + let expected = node.openStart.value; |
| 80 | + for (const attr of node.attributes) { |
| 81 | + expected += `${spacer}${attr.key.value}`; |
| 82 | + if (attr.startWrapper && attr.value && attr.endWrapper) { |
| 83 | + expected += `=${attr.startWrapper.value}${attr.value.value}${attr.endWrapper.value}`; |
| 84 | + } |
| 85 | + } |
| 86 | + if (shouldBeMultiline && closeStyle === "newline") { |
| 87 | + expected += "\n"; |
| 88 | + } else if (node.selfClosing) { |
| 89 | + expected += " "; |
| 90 | + } |
| 91 | + expected += node.openEnd.value; |
| 92 | + |
| 93 | + return fixer.replaceTextRange( |
| 94 | + [node.openStart.range[0], node.openEnd.range[1]], |
| 95 | + expected |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if (shouldBeMultiline) { |
| 100 | + let index = 0; |
| 101 | + for (const attr of node.attributes) { |
| 102 | + const attrPrevious = node.attributes[index - 1]; |
| 103 | + const relativeToNode = attrPrevious || node.openStart; |
| 104 | + if (attr.loc.start.line === relativeToNode.loc.end.line) { |
| 105 | + return context.report({ |
| 106 | + node, |
| 107 | + data: { |
| 108 | + attrName: attr.key.value, |
| 109 | + }, |
| 110 | + fix, |
| 111 | + messageId: MESSAGE_ID.NEWLINE_MISSING, |
| 112 | + }); |
| 113 | + } |
| 114 | + index += 1; |
| 115 | + } |
| 116 | + |
| 117 | + const attrLast = node.attributes[node.attributes.length - 1]; |
| 118 | + const closeStyleActual = |
| 119 | + node.openEnd.loc.start.line === attrLast.loc.end.line |
| 120 | + ? "sameline" |
| 121 | + : "newline"; |
| 122 | + if (closeStyle !== closeStyleActual) { |
| 123 | + return context.report({ |
| 124 | + node, |
| 125 | + data: { |
| 126 | + actual: closeStyleActual, |
| 127 | + expected: closeStyle, |
| 128 | + }, |
| 129 | + fix, |
| 130 | + messageId: MESSAGE_ID.CLOSE_STYLE_WRONG, |
| 131 | + }); |
| 132 | + } |
| 133 | + } else { |
| 134 | + let expectedLastLineNum = node.openStart.loc.start.line; |
| 135 | + for (const attr of node.attributes) { |
| 136 | + if (shouldBeMultiline) { |
| 137 | + expectedLastLineNum += 1; |
| 138 | + } |
| 139 | + if (attr.value) { |
| 140 | + const valueLineSpan = |
| 141 | + attr.value.loc.end.line - attr.value.loc.start.line; |
| 142 | + expectedLastLineNum += valueLineSpan; |
| 143 | + } |
| 144 | + } |
| 145 | + if (shouldBeMultiline && closeStyle === "newline") { |
| 146 | + expectedLastLineNum += 1; |
| 147 | + } |
| 148 | + |
| 149 | + if (node.openEnd.loc.end.line !== expectedLastLineNum) { |
| 150 | + return context.report({ |
| 151 | + node, |
| 152 | + data: { |
| 153 | + attrMin, |
| 154 | + }, |
| 155 | + fix, |
| 156 | + messageId: MESSAGE_ID.NEWLINE_UNEXPECTED, |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | + }, |
| 161 | + }; |
| 162 | + }, |
| 163 | +}; |
0 commit comments