Skip to content

Commit b75d312

Browse files
authored
Add vue/array-bracket-newline rule (#1326)
* Add `vue/array-bracket-newline` rule `vue/array-bracket-newline` rule that applies `array-bracket-newline` rule to expressions in <template>. * update
1 parent bebb9b3 commit b75d312

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
326326

327327
| Rule ID | Description | |
328328
|:--------|:------------|:---|
329+
| [vue/array-bracket-newline](./array-bracket-newline.md) | enforce linebreaks after opening and before closing array brackets | :wrench: |
329330
| [vue/array-bracket-spacing](./array-bracket-spacing.md) | enforce consistent spacing inside array brackets | :wrench: |
330331
| [vue/arrow-spacing](./arrow-spacing.md) | enforce consistent spacing before and after the arrow in arrow functions | :wrench: |
331332
| [vue/block-spacing](./block-spacing.md) | disallow or enforce spaces inside of blocks after opening block and before closing block | :wrench: |

docs/rules/array-bracket-newline.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/array-bracket-newline
5+
description: enforce linebreaks after opening and before closing array brackets
6+
---
7+
# vue/array-bracket-newline
8+
> enforce linebreaks after opening and before closing array brackets
9+
10+
- :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.
11+
12+
This rule is the same rule as core [array-bracket-newline] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further Reading
15+
16+
- [array-bracket-newline]
17+
18+
[array-bracket-newline]: https://eslint.org/docs/rules/array-bracket-newline
19+
20+
## :mag: Implementation
21+
22+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/array-bracket-newline.js)
23+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/array-bracket-newline.js)
24+
25+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/array-bracket-newline)</sup>

lib/configs/no-layout-rules.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
module.exports = {
77
rules: {
8+
'vue/array-bracket-newline': 'off',
89
'vue/array-bracket-spacing': 'off',
910
'vue/arrow-spacing': 'off',
1011
'vue/block-spacing': 'off',

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module.exports = {
99
rules: {
10+
'array-bracket-newline': require('./rules/array-bracket-newline'),
1011
'array-bracket-spacing': require('./rules/array-bracket-spacing'),
1112
'arrow-spacing': require('./rules/arrow-spacing'),
1213
'attribute-hyphenation': require('./rules/attribute-hyphenation'),

lib/rules/array-bracket-newline.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule('array-bracket-newline', {
10+
skipDynamicArguments: true
11+
})
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/array-bracket-newline')
8+
9+
const tester = new RuleTester({
10+
parser: require.resolve('vue-eslint-parser'),
11+
parserOptions: { ecmaVersion: 2015 }
12+
})
13+
14+
tester.run('array-bracket-newline', rule, {
15+
valid: [
16+
'<template><div :attr="[a]" /></template>',
17+
'<template><div :attr="[\na,\nb,\nc\n]" /></template>',
18+
{
19+
code: '<template><div :attr="[a]" /></template>',
20+
options: ['never']
21+
},
22+
{
23+
code: '<template><div :attr="[\na\n]" /></template>',
24+
options: ['always']
25+
},
26+
'<template><div :[attr]="a" /></template>',
27+
{
28+
code: '<template><div :[attr]="a" /></template>',
29+
options: ['always']
30+
},
31+
'<template><div :[[attr]]="a" /></template>',
32+
{
33+
code: '<template><div :[[attr]]="a" /></template>',
34+
options: ['always']
35+
}
36+
],
37+
invalid: [
38+
{
39+
code: '<template><div :attr="[\na]" /></template>',
40+
output: '<template><div :attr="[a]" /></template>',
41+
errors: ["There should be no linebreak after '['."]
42+
},
43+
{
44+
code: '<template><div :attr="[a\n]" /></template>',
45+
output: '<template><div :attr="[a]" /></template>',
46+
errors: ["There should be no linebreak before ']'."]
47+
},
48+
{
49+
code: '<template><div :attr="[\na\n]" /></template>',
50+
output: '<template><div :attr="[a]" /></template>',
51+
errors: [
52+
"There should be no linebreak after '['.",
53+
"There should be no linebreak before ']'."
54+
]
55+
},
56+
{
57+
code: '<template><div :attr="[\na]" /></template>',
58+
options: ['never'],
59+
output: '<template><div :attr="[a]" /></template>',
60+
errors: ["There should be no linebreak after '['."]
61+
},
62+
{
63+
code: '<template><div :attr="[a\n]" /></template>',
64+
options: ['never'],
65+
output: '<template><div :attr="[a]" /></template>',
66+
errors: ["There should be no linebreak before ']'."]
67+
},
68+
{
69+
code: '<template><div :attr="[\na\n]" /></template>',
70+
options: ['never'],
71+
output: '<template><div :attr="[a]" /></template>',
72+
errors: [
73+
"There should be no linebreak after '['.",
74+
"There should be no linebreak before ']'."
75+
]
76+
},
77+
{
78+
code: '<template><div :attr="[\na]" /></template>',
79+
options: ['always'],
80+
output: '<template><div :attr="[\na\n]" /></template>',
81+
errors: ["A linebreak is required before ']'."]
82+
},
83+
{
84+
code: '<template><div :attr="[a\n]" /></template>',
85+
options: ['always'],
86+
output: '<template><div :attr="[\na\n]" /></template>',
87+
errors: ["A linebreak is required after '['."]
88+
},
89+
{
90+
code: '<template><div :attr="[a]" /></template>',
91+
options: ['always'],
92+
output: '<template><div :attr="[\na\n]" /></template>',
93+
errors: [
94+
"A linebreak is required after '['.",
95+
"A linebreak is required before ']'."
96+
]
97+
},
98+
{
99+
code: '<template><div :[[attr]]="[a]" /></template>',
100+
options: ['always'],
101+
output: '<template><div :[[attr]]="[\na\n]" /></template>',
102+
errors: [
103+
"A linebreak is required after '['.",
104+
"A linebreak is required before ']'."
105+
]
106+
}
107+
]
108+
})

0 commit comments

Comments
 (0)