Skip to content

Commit

Permalink
[New] jsx-one-expression-per-line: add non-jsx option to allow no…
Browse files Browse the repository at this point in the history
…n-JSX children in one line
  • Loading branch information
burtek committed Jan 11, 2024
1 parent 3730edb commit e8cef50
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/rules/jsx-one-expression-per-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,11 @@ Examples of **correct** code for this rule, when configured as `"single-child"`:

<App><Hello /></App>
```

Examples of **correct** code for this rule, when configured as `"non-jsx"`:

```jsx
<App>Hello {someVariable}</App>

<App>Hello {<Hello />} there!</App>
```
7 changes: 6 additions & 1 deletion lib/rules/jsx-one-expression-per-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
type: 'object',
properties: {
allow: {
enum: ['none', 'literal', 'single-child'],
enum: ['none', 'literal', 'single-child', 'non-jsx'],
},
},
default: optionDefaults,
Expand All @@ -65,6 +65,11 @@ module.exports = {
return;
}

if (options.allow === 'non-jsx'
&& !children.find((child) => (child.type === 'JSXFragment' || child.type === 'JSXElement'))) {
return;
}

const openingElement = node.openingElement || node.openingFragment;
const closingElement = node.closingElement || node.closingFragment;
const openingElementStartLine = openingElement.loc.start.line;
Expand Down
66 changes: 66 additions & 0 deletions tests/lib/rules/jsx-one-expression-per-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
code: '<App>{"foo"}</App>',
options: [{ allow: 'single-child' }],
},
{
code: '<App>123</App>',
options: [{ allow: 'non-jsx' }],
},
{
code: '<App>foo</App>',
options: [{ allow: 'non-jsx' }],
},
{
code: '<App>{"foo"}</App>',
options: [{ allow: 'non-jsx' }],
},
{
code: '<App>{<Bar />}</App>',
options: [{ allow: 'non-jsx' }],
},
{
code: '<App>{foo && <Bar />}</App>',
options: [{ allow: 'single-child' }],
Expand Down Expand Up @@ -184,6 +200,17 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
`,
features: ['fragment', 'no-ts-old'], // TODO: FIXME: remove no-ts-old and fix
},
{
code: '<App>Hello {name}</App>',
options: [{ allow: 'non-jsx' }],
},
{
code: `
<App>
Hello {name} there!
</App>`,
options: [{ allow: 'non-jsx' }],
},
]),

invalid: parsers.all([
Expand Down Expand Up @@ -493,6 +520,28 @@ foo
],
parserOptions,
},
{
code: `
<Text style={styles.foo}>
<Bar /> <Baz />
</Text>
`,
output: `
<Text style={styles.foo}>
<Bar />${' '/* intentional trailing space */}
{' '}
<Baz />
</Text>
`,
errors: [
{
messageId: 'moveToNewLine',
data: { descriptor: 'Baz' },
},
],
options: [{ allow: 'non-jsx' }],
parserOptions,
},
{
code: `
<Text style={styles.foo}>
Expand Down Expand Up @@ -1257,6 +1306,23 @@ foo
},
],
},
{
code: `
<App><Foo /></App>
`,
output: `
<App>
<Foo />
</App>
`,
options: [{ allow: 'non-jsx' }],
errors: [
{
messageId: 'moveToNewLine',
data: { descriptor: 'Foo' },
},
],
},
{
code: `
<App
Expand Down

0 comments on commit e8cef50

Please sign in to comment.