Skip to content

Commit 580b303

Browse files
committed
resolved pr comments related to docs and rule
1 parent 493e23a commit 580b303

File tree

3 files changed

+69
-89
lines changed

3 files changed

+69
-89
lines changed

Diff for: README.md

+45-38
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,47 @@ You should also specify settings that will be shared across all the plugin rules
3434

3535
```json5
3636
{
37-
settings: {
38-
react: {
39-
createClass: 'createReactClass', // Regex for Component Factory to use,
40-
// default to "createReactClass"
41-
pragma: 'React', // Pragma to use, default to "React"
42-
fragment: 'Fragment', // Fragment to use (may be a property of <pragma>), default to "Fragment"
43-
version: 'detect', // React version. "detect" automatically picks the version you have installed.
44-
// You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
45-
// Defaults to the "defaultVersion" setting and warns if missing, and to "detect" in the future
46-
defaultVersion: '', // Default React version to use when the version you have installed cannot be detected.
47-
// If not provided, defaults to the latest React version.
48-
flowVersion: '0.53', // Flow version
37+
"settings": {
38+
"react": {
39+
"createClass": "createReactClass", // Regex for Component Factory to use,
40+
// default to "createReactClass"
41+
"pragma": "React", // Pragma to use, default to "React"
42+
"fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
43+
"version": "detect", // React version. "detect" automatically picks the version you have installed.
44+
// You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
45+
// Defaults to the "defaultVersion" setting and warns if missing, and to "detect" in the future
46+
"defaultVersion": "", // Default React version to use when the version you have installed cannot be detected.
47+
// If not provided, defaults to the latest React version.
48+
"flowVersion": "0.53" // Flow version
4949
},
50-
propWrapperFunctions: [
51-
// The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
52-
'forbidExtraProps',
53-
{ property: 'freeze', object: 'Object' },
54-
{ property: 'myFavoriteWrapper' },
55-
// for rules that check exact prop wrappers
56-
{ property: 'forbidExtraProps', exact: true },
50+
"propWrapperFunctions": [
51+
// The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
52+
"forbidExtraProps",
53+
{"property": "freeze", "object": "Object"},
54+
{"property": "myFavoriteWrapper"},
55+
// for rules that check exact prop wrappers
56+
{"property": "forbidExtraProps", "exact": true}
5757
],
58-
componentWrapperFunctions: [
59-
// The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn't set, components wrapped by these functions will be skipped.
60-
'observer', // `property`
61-
{ property: 'styled' }, // `object` is optional
62-
{ property: 'observer', object: 'Mobx' },
63-
{ property: 'observer', object: '<pragma>' }, // sets `object` to whatever value `settings.react.pragma` is set to
58+
"componentWrapperFunctions": [
59+
// The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn't set, components wrapped by these functions will be skipped.
60+
"observer", // `property`
61+
{"property": "styled"}, // `object` is optional
62+
{"property": "observer", "object": "Mobx"},
63+
{"property": "observer", "object": "<pragma>"} // sets `object` to whatever value `settings.react.pragma` is set to
6464
],
65-
formComponents: [
65+
"formComponents": [
6666
// Components used as alternatives to <form> for forms, eg. <Form endpoint={ url } />
67-
'CustomForm',
68-
{ name: 'SimpleForm', formAttribute: 'endpoint' },
69-
{ name: 'Form', formAttribute: ['registerEndpoint', 'loginEndpoint'] }, // allows specifying multiple properties if necessary
67+
"CustomForm",
68+
{"name": "SimpleForm", "formAttribute": "endpoint"},
69+
{"name": "Form", "formAttribute": ["registerEndpoint", "loginEndpoint"]}, // allows specifying multiple properties if necessary
7070
],
71-
linkComponents: [
71+
"linkComponents": [
7272
// Components used as alternatives to <a> for linking, eg. <Link to={ url } />
73-
'Hyperlink',
74-
{ name: 'MyLink', linkAttribute: 'to' },
75-
{ name: 'Link', linkAttribute: ['to', 'href'] }, // allows specifying multiple properties if necessary
76-
],
77-
},
73+
"Hyperlink",
74+
{"name": "MyLink", "linkAttribute": "to"},
75+
{"name": "Link", "linkAttribute": ["to", "href"]}, // allows specifying multiple properties if necessary
76+
]
77+
}
7878
}
7979
```
8080

@@ -84,7 +84,9 @@ Add "react" to the plugins section.
8484

8585
```json
8686
{
87-
"plugins": ["react"]
87+
"plugins": [
88+
"react"
89+
]
8890
}
8991
```
9092

@@ -134,7 +136,9 @@ This pairs well with the `eslint:all` rule.
134136

135137
```json
136138
{
137-
"plugins": ["react"],
139+
"plugins": [
140+
"react"
141+
],
138142
"extends": ["eslint:all", "plugin:react/all"]
139143
}
140144
```
@@ -201,7 +205,6 @@ Refer to the [official docs](https://eslint.org/docs/latest/user-guide/configuri
201205
The schema of the `settings.react` object would be identical to that of what's already described above in the legacy config section.
202206

203207
<!-- markdownlint-disable-next-line no-duplicate-heading -->
204-
205208
### Flat Configs
206209

207210
This plugin exports 3 flat configs:
@@ -405,11 +408,15 @@ module.exports = [
405408

406409
[npm-url]: https://npmjs.org/package/eslint-plugin-react
407410
[npm-image]: https://img.shields.io/npm/v/eslint-plugin-react.svg
411+
408412
[status-url]: https://github.com/jsx-eslint/eslint-plugin-react/pulse
409413
[status-image]: https://img.shields.io/github/last-commit/jsx-eslint/eslint-plugin-react.svg
414+
410415
[tidelift-url]: https://tidelift.com/subscription/pkg/npm-eslint-plugin-react?utm_source=npm-eslint-plugin-react&utm_medium=referral&utm_campaign=readme
411416
[tidelift-image]: https://tidelift.com/badges/package/npm/eslint-plugin-react?style=flat
417+
412418
[package-url]: https://npmjs.org/package/eslint-plugin-react
413419
[npm-version-svg]: https://versionbadg.es/jsx-eslint/eslint-plugin-react.svg
420+
414421
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/jsx-eslint/eslint-plugin-react
415422
[actions-url]: https://github.com/jsx-eslint/eslint-plugin-react/actions

Diff for: docs/rules/padding-lines-between-tags.md

+7-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Require or disallow newlines between sibling tags in React.
1919

2020
This rule requires blank lines between each sibling HTML tag by default.
2121

22-
A configuration is an object which has 3 properties; `blankLine`, `prev` and `next`. For example, `{ blankLine: "always", prev: "br", next: "div" }` means “one or more blank lines are required between a `br` tag and a `div` tag.” You can supply any number of configurations. If a tag pair matches multiple configurations, the last matched configuration will be used.
22+
A configuration is an object which has 3 properties; `blankLine`, `prev`, and `next`. For example, `{ blankLine: "always", prev: "br", next: "div" }` means “one or more blank lines are required between a `br` tag and a `div` tag.” You can supply any number of configurations. If a tag pair matches multiple configurations, the last matched configuration will be used.
2323

2424
- `blankLine` is one of the following:
2525
- `always` requires one or more blank lines.
@@ -32,11 +32,7 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
3232

3333
`{ blankLine: 'never', prev: '*', next: '*' }`
3434

35-
<eslint-code-block fix :rules="{'padding-line-between-tags': ['error', [
36-
{ blankLine: 'never', prev: '*', next: '*' }
37-
]]}">
38-
39-
```react
35+
```jsx
4036
<App>
4137
<div>
4238
<div></div>
@@ -47,17 +43,11 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
4743
</App>
4844
```
4945

50-
</eslint-code-block>
51-
5246
### Require newlines after `<br>`
5347

5448
`{ blankLine: 'always', prev: 'br', next: '*' }`
5549

56-
<eslint-code-block fix :rules="{'react/padding-line-between-tags': ['error', [
57-
{ blankLine: 'always', prev: 'br', next: '*' }
58-
]]}">
59-
60-
```react
50+
```jsx
6151
<App>
6252
<div>
6353
<ul>
@@ -72,18 +62,11 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
7262
</App>
7363
```
7464

75-
</eslint-code-block>
76-
7765
### Require newlines between `<br>` and `<img>`
7866

7967
`{ blankLine: 'always', prev: 'br', next: 'img' }`
8068

81-
<eslint-code-block fix :rules="{'react/padding-line-between-tags': ['error', [
82-
{ blankLine: 'always', prev: 'br', next: 'img' },
83-
{ blankLine: 'always', prev: 'li', next: 'li' },
84-
]]}">
85-
86-
```react
69+
```jsx
8770
<App>
8871
<div>
8972
<ul>
@@ -100,7 +83,7 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
10083
</App>
10184
```
10285

103-
```react [Fixed]
86+
```jsx [Fixed]
10487
<App>
10588
<div>
10689
<ul>
@@ -119,17 +102,11 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
119102
</App>
120103
```
121104

122-
</eslint-code-block>
123-
124105
### Require consistent newlines
125106

126107
`{ blankLine: 'consistent', prev: '*', next: '*' }`
127108

128-
<eslint-code-block fix :rules="{'react/padding-line-between-tags': ['error', [
129-
{ blankLine: 'consistent', prev: '*', next: '*' }
130-
]]}">
131-
132-
```react
109+
```jsx
133110
<App>
134111
<div>
135112
<ul>
@@ -145,8 +122,6 @@ A configuration is an object which has 3 properties; `blankLine`, `prev` and `ne
145122
</App>
146123
```
147124

148-
</eslint-code-block>
149-
150125
## When Not To Use It
151126

152-
If you are not using React.
127+
If you are not using jsx.

Diff for: lib/rules/padding-lines-between-tags.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,40 @@
88
'use strict';
99

1010
const docsUrl = require('../util/docsUrl');
11+
const report = require('../util/report');
12+
const eslintUtil = require('../util/eslint');
1113

1214
/**
1315
* Split the source code into multiple lines based on the line delimiters.
1416
* Copied from padding-line-between-blocks
1517
* @param {string} text Source code as a string.
1618
* @returns {string[]} Array of source code lines.
1719
*/
18-
const messages = {
19-
never: 'Unexpected blank line before this tag.',
20-
always: 'Expected blank line before this tag.',
21-
};
2220

2321
function splitLines(text) {
2422
return text.split(/\r\n|[\r\n\u2028\u2029]/gu);
2523
}
2624

27-
function insertNewLine(context, tag, sibling, lineDifference) {
25+
function insertNewLine(tag, sibling, lineDifference) {
2826
const endTag = tag.closingElement || tag.openingElement;
2927

3028
if (lineDifference === 1) {
31-
context.report({
32-
messageId: 'always',
29+
report({
30+
message: 'Unexpected blank line before {{endTag}}.',
3331
loc: sibling && sibling.loc,
3432
// @ts-ignore
3533
fix(fixer) {
3634
return fixer.insertTextAfter(tag, '\n');
3735
},
3836
});
3937
} else if (lineDifference === 0) {
40-
context.report({
41-
messageId: 'always',
38+
report({
39+
message: 'Expected blank line before {{endTag}}.',
4240
loc: sibling && sibling.loc,
4341
// @ts-ignore
4442
fix(fixer) {
4543
const lastSpaces = /** @type {RegExpExecArray} */ (
46-
/^\s*/.exec(context.getSourceCode().lines[endTag.loc.start.line - 1])
44+
/^\s*/.exec(eslintUtil.getSourceCode().lines[endTag.loc.start.line - 1])
4745
)[0];
4846

4947
return fixer.insertTextAfter(endTag, `\n\n${lastSpaces}`);
@@ -52,25 +50,25 @@ function insertNewLine(context, tag, sibling, lineDifference) {
5250
}
5351
}
5452

55-
function removeExcessLines(context, endTag, sibling, lineDifference) {
53+
function removeExcessLines(endTag, sibling, lineDifference) {
5654
if (lineDifference > 1) {
5755
let hasOnlyTextBetween = true;
5856
for (
5957
let i = endTag.loc && endTag.loc.start.line;
6058
i < sibling.loc.start.line - 1 && hasOnlyTextBetween;
6159
i++
6260
) {
63-
hasOnlyTextBetween = !/^\s*$/.test(context.getSourceCode().lines[i]);
61+
hasOnlyTextBetween = !/^\s*$/.test(eslintUtil.getSourceCode().lines[i]);
6462
}
6563
if (!hasOnlyTextBetween) {
66-
context.report({
64+
report({
6765
messageId: 'never',
6866
loc: sibling && sibling.loc,
6967
// @ts-ignore
7068
fix(fixer) {
7169
const start = endTag.range[1];
7270
const end = sibling.range[0];
73-
const paddingText = context.getSourceCode().text.slice(start, end);
71+
const paddingText = eslintUtil.getSourceCode().text.slice(start, end);
7472
const textBetween = splitLines(paddingText);
7573
let newTextBetween = `\n${textBetween.pop()}`;
7674
for (let i = textBetween.length - 1; i >= 0; i--) {
@@ -87,7 +85,7 @@ function removeExcessLines(context, endTag, sibling, lineDifference) {
8785
}
8886
}
8987

90-
function checkNewLine(context, configureList) {
88+
function checkNewLine(configureList) {
9189
const firstConsistentBlankLines = new Map();
9290

9391
const reverseConfigureList = [...configureList].reverse();
@@ -143,15 +141,16 @@ function checkNewLine(context, configureList) {
143141
}
144142

145143
if (blankLine === 'always') {
146-
insertNewLine(context, node, closestSibling, lineDifference);
144+
insertNewLine(node, closestSibling, lineDifference);
147145
} else {
148-
removeExcessLines(context, endTag, closestSibling, lineDifference);
146+
removeExcessLines(endTag, closestSibling, lineDifference);
149147
}
150148
};
151149
}
152150

153151
/** @type {import('eslint').Rule.RuleModule} */
154152
module.exports = {
153+
// eslint-disable-next-line eslint-plugin/prefer-message-ids
155154
meta: {
156155
docs: {
157156
description: 'Enforce no padding lines between tags for React Components',
@@ -160,7 +159,6 @@ module.exports = {
160159
url: docsUrl('padding-lines-between-tags'),
161160
},
162161
fixable: 'code',
163-
messages,
164162
schema: [
165163
{
166164
type: 'array',
@@ -183,7 +181,7 @@ module.exports = {
183181
{ blankLine: 'always', prev: '*', next: '*' },
184182
];
185183
return {
186-
JSXElement: checkNewLine(context, configureList),
184+
JSXElement: checkNewLine(configureList),
187185
};
188186
},
189187
};

0 commit comments

Comments
 (0)