Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsx-closing-bracket-atomic rule (Fixes #693) #706

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var rules = {
'jsx-no-literals': require('./lib/rules/jsx-no-literals'),
'jsx-indent-props': require('./lib/rules/jsx-indent-props'),
'jsx-indent': require('./lib/rules/jsx-indent'),
'jsx-closing-bracket-atomic': require('./lib/rules/jsx-closing-bracket-atomic'),
'jsx-closing-bracket-location': require('./lib/rules/jsx-closing-bracket-location'),
'jsx-space-before-closing': require('./lib/rules/jsx-space-before-closing'),
'no-direct-mutation-state': require('./lib/rules/no-direct-mutation-state'),
Expand Down
40 changes: 40 additions & 0 deletions lib/rules/jsx-closing-bracket-atomic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @fileoverview Ensures the "/>" and "</" closing indicators are written as a single token
* @author Diogo Franco (Kovensky)
*/
'use strict';

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------

var MESSAGE_SELF_CLOSING = 'Self-closing JSX elements must not have whitespace between the `/` and the `>`';
var MESSAGE_CLOSING = 'JSX closing tags must not have whitespace between the `<` and the `/`';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = function(context) {
return {
JSXOpeningElement: function(node) {
if (node.selfClosing) {
var sourceCode = context.getSourceCode();
var sourceText = sourceCode.getText(node);
if (sourceText.lastIndexOf('/>') !== sourceText.length - 2) {
context.report(node, MESSAGE_SELF_CLOSING);
}
}
},
JSXClosingElement: function(node) {
var sourceCode = context.getSourceCode();
var sourceText = sourceCode.getText(node);
if (sourceText.indexOf('</') !== 0) {
context.report(node, MESSAGE_CLOSING);
}
}
};

};

module.exports.schema = [];
57 changes: 57 additions & 0 deletions tests/lib/rules/jsx-closing-bracket-atomic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @fileoverview Tests for jsx-closing-bracket-atomic
* @author Diogo Franco (Kovensky)
*/

'use strict';

// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------

var rule = require('../../../lib/rules/jsx-closing-bracket-atomic');
var RuleTester = require('eslint').RuleTester;

var parserOptions = {
ecmaVersion: 6,
ecmaFeatures: {
jsx: true
}
};

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run('jsx-closing-bracket-atomic', rule, {
valid: [
{code: '<App/>;', parserOptions: parserOptions},
{code: '<App />;', parserOptions: parserOptions},
{code: '<div className="bar"></div>;', parserOptions: parserOptions},
{code: '<div className="bar"></ div>;', parserOptions: parserOptions}
],
invalid: [{
code: '<App/ >;',
errors: [{message: 'Self-closing JSX elements must not have whitespace between the `/` and the `>`'}],
parserOptions: parserOptions
}, {
code: [
'<App/',
'>'
].join('\n'),
errors: [{message: 'Self-closing JSX elements must not have whitespace between the `/` and the `>`'}],
parserOptions: parserOptions
}, {
code: '<div className="bar">< /div>;',
errors: [{message: 'JSX closing tags must not have whitespace between the `<` and the `/`'}],
parserOptions: parserOptions
}, {
code: [
'<div className="bar"><',
'/div>;'
].join('\n'),
errors: [{message: 'JSX closing tags must not have whitespace between the `<` and the `/`'}],
parserOptions: parserOptions
}]
});