Skip to content

Commit 781692c

Browse files
committed
refactor: autofix
1 parent eb0d6d2 commit 781692c

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

Diff for: lib/rules/no-import-compiler-macros.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ function isComma(node) {
2323
return node.type === 'Punctuator' && node.value === ','
2424
}
2525

26-
/**
27-
* @param {Token} node
28-
*/
29-
function isLeftCurlyBrace(node) {
30-
return node.type === 'Punctuator' && node.value === '{'
31-
}
32-
3326
module.exports = {
3427
meta: {
3528
type: 'problem',
@@ -73,22 +66,25 @@ module.exports = {
7366
name: specifier.imported.name
7467
},
7568
fix: (fixer) => {
76-
const tokenAfter = sourceCode.getTokenAfter(specifier)
77-
const tokenBefore = sourceCode.getTokenBefore(specifier)
78-
79-
const hasCommaAfter = isComma(tokenAfter)
80-
const isFirstSpecifier = isLeftCurlyBrace(tokenBefore)
81-
82-
const codeStart = hasCommaAfter
83-
? tokenBefore.range[1]
84-
: isFirstSpecifier
85-
? specifier.range[0]
86-
: tokenBefore.range[0]
87-
const codeEnd = hasCommaAfter
88-
? tokenAfter.range[1]
89-
: specifier.range[1]
69+
const isOnlySpecifier = node.specifiers.length === 1
70+
const isLastSpecifier =
71+
specifier === node.specifiers[node.specifiers.length - 1]
9072

91-
return fixer.removeRange([codeStart, codeEnd])
73+
if (isOnlySpecifier) {
74+
return fixer.remove(node)
75+
} else if (isLastSpecifier) {
76+
const commaToken = sourceCode.getTokenBefore(specifier, isComma)
77+
return fixer.removeRange([
78+
commaToken ? commaToken.range[0] : specifier.range[0],
79+
specifier.range[1]
80+
])
81+
} else {
82+
const commaToken = sourceCode.getTokenAfter(specifier, isComma)
83+
return fixer.removeRange([
84+
specifier.range[0],
85+
commaToken ? commaToken.range[1] : specifier.range[1]
86+
])
87+
}
9288
}
9389
})
9490
}

Diff for: tests/lib/rules/no-import-compiler-macros.js

+42-19
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ tester.run('no-import-compiler-macros', rule, {
4040
filename: 'test.vue',
4141
code: `
4242
<script setup>
43-
import { computed, defineProps } from 'vue'
44-
import { defineEmits, ref, withDefaults } from '@vue/runtime-core'
45-
import { defineExpose, watch } from '@vue/runtime-dom'
43+
import { defineProps } from 'vue'
4644
</script>
4745
`,
4846
output: `
4947
<script setup>
50-
import { computed } from 'vue'
51-
import { ref } from '@vue/runtime-core'
52-
import { watch } from '@vue/runtime-dom'
48+
5349
</script>
5450
`,
5551
errors: [
@@ -59,7 +55,34 @@ tester.run('no-import-compiler-macros', rule, {
5955
name: 'defineProps'
6056
},
6157
line: 3,
62-
column: 26
58+
column: 16
59+
}
60+
]
61+
},
62+
{
63+
filename: 'test.vue',
64+
code: `
65+
<script setup>
66+
import { ref, defineProps } from 'vue'
67+
import { defineEmits, computed } from '@vue/runtime-core'
68+
import { defineExpose, watch, withDefaults } from '@vue/runtime-dom'
69+
</script>
70+
`,
71+
output: `
72+
<script setup>
73+
import { ref } from 'vue'
74+
import { computed } from '@vue/runtime-core'
75+
import { watch } from '@vue/runtime-dom'
76+
</script>
77+
`,
78+
errors: [
79+
{
80+
messageId: 'noImportCompilerMacros',
81+
data: {
82+
name: 'defineProps'
83+
},
84+
line: 3,
85+
column: 21
6386
},
6487
{
6588
messageId: 'noImportCompilerMacros',
@@ -72,46 +95,46 @@ tester.run('no-import-compiler-macros', rule, {
7295
{
7396
messageId: 'noImportCompilerMacros',
7497
data: {
75-
name: 'withDefaults'
98+
name: 'defineExpose'
7699
},
77-
line: 4,
78-
column: 34
100+
line: 5,
101+
column: 16
79102
},
80103
{
81104
messageId: 'noImportCompilerMacros',
82105
data: {
83-
name: 'defineExpose'
106+
name: 'withDefaults'
84107
},
85108
line: 5,
86-
column: 16
109+
column: 37
87110
}
88111
]
89112
},
90113
{
91114
filename: 'test.vue',
92115
code: `
93116
<script setup>
94-
import { defineProps, withDefaults, ref } from 'vue'
117+
import { defineModel, defineOptions } from 'vue'
95118
</script>
96119
`,
97120
output: `
98121
<script setup>
99-
import { withDefaults, ref } from 'vue'
122+
import { defineOptions } from 'vue'
100123
</script>
101124
`,
102125
errors: [
103126
{
104127
messageId: 'noImportCompilerMacros',
105128
data: {
106-
name: 'defineProps'
129+
name: 'defineModel'
107130
},
108131
line: 3,
109132
column: 16
110133
},
111134
{
112135
messageId: 'noImportCompilerMacros',
113136
data: {
114-
name: 'withDefaults'
137+
name: 'defineOptions'
115138
},
116139
line: 3,
117140
column: 29
@@ -122,12 +145,12 @@ tester.run('no-import-compiler-macros', rule, {
122145
filename: 'test.vue',
123146
code: `
124147
<script setup lang="ts">
125-
import { ref as refFoo, defineProps as definePropsFoo, type computed } from '@vue/runtime-core'
148+
import { ref as refFoo, defineSlots as defineSlotsFoo, type computed } from '@vue/runtime-core'
126149
</script>
127150
`,
128151
output: `
129152
<script setup lang="ts">
130-
import { ref as refFoo, type computed } from '@vue/runtime-core'
153+
import { ref as refFoo, type computed } from '@vue/runtime-core'
131154
</script>
132155
`,
133156
languageOptions: {
@@ -139,7 +162,7 @@ tester.run('no-import-compiler-macros', rule, {
139162
{
140163
messageId: 'noImportCompilerMacros',
141164
data: {
142-
name: 'defineProps'
165+
name: 'defineSlots'
143166
},
144167
line: 3,
145168
column: 31

0 commit comments

Comments
 (0)