Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/rules/prefer-define-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ module.exports = {
let defineOptionsNode = null
/** @type {ExportDefaultDeclaration | null} */
let exportDefaultDeclaration = null
/** @type {ImportDeclaration|null} */
let lastImportDeclaration = null

return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
ImportDeclaration(node) {
lastImportDeclaration = node
},
onDefineOptionsEnter(node) {
defineOptionsNode = node
}
Expand Down Expand Up @@ -109,10 +114,13 @@ module.exports = {
})
}

/** @type {VStartTag | ImportDeclaration} */
const insertAfterTag = lastImportDeclaration || scriptSetup.startTag

return [
fixer.removeRange(removeRange),
fixer.insertTextAfter(
scriptSetup.startTag,
insertAfterTag,
`\ndefineOptions(${sourceCode.getText(node.declaration)})\n`
)
]
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/prefer-define-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ defineOptions({ name: 'Foo' })
line: 4
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import { ref } from 'vue'
const props = defineProps(['foo'])
</script>
<script>
export default { name: 'Foo' }
</script>
`,
output: `
<script setup>
import { ref } from 'vue'
defineOptions({ name: 'Foo' })

const props = defineProps(['foo'])
</script>
`,
errors: [
{
message: 'Use `defineOptions` instead of default export.',
line: 7
}
]
}
]
})