-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathno-unknown-property.ts
More file actions
224 lines (196 loc) · 6.3 KB
/
Copy pathno-unknown-property.ts
File metadata and controls
224 lines (196 loc) · 6.3 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Ported from https://github.com/jsx-eslint/eslint-plugin-react/blob/master/lib/rules/no-unknown-property.js
import { createRule } from "@/utils/create-rule";
import { type RuleContext, type RuleFeature, merge } from "@eslint-react/eslint";
import {
getAttributeTagsMap,
getStandardName,
getTagName,
getText,
has,
hasUpperCaseCharacter,
isValidAriaAttribute,
isValidDataAttribute,
isValidHTMLTagInJSX,
normalizeAttributeCase,
tagNameHasDot,
} from "./lib";
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
export const RULE_NAME = "no-unknown-property";
export const RULE_FEATURES = [
"FIX",
"CFG",
] as const satisfies RuleFeature[];
// ------------------------------------------------------------------------------
// Types
// ------------------------------------------------------------------------------
type MessageID =
| "dataLowercaseRequired"
| "invalidPropOnTag"
| "unknownProp"
| "unknownPropWithStandardName";
interface Options {
ignore?: string[];
requireDataLowercase?: boolean;
}
// ------------------------------------------------------------------------------
// Default Options
// ------------------------------------------------------------------------------
const DEFAULTS: {
ignore: string[];
requireDataLowercase: boolean;
} = {
ignore: [],
requireDataLowercase: false,
};
// ------------------------------------------------------------------------------
// Rule Definition & Implementation
// ------------------------------------------------------------------------------
const messages = {
dataLowercaseRequired:
"React does not recognize data-* props with uppercase characters on a DOM element. Found '{{name}}', use '{{lowerCaseName}}' instead",
invalidPropOnTag:
"Invalid property '{{name}}' found on tag '{{tagName}}', but it is only allowed on: {{allowedTags}}",
unknownProp: "Unknown property '{{name}}' found",
unknownPropWithStandardName: "Unknown property '{{name}}' found, use '{{standardName}}' instead",
};
export default createRule({
meta: {
type: "problem",
docs: {
description: "Disallows unknown 'DOM' properties.",
},
fixable: "code",
messages,
schema: [{
type: "object",
additionalProperties: false,
properties: {
ignore: {
type: "array",
items: {
type: "string",
},
},
requireDataLowercase: {
type: "boolean",
default: false,
},
},
}],
},
name: RULE_NAME,
create,
defaultOptions: [],
});
/**
* Create function for the ESLint rule
* @param context ESLint rule context
* @returns Rule listener
*/
export function create(context: RuleContext<MessageID, Options[]>) {
/**
* Gets the ignore configuration from rule options
* @returns Array of attribute names to ignore
*/
function getIgnoreConfig(): string[] {
return context.options[0]?.ignore ?? DEFAULTS.ignore;
}
/**
* Gets the requireDataLowercase option from rule options
* @returns Whether data attributes must be lowercase
*/
function getRequireDataLowercase(): boolean {
return context.options[0]?.requireDataLowercase ?? DEFAULTS.requireDataLowercase;
}
return merge(
{
JSXAttribute(node): void {
const ignoreNames: string[] = getIgnoreConfig();
const actualName: string = getText(context, node.name);
// Skip checking if the attribute name is in the ignore list
if (ignoreNames.includes(actualName)) {
return;
}
const name: string = normalizeAttributeCase(actualName);
// Ignore tags like <Foo.bar />
if (tagNameHasDot(node)) {
return;
}
// Handle data-* attributes
if (isValidDataAttribute(name)) {
if (getRequireDataLowercase() && hasUpperCaseCharacter(name)) {
context.report({
data: {
name: actualName,
lowerCaseName: actualName.toLowerCase(),
},
messageId: "dataLowercaseRequired",
node,
});
}
return;
}
// Handle ARIA attributes
if (isValidAriaAttribute(name)) return;
const tagName: string | null = getTagName(node);
// Special case for fbt/fbs nodes
if (tagName === "fbt" || tagName === "fbs") return;
// Only validate HTML/DOM elements, not React components
if (!isValidHTMLTagInJSX(node)) return;
// Check if attribute is allowed only on specific tags
const attributeTagsMap = getAttributeTagsMap(context);
const allowedTags = has(attributeTagsMap, name)
? attributeTagsMap[name]
: null;
if (tagName != null && allowedTags != null) {
// Report if attribute is used on a tag where it's not allowed
if (!allowedTags.includes(tagName)) {
context.report({
data: {
name: actualName,
allowedTags: allowedTags.join(", "),
tagName,
},
messageId: "invalidPropOnTag",
node,
});
}
return;
}
// Check if the attribute name is similar to a standard property name
const standardName: string | null = getStandardName(name, context);
const hasStandardNameButIsNotUsed = standardName != null && standardName !== name;
const usesStandardName = standardName != null && standardName === name;
if (usesStandardName) {
// Attribute name is correct, nothing to do
return;
}
if (hasStandardNameButIsNotUsed) {
// Suggest the correct standard name
context.report({
data: {
name: actualName,
standardName,
},
fix(fixer) {
return fixer.replaceText(node.name, standardName);
},
messageId: "unknownPropWithStandardName",
node,
});
return;
}
// Report unknown attribute
context.report({
data: {
name: actualName,
},
messageId: "unknownProp",
node,
});
},
},
);
}