/
match-component-file-name.js
157 lines (139 loc) · 4.01 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @fileoverview Require component name property to match its file name
* @author Rodrigo Pedra Brum <rodrigo.pedra@gmail.com>
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
const casing = require('../utils/casing')
const path = require('path')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* @param {Expression | SpreadElement} node
* @returns {node is (Literal | TemplateLiteral)}
*/
function canVerify(node) {
return (
node.type === 'Literal' ||
(node.type === 'TemplateLiteral' &&
node.expressions.length === 0 &&
node.quasis.length === 1)
)
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require component name property to match its file name',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/match-component-file-name.html'
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
extensions: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true,
additionalItems: false
},
shouldMatchCase: {
type: 'boolean'
}
},
additionalProperties: false
}
]
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0]
const shouldMatchCase = (options && options.shouldMatchCase) || false
const extensionsArray = options && options.extensions
const allowedExtensions = Array.isArray(extensionsArray)
? extensionsArray
: ['jsx']
const extension = path.extname(context.getFilename())
const filename = path.basename(context.getFilename(), extension)
/** @type {Rule.ReportDescriptor[]} */
const errors = []
let componentCount = 0
if (!allowedExtensions.includes(extension.replace(/^\./, ''))) {
return {}
}
// ----------------------------------------------------------------------
// Private
// ----------------------------------------------------------------------
/**
* @param {string} name
* @param {string} filename
*/
function compareNames(name, filename) {
if (shouldMatchCase) {
return name === filename
}
return (
casing.pascalCase(name) === filename ||
casing.kebabCase(name) === filename
)
}
/**
* @param {Literal | TemplateLiteral} node
*/
function verifyName(node) {
let name
let quote = ''
if (node.type === 'TemplateLiteral') {
const quasis = node.quasis[0]
name = quasis.value.cooked
quote = '`'
} else {
name = `${node.value}`
quote = node.raw[0]
}
if (!compareNames(name, filename)) {
errors.push({
node,
message:
'Component name `{{name}}` should match file name `{{filename}}`.',
data: { filename, name },
fix(fixer) {
return fixer.replaceText(node, `${quote}${filename}${quote}`)
}
})
}
}
return Object.assign(
{},
utils.executeOnCallVueComponent(context, (node) => {
if (node.arguments.length === 2) {
const argument = node.arguments[0]
if (canVerify(argument)) {
verifyName(argument)
}
}
}),
utils.executeOnVue(context, (object) => {
const node = utils.findProperty(object, 'name')
componentCount++
if (!node) return
if (!canVerify(node.value)) return
verifyName(node.value)
}),
{
'Program:exit'() {
if (componentCount > 1) return
for (const error of errors) context.report(error)
}
}
)
}
}