Skip to content

Commit

Permalink
* Remove ecmaScript rule
Browse files Browse the repository at this point in the history
* Remove rest/spread on import
* Add fixer
  • Loading branch information
Ross Solomon committed May 20, 2017
1 parent b863f9f commit 29c659e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
50 changes: 47 additions & 3 deletions lib/rules/jsx-closing-tag-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,39 @@
module.exports = {
meta: {
docs: {
description: 'Validate closing tag location in JSX',
description: 'Validate closing tag location for multiline JSX',
category: 'Stylistic Issues',
recommended: false
}
},
fixable: 'whitespace'
},

create: function(context) {
var sourceCode = context.getSourceCode();

/**
* Checks if the node is the first in its line, excluding whitespace.
* @param {ASTNode} node The node to check
* @return {Boolean} true if its the first node in its line
*/
function isNodeFirstInLine(node) {
let token = node;
let lines;
do {
token = sourceCode.getTokenBefore(token);
lines = token.type === 'JSXText'
? token.value.split('\n')
: null;
} while (
token.type === 'JSXText' &&
/^\s*$/.test(lines[lines.length - 1])
);

var startLine = node.loc.start.line;
var endLine = token ? token.loc.end.line : -1;
return startLine !== endLine;
}

return {
JSXClosingElement: function(node) {
if (!node.parent) {
Expand All @@ -32,10 +58,28 @@ module.exports = {
return;
}

let message;
if (!isNodeFirstInLine(node)) {
message = 'Closing tag of a multiline JSX expression must be on its own line.';
} else {
message = 'Expected closing tag to match indentation of opening.';
}

context.report({
node: node,
loc: node.loc,
message: 'Expected closing tag to match indentation of opening.'
message,
fix: function(fixer) {
const indent = Array(opening.loc.start.column + 1).join(' ');
if (isNodeFirstInLine(node)) {
return fixer.replaceTextRange(
[node.start - node.loc.start.column, node.start],
indent
);
}

return fixer.insertTextBefore(node, `\n${indent}`);
}
});
}
};
Expand Down
16 changes: 13 additions & 3 deletions tests/lib/rules/jsx-closing-tag-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/jsx-closing-tag-location');
const {RuleTester} = require('eslint');
const RuleTester = require('eslint').RuleTester;
const parserOptions = {
ecmaVersion: 8,
sourceType: 'module',
ecmaFeatures: {
experimentalObjectRestSpread: true,
Expand All @@ -20,6 +19,7 @@ const parserOptions = {
};

const MESSAGE_MATCH_INDENTATION = [{message: 'Expected closing tag to match indentation of opening.'}];
const MESSAGE_OWN_LINE = [{message: 'Closing tag of a multiline JSX expression must be on its own line.'}];

// ------------------------------------------------------------------------------
// Tests
Expand All @@ -45,12 +45,22 @@ ruleTester.run('jsx-closing-tag-location', rule, {
' foo',
' </App>'
].join('\n'),
output: [
'<App>',
' foo',
'</App>'
].join('\n'),
errors: MESSAGE_MATCH_INDENTATION
}, {
code: [
'<App>',
' foo</App>'
].join('\n'),
errors: MESSAGE_MATCH_INDENTATION
output: [
'<App>',
' foo',
'</App>'
].join('\n'),
errors: MESSAGE_OWN_LINE
}]
});

0 comments on commit 29c659e

Please sign in to comment.