|
| 1 | +/** |
| 2 | + * @author Toru Nagashima |
| 3 | + * @copyright 2016 Toru Nagashima. All rights reserved. |
| 4 | + * See LICENSE file in root directory for full license. |
| 5 | + */ |
| 6 | +'use strict' |
| 7 | + |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const utils = require('../utils') |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Helpers |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +function getPhrase (lineBreaks) { |
| 19 | + switch (lineBreaks) { |
| 20 | + case 0: return 'no line breaks' |
| 21 | + case 1: return '1 line break' |
| 22 | + default: return `${lineBreaks} line breaks` |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Creates AST event handlers for html-closing-bracket-newline. |
| 28 | + * |
| 29 | + * @param {RuleContext} context - The rule context. |
| 30 | + * @returns {object} AST event handlers. |
| 31 | + */ |
| 32 | +function create (context) { |
| 33 | + const options = context.options[0] || {} |
| 34 | + const template = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore() |
| 35 | + |
| 36 | + return utils.defineTemplateBodyVisitor(context, { |
| 37 | + 'VStartTag, VEndTag' (node) { |
| 38 | + const closingBracketToken = template.getLastToken(node) |
| 39 | + if (closingBracketToken.type !== 'HTMLSelfClosingTagClose' && closingBracketToken.type !== 'HTMLTagClose') { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + const prevToken = template.getTokenBefore(closingBracketToken) |
| 44 | + const type = (node.loc.start.line === prevToken.loc.end.line) ? 'singleline' : 'multiline' |
| 45 | + const expectedLineBreaks = (options[type] === 'always') ? 1 : 0 |
| 46 | + const actualLineBreaks = (closingBracketToken.loc.start.line - prevToken.loc.end.line) |
| 47 | + |
| 48 | + if (actualLineBreaks !== expectedLineBreaks) { |
| 49 | + context.report({ |
| 50 | + node, |
| 51 | + loc: { |
| 52 | + start: prevToken.loc.end, |
| 53 | + end: closingBracketToken.loc.start |
| 54 | + }, |
| 55 | + message: 'Expected {{expected}} before closing bracket, but {{actual}} found.', |
| 56 | + data: { |
| 57 | + expected: getPhrase(expectedLineBreaks), |
| 58 | + actual: getPhrase(actualLineBreaks) |
| 59 | + }, |
| 60 | + fix (fixer) { |
| 61 | + const range = [prevToken.range[1], closingBracketToken.range[0]] |
| 62 | + const text = '\n'.repeat(expectedLineBreaks) |
| 63 | + return fixer.replaceTextRange(range, text) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +// ------------------------------------------------------------------------------ |
| 72 | +// Rule Definition |
| 73 | +// ------------------------------------------------------------------------------ |
| 74 | + |
| 75 | +module.exports = { |
| 76 | + create, |
| 77 | + meta: { |
| 78 | + docs: { |
| 79 | + description: "require or disallow a line break before tag's closing brackets", |
| 80 | + category: 'Stylistic Issues', |
| 81 | + recommended: false |
| 82 | + }, |
| 83 | + fixable: 'whitespace', |
| 84 | + schema: [{ |
| 85 | + type: 'object', |
| 86 | + properties: { |
| 87 | + 'singleline': { enum: ['always', 'never'] }, |
| 88 | + 'multiline': { enum: ['always', 'never'] } |
| 89 | + }, |
| 90 | + additionalProperties: false |
| 91 | + }] |
| 92 | + } |
| 93 | +} |
0 commit comments