-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: jsx-child-element-spacing proposal #1519
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a6e39d0
Failing tests for jsx-child-element-spacing proposal
pfhayes 0922c56
Add some more test cases
pfhayes cc34480
Temp commit
pfhayes 9de7c73
Cleanup
pfhayes 5b2e1d4
Remove some cases
pfhayes bda68ce
Merge remote-tracking branch 'upstream/master' into spaces
pfhayes c8ee193
Avoid incldues
pfhayes 7def79f
Review comments
pfhayes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
'use strict'; | ||
|
||
// This list is taken from https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements | ||
const INLINE_ELEMENTS = new Set([ | ||
'a', | ||
'abbr', | ||
'acronym', | ||
'b', | ||
'bdo', | ||
'big', | ||
'br', | ||
'button', | ||
'cite', | ||
'code', | ||
'dfn', | ||
'em', | ||
'i', | ||
'img', | ||
'input', | ||
'kbd', | ||
'label', | ||
'map', | ||
'object', | ||
'q', | ||
'samp', | ||
'script', | ||
'select', | ||
'small', | ||
'span', | ||
'strong', | ||
'sub', | ||
'sup', | ||
'textarea', | ||
'tt', | ||
'var' | ||
]); | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Ensures inline tags are not rendered without spaces between them', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
fixable: false, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: {}, | ||
default: {}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
create: function (context) { | ||
const elementName = node => ( | ||
node.openingElement && | ||
node.openingElement.name && | ||
node.openingElement.name.type === 'JSXIdentifier' && | ||
node.openingElement.name.name | ||
); | ||
|
||
const isInlineElement = node => ( | ||
node.type === 'JSXElement' && | ||
INLINE_ELEMENTS.has(elementName(node)) | ||
); | ||
|
||
const TEXT_FOLLOWING_ELEMENT_PATTERN = /^\s*\n\s*\S/; | ||
const TEXT_PRECEDING_ELEMENT_PATTERN = /\S\s*\n\s*$/; | ||
|
||
return { | ||
JSXElement: function(node) { | ||
let lastChild = null; | ||
let child = null; | ||
(node.children.concat([null])).forEach(nextChild => { | ||
if ( | ||
(lastChild || nextChild) && | ||
(!lastChild || isInlineElement(lastChild)) && | ||
(child && child.type === 'Literal') && | ||
(!nextChild || isInlineElement(nextChild)) && | ||
true | ||
) { | ||
if (lastChild && child.value.match(TEXT_FOLLOWING_ELEMENT_PATTERN)) { | ||
context.report({ | ||
node: child, | ||
loc: child.loc, | ||
message: `Ambiguous spacing after previous element ${elementName(lastChild)}` | ||
}); | ||
} else if (nextChild && child.value.match(TEXT_PRECEDING_ELEMENT_PATTERN)) { | ||
context.report({ | ||
node: child, | ||
loc: child.loc, | ||
message: `Ambiguous spacing before next element ${elementName(nextChild)}` | ||
}); | ||
} | ||
} | ||
lastChild = child; | ||
child = nextChild; | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
This file contains hidden or 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,196 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/jsx-child-element-spacing'); | ||
const RuleTester = require('eslint').RuleTester; | ||
const parserOptions = { | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}; | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('jsx-child-element-spacing', rule, { | ||
valid: [{ | ||
code: ` | ||
<App> | ||
foo | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<a>bar</a> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<a> | ||
<b>nested</b> | ||
</a> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo | ||
bar | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo<a>bar</a>baz | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo | ||
{' '} | ||
<a>bar</a> | ||
{' '} | ||
baz | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo | ||
{' '}<a>bar</a>{' '} | ||
baz | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo{' '} | ||
<a>bar</a> | ||
{' '}baz | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
foo{/* | ||
*/}<a>bar</a>{/* | ||
*/}baz | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
Please take a look at <a href="https://js.org">this link</a>. | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
Please take a look at | ||
{' '} | ||
<a href="https://js.org">this link</a>. | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<p>A</p> | ||
<p>B</p> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<p>A</p><p>B</p> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<a>foo</a> | ||
<a>bar</a> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
<a> | ||
<b>nested1</b> | ||
<b>nested2</b> | ||
</a> | ||
</App> | ||
` | ||
}, { | ||
code: ` | ||
<App> | ||
A | ||
B | ||
</App> | ||
` | ||
}], | ||
|
||
invalid: [{ | ||
code: ` | ||
<App> | ||
foo | ||
<a>bar</a> | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing before next element a'} | ||
] | ||
}, { | ||
code: ` | ||
<App> | ||
<a>bar</a> | ||
baz | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing after previous element a'} | ||
] | ||
}, { | ||
code: ` | ||
<App> | ||
{' '}<a>bar</a> | ||
baz | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing after previous element a'} | ||
] | ||
}, { | ||
code: ` | ||
<App> | ||
Please take a look at | ||
<a href="https://js.org">this link</a>. | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing before next element a'} | ||
] | ||
}, { | ||
code: ` | ||
<App> | ||
Some <code>loops</code> and some | ||
<code>if</code> statements. | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing before next element code'} | ||
] | ||
}, { | ||
code: ` | ||
<App> | ||
Here is | ||
<a href="https://js.org">a link</a> and here is | ||
<a href="https://js.org">another</a> | ||
</App> | ||
`, | ||
errors: [ | ||
{message: 'Ambiguous spacing before next element a'}, | ||
{message: 'Ambiguous spacing before next element a'} | ||
] | ||
}] | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would the output be if you wanted this autofixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think as you pointed out in the original issue, the intent is ambiguous so autofixing may not be feasible.
If we were to autofix, replacing with the comment form would maintain the semantic without too much disruption (though would perhaps be less aesthetically pleasing)
I would advocate for no automatic action since, in my experience, the developer most often intended to add a space, though I could be convinced otherwise