Skip to content

Create vue/multiline-ternary extension rule #1996

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 17 commits into from
Oct 10, 2022
19 changes: 19 additions & 0 deletions docs/rules/multiline-ternary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/multiline-ternary
description: Enforce newlines between operands of ternary expressions in `<template>`
---
# vue/multiline-ternary

- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

This rule is the same rule as core [multiline-ternary] rule but it applies to the expressions in `<template>`.

## :books: Further Reading

- [multiline-ternary]

[multiline-ternary]: https://eslint.org/docs/rules/multiline-ternary
16 changes: 16 additions & 0 deletions lib/rules/multiline-ternary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @author dev1437
* See LICENSE file in root directory for full license.
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule('multiline-ternary', {
skipDynamicArguments: true
})
143 changes: 143 additions & 0 deletions tests/lib/rules/multiline-ternary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* @author dev1437
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/multiline-ternary')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('multiline-ternary', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'test': someReallyLongCondition ?
aVeryLongOutput :
thisCantFitOnASingleLine
}">
</div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine
}">
</div>
</template>
`,
options: ['never']
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'test': someReallyLongCondition ?
aVeryLongOutput : thisCantFitOnASingleLine
}">
</div>
</template>
`,
output: `
<template>
<div :class="{
'test': someReallyLongCondition ?
aVeryLongOutput
: thisCantFitOnASingleLine
}">
</div>
</template>
`,
errors: [
{
message:
'Expected newline between consequent and alternate of ternary expression.',
line: 5,
column: 13
}
],
options: ['always-multiline']
},
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'test': someReallyLongCondition ?
aVeryLongOutput : thisCantFitOnASingleLine
}">
</div>
</template>
`,
output: `
<template>
<div :class="{
'test': someReallyLongCondition ?aVeryLongOutput : thisCantFitOnASingleLine
}">
</div>
</template>
`,
errors: [
{
message:
'Unexpected newline between test and consequent of ternary expression.',
line: 4,
column: 19
}
],
options: ['never']
},
{
filename: 'test.vue',
code: `
<template>
<div :class="{
'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine
}">
</div>
</template>
`,
output: `
<template>
<div :class="{
'test': someReallyLongCondition
? aVeryLongOutput
: thisCantFitOnASingleLine
}">
</div>
</template>
`,
errors: [
{
message:
'Expected newline between test and consequent of ternary expression.',
line: 4,
column: 19
},
{
message:
'Expected newline between consequent and alternate of ternary expression.',
line: 4,
column: 45
}
]
}
]
})