From 8c981ea43cfce21cae67f4b0aeb60a447f634c0b Mon Sep 17 00:00:00 2001 From: Adam Haglund Date: Tue, 14 Jul 2020 04:56:13 +0200 Subject: [PATCH] Check `@vue/composition-api` Usages in `no-ref-as-operand` (#1225) * 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 --- lib/rules/no-ref-as-operand.js | 36 ++++++++--------- lib/utils/index.js | 9 +++++ tests/lib/rules/no-ref-as-operand.js | 59 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 19 deletions(-) diff --git a/lib/rules/no-ref-as-operand.js b/lib/rules/no-ref-as-operand.js index bfb344d24..fb5dc0881 100644 --- a/lib/rules/no-ref-as-operand.js +++ b/lib/rules/no-ref-as-operand.js @@ -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 diff --git a/lib/utils/index.js b/lib/utils/index.js index 7fae65530..085c596ad 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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. diff --git a/tests/lib/rules/no-ref-as-operand.js b/tests/lib/rules/no-ref-as-operand.js index 5d53a5571..ca553e35c 100644 --- a/tests/lib/rules/no-ref-as-operand.js +++ b/tests/lib/rules/no-ref-as-operand.js @@ -39,6 +39,23 @@ tester.run('no-ref-as-operand', rule, { `, ` + + `, + ` import { ref } from 'vue' const count = ref(0) if (count.value) {} @@ -202,6 +219,48 @@ tester.run('no-ref-as-operand', rule, { } ] }, + { + code: ` + + `, + 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'