Skip to content

Commit

Permalink
Insert defineOptions after import declarations in `vue/prefer-defin…
Browse files Browse the repository at this point in the history
…e-options` (#2275)
  • Loading branch information
ItMaga authored Oct 11, 2023
1 parent 8ba6d9a commit 15b99af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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
}
]
}
]
})

0 comments on commit 15b99af

Please sign in to comment.