Skip to content

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 8 commits into from
Dec 1, 2017
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
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const allRules = {
'display-name': require('./lib/rules/display-name'),
'forbid-component-props': require('./lib/rules/forbid-component-props'),
'forbid-elements': require('./lib/rules/forbid-elements'),
'forbid-prop-types': require('./lib/rules/forbid-prop-types'),
'forbid-foreign-prop-types': require('./lib/rules/forbid-foreign-prop-types'),
'forbid-prop-types': require('./lib/rules/forbid-prop-types'),
'jsx-boolean-value': require('./lib/rules/jsx-boolean-value'),
'jsx-child-element-spacing': require('./lib/rules/jsx-child-element-spacing'),
'jsx-closing-bracket-location': require('./lib/rules/jsx-closing-bracket-location'),
'jsx-closing-tag-location': require('./lib/rules/jsx-closing-tag-location'),
'jsx-curly-spacing': require('./lib/rules/jsx-curly-spacing'),
Expand All @@ -23,12 +24,12 @@ const allRules = {
'jsx-indent-props': require('./lib/rules/jsx-indent-props'),
'jsx-key': require('./lib/rules/jsx-key'),
'jsx-max-props-per-line': require('./lib/rules/jsx-max-props-per-line'),
'jsx-one-expression-per-line': require('./lib/rules/jsx-one-expression-per-line'),
'jsx-no-bind': require('./lib/rules/jsx-no-bind'),
'jsx-no-comment-textnodes': require('./lib/rules/jsx-no-comment-textnodes'),
'jsx-no-duplicate-props': require('./lib/rules/jsx-no-duplicate-props'),
'jsx-no-literals': require('./lib/rules/jsx-no-literals'),
'jsx-no-target-blank': require('./lib/rules/jsx-no-target-blank'),
'jsx-one-expression-per-line': require('./lib/rules/jsx-one-expression-per-line'),
'button-has-type': require('./lib/rules/button-has-type'),
'jsx-no-undef': require('./lib/rules/jsx-no-undef'),
'jsx-curly-brace-presence': require('./lib/rules/jsx-curly-brace-presence'),
Expand Down
103 changes: 103 additions & 0 deletions lib/rules/jsx-child-element-spacing.js
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;
});
}
};
}
};
196 changes: 196 additions & 0 deletions tests/lib/rules/jsx-child-element-spacing.js
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>
Copy link
Member

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?

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 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'}
]
}]
});