-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathjsx-pascal-case.js
164 lines (144 loc) · 4.46 KB
/
jsx-pascal-case.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
158
159
160
161
162
163
164
/**
* @fileoverview Enforce PascalCase for user-defined JSX components
* @author Jake Marsh
*/
'use strict';
const elementType = require('jsx-ast-utils/elementType');
const minimatch = require('minimatch');
const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
function testDigit(char) {
const charCode = char.charCodeAt(0);
return charCode >= 48 && charCode <= 57;
}
function testUpperCase(char) {
const upperCase = char.toUpperCase();
return char === upperCase && upperCase !== char.toLowerCase();
}
function testLowerCase(char) {
const lowerCase = char.toLowerCase();
return char === lowerCase && lowerCase !== char.toUpperCase();
}
function testPascalCase(name) {
if (!testUpperCase(name.charAt(0))) {
return false;
}
const anyNonAlphaNumeric = Array.prototype.some.call(
name.slice(1),
(char) => char.toLowerCase() === char.toUpperCase() && !testDigit(char)
);
if (anyNonAlphaNumeric) {
return false;
}
return Array.prototype.some.call(
name.slice(1),
(char) => testLowerCase(char) || testDigit(char)
);
}
function testAllCaps(name) {
const firstChar = name.charAt(0);
if (!(testUpperCase(firstChar) || testDigit(firstChar))) {
return false;
}
for (let i = 1; i < name.length - 1; i += 1) {
const char = name.charAt(i);
if (!(testUpperCase(char) || testDigit(char) || char === '_')) {
return false;
}
}
const lastChar = name.charAt(name.length - 1);
if (!(testUpperCase(lastChar) || testDigit(lastChar))) {
return false;
}
return true;
}
function ignoreCheck(ignore, name) {
return ignore.some(
(entry) => name === entry || minimatch(name, entry, { noglobstar: true })
);
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
usePascalCase: 'Imported JSX component {{name}} must be in PascalCase',
usePascalOrSnakeCase: 'Imported JSX component {{name}} must be in PascalCase or SCREAMING_SNAKE_CASE',
};
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce PascalCase for user-defined JSX components',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-pascal-case'),
},
messages,
schema: [{
type: 'object',
properties: {
allowAllCaps: {
type: 'boolean',
},
allowLeadingUnderscore: {
type: 'boolean',
},
allowNamespace: {
type: 'boolean',
},
ignore: {
items: [
{
type: 'string',
},
],
minItems: 0,
type: 'array',
uniqueItems: true,
},
},
additionalProperties: false,
}],
},
create(context) {
const configuration = context.options[0] || {};
const allowAllCaps = configuration.allowAllCaps || false;
const allowLeadingUnderscore = configuration.allowLeadingUnderscore || false;
const allowNamespace = configuration.allowNamespace || false;
const ignore = configuration.ignore || [];
return {
JSXOpeningElement(node) {
const isCompatTag = jsxUtil.isDOMComponent(node);
if (isCompatTag) return undefined;
const name = elementType(node);
let checkNames = [name];
let index = 0;
if (name.lastIndexOf(':') > -1) {
checkNames = name.split(':');
} else if (name.lastIndexOf('.') > -1) {
checkNames = name.split('.');
}
do {
const splitName = checkNames[index];
if (splitName.length === 1) return undefined;
const isIgnored = ignoreCheck(ignore, splitName);
const checkName = allowLeadingUnderscore && splitName.startsWith('_') ? splitName.slice(1) : splitName;
const isPascalCase = testPascalCase(checkName);
const isAllowedAllCaps = allowAllCaps && testAllCaps(checkName);
if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
const messageId = allowAllCaps ? 'usePascalOrSnakeCase' : 'usePascalCase';
report(context, messages[messageId], messageId, {
node,
data: {
name: splitName,
},
});
break;
}
index++;
} while (index < checkNames.length && !allowNamespace);
},
};
},
};