Skip to content

Commit

Permalink
feat: change to suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh committed Jan 7, 2024
1 parent c16c5e4 commit 3c704bb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 50 deletions.
23 changes: 13 additions & 10 deletions lib/rules/define-macros-order.js
Expand Up @@ -196,9 +196,14 @@ function create(context) {
node,
loc: node.loc,
messageId: 'defineExposeNotTheLast',
fix(fixer) {
return moveNodeToLast(fixer, node, lastNode)
}
suggest: [
{
messageId: 'putExposeAtTheLast',
fix(fixer) {
return moveNodeToLast(fixer, node, lastNode)
}
}
]
})
}

Expand Down Expand Up @@ -228,13 +233,8 @@ function create(context) {
node.range[0] - beforeNodeToken.range[1]
)

// insert position: after target and comments (if any)
const afterTargetComment = sourceCode.getTokenAfter(target, {
includeComments: true
})

return [
fixer.insertTextAfter(afterTargetComment, textNode),
fixer.insertTextAfter(target, textNode),
fixer.removeRange([cutStart, cutEnd])
]
}
Expand Down Expand Up @@ -322,6 +322,7 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/define-macros-order.html'
},
fixable: 'code',
hasSuggestions: true,
schema: [
{
type: 'object',
Expand All @@ -345,7 +346,9 @@ module.exports = {
macrosNotOnTop:
'{{macro}} should be the first statement in `<script setup>` (after any potential import statements or type definitions).',
defineExposeNotTheLast:
'`defineExpose` should be the last statement in `<script setup>`.'
'`defineExpose` should be the last statement in `<script setup>`.',
putExposeAtTheLast:
'Put `defineExpose` as the last statement in `<script setup>`.'
}
},
create
Expand Down
96 changes: 56 additions & 40 deletions tests/lib/rules/define-macros-order.js
Expand Up @@ -40,6 +40,9 @@ function message(macro) {
const defineExposeNotTheLast =
'`defineExpose` should be the last statement in `<script setup>`.'

const putExposeAtBottom =
'Put `defineExpose` as the last statement in `<script setup>`.'

tester.run('define-macros-order', rule, {
valid: [
{
Expand Down Expand Up @@ -678,58 +681,52 @@ tester.run('define-macros-order', rule, {
filename: 'test.vue',
code: `
<script setup>
defineProps({
test: Boolean
})
/** expose */
defineExpose({
foo: 'bar'
})
/** console start */
console.log('test')
/** console end */
</script>
`,
output: `
<script setup>
defineProps({
test: Boolean
})
/** console start */
console.log('test')
/** console end */
/** expose */
defineExpose({
foo: 'bar'
})
/** emits */
defineEmits(['update:foo'])
/** expose */
defineExpose({})
/** slots */
const slots = defineSlots()
</script>
`,
output: null,
options: optionsExposeLast,
errors: [
{
message: defineExposeNotTheLast,
line: 8
line: 6,
suggestions: [
{
desc: putExposeAtBottom,
output: `
<script setup>
/** emits */
defineEmits(['update:foo'])
/** slots */
const slots = defineSlots()
/** expose */
defineExpose({})
</script>
`
}
]
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
/** slots */
const slots = defineSlots()
/** options */
defineOptions({})
/** emits */
defineEmits(['update:foo'])
/** expose */
defineExpose({})
/** options */
defineOptions({})
/** props */
const props = defineProps(['foo'])
/** slots */
const slots = defineSlots()
</script>
`,
output: `
Expand All @@ -738,28 +735,47 @@ tester.run('define-macros-order', rule, {
defineOptions({})
/** emits */
defineEmits(['update:foo'])
/** expose */
defineExpose({})
/** props */
const props = defineProps(['foo'])
/** slots */
const slots = defineSlots()
/** expose */
defineExpose({})
</script>
`,
options: [
{
order: ['defineOptions', 'defineEmits', 'defineProps', 'defineSlots'],
order: ['defineOptions', 'defineEmits', 'defineProps'],
defineExposeLast: true
}
],
errors: [
{
message: message('defineOptions'),
line: 6
message: defineExposeNotTheLast,
line: 6,
suggestions: [
{
desc: putExposeAtBottom,
output: `
<script setup>
/** emits */
defineEmits(['update:foo'])
/** options */
defineOptions({})
/** props */
const props = defineProps(['foo'])
/** slots */
const slots = defineSlots()
/** expose */
defineExpose({})
</script>
`
}
]
},
{
message: defineExposeNotTheLast,
line: 10
message: message('defineOptions'),
line: 8
}
]
}
Expand Down

0 comments on commit 3c704bb

Please sign in to comment.