Repository: vuejs/eslint-plugin-vue
Pull request #2066 (2022-12-22 08:43:24)

Added array-element-newline rule from eslint
Hello,
I'm currently using `eslint-plugin-vue` and it works great !
Even though it's not advised we depends exclusively on eslint for code style and format.

There is one rule in template I'd like to see: https://eslint.org/docs/latest/rules/array-element-newline that why I propose to you this pull request.

I've read the https://eslint.vuejs.org/developer-guide/ but this is my first pull request so let me know if I miss something.



Patch:
diff --git a/docs/rules/array-element-newline.md b/docs/rules/array-element-newline.md
new file mode 100644
index 00000000..024dcafe
--- /dev/null
+++ b/docs/rules/array-element-newline.md
@@ -0,0 +1,31 @@
+---
+pageClass: rule-details
+sidebarDepth: 0
+title: vue/array-element-newline
+description: Enforce line breaks after each array element in `<template>`
+---
+# vue/array-element-newline
+
+> Enforce line breaks after each array element 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 [array-element-newline] rule but it applies to the expressions in `<template>`.
+
+## :books: Further Reading
+
+- [array-bracket-spacing]
+
+[array-bracket-spacing]: https://eslint.org/docs/rules/array-bracket-spacing
+
+- [array-bracket-newline]
+
+[array-bracket-newline]: https://eslint.org/docs/rules/array-bracket-newline
+
+## :mag: Implementation
+
+- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/array-element-newline.js)
+- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/array-element-newline.js)
+
+<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/array-element-newline)</sup>
diff --git a/docs/rules/index.md b/docs/rules/index.md
index 224b4031..2e21edc5 100644
--- a/docs/rules/index.md
+++ b/docs/rules/index.md
@@ -279,6 +279,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
 |:--------|:------------|:--:|:--:|
 | [vue/array-bracket-newline](./array-bracket-newline.md) | Enforce linebreaks after opening and before closing array brackets in `<template>` | :wrench: | :lipstick: |
 | [vue/array-bracket-spacing](./array-bracket-spacing.md) | Enforce consistent spacing inside array brackets in `<template>` | :wrench: | :lipstick: |
+| [vue/array-element-newline](./array-element-newline.md) | Enforce line breaks after each array element in `<template>` | :wrench: | :lipstick: |
 | [vue/arrow-spacing](./arrow-spacing.md) | Enforce consistent spacing before and after the arrow in arrow functions in `<template>` | :wrench: | :lipstick: |
 | [vue/block-spacing](./block-spacing.md) | Disallow or enforce spaces inside of blocks after opening block and before closing block in `<template>` | :wrench: | :lipstick: |
 | [vue/brace-style](./brace-style.md) | Enforce consistent brace style for blocks in `<template>` | :wrench: | :lipstick: |
diff --git a/lib/configs/no-layout-rules.js b/lib/configs/no-layout-rules.js
index 58e62b60..b1486f15 100644
--- a/lib/configs/no-layout-rules.js
+++ b/lib/configs/no-layout-rules.js
@@ -7,6 +7,7 @@ module.exports = {
   rules: {
     'vue/array-bracket-newline': 'off',
     'vue/array-bracket-spacing': 'off',
+    'vue/array-element-newline': 'off',
     'vue/arrow-spacing': 'off',
     'vue/block-spacing': 'off',
     'vue/block-tag-newline': 'off',
diff --git a/lib/index.js b/lib/index.js
index 2d14cd40..f5da9a70 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -9,6 +9,7 @@ module.exports = {
   rules: {
     'array-bracket-newline': require('./rules/array-bracket-newline'),
     'array-bracket-spacing': require('./rules/array-bracket-spacing'),
+    'array-element-newline': require('./rules/array-element-newline'),
     'arrow-spacing': require('./rules/arrow-spacing'),
     'attribute-hyphenation': require('./rules/attribute-hyphenation'),
     'attributes-order': require('./rules/attributes-order'),
diff --git a/lib/rules/array-element-newline.js b/lib/rules/array-element-newline.js
new file mode 100644
index 00000000..251d72de
--- /dev/null
+++ b/lib/rules/array-element-newline.js
@@ -0,0 +1,11 @@
+/**
+ * @author alshyra
+ */
+'use strict'
+
+const { wrapCoreRule } = require('../utils')
+
+// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
+module.exports = wrapCoreRule('array-element-newline', {
+  skipDynamicArguments: true
+})


Test patch:
diff --git a/tests/lib/rules/array-element-newline.js b/tests/lib/rules/array-element-newline.js
new file mode 100644
index 00000000..54e60f83
--- /dev/null
+++ b/tests/lib/rules/array-element-newline.js
@@ -0,0 +1,270 @@
+/**
+ * @author alshyra
+ * See LICENSE file in root directory for full license.
+ */
+'use strict'
+
+const RuleTester = require('eslint').RuleTester
+const rule = require('../../../lib/rules/array-element-newline')
+
+const tester = new RuleTester({
+  parser: require.resolve('vue-eslint-parser'),
+  parserOptions: {
+    ecmaVersion: 2020,
+    sourceType: 'module'
+  }
+})
+
+tester.run('array-element-newline', rule, {
+  valid: [
+    '<template><div :attr="[]" /></template>',
+    '<template><div :attr="[a]" /></template>',
+    `
+      <template>
+        <div :attr="[a,
+                     b,
+                     c]" />
+      </template>`,
+    `<template>
+      <div :attr="[a,
+                   b,
+                   c
+                  ]" />
+    </template>`,
+    '<template><div :[attr]="a" /></template>',
+    '<template><div :[[attr]]="a" /></template>',
+    `<template>
+      <div :attr="[
+                   a,
+                   b,
+                   c
+                  ]" />
+    </template>`,
+    `
+      <template>
+        <div :attr="[a
+                     b]" />
+      </template>`,
+    {
+      code: `
+        <template>
+          <div :attr="[a,
+                       b,
+                       c]" />
+        </template>`,
+      options: ['always']
+    },
+    {
+      code: '<template><div :attr="[a]" /></template>',
+      options: ['never']
+    },
+    {
+      code: '<template><div :attr="[a,b,c]" /></template>',
+      options: ['never']
+    },
+    {
+      code: '<template><div :attr="[a, b, c]" /></template>',
+      options: [{ multiline: true }]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a, 
+                      {
+                        b:c
+                      }]" />
+        </template>`,
+      options: [{ multiline: true }]
+    },
+    {
+      code: '<template><div :attr="[a, b, c]" /></template>',
+      options: ['consistent']
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,
+                       b,
+                       c
+                      ]" />
+        </template>`,
+      options: ['consistent']
+    },
+    {
+      code: '<template><div :attr="[a,b]" /></template>',
+      options: [{ minItems: 3 }]
+    }
+  ],
+  invalid: [
+    {
+      code: `
+        <template>
+          <div :attr="[a, b]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+b]" />
+        </template>`,
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 26,
+          endLine: 3,
+          endColumn: 27
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,b,c]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+b,
+c]" />
+        </template>`,
+      options: ['always'],
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 26,
+          endLine: 3,
+          endColumn: 26
+        },
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 28,
+          endLine: 3,
+          endColumn: 28
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,
+                      b,c]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+                      b,
+c]" />
+        </template>`,
+      options: ['always'],
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 4,
+          column: 25,
+          endLine: 4,
+          endColumn: 25
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,
+                       b,c]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+                       b,
+c]" />
+        </template>`,
+      options: ['consistent'],
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 4,
+          column: 26,
+          endLine: 4,
+          endColumn: 26
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,
+                       b, c]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a, b, c]" />
+        </template>`,
+      options: [{ multiline: true }],
+      errors: [
+        {
+          message: 'There should be no linebreak here.',
+          line: 3,
+          column: 26,
+          endLine: 4,
+          endColumn: 24
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a, {
+                            b:c
+                          }]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+{
+                            b:c
+                          }]" />
+        </template>`,
+      options: [{ multiline: true }],
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 26,
+          endLine: 3,
+          endColumn: 27
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <div :attr="[a,b,c]" />
+        </template>`,
+      output: `
+        <template>
+          <div :attr="[a,
+b,
+c]" />
+        </template>`,
+      options: [{ minItems: 2 }],
+      errors: [
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 26,
+          endLine: 3,
+          endColumn: 26
+        },
+        {
+          message: 'There should be a linebreak after this element.',
+          line: 3,
+          column: 28,
+          endLine: 3,
+          endColumn: 28
+        }
+      ]
+    }
+  ]
+})