-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathmatch-component-file-name.js
57 lines (48 loc) · 1.66 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
/**
* @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: 'recommended',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.4/docs/rules/match-component-file-name.md'
},
fixable: null,
schema: [
{ enum: ['jsx', 'both'] }
]
},
create (context) {
return utils.executeOnVueComponent(context, (object) => {
const nameProperty = object.properties.find((prop) => prop.key.name === 'name')
if (!nameProperty) {
return
}
const allowedExtensions = context.options[0] || 'jsx'
const name = nameProperty.value.value
const [, filename, extension] = /^(.+?)\.(.*)$/g.exec(path.basename(context.getFilename()))
if (extension === 'vue' && allowedExtensions !== 'both') {
return
}
if (name !== filename) {
context.report({
obj: object,
loc: object.loc,
message: 'Component name should match file name ({{filename}} / {{name}}).',
data: { filename, name }
})
}
})
}
}