Skip to content

Computed properties that contain state-changing functions trigger vue/no-side-effects-in-computed-properties #873

Closed
@AlbinoDrought

Description

@AlbinoDrought

Tell us about your environment

  • ESLint version: 5.16.0
  • eslint-plugin-vue version: 4.7.1
  • Node version: 9.2.1

Please show your full configuration:

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: [
    'plugin:vue/essential',
    '@vue/airbnb',
  ],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-param-reassign': 'off',
    'prefer-destructuring': 'off',
  },
  parserOptions: {
    parser: 'babel-eslint',
  },
};

What did you do?

<template>
  <div>
    <div
      v-for="tab in tabs"
      :key="tab.id"
      v-text="tab.text"
      :class="{ active: tab.active }"
      @click.prevent="tab.click"
    />
  </div>
</template>

<script>
export default {
  computed: {
    tabs() {
      return this.groups.map((group, i) => ({
        id: i,
        text: group.name,
        active: i === this.selectedTab,
        click: () => {
          // the below line is underlined, (vue/no-side-effects-in-computed-properties)
          this.selectedTab = i;
        },
      }));
    },
    groups() {
      return this.unfilteredGroups.filter(group => group.visible);
    },
  },

  data() {
    return {
      selectedTab: 0,
      unfilteredGroups: [
        { name: 'foo', visible: true },
        { name: 'bar', visible: true },
        { name: 'foobar', visible: false },
      ],
    };
  },
};
</script>

<style scoped>
.active {
  background-color: black;
  color: white;
  font-size: 18px;
  font-family: 'Comic Sans MS', monospace;
}
</style>

What did you expect to happen?

I expected to get no ESLint errors. In my opinion, this computed property does not have any side-effects - it only returns a value and does not change any state by itself.

What actually happened?

/home/sean/projects/frontend/src/App.vue
  23:11  error  Unexpected side effect in "tabs" computed property  vue/no-side-effects-in-computed-properties

✖ 1 problem (1 error, 0 warnings)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions