-
-
Notifications
You must be signed in to change notification settings - Fork 679
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
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
908d4a4
Create initial multiline ternary rule
dev1437 9a65bc9
Docs
dev1437 1abefe2
Linting
dev1437 bc6f53c
Tests and apply Document
dev1437 0c54384
Fix import
dev1437 e3f4a0f
Fix tests
dev1437 18d5d7b
Fix test
dev1437 15eed56
Run npm run update
dev1437 c804614
Update tests
dev1437 1f059e0
lint
dev1437 00ce731
Update doc
dev1437 9417197
Add tests for script tag
dev1437 7fadb77
Update tests/lib/rules/multiline-ternary.js
dev1437 1bc02bd
Add example
dev1437 e0c7f09
Merge branch 'rule/multiline-ternary' of github.com:dev1437/eslint-pl…
dev1437 7a7a10d
Lint
dev1437 16e33a8
Merge branch 'master' into rule/multiline-ternary
dev1437 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,27 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/multiline-ternary | ||
description: Enforce newlines between operands of ternary expressions in `<template>` | ||
--- | ||
# vue/multiline-ternary | ||
|
||
> Enforce newlines between operands of ternary expressions in `<template>` | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :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. | ||
|
||
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 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/multiline-ternary.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/multiline-ternary.js) | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/multiline-ternary)</sup> |
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
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,17 @@ | ||
/** | ||
* @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, | ||
applyDocument: true | ||
}) |
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,276 @@ | ||
/** | ||
* @author dev1437 | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const { RuleTester, ESLint } = require('../../eslint-compat') | ||
const rule = require('../../../lib/rules/multiline-ternary') | ||
const semver = require('semver') | ||
|
||
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: [ | ||
...(semver.gte(ESLint.version, '7.1.0') | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? [ | ||
{ | ||
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 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div :style="{ | ||
'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine | ||
}"> | ||
</div> | ||
</template> | ||
`, | ||
output: ` | ||
<template> | ||
<div :style="{ | ||
'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 | ||
} | ||
] | ||
} | ||
] | ||
: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div :class="{ | ||
'test': someReallyLongCondition ? | ||
aVeryLongOutput : thisCantFitOnASingleLine | ||
}"> | ||
</div> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Expected newline between consequent and alternate of ternary expression.', | ||
line: 5, | ||
column: 15 | ||
} | ||
], | ||
options: ['always-multiline'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div :class="{ | ||
'test': someReallyLongCondition ? | ||
aVeryLongOutput : thisCantFitOnASingleLine | ||
}"> | ||
</div> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Unexpected newline between test and consequent of ternary expression.', | ||
line: 4, | ||
column: 21 | ||
} | ||
], | ||
options: ['never'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div :class="{ | ||
'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine | ||
}"> | ||
</div> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Expected newline between test and consequent of ternary expression.', | ||
line: 4, | ||
column: 21 | ||
}, | ||
{ | ||
message: | ||
'Expected newline between consequent and alternate of ternary expression.', | ||
line: 4, | ||
column: 47 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div :style="{ | ||
'test': someReallyLongCondition ? aVeryLongOutput : thisCantFitOnASingleLine | ||
}"> | ||
</div> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Expected newline between test and consequent of ternary expression.', | ||
line: 4, | ||
column: 21 | ||
}, | ||
{ | ||
message: | ||
'Expected newline between consequent and alternate of ternary expression.', | ||
line: 4, | ||
column: 47 | ||
} | ||
] | ||
} | ||
]) | ||
] | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.