Repository: vuejs/eslint-plugin-vue
Pull request #1116 (2020-05-09 07:04:42)

Add `disallowVueBuiltInComponents` and `disallowVue3BuiltInComponents` option that reports Vue built-in component names to the `vue/no-reserved-component-names` rule.


Patch:
diff --git a/docs/rules/README.md b/docs/rules/README.md
index 89fa12b5..4357d7e9 100644
--- a/docs/rules/README.md
+++ b/docs/rules/README.md
@@ -42,6 +42,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
 | [vue/no-deprecated-data-object-declaration](./no-deprecated-data-object-declaration.md) | disallow using deprecated object declaration on data (in Vue.js 3.0.0+) | :wrench: |
 | [vue/no-deprecated-events-api](./no-deprecated-events-api.md) | disallow using deprecated events api (in Vue.js 3.0.0+) |  |
 | [vue/no-deprecated-filter](./no-deprecated-filter.md) | disallow using deprecated filters syntax (in Vue.js 3.0.0+) |  |
+| [vue/no-deprecated-html-element-is](./no-deprecated-html-element-is.md) | disallow using deprecated the `is` attribute on HTML elements (in Vue.js 3.0.0+) |  |
 | [vue/no-deprecated-inline-template](./no-deprecated-inline-template.md) | disallow using deprecated `inline-template` attribute (in Vue.js 3.0.0+) |  |
 | [vue/no-deprecated-scope-attribute](./no-deprecated-scope-attribute.md) | disallow deprecated `scope` attribute (in Vue.js 2.5.0+) | :wrench: |
 | [vue/no-deprecated-slot-attribute](./no-deprecated-slot-attribute.md) | disallow deprecated `slot` attribute (in Vue.js 2.6.0+) | :wrench: |
diff --git a/docs/rules/no-deprecated-html-element-is.md b/docs/rules/no-deprecated-html-element-is.md
new file mode 100644
index 00000000..800072b9
--- /dev/null
+++ b/docs/rules/no-deprecated-html-element-is.md
@@ -0,0 +1,43 @@
+---
+pageClass: rule-details
+sidebarDepth: 0
+title: vue/no-deprecated-html-element-is
+description: disallow using deprecated the `is` attribute on HTML elements (in Vue.js 3.0.0+)
+---
+# vue/no-deprecated-html-element-is
+> disallow using deprecated the `is` attribute on HTML elements (in Vue.js 3.0.0+)
+
+- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/vue3-strongly-recommended"` and `"plugin:vue/vue3-recommended"`.
+
+## :book: Rule Details
+
+This rule reports deprecated the `is` attribute on HTML elements (removed in Vue.js v3.0.0+)
+
+<eslint-code-block :rules="{'vue/no-deprecated-html-element-is': ['error']}">
+
+```vue
+<template>
+  <!-- ✓ GOOD -->
+  <div />
+  <component is="foo">
+
+  <!-- ✗ BAD -->
+  <div is="foo" />
+  <div :is="foo" />
+</template>
+```
+
+</eslint-code-block>
+
+### :wrench: Options
+
+Nothing.
+
+## :books: Further Reading
+
+- [Vue RFCs - 0027-custom-elements-interop](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0027-custom-elements-interop.md)
+
+## :mag: Implementation
+
+- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-deprecated-html-element-is.js)
+- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-deprecated-html-element-is.js)
diff --git a/docs/rules/no-reserved-component-names.md b/docs/rules/no-reserved-component-names.md
index 3aa5f5db..2d11b233 100644
--- a/docs/rules/no-reserved-component-names.md
+++ b/docs/rules/no-reserved-component-names.md
@@ -9,7 +9,7 @@ description: disallow the use of reserved names in component definitions
 
 ## :book: Rule Details
 
-This rule prevents name collisions between vue components and standard html elements. 
+This rule prevents name collisions between Vue components and standard HTML elements and built-in components.
 
 <eslint-code-block :rules="{'vue/no-reserved-component-names': ['error']}">
 
@@ -26,7 +26,47 @@ export default {
 
 ## :wrench: Options
 
-Nothing.
+```json
+{
+  "vue/no-reserved-component-names": ["error", {
+    "disallowVueBuiltInComponents": false,
+    "disallowVue3BuiltInComponents": false
+  }]
+}
+```
+
+- `disallowVueBuiltInComponents` (`boolean`) ... If `true`, disallow Vue.js 2.x built-in component names. Default is `false`.
+- `disallowVue3BuiltInComponents` (`boolean`) ... If `true`, disallow Vue.js 3.x built-in component names. Default is `false`.
+
+### `"disallowVueBuiltInComponents": true`
+
+<eslint-code-block :rules="{'vue/no-reserved-component-names': ['error', {disallowVueBuiltInComponents: true}]}">
+
+```vue
+<script>
+/* ✗ BAD */
+export default {
+  name: 'transition-group'
+}
+</script>
+```
+
+</eslint-code-block>
+
+### `"disallowVue3BuiltInComponents": true`
+
+<eslint-code-block :rules="{'vue/no-reserved-component-names': ['error', {disallowVue3BuiltInComponents: true}]}">
+
+```vue
+<script>
+/* ✗ BAD */
+export default {
+  name: 'teleport'
+}
+</script>
+```
+
+</eslint-code-block>
 
 ## :books: Further reading
 
@@ -34,6 +74,7 @@ Nothing.
 - [List of SVG elements](https://developer.mozilla.org/en-US/docs/Web/SVG/Element)
 - [Kebab case elements](https://stackoverflow.com/questions/22545621/do-custom-elements-require-a-dash-in-their-name/22545622#22545622)
 - [Valid custom element name](https://w3c.github.io/webcomponents/spec/custom/#valid-custom-element-name)
+- [API - Built-In Components](https://vuejs.org/v2/api/index.html#Built-In-Components)
 
 ## :mag: Implementation
 
diff --git a/lib/configs/vue3-essential.js b/lib/configs/vue3-essential.js
index eb137448..f21d544c 100644
--- a/lib/configs/vue3-essential.js
+++ b/lib/configs/vue3-essential.js
@@ -10,6 +10,7 @@ module.exports = {
     'vue/no-deprecated-data-object-declaration': 'error',
     'vue/no-deprecated-events-api': 'error',
     'vue/no-deprecated-filter': 'error',
+    'vue/no-deprecated-html-element-is': 'error',
     'vue/no-deprecated-inline-template': 'error',
     'vue/no-deprecated-scope-attribute': 'error',
     'vue/no-deprecated-slot-attribute': 'error',
diff --git a/lib/index.js b/lib/index.js
index 740b49e3..adf6185f 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -43,6 +43,7 @@ module.exports = {
     'no-deprecated-data-object-declaration': require('./rules/no-deprecated-data-object-declaration'),
     'no-deprecated-events-api': require('./rules/no-deprecated-events-api'),
     'no-deprecated-filter': require('./rules/no-deprecated-filter'),
+    'no-deprecated-html-element-is': require('./rules/no-deprecated-html-element-is'),
     'no-deprecated-inline-template': require('./rules/no-deprecated-inline-template'),
     'no-deprecated-scope-attribute': require('./rules/no-deprecated-scope-attribute'),
     'no-deprecated-slot-attribute': require('./rules/no-deprecated-slot-attribute'),
diff --git a/lib/rules/no-deprecated-html-element-is.js b/lib/rules/no-deprecated-html-element-is.js
new file mode 100644
index 00000000..b45a700f
--- /dev/null
+++ b/lib/rules/no-deprecated-html-element-is.js
@@ -0,0 +1,50 @@
+/**
+ * @author Yosuke Ota
+ * See LICENSE file in root directory for full license.
+ */
+'use strict'
+
+// ------------------------------------------------------------------------------
+// Requirements
+// ------------------------------------------------------------------------------
+
+const utils = require('../utils')
+
+// ------------------------------------------------------------------------------
+// Rule Definition
+// ------------------------------------------------------------------------------
+
+module.exports = {
+  meta: {
+    type: 'problem',
+    docs: {
+      description: 'disallow using deprecated the `is` attribute on HTML elements (in Vue.js 3.0.0+)',
+      categories: ['vue3-essential'],
+      url: 'https://eslint.vuejs.org/rules/no-deprecated-html-element-is.html'
+    },
+    fixable: null,
+    schema: [],
+    messages: {
+      unexpected: 'The `is` attribute on HTML element are deprecated.'
+    }
+  },
+
+  create: function (context) {
+    return utils.defineTemplateBodyVisitor(context, {
+      "VAttribute[directive=true][key.name.name='bind'][key.argument.name='is'], VAttribute[directive=false][key.name='is']" (node) {
+        const element = node.parent.parent
+        if (
+          !utils.isHtmlWellKnownElementName(element.rawName) &&
+          !utils.isSvgWellKnownElementName(element.rawName)
+        ) {
+          return
+        }
+        context.report({
+          node,
+          loc: node.loc,
+          messageId: 'unexpected'
+        })
+      }
+    })
+  }
+}
diff --git a/lib/rules/no-reserved-component-names.js b/lib/rules/no-reserved-component-names.js
index 75e53556..c566684e 100644
--- a/lib/rules/no-reserved-component-names.js
+++ b/lib/rules/no-reserved-component-names.js
@@ -22,20 +22,44 @@ const kebabCaseElements = [
   'missing-glyph'
 ]
 
+// https://vuejs.org/v2/api/index.html#Built-In-Components
+const vueBuiltInComponents = [
+  'component',
+  'transition',
+  'transition-group',
+  'keep-alive',
+  'slot'
+]
+
+const vue3BuiltInComponents = [
+  'teleport',
+  'suspense'
+]
+
 const isLowercase = (word) => /^[a-z]*$/.test(word)
 const capitalizeFirstLetter = (word) => word[0].toUpperCase() + word.substring(1, word.length)
 
-const RESERVED_NAMES = new Set(
-  [
-    ...kebabCaseElements,
-    ...kebabCaseElements.map(casing.pascalCase),
-    ...htmlElements,
-    ...htmlElements.map(capitalizeFirstLetter),
-    ...deprecatedHtmlElements,
-    ...deprecatedHtmlElements.map(capitalizeFirstLetter),
-    ...svgElements,
-    ...svgElements.filter(isLowercase).map(capitalizeFirstLetter)
-  ])
+const RESERVED_NAMES_IN_HTML = new Set([
+  ...htmlElements,
+  ...htmlElements.map(capitalizeFirstLetter)
+])
+const RESERVED_NAMES_IN_VUE = new Set([
+  ...vueBuiltInComponents,
+  ...vueBuiltInComponents.map(casing.pascalCase)
+])
+const RESERVED_NAMES_IN_VUE3 = new Set([
+  ...RESERVED_NAMES_IN_VUE,
+  ...vue3BuiltInComponents,
+  ...vue3BuiltInComponents.map(casing.pascalCase)
+])
+const RESERVED_NAMES_IN_OTHERS = new Set([
+  ...deprecatedHtmlElements,
+  ...deprecatedHtmlElements.map(capitalizeFirstLetter),
+  ...kebabCaseElements,
+  ...kebabCaseElements.map(casing.pascalCase),
+  ...svgElements,
+  ...svgElements.filter(isLowercase).map(capitalizeFirstLetter)
+])
 
 // ------------------------------------------------------------------------------
 // Rule Definition
@@ -46,14 +70,41 @@ module.exports = {
     type: 'suggestion',
     docs: {
       description: 'disallow the use of reserved names in component definitions',
-      categories: undefined, // 'essential'
+      categories: undefined,
       url: 'https://eslint.vuejs.org/rules/no-reserved-component-names.html'
     },
     fixable: null,
-    schema: []
+    schema: [{
+      type: 'object',
+      properties: {
+        disallowVueBuiltInComponents: {
+          type: 'boolean'
+        },
+        disallowVue3BuiltInComponents: {
+          type: 'boolean'
+        }
+      }
+    }],
+    messages: {
+      reserved: 'Name "{{name}}" is reserved.',
+      reservedInHtml: 'Name "{{name}}" is reserved in HTML.',
+      reservedInVue: 'Name "{{name}}" is reserved in Vue.js.',
+      reservedInVue3: 'Name "{{name}}" is reserved in Vue.js 3.x.'
+    }
   },
 
   create (context) {
+    const options = context.options[0] || {}
+    const disallowVueBuiltInComponents = options.disallowVueBuiltInComponents === true
+    const disallowVue3BuiltInComponents = options.disallowVue3BuiltInComponents === true
+
+    const reservedNames = new Set([
+      ...RESERVED_NAMES_IN_HTML,
+      ...(disallowVueBuiltInComponents ? RESERVED_NAMES_IN_VUE : []),
+      ...(disallowVue3BuiltInComponents ? RESERVED_NAMES_IN_VUE3 : []),
+      ...RESERVED_NAMES_IN_OTHERS
+    ])
+
     function canVerify (node) {
       return node.type === 'Literal' || (
         node.type === 'TemplateLiteral' &&
@@ -70,7 +121,7 @@ module.exports = {
       } else {
         name = node.value
       }
-      if (RESERVED_NAMES.has(name)) {
+      if (reservedNames.has(name)) {
         report(node, name)
       }
     }
@@ -78,7 +129,9 @@ module.exports = {
     function report (node, name) {
       context.report({
         node: node,
-        message: 'Name "{{name}}" is reserved.',
+        messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml'
+          : RESERVED_NAMES_IN_VUE.has(name) ? 'reservedInVue'
+            : RESERVED_NAMES_IN_VUE3.has(name) ? 'reservedInVue3' : 'reserved',
         data: {
           name: name
         }
@@ -98,7 +151,7 @@ module.exports = {
       utils.executeOnVue(context, (obj) => {
         // Report if a component has been registered locally with a reserved name.
         utils.getRegisteredComponents(obj)
-          .filter(({ name }) => RESERVED_NAMES.has(name))
+          .filter(({ name }) => reservedNames.has(name))
           .forEach(({ node, name }) => report(node, name))
 
         const node = obj.properties


Test patch:
diff --git a/tests/lib/rules/no-deprecated-html-element-is.js b/tests/lib/rules/no-deprecated-html-element-is.js
new file mode 100644
index 00000000..efa52e27
--- /dev/null
+++ b/tests/lib/rules/no-deprecated-html-element-is.js
@@ -0,0 +1,69 @@
+/**
+ * @author Yosuke Ota
+ * See LICENSE file in root directory for full license.
+ */
+'use strict'
+
+// ------------------------------------------------------------------------------
+// Requirements
+// ------------------------------------------------------------------------------
+
+const rule = require('../../../lib/rules/no-deprecated-html-element-is')
+const RuleTester = require('eslint').RuleTester
+
+// ------------------------------------------------------------------------------
+// Tests
+// ------------------------------------------------------------------------------
+
+const ruleTester = new RuleTester({
+  parser: require.resolve('vue-eslint-parser'),
+  parserOptions: { ecmaVersion: 2019 }
+})
+
+ruleTester.run('no-deprecated-inline-template', rule, {
+  valid: [
+    {
+      filename: 'test.vue',
+      code: '<template><component is="foo" /></template>'
+    },
+    {
+      filename: 'test.vue',
+      code: '<template><div /></template>'
+    },
+    {
+      filename: 'test.vue',
+      code: '<template><Foo is="foo" /></template>'
+    },
+    {
+      filename: 'test.vue',
+      code: '<template><component :is="\'foo\'" /></template>'
+    }
+  ],
+
+  invalid: [
+    {
+      filename: 'test.vue',
+      code: '<template><div is="foo" /></template>',
+      errors: [
+        {
+          line: 1,
+          column: 16,
+          messageId: 'unexpected',
+          endLine: 1,
+          endColumn: 24
+        }
+      ]
+    },
+    {
+      filename: 'test.vue',
+      code: '<template><div :is="foo" /></template>',
+      errors: [
+        {
+          line: 1,
+          column: 16,
+          messageId: 'unexpected'
+        }
+      ]
+    }
+  ]
+})
diff --git a/tests/lib/rules/no-reserved-component-names.js b/tests/lib/rules/no-reserved-component-names.js
index 0585b5c9..a3bc3bb0 100644
--- a/tests/lib/rules/no-reserved-component-names.js
+++ b/tests/lib/rules/no-reserved-component-names.js
@@ -11,6 +11,12 @@
 const rule = require('../../../lib/rules/no-reserved-component-names')
 const RuleTester = require('eslint').RuleTester
 
+const htmlElements = require('../../../lib/utils/html-elements.json')
+const RESERVED_NAMES_IN_HTML = new Set([
+  ...htmlElements,
+  ...htmlElements.map((word) => word[0].toUpperCase() + word.substring(1, word.length))
+])
+
 // ------------------------------------------------------------------------------
 // Tests
 // ------------------------------------------------------------------------------
@@ -234,6 +240,18 @@ const invalidElements = [
   'xmp', 'Xmp'
 ]
 
+const vue2BuiltInComponents = [
+  'component', 'Component',
+  'transition', 'Transition',
+  'transition-group', 'TransitionGroup',
+  'keep-alive', 'KeepAlive'
+]
+
+const vue3BuiltInComponents = [
+  'teleport', 'Teleport',
+  'suspense', 'Suspense'
+]
+
 const parserOptions = {
   ecmaVersion: 2018,
   sourceType: 'module'
@@ -269,6 +287,16 @@ ruleTester.run('no-reserved-component-names', rule, {
       `,
       parserOptions
     },
+    {
+      filename: 'test.vue',
+      code: `
+        export default {
+          name: 'FooBar'
+        }
+      `,
+      options: [{ disallowVueBuiltInComponents: true, disallowVue3BuiltInComponents: true }],
+      parserOptions
+    },
     {
       filename: 'test.vue',
       code: `Vue.component('FooBar', {})`,
@@ -322,7 +350,41 @@ ruleTester.run('no-reserved-component-names', rule, {
       filename: 'test.js',
       code: `fn1(component.data)`,
       parserOptions
-    }
+    },
+    ...vue2BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions
+      }
+    }),
+    ...vue3BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions
+      }
+    }),
+    ...vue3BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions,
+        options: [{ disallowVueBuiltInComponents: true }]
+      }
+    })
   ],
 
   invalid: [
@@ -336,7 +398,8 @@ ruleTester.run('no-reserved-component-names', rule, {
         `,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'Literal',
           line: 3
         }]
@@ -348,7 +411,8 @@ ruleTester.run('no-reserved-component-names', rule, {
         code: `Vue.component('${name}', component)`,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'Literal',
           line: 1
         }]
@@ -360,7 +424,8 @@ ruleTester.run('no-reserved-component-names', rule, {
         code: `app.component('${name}', component)`,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'Literal',
           line: 1
         }]
@@ -372,7 +437,8 @@ ruleTester.run('no-reserved-component-names', rule, {
         code: `Vue.component(\`${name}\`, {})`,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'TemplateLiteral',
           line: 1
         }]
@@ -384,7 +450,8 @@ ruleTester.run('no-reserved-component-names', rule, {
         code: `app.component(\`${name}\`, {})`,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'TemplateLiteral',
           line: 1
         }]
@@ -400,11 +467,66 @@ ruleTester.run('no-reserved-component-names', rule, {
         }`,
         parserOptions,
         errors: [{
-          message: `Name "${name}" is reserved.`,
+          messageId: RESERVED_NAMES_IN_HTML.has(name) ? 'reservedInHtml' : 'reserved',
+          data: { name },
           type: 'Property',
           line: 3
         }]
       }
+    }),
+    ...vue2BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions,
+        options: [{ disallowVueBuiltInComponents: true }],
+        errors: [{
+          messageId: 'reservedInVue',
+          data: { name },
+          type: 'Literal',
+          line: 3
+        }]
+      }
+    }),
+    ...vue2BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions,
+        options: [{ disallowVue3BuiltInComponents: true }],
+        errors: [{
+          messageId: 'reservedInVue',
+          data: { name },
+          type: 'Literal',
+          line: 3
+        }]
+      }
+    }),
+    ...vue3BuiltInComponents.map(name => {
+      return {
+        filename: `${name}.vue`,
+        code: `
+          export default {
+            name: '${name}'
+          }
+        `,
+        parserOptions,
+        options: [{ disallowVue3BuiltInComponents: true }],
+        errors: [{
+          messageId: 'reservedInVue3',
+          data: { name },
+          type: 'Literal',
+          line: 3
+        }]
+      }
     })
   ]
 })