diff --git a/lib/rules/prefer-define-options.js b/lib/rules/prefer-define-options.js index cd9240445..90c83fa9a 100644 --- a/lib/rules/prefer-define-options.js +++ b/lib/rules/prefer-define-options.js @@ -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 } @@ -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` ) ] diff --git a/tests/lib/rules/prefer-define-options.js b/tests/lib/rules/prefer-define-options.js index 2fd9bdfec..f3d9d88b0 100644 --- a/tests/lib/rules/prefer-define-options.js +++ b/tests/lib/rules/prefer-define-options.js @@ -104,6 +104,32 @@ defineOptions({ name: 'Foo' }) line: 4 } ] + }, + { + filename: 'test.vue', + code: ` + + + `, + output: ` + + `, + errors: [ + { + message: 'Use `defineOptions` instead of default export.', + line: 7 + } + ] } ] })