|
| 1 | +/** |
| 2 | + * @fileoverview Enforce the location of first attribute |
| 3 | + * @author Yosuke Ota |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +const utils = require('../utils') |
| 11 | + |
| 12 | +module.exports = { |
| 13 | + meta: { |
| 14 | + type: 'layout', |
| 15 | + docs: { |
| 16 | + description: 'enforce the location of first attribute', |
| 17 | + categories: ['vue3-strongly-recommended', 'strongly-recommended'], |
| 18 | + url: 'https://eslint.vuejs.org/rules/first-attribute-linebreak.html' |
| 19 | + }, |
| 20 | + fixable: 'whitespace', // or "code" or "whitespace" |
| 21 | + schema: [ |
| 22 | + { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + multiline: { enum: ['below', 'beside', 'ignore'] }, |
| 26 | + singleline: { enum: ['below', 'beside', 'ignore'] } |
| 27 | + }, |
| 28 | + additionalProperties: false |
| 29 | + } |
| 30 | + ], |
| 31 | + messages: { |
| 32 | + expected: 'Expected a linebreak before this attribute.', |
| 33 | + unexpected: 'Expected no linebreak before this attribute.' |
| 34 | + } |
| 35 | + }, |
| 36 | + /** @param {RuleContext} context */ |
| 37 | + create(context) { |
| 38 | + /** @type {"below" | "beside" | "ignore"} */ |
| 39 | + const singleline = |
| 40 | + (context.options[0] && context.options[0].singleline) || 'ignore' |
| 41 | + /** @type {"below" | "beside" | "ignore"} */ |
| 42 | + const multiline = |
| 43 | + (context.options[0] && context.options[0].multiline) || 'below' |
| 44 | + |
| 45 | + const template = |
| 46 | + context.parserServices.getTemplateBodyTokenStore && |
| 47 | + context.parserServices.getTemplateBodyTokenStore() |
| 48 | + |
| 49 | + /** |
| 50 | + * Report attribute |
| 51 | + * @param {VAttribute | VDirective} firstAttribute |
| 52 | + * @param { "below" | "beside"} location |
| 53 | + */ |
| 54 | + function report(firstAttribute, location) { |
| 55 | + context.report({ |
| 56 | + node: firstAttribute, |
| 57 | + messageId: location === 'beside' ? 'unexpected' : 'expected', |
| 58 | + fix(fixer) { |
| 59 | + const prevToken = template.getTokenBefore(firstAttribute, { |
| 60 | + includeComments: true |
| 61 | + }) |
| 62 | + return fixer.replaceTextRange( |
| 63 | + [prevToken.range[1], firstAttribute.range[0]], |
| 64 | + location === 'beside' ? ' ' : '\n' |
| 65 | + ) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + return utils.defineTemplateBodyVisitor(context, { |
| 71 | + VStartTag(node) { |
| 72 | + const firstAttribute = node.attributes[0] |
| 73 | + if (!firstAttribute) return |
| 74 | + |
| 75 | + const lastAttribute = node.attributes[node.attributes.length - 1] |
| 76 | + |
| 77 | + const location = |
| 78 | + firstAttribute.loc.start.line === lastAttribute.loc.end.line |
| 79 | + ? singleline |
| 80 | + : multiline |
| 81 | + if (location === 'ignore') { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + if (location === 'beside') { |
| 86 | + if (node.loc.start.line === firstAttribute.loc.start.line) { |
| 87 | + return |
| 88 | + } |
| 89 | + } else { |
| 90 | + if (node.loc.start.line < firstAttribute.loc.start.line) { |
| 91 | + return |
| 92 | + } |
| 93 | + } |
| 94 | + report(firstAttribute, location) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments