-
-
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
Changes from 7 commits
a6e39d0
0922c56
cc34480
9de7c73
5b2e1d4
bda68ce
c8ee193
7def79f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict'; | ||
|
||
const INLINE_ELEMENTS = [ | ||
'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.indexOf(elementName(node)) !== -1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be made into a Set instead? |
||
); | ||
|
||
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; | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 |
||
</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'} | ||
] | ||
}] | ||
}); |
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.
could you add a comment here that links to the source of this list?