|
| 1 | +/** |
| 2 | + * @fileoverview Disallow direct composable usage in event handler. |
| 3 | + * @author Nils Haberkamp |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const rule = require('../../../lib/rules/no-direct-composable-in-event-handler') |
| 8 | +const RuleTester = require('../../eslint-compat').RuleTester |
| 9 | + |
| 10 | +const ruleTester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + ecmaVersion: 2018, |
| 13 | + sourceType: 'module' |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +ruleTester.run('no-direct-composable-in-event-handler', rule, { |
| 18 | + valid: [ |
| 19 | + { |
| 20 | + filename: 'test.vue', |
| 21 | + code: ` |
| 22 | + <template> |
| 23 | + <button @click="foo">Click me</button> |
| 24 | + </template> |
| 25 | + <script setup> |
| 26 | + function foo() {} |
| 27 | + </script> |
| 28 | + `, |
| 29 | + languageOptions: { parser: require('vue-eslint-parser') } |
| 30 | + }, |
| 31 | + { |
| 32 | + filename: 'test.vue', |
| 33 | + code: ` |
| 34 | + <template> |
| 35 | + <button @click="() => console.log('foo')">Click me</button> |
| 36 | + </template>`, |
| 37 | + languageOptions: { parser: require('vue-eslint-parser') } |
| 38 | + } |
| 39 | + ], |
| 40 | + |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + filename: 'test.vue', |
| 44 | + code: ` |
| 45 | + <template> |
| 46 | + <button @click="useFoo">Click me</button> |
| 47 | + </template> |
| 48 | + <script> |
| 49 | + export default { |
| 50 | + setup() { |
| 51 | + function useFoo() {} |
| 52 | +
|
| 53 | + return { useFoo } |
| 54 | + } |
| 55 | + } |
| 56 | + </script> |
| 57 | + `, |
| 58 | + languageOptions: { parser: require('vue-eslint-parser') }, |
| 59 | + errors: [ |
| 60 | + { |
| 61 | + message: 'Direct composable usage in event handler is not allowed.', |
| 62 | + line: 3 |
| 63 | + } |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + filename: 'test.vue', |
| 68 | + code: ` |
| 69 | + <template> |
| 70 | + <button @click="useFoo">Click me</button> |
| 71 | + </template> |
| 72 | + <script setup> |
| 73 | + function useFoo() {} |
| 74 | + </script> |
| 75 | + `, |
| 76 | + languageOptions: { parser: require('vue-eslint-parser') }, |
| 77 | + errors: [ |
| 78 | + { |
| 79 | + message: 'Direct composable usage in event handler is not allowed.', |
| 80 | + line: 3 |
| 81 | + } |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + filename: 'test.vue', |
| 86 | + code: ` |
| 87 | + <template> |
| 88 | + <button @keydown.enter="useFoo">Click me</button> |
| 89 | + </template> |
| 90 | + <script setup> |
| 91 | + function useFoo() {} |
| 92 | + </script> |
| 93 | + `, |
| 94 | + languageOptions: { parser: require('vue-eslint-parser') }, |
| 95 | + errors: [ |
| 96 | + { |
| 97 | + message: 'Direct composable usage in event handler is not allowed.', |
| 98 | + line: 3 |
| 99 | + } |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + filename: 'test.vue', |
| 104 | + code: ` |
| 105 | + <template> |
| 106 | + <button @click="useFoo">Click me</button> |
| 107 | + </template> |
| 108 | + <script setup lang="ts"> |
| 109 | + function useFoo() {} |
| 110 | + </script> |
| 111 | + `, |
| 112 | + languageOptions: { |
| 113 | + parser: require('vue-eslint-parser'), |
| 114 | + parserOptions: { parser: require.resolve('@typescript-eslint/parser') } |
| 115 | + }, |
| 116 | + errors: [ |
| 117 | + { |
| 118 | + message: 'Direct composable usage in event handler is not allowed.', |
| 119 | + line: 3 |
| 120 | + } |
| 121 | + ] |
| 122 | + } |
| 123 | + ] |
| 124 | +}) |
0 commit comments