Skip to content

Check @vue/composition-api Usages in no-ref-as-operand #1225

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

Merged
merged 7 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions lib/rules/no-ref-as-operand.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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