Repository: vuejs/eslint-plugin-vue
Pull request #773 (2019-01-14 02:07:12)

⭐️New: Add `vue/comma-dangle` rule
This PR adds the wrapper rule of the `comma-dangle` core rule to apply to the expressions in `<template>`.


Patch:
diff --git a/docs/rules/README.md b/docs/rules/README.md
index caa45f6c..f7e7d7ec 100644
--- a/docs/rules/README.md
+++ b/docs/rules/README.md
@@ -144,6 +144,7 @@ For example:
 | [vue/block-spacing](./block-spacing.md) | disallow or enforce spaces inside of blocks after opening block and before closing block | :wrench: |
 | [vue/brace-style](./brace-style.md) | enforce consistent brace style for blocks | :wrench: |
 | [vue/camelcase](./camelcase.md) | enforce camelcase naming convention |  |
+| [vue/comma-dangle](./comma-dangle.md) | require or disallow trailing commas | :wrench: |
 | [vue/component-name-in-template-casing](./component-name-in-template-casing.md) | enforce specific casing for the component naming style in template | :wrench: |
 | [vue/eqeqeq](./eqeqeq.md) | require the use of `===` and `!==` | :wrench: |
 | [vue/key-spacing](./key-spacing.md) | enforce consistent spacing between keys and values in object literal properties | :wrench: |
diff --git a/docs/rules/comma-dangle.md b/docs/rules/comma-dangle.md
new file mode 100644
index 00000000..0ccdf151
--- /dev/null
+++ b/docs/rules/comma-dangle.md
@@ -0,0 +1,23 @@
+---
+pageClass: rule-details
+sidebarDepth: 0
+title: vue/comma-dangle
+description: require or disallow trailing commas
+---
+# vue/comma-dangle
+> require or disallow trailing commas
+
+- :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 [comma-dangle] rule but it applies to the expressions in `<template>`.
+
+## :books: Further reading
+
+- [comma-dangle]
+
+[comma-dangle]: https://eslint.org/docs/rules/comma-dangle
+
+## :mag: Implementation
+
+- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/comma-dangle.js)
+- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/comma-dangle.js)
diff --git a/lib/configs/no-layout-rules.js b/lib/configs/no-layout-rules.js
index d6bea10c..2b23f1e6 100644
--- a/lib/configs/no-layout-rules.js
+++ b/lib/configs/no-layout-rules.js
@@ -9,6 +9,7 @@ module.exports = {
     'vue/arrow-spacing': 'off',
     'vue/block-spacing': 'off',
     'vue/brace-style': 'off',
+    'vue/comma-dangle': 'off',
     'vue/html-closing-bracket-newline': 'off',
     'vue/html-closing-bracket-spacing': 'off',
     'vue/html-indent': 'off',
diff --git a/lib/index.js b/lib/index.js
index 3863dbe9..88173639 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -14,6 +14,7 @@ module.exports = {
     'block-spacing': require('./rules/block-spacing'),
     'brace-style': require('./rules/brace-style'),
     'camelcase': require('./rules/camelcase'),
+    'comma-dangle': require('./rules/comma-dangle'),
     'comment-directive': require('./rules/comment-directive'),
     'component-name-in-template-casing': require('./rules/component-name-in-template-casing'),
     'eqeqeq': require('./rules/eqeqeq'),
diff --git a/lib/rules/comma-dangle.js b/lib/rules/comma-dangle.js
new file mode 100644
index 00000000..cfd0a418
--- /dev/null
+++ b/lib/rules/comma-dangle.js
@@ -0,0 +1,9 @@
+/**
+ * @author Yosuke Ota
+ */
+'use strict'
+
+const { wrapCoreRule } = require('../utils')
+
+// eslint-disable-next-line
+module.exports = wrapCoreRule(require('eslint/lib/rules/comma-dangle'))


Test patch:
diff --git a/tests/lib/rules/comma-dangle.js b/tests/lib/rules/comma-dangle.js
new file mode 100644
index 00000000..aca71019
--- /dev/null
+++ b/tests/lib/rules/comma-dangle.js
@@ -0,0 +1,108 @@
+/**
+ * @author Yosuke Ota
+ */
+'use strict'
+
+const RuleTester = require('eslint').RuleTester
+const rule = require('../../../lib/rules/comma-dangle')
+
+const tester = new RuleTester({
+  parser: 'vue-eslint-parser',
+  parserOptions: { ecmaVersion: 2018 }
+})
+
+tester.run('comma-dangle', rule, {
+  valid: [
+    `<template>
+      <button @click="() => fn([a, b])" ></button>
+    </template>`,
+    {
+      code: `
+        <template>
+          <CustomButton @click="($event) => fn()" />
+        </template>`,
+      options: [{
+        'functions': 'never'
+      }]
+    },
+    {
+      code: `
+        <template>
+          <button @click="() => fn([a, b, ])" ></button>
+        </template>`,
+      options: [{
+        'arrays': 'ignore'
+      }]
+    }
+  ],
+  invalid: [
+    {
+      code: `
+        <template>
+          <button @click="() => fn([a, b,])" ></button>
+        </template>`,
+      output: `
+        <template>
+          <button @click="() => fn([a, b])" ></button>
+        </template>`,
+      errors: [
+        {
+          message: 'Unexpected trailing comma.',
+          line: 3
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <CustomButton @click="($event, ) => fn()" />
+        </template>`,
+      options: [{
+        'functions': 'never'
+      }],
+      output: `
+        <template>
+          <CustomButton @click="($event ) => fn()" />
+        </template>`,
+      errors: [
+        {
+          message: 'Unexpected trailing comma.',
+          line: 3
+        }
+      ]
+    },
+    {
+      code: `
+        <template>
+          <button @click="() => {
+            fn([a, b, ])
+            fn([
+              a,
+              b
+            ])
+          }"></button>
+        </template>`,
+      options: ['always-multiline'],
+      output: `
+        <template>
+          <button @click="() => {
+            fn([a, b ])
+            fn([
+              a,
+              b,
+            ])
+          }"></button>
+        </template>`,
+      errors: [
+        {
+          message: 'Unexpected trailing comma.',
+          line: 4
+        },
+        {
+          message: 'Missing trailing comma.',
+          line: 7
+        }
+      ]
+    }
+  ]
+})