|
| 1 | +/** |
| 2 | + * @author Barthy Bonhomme <[email protected]> <github.com/barthy-koeln> |
| 3 | + * @author Sergio Arbeo <[email protected]> |
| 4 | + * Adapted from https://github.com/DockYard/eslint-plugin-ember-suave/blob/master/lib/rules/lines-between-object-properties.js |
| 5 | + * |
| 6 | + * See LICENSE file in root directory for full license. |
| 7 | + */ |
| 8 | +'use strict' |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Requirements |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +const path = require('path') |
| 15 | +const utils = require('../utils') |
| 16 | + |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | +// Helpers |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +// ... |
| 22 | + |
| 23 | +// ------------------------------------------------------------------------------ |
| 24 | +// Rule Definition |
| 25 | +// ------------------------------------------------------------------------------ |
| 26 | + |
| 27 | +module.exports = { |
| 28 | + meta: { |
| 29 | + type: 'layout', |
| 30 | + docs: { |
| 31 | + description: 'enforce empty lines between top-level options', |
| 32 | + categories: undefined, |
| 33 | + url: 'https://eslint.vuejs.org/rules/empty-line-between-options.html' |
| 34 | + }, |
| 35 | + fixable: 'whitespace', |
| 36 | + schema: [{ enum: ['always', 'never'] }], |
| 37 | + messages: { |
| 38 | + never: 'Unexpected blank line between Vue component options.', |
| 39 | + always: 'Expected blank line between Vue component options.' |
| 40 | + } |
| 41 | + }, |
| 42 | + /** @param {RuleContext} context */ |
| 43 | + create(context) { |
| 44 | + const isVueFile = utils.isVueFile(context.getFilename()) |
| 45 | + if (!isVueFile) { |
| 46 | + return {} |
| 47 | + } |
| 48 | + |
| 49 | + const sourceCode = context.getSourceCode() |
| 50 | + const shouldPad = (context.options[0] || 'always') === 'always' |
| 51 | + |
| 52 | + const fixFunctions = { |
| 53 | + /** |
| 54 | + * Removes newlines between component options |
| 55 | + * @param {RuleFixer} fixer |
| 56 | + * @param {Token} currentLast |
| 57 | + * @param {Token} nextFirst |
| 58 | + * @return {Fix} |
| 59 | + */ |
| 60 | + never(fixer, currentLast, nextFirst) { |
| 61 | + return fixer.replaceTextRange( |
| 62 | + [currentLast.range[1], nextFirst.range[0]], |
| 63 | + ',\n' |
| 64 | + ) |
| 65 | + }, |
| 66 | + /** |
| 67 | + * Add newlines between component options |
| 68 | + * @param {RuleFixer} fixer |
| 69 | + * @param {Token} currentLast |
| 70 | + * @return {Fix} |
| 71 | + */ |
| 72 | + always(fixer, currentLast /*, nextFirst*/) { |
| 73 | + const tokenAfterLastToken = sourceCode.getTokenAfter(currentLast) |
| 74 | + const tokenToLineBreakAfter = |
| 75 | + tokenAfterLastToken.value === ',' ? tokenAfterLastToken : currentLast |
| 76 | + |
| 77 | + return fixer.insertTextAfter(tokenToLineBreakAfter, '\n') |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Checks if there is an empty line between two tokens |
| 83 | + * @param {Token} first The first token |
| 84 | + * @param {Token} second The second token |
| 85 | + * @returns {boolean} True if there is at least a line between the tokens |
| 86 | + */ |
| 87 | + function isPaddingBetweenTokens(first, second) { |
| 88 | + const comments = sourceCode.getCommentsBefore(second) |
| 89 | + const len = comments.length |
| 90 | + |
| 91 | + // If there is no comments |
| 92 | + if (len === 0) { |
| 93 | + const linesBetweenFstAndSnd = |
| 94 | + second.loc.start.line - first.loc.end.line - 1 |
| 95 | + |
| 96 | + return linesBetweenFstAndSnd >= 1 |
| 97 | + } |
| 98 | + |
| 99 | + // If there are comments |
| 100 | + let sumOfCommentLines = 0 // the numbers of lines of comments |
| 101 | + let prevCommentLineNum = -1 // line number of the end of the previous comment |
| 102 | + |
| 103 | + for (let i = 0; i < len; i++) { |
| 104 | + const commentLinesOfThisComment = |
| 105 | + comments[i].loc.end.line - comments[i].loc.start.line + 1 |
| 106 | + |
| 107 | + sumOfCommentLines += commentLinesOfThisComment |
| 108 | + |
| 109 | + /* |
| 110 | + * If this comment and the previous comment are in the same line, |
| 111 | + * the count of comment lines is duplicated. So decrement sumOfCommentLines. |
| 112 | + */ |
| 113 | + if (prevCommentLineNum === comments[i].loc.start.line) { |
| 114 | + sumOfCommentLines -= 1 |
| 115 | + } |
| 116 | + |
| 117 | + prevCommentLineNum = comments[i].loc.end.line |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * If the first block and the first comment are in the same line, |
| 122 | + * the count of comment lines is duplicated. So decrement sumOfCommentLines. |
| 123 | + */ |
| 124 | + if (first.loc.end.line === comments[0].loc.start.line) { |
| 125 | + sumOfCommentLines -= 1 |
| 126 | + } |
| 127 | + |
| 128 | + /* |
| 129 | + * If the last comment and the second block are in the same line, |
| 130 | + * the count of comment lines is duplicated. So decrement sumOfCommentLines. |
| 131 | + */ |
| 132 | + if (comments[len - 1].loc.end.line === second.loc.start.line) { |
| 133 | + sumOfCommentLines -= 1 |
| 134 | + } |
| 135 | + |
| 136 | + const linesBetweenFstAndSnd = |
| 137 | + second.loc.start.line - first.loc.end.line - 1 |
| 138 | + |
| 139 | + return linesBetweenFstAndSnd - sumOfCommentLines >= 1 |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Report error based on configuration |
| 144 | + * @param {ASTNode} node Where to report errors |
| 145 | + * @param {boolean} isPadded True if the option is followed by an empty line |
| 146 | + * @param {Token} currentLast End of checked token |
| 147 | + * @param {Token} nextFirst Start of next token |
| 148 | + */ |
| 149 | + function reportError(node, isPadded, currentLast, nextFirst) { |
| 150 | + const key = isPadded ? 'never' : 'always' |
| 151 | + const fixFunction = fixFunctions[key] |
| 152 | + |
| 153 | + context.report({ |
| 154 | + node, |
| 155 | + messageId: key, |
| 156 | + fix: (fixer) => fixFunction(fixer, currentLast, nextFirst) |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Compares options and decides what to do |
| 162 | + * @param {ASTNode} option current option to check |
| 163 | + * @param {ASTNode} nextNode next node to check against |
| 164 | + */ |
| 165 | + function checkOption(option, nextNode) { |
| 166 | + const currentLast = sourceCode.getLastToken(option) |
| 167 | + const nextFirst = sourceCode.getFirstToken(nextNode) |
| 168 | + const isPadded = isPaddingBetweenTokens(currentLast, nextFirst) |
| 169 | + |
| 170 | + if (shouldPad === isPadded) { |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + reportError(nextNode, isPadded, currentLast, nextFirst) |
| 175 | + } |
| 176 | + |
| 177 | + return { |
| 178 | + /** |
| 179 | + * @param {import('vue-eslint-parser/ast').Node} node |
| 180 | + */ |
| 181 | + ObjectExpression(node) { |
| 182 | + if (node.parent && node.parent.type !== 'ExportDefaultDeclaration') { |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + const { properties } = node |
| 187 | + |
| 188 | + for (let i = 0; i < properties.length - 1; i++) { |
| 189 | + const property = properties[i] |
| 190 | + const nextProperty = properties[i + 1] |
| 191 | + |
| 192 | + checkOption(property, nextProperty) |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments