Skip to content

Commit e049316

Browse files
committed
update
1 parent 204cca9 commit e049316

20 files changed

+360
-340
lines changed

lib/rules/comment-directive.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
'use strict'
77

8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const utils = require('../utils')
13+
814
/**
915
* @typedef {object} RuleAndLocation
1016
* @property {string} RuleAndLocation.ruleId
@@ -252,15 +258,7 @@ function locToKey(location) {
252258
* @returns {VElement[]} The top-level elements
253259
*/
254260
function extractTopLevelHTMLElements(documentFragment) {
255-
return documentFragment.children.filter(isVElement)
256-
257-
/**
258-
* @param {any} e
259-
* @returns {e is VElement}
260-
*/
261-
function isVElement(e) {
262-
return e.type === 'VElement'
263-
}
261+
return documentFragment.children.filter(utils.isVElement)
264262
}
265263
/**
266264
* Extracts the top-level comments in document fragment.

lib/rules/component-definition-name-casing.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,10 @@ module.exports = {
9797
}
9898
}),
9999
utils.executeOnVue(context, (obj) => {
100-
const node = obj.properties.find(
101-
/**
102-
* @param {ObjectExpression['properties'][0]} item
103-
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
104-
*/
105-
(item) =>
106-
item.type === 'Property' &&
107-
utils.getStaticPropertyName(item) === 'name' &&
108-
canConvert(item.value)
109-
)
100+
const node = utils.findProperty(obj, 'name')
110101

111102
if (!node) return
103+
if (!canConvert(node.value)) return
112104
convertName(node.value)
113105
})
114106
)

lib/rules/component-tags-order.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ module.exports = {
8181

8282
function getTopLevelHTMLElements() {
8383
if (documentFragment) {
84-
return documentFragment.children.filter(
85-
/**
86-
* @param {VElement | VText | VExpressionContainer} e
87-
* @returns {e is VElement}
88-
*/
89-
(e) => e.type === 'VElement'
90-
)
84+
return documentFragment.children.filter(utils.isVElement)
9185
}
9286
return []
9387
}

lib/rules/custom-event-name-casing.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,9 @@ module.exports = {
131131
const contextReferenceIds = new Set()
132132
const emitReferenceIds = new Set()
133133
if (contextParam.type === 'ObjectPattern') {
134-
const emitProperty = contextParam.properties.find(
135-
/**
136-
* @param {ESNode} p
137-
* @returns {p is AssignmentProperty}
138-
*/
139-
(p) =>
140-
p.type === 'Property' &&
141-
utils.getStaticPropertyName(p) === 'emit'
134+
const emitProperty = utils.findAssignmentProperty(
135+
contextParam,
136+
'emit'
142137
)
143138
if (!emitProperty || emitProperty.value.type !== 'Identifier') {
144139
return

lib/rules/match-component-file-name.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,12 @@ module.exports = {
131131
}
132132
}),
133133
utils.executeOnVue(context, (object) => {
134-
const node = object.properties.find(
135-
/**
136-
* @param {ObjectExpression['properties'][0]} item
137-
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
138-
*/
139-
(item) =>
140-
item.type === 'Property' &&
141-
utils.getStaticPropertyName(item) === 'name' &&
142-
canVerify(item.value)
143-
)
134+
const node = utils.findProperty(object, 'name')
144135

145136
componentCount++
146137

147138
if (!node) return
139+
if (!canVerify(node.value)) return
148140
verifyName(node.value)
149141
}),
150142
{

lib/rules/name-property-casing.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,28 @@ module.exports = {
4141
// ----------------------------------------------------------------------
4242

4343
return utils.executeOnVue(context, (obj) => {
44-
const node = obj.properties.find(
45-
/**
46-
* @param {ESNode} item
47-
* @returns {item is Property & { value : Literal } }
48-
*/
49-
(item) =>
50-
item.type === 'Property' &&
51-
utils.getStaticPropertyName(item) === 'name' &&
52-
item.value.type === 'Literal'
53-
)
44+
const node = utils.findProperty(obj, 'name')
5445

5546
if (!node) return
47+
const valueNode = node.value
48+
if (valueNode.type !== 'Literal') return
5649

57-
if (!casing.getChecker(caseType)(`${node.value.value}`)) {
58-
const value = casing.getExactConverter(caseType)(`${node.value.value}`)
50+
if (!casing.getChecker(caseType)(`${valueNode.value}`)) {
51+
const value = casing.getExactConverter(caseType)(`${valueNode.value}`)
5952
context.report({
60-
node: node.value,
53+
node: valueNode,
6154
message: 'Property name "{{value}}" is not {{caseType}}.',
6255
data: {
63-
value: `${node.value.value}`,
56+
value: `${valueNode.value}`,
6457
caseType
6558
},
6659
fix: (fixer) =>
6760
fixer.replaceText(
68-
node.value,
61+
valueNode,
6962
context
7063
.getSourceCode()
71-
.getText(node.value)
72-
.replace(`${node.value.value}`, value)
64+
.getText(valueNode)
65+
.replace(`${valueNode.value}`, value)
7366
)
7467
})
7568
}

lib/rules/no-arrow-functions-in-watch.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@ module.exports = {
1919
/** @param {RuleContext} context */
2020
create(context) {
2121
return utils.executeOnVue(context, (obj) => {
22-
const watchNode = obj.properties.find(
23-
/**
24-
* @param {ESNode} property
25-
* @returns {property is Property}
26-
*/
27-
(property) =>
28-
property.type === 'Property' &&
29-
utils.getStaticPropertyName(property) === 'watch'
30-
)
22+
const watchNode = utils.findProperty(obj, 'watch')
3123
if (watchNode == null) {
3224
return
3325
}

lib/rules/no-boolean-default.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,7 @@ function getBooleanProps(props) {
5353
* @param {ObjectExpressionProp} propDef
5454
*/
5555
function getDefaultNode(propDef) {
56-
return propDef.value.properties.find(
57-
/**
58-
* @param {ESNode} p
59-
* @returns {p is Property}
60-
*/
61-
(p) => {
62-
return (
63-
p.type === 'Property' &&
64-
p.key.type === 'Identifier' &&
65-
p.key.name === 'default'
66-
)
67-
}
68-
)
56+
return utils.findProperty(propDef.value, 'default')
6957
}
7058

7159
module.exports = {

lib/rules/no-deprecated-data-object-declaration.js

+21-26
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,29 @@ module.exports = {
6767
const sourceCode = context.getSourceCode()
6868

6969
return utils.executeOnVue(context, (obj) => {
70-
obj.properties
71-
.filter(
72-
/**
73-
* @param {ESNode} p
74-
* @returns {p is Property}
75-
*/
76-
(p) =>
77-
p.type === 'Property' &&
78-
p.key.type === 'Identifier' &&
79-
p.key.name === 'data' &&
80-
p.value.type !== 'FunctionExpression' &&
81-
p.value.type !== 'ArrowFunctionExpression' &&
82-
p.value.type !== 'Identifier'
83-
)
84-
.forEach((p) => {
85-
context.report({
86-
node: p,
87-
messageId: 'objectDeclarationIsDeprecated',
88-
fix(fixer) {
89-
const tokens = getFirstAndLastTokens(p.value, sourceCode)
70+
const invalidData = utils.findProperty(
71+
obj,
72+
'data',
73+
(p) =>
74+
p.value.type !== 'FunctionExpression' &&
75+
p.value.type !== 'ArrowFunctionExpression' &&
76+
p.value.type !== 'Identifier'
77+
)
9078

91-
return [
92-
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
93-
fixer.insertTextAfter(tokens.last, ';\n}')
94-
]
95-
}
96-
})
79+
if (invalidData) {
80+
context.report({
81+
node: invalidData,
82+
messageId: 'objectDeclarationIsDeprecated',
83+
fix(fixer) {
84+
const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)
85+
86+
return [
87+
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
88+
fixer.insertTextAfter(tokens.last, ';\n}')
89+
]
90+
}
9791
})
92+
}
9893
})
9994
}
10095
}

lib/rules/no-duplicate-attr-inheritance.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ module.exports = {
3232

3333
return Object.assign(
3434
utils.executeOnVue(context, (node) => {
35-
const inheritAttrsProp = node.properties.find(
36-
/** @param {ESNode} prop @returns {prop is Property} */
37-
(prop) =>
38-
prop.type === 'Property' &&
39-
utils.getStaticPropertyName(prop) === 'inheritAttrs'
40-
)
35+
const inheritAttrsProp = utils.findProperty(node, 'inheritAttrs')
4136

4237
if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
4338
inheritsAttrs = inheritAttrsProp.value.value

lib/rules/no-reserved-component-names.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,10 @@ module.exports = {
180180
.filter(({ name }) => reservedNames.has(name))
181181
.forEach(({ node, name }) => report(node, name))
182182

183-
const node = obj.properties.find(
184-
/**
185-
* @param {ObjectExpression['properties'][0]} item
186-
* @returns {item is Property & {value: (Literal | TemplateLiteral)}}
187-
*/
188-
(item) =>
189-
item.type === 'Property' &&
190-
item.key.type === 'Identifier' &&
191-
item.key.name === 'name' &&
192-
canVerify(item.value)
193-
)
183+
const node = utils.findProperty(obj, 'name')
194184

195185
if (!node) return
186+
if (!canVerify(node.value)) return
196187
reportIfInvalid(node.value)
197188
})
198189
)

lib/rules/no-shared-component-data.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,28 @@ module.exports = {
5757
const sourceCode = context.getSourceCode()
5858

5959
return utils.executeOnVueComponent(context, (obj) => {
60-
obj.properties
61-
.filter(
62-
/**
63-
* @param {ESNode} p
64-
* @returns {p is Property}
65-
*/
66-
(p) =>
67-
p.type === 'Property' &&
68-
p.key.type === 'Identifier' &&
69-
p.key.name === 'data' &&
70-
p.value.type !== 'FunctionExpression' &&
71-
p.value.type !== 'ArrowFunctionExpression' &&
72-
p.value.type !== 'Identifier'
73-
)
74-
.forEach((p) => {
75-
context.report({
76-
node: p,
77-
message: '`data` property in component must be a function.',
78-
fix(fixer) {
79-
const tokens = getFirstAndLastTokens(p.value, sourceCode)
60+
const invalidData = utils.findProperty(
61+
obj,
62+
'data',
63+
(p) =>
64+
p.value.type !== 'FunctionExpression' &&
65+
p.value.type !== 'ArrowFunctionExpression' &&
66+
p.value.type !== 'Identifier'
67+
)
68+
if (invalidData) {
69+
context.report({
70+
node: invalidData,
71+
message: '`data` property in component must be a function.',
72+
fix(fixer) {
73+
const tokens = getFirstAndLastTokens(invalidData.value, sourceCode)
8074

81-
return [
82-
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
83-
fixer.insertTextAfter(tokens.last, ';\n}')
84-
]
85-
}
86-
})
75+
return [
76+
fixer.insertTextBefore(tokens.first, 'function() {\nreturn '),
77+
fixer.insertTextAfter(tokens.last, ';\n}')
78+
]
79+
}
8780
})
81+
}
8882
})
8983
}
9084
}

lib/rules/order-in-components.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,7 @@ module.exports = {
250250
*/
251251
function checkOrder(propertiesNodes) {
252252
const properties = propertiesNodes
253-
.filter(
254-
/**
255-
* @param {ASTNode} property
256-
* @returns {property is Property}
257-
*/
258-
(property) => property.type === 'Property'
259-
)
253+
.filter(utils.isProperty)
260254
.map((property) => {
261255
return {
262256
node: property,

lib/rules/padding-line-between-blocks.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,7 @@ module.exports = {
151151
* @returns {VElement[]}
152152
*/
153153
function getTopLevelHTMLElements() {
154-
return documentFragment.children.filter(
155-
/**
156-
* @param {VElement | VExpressionContainer | VText} e
157-
* @returns {e is VElement}
158-
*/
159-
(e) => e.type === 'VElement'
160-
)
154+
return documentFragment.children.filter(utils.isVElement)
161155
}
162156

163157
/**

0 commit comments

Comments
 (0)