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

Repair --fix for props-aligned in jsx-closing-bracket-location #684

Merged
merged 3 commits into from
Jul 18, 2016
Merged
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
11 changes: 2 additions & 9 deletions lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ module.exports = function(context) {
'JSXOpeningElement:exit': function(node) {
var attributeNode = lastAttributeNode[getOpeningElementId(node)];
var cachedLastAttributeEndPos = attributeNode ? attributeNode.end : null;
var cachedLastAttributeStartPos = attributeNode ? attributeNode.start : null;
var expectedNextLine;
var tokens = getTokensLocations(node);
var expectedLocation = getExpectedLocation(tokens);
Expand Down Expand Up @@ -205,17 +204,11 @@ module.exports = function(context) {
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
(expectedNextLine ? '\n' : '') + closingTag);
case 'props-aligned':
var spaces = new Array(cachedLastAttributeEndPos - cachedLastAttributeStartPos);
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
'\n' + spaces.join(' ') + closingTag);
case 'tag-aligned':
var tagSpaces = new Array(+correctColumn + 1);
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
'\n' + tagSpaces.join(' ') + closingTag);
case 'line-aligned':
var lineSpaces = new Array(+correctColumn + 1);
var spaces = new Array(+correctColumn + 1);
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.end],
'\n' + lineSpaces.join(' ') + closingTag);
'\n' + spaces.join(' ') + closingTag);
default:
return true;
}
Expand Down
96 changes: 96 additions & 0 deletions tests/lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,102 @@ ruleTester.run('jsx-closing-bracket-location', rule, {
line: 2,
column: 8
}]
}, {
code: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
output: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
options: ['props-aligned'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add a test for the "line-aligned" option? We might want to add explicit tests for all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a similar test for line-aligned and tag-aligned

parserOptions: parserOptions,
errors: [{
message: messageWithDetails(MESSAGE_PROPS_ALIGNED, 7, false),
line: 6,
column: 37
}]
}, {
code: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
output: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
options: ['tag-aligned'],
parserOptions: parserOptions,
errors: [{
message: messageWithDetails(MESSAGE_TAG_ALIGNED, 5, false),
line: 6,
column: 37
}]
}, {
code: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
output: [
'const Button = function(props) {',
' return (',
' <Button',
' size={size}',
' onClick={onClick}',
' >',
' Button Text',
' </Button>',
' );',
'};'
].join('\n'),
options: ['line-aligned'],
parserOptions: parserOptions,
errors: [{
message: messageWithDetails(MESSAGE_LINE_ALIGNED, 5, false),
line: 6,
column: 37
}]
}, {
code: [
'<Provider',
Expand Down