[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Ensure proper position of the first property in JSX
|
---|
| 3 | * @author Joachim Seminck
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const docsUrl = require('../util/docsUrl');
|
---|
| 9 | const report = require('../util/report');
|
---|
| 10 |
|
---|
| 11 | // ------------------------------------------------------------------------------
|
---|
| 12 | // Rule Definition
|
---|
| 13 | // ------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | const messages = {
|
---|
| 16 | propOnNewLine: 'Property should be placed on a new line',
|
---|
| 17 | propOnSameLine: 'Property should be placed on the same line as the component declaration',
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
| 21 | module.exports = {
|
---|
| 22 | meta: {
|
---|
| 23 | docs: {
|
---|
| 24 | description: 'Enforce proper position of the first property in JSX',
|
---|
| 25 | category: 'Stylistic Issues',
|
---|
| 26 | recommended: false,
|
---|
| 27 | url: docsUrl('jsx-first-prop-new-line'),
|
---|
| 28 | },
|
---|
| 29 | fixable: 'code',
|
---|
| 30 |
|
---|
| 31 | messages,
|
---|
| 32 |
|
---|
| 33 | schema: [{
|
---|
| 34 | enum: ['always', 'never', 'multiline', 'multiline-multiprop', 'multiprop'],
|
---|
| 35 | }],
|
---|
| 36 | },
|
---|
| 37 |
|
---|
| 38 | create(context) {
|
---|
| 39 | const configuration = context.options[0] || 'multiline-multiprop';
|
---|
| 40 |
|
---|
| 41 | function isMultilineJSX(jsxNode) {
|
---|
| 42 | return jsxNode.loc.start.line < jsxNode.loc.end.line;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | return {
|
---|
| 46 | JSXOpeningElement(node) {
|
---|
| 47 | if (
|
---|
| 48 | (configuration === 'multiline' && isMultilineJSX(node))
|
---|
| 49 | || (configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1)
|
---|
| 50 | || (configuration === 'multiprop' && node.attributes.length > 1)
|
---|
| 51 | || (configuration === 'always')
|
---|
| 52 | ) {
|
---|
| 53 | node.attributes.some((decl) => {
|
---|
| 54 | if (decl.loc.start.line === node.loc.start.line) {
|
---|
| 55 | report(context, messages.propOnNewLine, 'propOnNewLine', {
|
---|
| 56 | node: decl,
|
---|
| 57 | fix(fixer) {
|
---|
| 58 | return fixer.replaceTextRange([(node.typeParameters || node.name).range[1], decl.range[0]], '\n');
|
---|
| 59 | },
|
---|
| 60 | });
|
---|
| 61 | }
|
---|
| 62 | return true;
|
---|
| 63 | });
|
---|
| 64 | } else if (
|
---|
| 65 | (configuration === 'never' && node.attributes.length > 0)
|
---|
| 66 | || (configuration === 'multiprop' && isMultilineJSX(node) && node.attributes.length <= 1)
|
---|
| 67 | ) {
|
---|
| 68 | const firstNode = node.attributes[0];
|
---|
| 69 | if (node.loc.start.line < firstNode.loc.start.line) {
|
---|
| 70 | report(context, messages.propOnSameLine, 'propOnSameLine', {
|
---|
| 71 | node: firstNode,
|
---|
| 72 | fix(fixer) {
|
---|
| 73 | return fixer.replaceTextRange([node.name.range[1], firstNode.range[0]], ' ');
|
---|
| 74 | },
|
---|
| 75 | });
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | },
|
---|
| 79 | };
|
---|
| 80 | },
|
---|
| 81 | };
|
---|