-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add indentation rule for closing tag of multi-line jsx
- Loading branch information
Ross Solomon
committed
May 20, 2017
1 parent
a8d4c9b
commit b863f9f
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @fileoverview Validate closing tag location in JSX | ||
* @author Ross Solomon | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Validate closing tag location in JSX', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
} | ||
}, | ||
|
||
create: function(context) { | ||
return { | ||
JSXClosingElement: function(node) { | ||
if (!node.parent) { | ||
return; | ||
} | ||
|
||
const opening = node.parent.openingElement; | ||
if (opening.loc.start.line === node.loc.start.line) { | ||
return; | ||
} | ||
|
||
if (opening.loc.start.column === node.loc.start.column) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node, | ||
loc: node.loc, | ||
message: 'Expected closing tag to match indentation of opening.' | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileoverview Validate closing tag location in JSX | ||
* @author Ross Solomon | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/jsx-closing-tag-location'); | ||
const {RuleTester} = require('eslint'); | ||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
const MESSAGE_MATCH_INDENTATION = [{message: 'Expected closing tag to match indentation of opening.'}]; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('jsx-closing-tag-location', rule, { | ||
valid: [{ | ||
code: [ | ||
'<App>', | ||
' foo', | ||
'</App>' | ||
].join('\n') | ||
}, { | ||
code: [ | ||
'<App>foo</App>' | ||
].join('\n') | ||
}], | ||
|
||
invalid: [{ | ||
code: [ | ||
'<App>', | ||
' foo', | ||
' </App>' | ||
].join('\n'), | ||
errors: MESSAGE_MATCH_INDENTATION | ||
}, { | ||
code: [ | ||
'<App>', | ||
' foo</App>' | ||
].join('\n'), | ||
errors: MESSAGE_MATCH_INDENTATION | ||
}] | ||
}); |