Skip to content

Rule proposal: simple-expressions-in-templates #253

Open
@michalsnik

Description

@michalsnik

Style guide:
https://vuejs.org/v2/style-guide/#Simple-expressions-in-templates-strongly-recommended

Description:
This rule would enforce usage of simple expressions in templates. Instead of writing complex logic in templates it should be moved to computed properties and only referenced using them in templates.

What should we check?

  • expressions ({{}})
  • bindings (v-bind:foo="" and :foo="")

When should we throw a warning?
Simplest solution would be to define number of letters inside expressions, and to be honest I think it should work well for most cases.

Let's say we throw error when there are at least 20 signs in expressions:

<!-- Bad -->
{{todos.map(todo => todo.name)}}
<!-- Good -->
{{todosName}}

The bad example has 26 non-empty signs. It's clear that it would be too hard to fit any logic in 20 signs, so we could be quite certain this is a good and not overcomplicated solution.

On the other hand I can think of longer than 20 signs words, e.g.:
allNotCompletedTodosNames

So the other way would be to check for existence of one of the following types of nodes inside expressions:

  • CallExpression
  • FunctionExpression

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