Skip to content

Commit 15b99af

Browse files
authored
Insert defineOptions after import declarations in vue/prefer-define-options (#2275)
1 parent 8ba6d9a commit 15b99af

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/rules/prefer-define-options.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ module.exports = {
3434
let defineOptionsNode = null
3535
/** @type {ExportDefaultDeclaration | null} */
3636
let exportDefaultDeclaration = null
37+
/** @type {ImportDeclaration|null} */
38+
let lastImportDeclaration = null
3739

3840
return utils.compositingVisitors(
3941
utils.defineScriptSetupVisitor(context, {
42+
ImportDeclaration(node) {
43+
lastImportDeclaration = node
44+
},
4045
onDefineOptionsEnter(node) {
4146
defineOptionsNode = node
4247
}
@@ -109,10 +114,13 @@ module.exports = {
109114
})
110115
}
111116

117+
/** @type {VStartTag | ImportDeclaration} */
118+
const insertAfterTag = lastImportDeclaration || scriptSetup.startTag
119+
112120
return [
113121
fixer.removeRange(removeRange),
114122
fixer.insertTextAfter(
115-
scriptSetup.startTag,
123+
insertAfterTag,
116124
`\ndefineOptions(${sourceCode.getText(node.declaration)})\n`
117125
)
118126
]

tests/lib/rules/prefer-define-options.js

+26
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,32 @@ defineOptions({ name: 'Foo' })
104104
line: 4
105105
}
106106
]
107+
},
108+
{
109+
filename: 'test.vue',
110+
code: `
111+
<script setup>
112+
import { ref } from 'vue'
113+
const props = defineProps(['foo'])
114+
</script>
115+
<script>
116+
export default { name: 'Foo' }
117+
</script>
118+
`,
119+
output: `
120+
<script setup>
121+
import { ref } from 'vue'
122+
defineOptions({ name: 'Foo' })
123+
124+
const props = defineProps(['foo'])
125+
</script>
126+
`,
127+
errors: [
128+
{
129+
message: 'Use `defineOptions` instead of default export.',
130+
line: 7
131+
}
132+
]
107133
}
108134
]
109135
})

0 commit comments

Comments
 (0)