Wenfang Du
Wenfang Du

Reputation: 11307

How to ban console log in the Vue template?

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:

Upvotes: 4

Views: 325

Answers (1)

Wenfang Du
Wenfang Du

Reputation: 11307

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.",
      },
    ],
  },
}

Upvotes: 5

Related Questions