ESLint & Vue - How to ban the use of `$log`?

≡放荡痞女 提交于 2021-02-07 20:40:49

问题


Origin of $log:

Vue.prototype.$log = console.log

Places to be banned:

<template>
  <!-- Place 1 -->
  <div @click="$log">
    <!-- Place 2 -->
    {{ $log }}
    <!-- Place 3 -->
    {{ $log('foo') }}
  </div>
</template>

<script>
import Vue from 'vue'

// Place 4
Vue.prototype.$log('foo')

export default {
  created() {
    // Place 5
    this.$log('foo')
  },
}
</script>

Some additional information that might help:

  • ESLint - How to restrict property of this
  • no-restricted-syntax
  • vue/no-restricted-syntax

回答1:


After digging into no-restricted-syntax, vue/no-restricted-syntax rules and ASTs, I finally got this working, here're the working rules:

{
  rules: {
    'no-restricted-syntax': [
      'error',
      {
        selector: '[name=$log]',
        message: "Using '$log' is not allowed.",
      },
    ],
    'vue/no-restricted-syntax': [
      'error',
      {
        selector: '[name=$log]',
        message: "Using '$log' is not allowed.",
      },
    ],
  },
}


来源:https://stackoverflow.com/questions/65434989/eslint-vue-how-to-ban-the-use-of-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!