Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Add vue/no-computed-in-data Rule #1075

Closed

Conversation

IWANABETHATGUY
Copy link
Contributor

@IWANABETHATGUY IWANABETHATGUY commented Mar 11, 2020

Fixes: #958

@IWANABETHATGUY
Copy link
Contributor Author

IWANABETHATGUY commented Mar 11, 2020

almost finish #958

lib/utils/index.js Outdated Show resolved Hide resolved
@IWANABETHATGUY
Copy link
Contributor Author

@ota-meshi could please help for review?

@IWANABETHATGUY IWANABETHATGUY changed the title Feature/no computed in data (WIP) Feature/no computed in data Mar 18, 2020
@IWANABETHATGUY IWANABETHATGUY changed the title Feature/no computed in data New Add vue/no-computed-in-data Rule Mar 18, 2020
Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply.
I have change requests.

lib/rules/no-computed-in-data.js Show resolved Hide resolved
lib/rules/no-computed-in-data.js Outdated Show resolved Hide resolved
lib/rules/no-computed-in-data.js Outdated Show resolved Hide resolved
lib/utils/index.js Outdated Show resolved Hide resolved
lib/rules/no-computed-in-data.js Outdated Show resolved Hide resolved
lib/rules/no-computed-in-data.js Outdated Show resolved Hide resolved
docs/rules/no-computed-in-data.md Outdated Show resolved Hide resolved
docs/rules/no-computed-in-data.md Outdated Show resolved Hide resolved
</script>
`
},
// should not warn when objectExpression is not a vue component
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
const computedPropertyNameList = utils.getComputedProperties(obj).map(item => `this.${item.key}`)
Object.keys(memberExpressionMap).forEach(fullName => {
const index = computedPropertyNameList.findIndex(name => fullName.startsWith(name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use startsWith.

Suggested change
const index = computedPropertyNameList.findIndex(name => fullName.startsWith(name))
const index = computedPropertyNameList.findIndex(name => fullName === name)

}
},
MemberExpression (node) {
if (dataAstNode && dataPropertyScope === topOfStack(scopeStack)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vue components may not be at the top level.

export default {
  components: {
    // @vue/component
    MyComponent: {
      data() {
        return {
          bar: this.baz
        }
      },
      computed: {
        baz() {}
      }
    }
  },
}

const fullName = utils.parseMemberExpression(node).slice(0, 2).join('.')
if (memberExpressionMap[fullName]) {
// check if parent node in this array, if true ignore this node, such as `{a: this.a.c.d}`, this traverse function visit order is `this.a.c.d` -> `a.c.d` -> `c.d`
const hasParentNodeInArray = memberExpressionMap[fullName].some(nodeInMap => node.parent === nodeInMap)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found an issue with duplicate reports when the chain is deep. Perhaps checking the parent is a problem.

      <script>
      export default {
        data() {
          return {
            a: this.test.foo.bar.baz,
          }
        },
        computed: {
          test() {
            return {}
          },
        }
      }
      </script>

// ------------------------------------------------------------------------------
const utils = require('../utils')
function topOfStack (stack) {
console.assert(Array.isArray(stack))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a console.assert call here?

@ota-meshi
Copy link
Member

This PR seems to be out of date, so I will close it.

@ota-meshi ota-meshi closed this Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Proposal: no-computed-in-data
3 participants