Skip to content

Commit b863f9f

Browse files
author
Ross Solomon
committed
Add indentation rule for closing tag of multi-line jsx
1 parent a8d4c9b commit b863f9f

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var allRules = {
4141
'jsx-indent-props': require('./lib/rules/jsx-indent-props'),
4242
'jsx-indent': require('./lib/rules/jsx-indent'),
4343
'jsx-closing-bracket-location': require('./lib/rules/jsx-closing-bracket-location'),
44+
'jsx-closing-tag-location': require('./lib/rules/jsx-closing-tag-location'),
4445
'jsx-space-before-closing': require('./lib/rules/jsx-space-before-closing'),
4546
'no-direct-mutation-state': require('./lib/rules/no-direct-mutation-state'),
4647
'forbid-component-props': require('./lib/rules/forbid-component-props'),

lib/rules/jsx-closing-tag-location.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @fileoverview Validate closing tag location in JSX
3+
* @author Ross Solomon
4+
*/
5+
'use strict';
6+
7+
// ------------------------------------------------------------------------------
8+
// Rule Definition
9+
// ------------------------------------------------------------------------------
10+
module.exports = {
11+
meta: {
12+
docs: {
13+
description: 'Validate closing tag location in JSX',
14+
category: 'Stylistic Issues',
15+
recommended: false
16+
}
17+
},
18+
19+
create: function(context) {
20+
return {
21+
JSXClosingElement: function(node) {
22+
if (!node.parent) {
23+
return;
24+
}
25+
26+
const opening = node.parent.openingElement;
27+
if (opening.loc.start.line === node.loc.start.line) {
28+
return;
29+
}
30+
31+
if (opening.loc.start.column === node.loc.start.column) {
32+
return;
33+
}
34+
35+
context.report({
36+
node: node,
37+
loc: node.loc,
38+
message: 'Expected closing tag to match indentation of opening.'
39+
});
40+
}
41+
};
42+
}
43+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @fileoverview Validate closing tag location in JSX
3+
* @author Ross Solomon
4+
*/
5+
'use strict';
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const rule = require('../../../lib/rules/jsx-closing-tag-location');
12+
const {RuleTester} = require('eslint');
13+
const parserOptions = {
14+
ecmaVersion: 8,
15+
sourceType: 'module',
16+
ecmaFeatures: {
17+
experimentalObjectRestSpread: true,
18+
jsx: true
19+
}
20+
};
21+
22+
const MESSAGE_MATCH_INDENTATION = [{message: 'Expected closing tag to match indentation of opening.'}];
23+
24+
// ------------------------------------------------------------------------------
25+
// Tests
26+
// ------------------------------------------------------------------------------
27+
28+
const ruleTester = new RuleTester({parserOptions});
29+
ruleTester.run('jsx-closing-tag-location', rule, {
30+
valid: [{
31+
code: [
32+
'<App>',
33+
' foo',
34+
'</App>'
35+
].join('\n')
36+
}, {
37+
code: [
38+
'<App>foo</App>'
39+
].join('\n')
40+
}],
41+
42+
invalid: [{
43+
code: [
44+
'<App>',
45+
' foo',
46+
' </App>'
47+
].join('\n'),
48+
errors: MESSAGE_MATCH_INDENTATION
49+
}, {
50+
code: [
51+
'<App>',
52+
' foo</App>'
53+
].join('\n'),
54+
errors: MESSAGE_MATCH_INDENTATION
55+
}]
56+
});

0 commit comments

Comments
 (0)