Skip to content

Commit

Permalink
New: add no-use-computed-property-like-method rules (#1234)
Browse files Browse the repository at this point in the history
* chore: init rule

* feat: getable methodProperties

* feat: move getMethodProperties into util

* feat: pass rule

* feat: remove getMethodProperties

* docs: add docs

* chore: run update

* feat: check return type of data props methods computed

* feat: add types to eslint utils getStaticValue

* feat: refactor rule

* docs: fix docs

* docs: fix docs

* feat: change report node

* chore: remove unused file

* refactor: improve JSDoc

* docs: remove template

* docs: fix docs

* docs: fix docs

* feat: change target node

* feat: rename getComponentPropsType

* rename eslint

* refactor: rename PropertyMap

* refactor: improve if statement

* Revert "refactor: improve if statement"

This reverts commit 8b825e5.

* feat: consider basic props type

* chore: run update

* refactor improve selector

* feat: addPropertyMap

* refactor: use findProperty

* refactor: add comment

* feat: cover ThisExpression

* feat: support return statement return nothing

* fix: support undefined of propertyMap[thisMember]

* style: run lint

* test: add test case
  • Loading branch information
tyankatsu0105 committed Jul 29, 2021
1 parent 8c3031e commit 9856621
Show file tree
Hide file tree
Showing 7 changed files with 1,310 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/rules/README.md
Expand Up @@ -320,6 +320,7 @@ For example:
| [vue/no-unsupported-features](./no-unsupported-features.md) | disallow unsupported Vue.js syntax on the specified version | :wrench: |
| [vue/no-unused-properties](./no-unused-properties.md) | disallow unused properties | |
| [vue/no-unused-refs](./no-unused-refs.md) | disallow unused refs | |
| [vue/no-use-computed-property-like-method](./no-use-computed-property-like-method.md) | disallow use computed property like method | |
| [vue/no-useless-mustaches](./no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
| [vue/no-useless-v-bind](./no-useless-v-bind.md) | disallow unnecessary `v-bind` directives | :wrench: |
| [vue/padding-line-between-blocks](./padding-line-between-blocks.md) | require or disallow padding lines between blocks | :wrench: |
Expand Down Expand Up @@ -366,7 +367,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
| [vue/no-sparse-arrays](./no-sparse-arrays.md) | disallow sparse arrays | |
| [vue/no-useless-concat](./no-useless-concat.md) | disallow unnecessary concatenation of literals or template literals | |
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks after opening and before closing braces | :wrench: |
| [vue/object-curly-newline](./object-curly-newline.md) | enforce consistent line breaks inside braces | :wrench: |
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
| [vue/object-property-newline](./object-property-newline.md) | enforce placing object properties on separate lines | :wrench: |
| [vue/operator-linebreak](./operator-linebreak.md) | enforce consistent linebreak style for operators | :wrench: |
Expand Down
340 changes: 340 additions & 0 deletions docs/rules/no-use-computed-property-like-method.md
@@ -0,0 +1,340 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-use-computed-property-like-method
description: disallow use computed property like method
---
# vue/no-use-computed-property-like-method

> disallow use computed property like method
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule disallows to use computed property like method.

<eslint-code-block :rules="{'vue/no-use-computed-property-like-method': ['error']}">

```vue
<script>
export default {
data() {
return {
dataString: 'dataString',
dataNumber: 10,
dataObject: {
inside: 'inside'
},
dataArray: [1, 2, 3, 4, 5],
dataBoolean: true,
dataFunction() {
alert('dataFunction')
}
}
},
props: {
propsString: String,
propsNumber: Number,
propsObject: Object,
propsArray: Array,
propsBoolean: Boolean,
propsFunction: Function,
objectPropsString: {
type: String
},
objectPropsNumber: {
type: Number
},
objectPropsObject: {
type: Object
},
objectPropsArray: {
type: Array
},
objectPropsBoolean: {
type: Boolean
},
objectPropsFunction: {
type: Function
}
},
computed: {
computedReturnDataString() {
return this.dataString
},
computedReturnDataNumber() {
return this.dataNumber
},
computedReturnDataObject() {
return this.dataObject
},
computedReturnDataArray() {
return this.dataArray
},
computedReturnDataBoolean() {
return this.dataBoolean
},
computedReturnDataFunction() {
return this.dataFunction
},
computedReturnPropsString() {
return this.propsString
},
computedReturnPropsNumber() {
return this.propsNumber
},
computedReturnPropsObject() {
return this.propsObject
},
computedReturnPropsArray() {
return this.propsArray
},
computedReturnPropsBoolean() {
return this.propsBoolean
},
computedReturnPropsFunction() {
return this.propsFunction
},
computedReturnObjectPropsString() {
return this.objectPropsString
},
computedReturnObjectPropsNumber() {
return this.objectPropsNumber
},
computedReturnObjectPropsObject() {
return this.objectPropsObject
},
computedReturnObjectPropsArray() {
return this.objectPropsArray
},
computedReturnObjectPropsBoolean() {
return this.objectPropsBoolean
},
computedReturnObjectPropsFunction() {
return this.objectPropsFunction
},
computedReturnString() {
return 'computedReturnString'
},
computedReturnNumber() {
return 10
},
computedReturnObject() {
return {
inside: 'inside'
}
},
computedReturnArray() {
return [1, 2, 3, 4, 5]
},
computedReturnBoolean() {
return true
},
computedReturnFunction() {
const fn = () => alert('computedReturnFunction')
return fn
},
computedReturnMethodsReturnString() {
return this.methodsReturnString
},
computedReturnMethodsReturnNumber() {
return this.methodsReturnNumber
},
computedReturnMethodsReturnObject() {
return this.methodsReturnObject
},
computedReturnMethodsReturnArray() {
return this.methodsReturnArray
},
computedReturnMethodsReturnBoolean() {
return this.methodsReturnBoolean
},
computedReturnMethodsReturnFunction() {
return this.methodsReturnFunction
}
},
methods: {
methodsReturnString() {
return 'methodsReturnString'
},
methodsReturnNumber() {
return 'methodsReturnNumber'
},
methodsReturnObject() {
return {
inside: 'inside'
}
},
methodsReturnArray() {
return [1, 2, 3, 4, 5]
},
methodsReturnBoolean() {
return true
},
methodsReturnFunction() {
const fn = () => alert('methodsReturnFunction')
return fn
},
fn() {
/* Reference data */
/* ✓ GOOD */
this.computedReturnDataString
this.computedReturnDataNumber
this.computedReturnDataObject
this.computedReturnDataArray
this.computedReturnDataBoolean
this.computedReturnDataFunction
this.computedReturnDataFunction()
/* ✗ BAD */
this.computedReturnDataString()
this.computedReturnDataNumber()
this.computedReturnDataObject()
this.computedReturnDataArray()
this.computedReturnDataBoolean()
/* Reference props */
/* ✓ GOOD */
this.computedReturnPropsString
this.computedReturnPropsNumber
this.computedReturnPropsObject
this.computedReturnPropsArray
this.computedReturnPropsBoolean
this.computedReturnPropsFunction
this.computedReturnPropsFunction()
/* ✗ BAD */
this.computedReturnPropsString()
this.computedReturnPropsNumber()
this.computedReturnPropsObject()
this.computedReturnPropsArray()
this.computedReturnPropsBoolean()
/* ✓ GOOD */
this.computedReturnObjectPropsString
this.computedReturnObjectPropsNumber
this.computedReturnObjectPropsObject
this.computedReturnObjectPropsArray
this.computedReturnObjectPropsBoolean
this.computedReturnObjectPropsFunction
this.computedReturnObjectPropsFunction()
/* ✗ BAD */
this.computedReturnObjectPropsString()
this.computedReturnObjectPropsNumber()
this.computedReturnObjectPropsObject()
this.computedReturnObjectPropsArray()
this.computedReturnObjectPropsBoolean()
/* Reference computed */
/* ✓ GOOD */
this.computedReturnString
this.computedReturnNumber
this.computedReturnObject
this.computedReturnArray
this.computedReturnBoolean
this.computedReturnFunction
this.computedReturnFunction()
/* ✗ BAD */
this.computedReturnString()
this.computedReturnNumber()
this.computedReturnObject()
this.computedReturnArray()
this.computedReturnBoolean()
/* Reference methods */
/* ✓ GOOD */
this.computedReturnMethodsReturnString
this.computedReturnMethodsReturnNumber
this.computedReturnMethodsReturnObject
this.computedReturnMethodsReturnArray
this.computedReturnMethodsReturnBoolean
this.computedReturnMethodsReturnFunction
this.computedReturnMethodsReturnFunction()
/* ✗ BAD */
this.computedReturnMethodsReturnString()
this.computedReturnMethodsReturnNumber()
this.computedReturnMethodsReturnObject()
this.computedReturnMethodsReturnArray()
this.computedReturnMethodsReturnBoolean()
}
}
}
</script>
```

</eslint-code-block>

This rule can't check if props is used as array:

<eslint-code-block :rules="{'vue/no-use-computed-property-like-method': ['error']}">

```vue
<script>
export default {
props: [
'propsString',
'propsNumber',
'propsObject',
'propsArray',
'propsBoolean',
'propsFunction'
],
computed: {
computedReturnPropsString() {
return this.propsString
},
computedReturnPropsNumber() {
return this.propsNumber
},
computedReturnPropsObject() {
return this.propsObject
},
computedReturnPropsArray() {
return this.propsArray
},
computedReturnPropsBoolean() {
return this.propsBoolean
},
computedReturnPropsFunction() {
return this.propsFunction
}
},
methods: {
fn() {
/* Reference props */
/* ✓ GOOD */
this.computedReturnPropsString
this.computedReturnPropsString()
this.computedReturnPropsNumber
this.computedReturnPropsNumber()
this.computedReturnPropsObject
this.computedReturnPropsObject()
this.computedReturnPropsArray
this.computedReturnPropsArray()
this.computedReturnPropsBoolean
this.computedReturnPropsBoolean()
this.computedReturnPropsFunction
this.computedReturnPropsFunction()
/* ✗ BAD */
/* Nope. everything is GOOD!! */
}
}
}
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-use-computed-property-like-method.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-use-computed-property-like-method.js)
4 changes: 2 additions & 2 deletions docs/rules/object-curly-newline.md
Expand Up @@ -2,12 +2,12 @@
pageClass: rule-details
sidebarDepth: 0
title: vue/object-curly-newline
description: enforce consistent line breaks after opening and before closing braces
description: enforce consistent line breaks inside braces
since: v7.0.0
---
# vue/object-curly-newline

> enforce consistent line breaks after opening and before closing braces
> enforce consistent line breaks inside braces
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -122,6 +122,7 @@ module.exports = {
'no-unused-properties': require('./rules/no-unused-properties'),
'no-unused-refs': require('./rules/no-unused-refs'),
'no-unused-vars': require('./rules/no-unused-vars'),
'no-use-computed-property-like-method': require('./rules/no-use-computed-property-like-method'),
'no-use-v-if-with-v-for': require('./rules/no-use-v-if-with-v-for'),
'no-useless-concat': require('./rules/no-useless-concat'),
'no-useless-mustaches': require('./rules/no-useless-mustaches'),
Expand Down

0 comments on commit 9856621

Please sign in to comment.