|
| 1 | +/** |
| 2 | + * @author Wayne Zhang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const RuleTester = require('../../eslint-compat').RuleTester |
| 8 | +const rule = require('../../../lib/rules/no-import-compiler-macros') |
| 9 | + |
| 10 | +const tester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parser: require('vue-eslint-parser'), |
| 13 | + ecmaVersion: 2020, |
| 14 | + sourceType: 'module' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +tester.run('no-import-compiler-macros', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <script setup> |
| 24 | + import { ref, computed } from 'vue' |
| 25 | + import { someFunction } from '@vue/runtime-core' |
| 26 | + </script> |
| 27 | + ` |
| 28 | + }, |
| 29 | + { |
| 30 | + filename: 'test.vue', |
| 31 | + code: ` |
| 32 | + <script> |
| 33 | + import { defineProps } from 'some-other-package' |
| 34 | + </script> |
| 35 | + ` |
| 36 | + } |
| 37 | + ], |
| 38 | + invalid: [ |
| 39 | + { |
| 40 | + filename: 'test.vue', |
| 41 | + code: ` |
| 42 | + <script setup> |
| 43 | + import { defineProps } from 'vue' |
| 44 | + </script> |
| 45 | + `, |
| 46 | + output: ` |
| 47 | + <script setup> |
| 48 | + |
| 49 | + </script> |
| 50 | + `, |
| 51 | + errors: [ |
| 52 | + { |
| 53 | + messageId: 'noImportCompilerMacros', |
| 54 | + data: { |
| 55 | + name: 'defineProps' |
| 56 | + }, |
| 57 | + line: 3, |
| 58 | + column: 16 |
| 59 | + } |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + filename: 'test.vue', |
| 64 | + code: ` |
| 65 | + <script setup> |
| 66 | + import { |
| 67 | + ref, |
| 68 | + defineProps |
| 69 | + } from 'vue' |
| 70 | + </script> |
| 71 | + `, |
| 72 | + output: ` |
| 73 | + <script setup> |
| 74 | + import { |
| 75 | + ref |
| 76 | + } from 'vue' |
| 77 | + </script> |
| 78 | + `, |
| 79 | + errors: [ |
| 80 | + { |
| 81 | + messageId: 'noImportCompilerMacros', |
| 82 | + data: { |
| 83 | + name: 'defineProps' |
| 84 | + }, |
| 85 | + line: 5, |
| 86 | + column: 9 |
| 87 | + } |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + filename: 'test.vue', |
| 92 | + code: ` |
| 93 | + <script setup> |
| 94 | + import { ref, defineProps } from 'vue' |
| 95 | + import { defineEmits, computed } from '@vue/runtime-core' |
| 96 | + import { defineExpose, watch, withDefaults } from '@vue/runtime-dom' |
| 97 | + </script> |
| 98 | + `, |
| 99 | + output: ` |
| 100 | + <script setup> |
| 101 | + import { ref } from 'vue' |
| 102 | + import { computed } from '@vue/runtime-core' |
| 103 | + import { watch } from '@vue/runtime-dom' |
| 104 | + </script> |
| 105 | + `, |
| 106 | + errors: [ |
| 107 | + { |
| 108 | + messageId: 'noImportCompilerMacros', |
| 109 | + data: { |
| 110 | + name: 'defineProps' |
| 111 | + }, |
| 112 | + line: 3, |
| 113 | + column: 21 |
| 114 | + }, |
| 115 | + { |
| 116 | + messageId: 'noImportCompilerMacros', |
| 117 | + data: { |
| 118 | + name: 'defineEmits' |
| 119 | + }, |
| 120 | + line: 4, |
| 121 | + column: 16 |
| 122 | + }, |
| 123 | + { |
| 124 | + messageId: 'noImportCompilerMacros', |
| 125 | + data: { |
| 126 | + name: 'defineExpose' |
| 127 | + }, |
| 128 | + line: 5, |
| 129 | + column: 16 |
| 130 | + }, |
| 131 | + { |
| 132 | + messageId: 'noImportCompilerMacros', |
| 133 | + data: { |
| 134 | + name: 'withDefaults' |
| 135 | + }, |
| 136 | + line: 5, |
| 137 | + column: 37 |
| 138 | + } |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + filename: 'test.vue', |
| 143 | + code: ` |
| 144 | + <script setup> |
| 145 | + import { defineModel, defineOptions } from 'vue' |
| 146 | + </script> |
| 147 | + `, |
| 148 | + output: ` |
| 149 | + <script setup> |
| 150 | + import { defineOptions } from 'vue' |
| 151 | + </script> |
| 152 | + `, |
| 153 | + errors: [ |
| 154 | + { |
| 155 | + messageId: 'noImportCompilerMacros', |
| 156 | + data: { |
| 157 | + name: 'defineModel' |
| 158 | + }, |
| 159 | + line: 3, |
| 160 | + column: 16 |
| 161 | + }, |
| 162 | + { |
| 163 | + messageId: 'noImportCompilerMacros', |
| 164 | + data: { |
| 165 | + name: 'defineOptions' |
| 166 | + }, |
| 167 | + line: 3, |
| 168 | + column: 29 |
| 169 | + } |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + filename: 'test.vue', |
| 174 | + code: ` |
| 175 | + <script setup lang="ts"> |
| 176 | + import { ref as refFoo, defineSlots as defineSlotsFoo, type computed } from '@vue/runtime-core' |
| 177 | + </script> |
| 178 | + `, |
| 179 | + output: ` |
| 180 | + <script setup lang="ts"> |
| 181 | + import { ref as refFoo, type computed } from '@vue/runtime-core' |
| 182 | + </script> |
| 183 | + `, |
| 184 | + languageOptions: { |
| 185 | + parserOptions: { |
| 186 | + parser: require.resolve('@typescript-eslint/parser') |
| 187 | + } |
| 188 | + }, |
| 189 | + errors: [ |
| 190 | + { |
| 191 | + messageId: 'noImportCompilerMacros', |
| 192 | + data: { |
| 193 | + name: 'defineSlots' |
| 194 | + }, |
| 195 | + line: 3, |
| 196 | + column: 31 |
| 197 | + } |
| 198 | + ] |
| 199 | + } |
| 200 | + ] |
| 201 | +}) |
0 commit comments