|
| 1 | +/** |
| 2 | + * @fileoverview This rule warns about the usage of extra whitespaces between attributes |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + meta: { |
| 13 | + docs: { |
| 14 | + description: 'This rule warns about the usage of extra whitespaces between attributes', |
| 15 | + category: 'Stylistic Issues', |
| 16 | + recommended: false |
| 17 | + }, |
| 18 | + fixable: 'whitespace', // or "code" or "whitespace" |
| 19 | + schema: [] |
| 20 | + }, |
| 21 | + |
| 22 | + /** |
| 23 | + * @param {RuleContext} context - The rule context. |
| 24 | + * @returns {Object} AST event handlers. |
| 25 | + */ |
| 26 | + create (context) { |
| 27 | + const sourceCode = context.getSourceCode() |
| 28 | + |
| 29 | + // ---------------------------------------------------------------------- |
| 30 | + // Public |
| 31 | + // ---------------------------------------------------------------------- |
| 32 | + |
| 33 | + return { |
| 34 | + Program (node) { |
| 35 | + // TODO: Check `context.parserServices.getTemplateBodyTokenStore` exists or not. |
| 36 | + const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
| 37 | + const tokens = tokenStore.getTokens(node.templateBody, { includeComments: true }) |
| 38 | + |
| 39 | + let prevToken = tokens.shift() |
| 40 | + for (const token of tokens) { |
| 41 | + if (sourceCode.isSpaceBetweenTokens(prevToken, token)) { |
| 42 | + const text = sourceCode.getText(token, (token.range[0] - prevToken.range[1] + 1), 0) |
| 43 | + |
| 44 | + const match = text.match(/([^\r\n\t\s])([ \t]+)([>]?)([\n\r]?)/) |
| 45 | + if (!match) return // there is no errors |
| 46 | + |
| 47 | + const spaces = match[2].length |
| 48 | + const requiredSpaces = match[3] === '>' || match[4] !== '' ? 0 : 1 |
| 49 | + |
| 50 | + if (spaces > requiredSpaces) { |
| 51 | + context.report({ |
| 52 | + node: token, |
| 53 | + loc: { |
| 54 | + start: { |
| 55 | + line: prevToken.loc.end.line, |
| 56 | + column: prevToken.loc.end.column + requiredSpaces |
| 57 | + }, |
| 58 | + end: { |
| 59 | + line: prevToken.loc.end.line, |
| 60 | + column: prevToken.loc.end.column + spaces |
| 61 | + } |
| 62 | + }, |
| 63 | + message: 'Extra whitespace detected.', |
| 64 | + fix: (fixer) => fixer.removeRange([prevToken.range[1] + requiredSpaces, prevToken.range[1] + spaces]) |
| 65 | + }) |
| 66 | + } |
| 67 | + } |
| 68 | + prevToken = token |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments