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