|
| 1 | +/** |
| 2 | + * @author Yosuke Ota <https://github.com/ota-meshi> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + meta: { |
| 11 | + type: 'suggestion', |
| 12 | + docs: { |
| 13 | + description: 'enforce use of `defineOptions` instead of default export.', |
| 14 | + categories: undefined, |
| 15 | + url: 'https://eslint.vuejs.org/rules/prefer-define-options.html' |
| 16 | + }, |
| 17 | + fixable: 'code', |
| 18 | + schema: [], |
| 19 | + messages: { |
| 20 | + preferDefineOptions: 'Use `defineOptions` instead of default export.' |
| 21 | + } |
| 22 | + }, |
| 23 | + /** |
| 24 | + * @param {RuleContext} context |
| 25 | + * @returns {RuleListener} |
| 26 | + */ |
| 27 | + create(context) { |
| 28 | + const scriptSetup = utils.getScriptSetupElement(context) |
| 29 | + if (!scriptSetup) { |
| 30 | + return {} |
| 31 | + } |
| 32 | + |
| 33 | + /** @type {CallExpression | null} */ |
| 34 | + let defineOptionsNode = null |
| 35 | + /** @type {ExportDefaultDeclaration | null} */ |
| 36 | + let exportDefaultDeclaration = null |
| 37 | + |
| 38 | + return utils.compositingVisitors( |
| 39 | + utils.defineScriptSetupVisitor(context, { |
| 40 | + onDefineOptionsEnter(node) { |
| 41 | + defineOptionsNode = node |
| 42 | + } |
| 43 | + }), |
| 44 | + { |
| 45 | + ExportDefaultDeclaration(node) { |
| 46 | + exportDefaultDeclaration = node |
| 47 | + }, |
| 48 | + 'Program:exit'() { |
| 49 | + if (!exportDefaultDeclaration) { |
| 50 | + return |
| 51 | + } |
| 52 | + context.report({ |
| 53 | + node: exportDefaultDeclaration, |
| 54 | + messageId: 'preferDefineOptions', |
| 55 | + fix: defineOptionsNode |
| 56 | + ? null |
| 57 | + : buildFix(exportDefaultDeclaration, scriptSetup) |
| 58 | + }) |
| 59 | + } |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + /** |
| 64 | + * @param {ExportDefaultDeclaration} node |
| 65 | + * @param {VElement} scriptSetup |
| 66 | + * @returns {(fixer: RuleFixer) => Fix[]} |
| 67 | + */ |
| 68 | + function buildFix(node, scriptSetup) { |
| 69 | + return (fixer) => { |
| 70 | + const sourceCode = context.getSourceCode() |
| 71 | + |
| 72 | + // Calc remove range |
| 73 | + /** @type {Range} */ |
| 74 | + let removeRange = [...node.range] |
| 75 | + |
| 76 | + const script = scriptSetup.parent.children |
| 77 | + .filter(utils.isVElement) |
| 78 | + .find( |
| 79 | + (node) => |
| 80 | + node.name === 'script' && !utils.hasAttribute(node, 'setup') |
| 81 | + ) |
| 82 | + if ( |
| 83 | + script && |
| 84 | + script.endTag && |
| 85 | + sourceCode |
| 86 | + .getTokensBetween(script.startTag, script.endTag, { |
| 87 | + includeComments: true |
| 88 | + }) |
| 89 | + .every( |
| 90 | + (token) => |
| 91 | + removeRange[0] <= token.range[0] && |
| 92 | + token.range[1] <= removeRange[1] |
| 93 | + ) |
| 94 | + ) { |
| 95 | + removeRange = [...script.range] |
| 96 | + } |
| 97 | + const removeStartLoc = sourceCode.getLocFromIndex(removeRange[0]) |
| 98 | + if ( |
| 99 | + sourceCode.lines[removeStartLoc.line - 1] |
| 100 | + .slice(0, removeStartLoc.column) |
| 101 | + .trim() === '' |
| 102 | + ) { |
| 103 | + removeRange[0] = |
| 104 | + removeStartLoc.line === 1 |
| 105 | + ? 0 |
| 106 | + : sourceCode.getIndexFromLoc({ |
| 107 | + line: removeStartLoc.line - 1, |
| 108 | + column: sourceCode.lines[removeStartLoc.line - 2].length |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + return [ |
| 113 | + fixer.removeRange(removeRange), |
| 114 | + fixer.insertTextAfter( |
| 115 | + scriptSetup.startTag, |
| 116 | + `\ndefineOptions(${sourceCode.getText(node.declaration)})\n` |
| 117 | + ) |
| 118 | + ] |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments