-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathmatch-component-file-name.js
80 lines (70 loc) · 2.24 KB
/
match-component-file-name.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
/**
* @fileoverview Require component name property to match its file name
* @author Rodrigo Pedra Brum <[email protected]>
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
const path = require('path')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'require component name property to match its file name',
category: undefined,
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.4/docs/rules/match-component-file-name.md'
},
fixable: null,
schema: [
{
type: 'array',
items: {
type: 'string'
},
uniqueItems: true,
additionalItems: false
}
]
},
create (context) {
const options = context.options[0]
const allowedExtensions = Array.isArray(options) ? options : ['jsx']
return utils.executeOnVue(context, (object) => {
const nameProperty = object.properties
.find(item =>
item.type === 'Property' &&
item.key.name === 'name' &&
item.key.type === 'Identifier' &&
(
item.value.type === 'Literal' || (
item.value.type === 'TemplateLiteral' &&
item.value.expressions.length === 0 &&
item.value.quasis.length === 1
)
)
)
if (!nameProperty) {
return
}
const name = nameProperty.value.type === 'TemplateLiteral'
? nameProperty.quasis[0].value.cooked
: nameProperty.value.value
const [, filename, extension] = /^(.+?)\.(.+)$/g.exec(path.basename(context.getFilename()))
if (!allowedExtensions.includes(extension)) {
return
}
if (name !== filename) {
context.report({
obj: object,
loc: object.loc,
message: 'Component name `{{name}}` should match file name {{filename}}.',
data: { filename, name }
})
}
})
}
}