Skip to content

Commit

Permalink
Check @vue/composition-api Usages in no-ref-as-operand (#1225)
Browse files Browse the repository at this point in the history
* check '@vue/composition-api' usages in no-ref-as-operand

* add test cases

* add composition-api checking to no-lifecycle-after-await

* add composition-api checking to no-watch-after-await

* prettier fix

* revert adding vue 2 support for async rules

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
  • Loading branch information
BeeeQueue and ota-meshi committed Jul 14, 2020
1 parent fec4e41 commit 8c981ea
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 19 deletions.
36 changes: 17 additions & 19 deletions lib/rules/no-ref-as-operand.js
Expand Up @@ -52,26 +52,24 @@ module.exports = {
return {
Program() {
const tracker = new ReferenceTracker(context.getScope())
const traceMap = {
vue: {
[ReferenceTracker.ESM]: true,
ref: {
[ReferenceTracker.CALL]: true
},
computed: {
[ReferenceTracker.CALL]: true
},
toRef: {
[ReferenceTracker.CALL]: true
},
customRef: {
[ReferenceTracker.CALL]: true
},
shallowRef: {
[ReferenceTracker.CALL]: true
}
const traceMap = utils.createCompositionApiTraceMap({
[ReferenceTracker.ESM]: true,
ref: {
[ReferenceTracker.CALL]: true
},
computed: {
[ReferenceTracker.CALL]: true
},
toRef: {
[ReferenceTracker.CALL]: true
},
customRef: {
[ReferenceTracker.CALL]: true
},
shallowRef: {
[ReferenceTracker.CALL]: true
}
}
})

for (const { node, path } of tracker.iterateEsmReferences(traceMap)) {
const variableDeclarator = node.parent
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/index.js
Expand Up @@ -1532,6 +1532,15 @@ module.exports = {
}
},

/**
* Wraps composition API trace map in both 'vue' and '@vue/composition-api' imports
* @param {import('eslint-utils').TYPES.TraceMap} map
*/
createCompositionApiTraceMap: (map) => ({
vue: map,
'@vue/composition-api': map
}),

/**
* Checks whether or not the tokens of two given nodes are same.
* @param {ASTNode} left A node 1 to compare.
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/rules/no-ref-as-operand.js
Expand Up @@ -39,6 +39,23 @@ tester.run('no-ref-as-operand', rule, {
</script>
`,
`
<script>
import { ref } from '@vue/composition-api'
export default {
setup() {
const count = ref(0)
console.log(count.value) // 0
count.value++
console.log(count.value) // 1
return {
count
}
}
}
</script>
`,
`
import { ref } from 'vue'
const count = ref(0)
if (count.value) {}
Expand Down Expand Up @@ -202,6 +219,48 @@ tester.run('no-ref-as-operand', rule, {
}
]
},
{
code: `
<script>
import { ref } from '@vue/composition-api'
export default {
setup() {
let count = ref(0)
count++ // error
console.log(count + 1) // error
console.log(1 + count) // error
return {
count
}
}
}
</script>
`,
errors: [
{
messageId: 'requireDotValue',
line: 8,
column: 13,
endLine: 8,
endColumn: 18
},
{
messageId: 'requireDotValue',
line: 9,
column: 25,
endLine: 9,
endColumn: 30
},
{
messageId: 'requireDotValue',
line: 10,
column: 29,
endLine: 10,
endColumn: 34
}
]
},
{
code: `
import { ref } from 'vue'
Expand Down

0 comments on commit 8c981ea

Please sign in to comment.