Skip to content

Commit 49fda12

Browse files
committed
[types] add type annotations
1 parent a2306e7 commit 49fda12

31 files changed

+143
-57
lines changed

lib/rules/button-has-type.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const messages = {
2828
forbiddenValue: '"{{value}}" is an invalid value for button type attribute',
2929
};
3030

31+
/** @type { import('eslint').Rule.RuleModule } */
3132
module.exports = {
3233
meta: {
3334
docs: {
@@ -149,9 +150,11 @@ module.exports = {
149150
}
150151

151152
const props = node.arguments[1].properties;
152-
const typeProp = props.find((prop) => prop.key && prop.key.name === 'type');
153+
const typeProp = props.find(
154+
(prop) => prop.type === 'Property' && prop.key && prop.key.type === 'Identifier' && prop.key.name === 'type'
155+
);
153156

154-
if (!typeProp) {
157+
if (!typeProp || typeProp.type !== 'Property') {
155158
reportMissing(node);
156159
return;
157160
}

lib/rules/checked-requires-onchange-or-readonly.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const defaultOptions = {
2424
};
2525

2626
/**
27-
* @param {string[]} properties
27+
* @param {object[]} properties
2828
* @param {string} keyName
2929
* @returns {Set<string>}
3030
*/
@@ -41,6 +41,7 @@ function extractTargetProps(properties, keyName) {
4141
);
4242
}
4343

44+
/** @type { import('eslint').Rule.RuleModule } */
4445
module.exports = {
4546
meta: {
4647
docs: {

lib/rules/forbid-elements.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const messages = {
2020
forbiddenElement_message: '<{{element}}> is forbidden, {{message}}',
2121
};
2222

23+
/** @type { import('eslint').Rule.RuleModule } */
2324
module.exports = {
2425
meta: {
2526
docs: {
@@ -105,13 +106,11 @@ module.exports = {
105106
return;
106107
}
107108

108-
const argType = argument.type;
109-
110-
if (argType === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
109+
if (argument.type === 'Identifier' && /^[A-Z_]/.test(argument.name)) {
111110
reportIfForbidden(argument.name, argument);
112-
} else if (argType === 'Literal' && /^[a-z][^.]*$/.test(argument.value)) {
111+
} else if (argument.type === 'Literal' && /^[a-z][^.]*$/.test(String(argument.value))) {
113112
reportIfForbidden(argument.value, argument);
114-
} else if (argType === 'MemberExpression') {
113+
} else if (argument.type === 'MemberExpression') {
115114
reportIfForbidden(getText(context, argument), argument);
116115
}
117116
},

lib/rules/forbid-foreign-prop-types.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const messages = {
1313
forbiddenPropType: 'Using propTypes from another component is not safe because they may be removed in production builds',
1414
};
1515

16+
/** @type { import('eslint').Rule.RuleModule } */
1617
module.exports = {
1718
meta: {
1819
docs: {
@@ -108,8 +109,8 @@ module.exports = {
108109
&& !ast.isAssignmentLHS(node)
109110
&& !isAllowedAssignment(node)
110111
)) || (
111-
(node.property.type === 'Literal' || node.property.type === 'JSXText')
112-
&& node.property.value === 'propTypes'
112+
// @ts-expect-error The JSXText type is not present in the estree type definitions
113+
(node.property.type === 'Literal' || node.property.type === 'JSXText') && node.property.value === 'propTypes'
113114
&& !ast.isAssignmentLHS(node)
114115
&& !isAllowedAssignment(node)
115116
)
@@ -121,7 +122,7 @@ module.exports = {
121122
},
122123

123124
ObjectPattern(node) {
124-
const propTypesNode = node.properties.find((property) => property.type === 'Property' && property.key.name === 'propTypes');
125+
const propTypesNode = node.properties.find((property) => property.type === 'Property' && property.key.type === 'Identifier' && property.key.name === 'propTypes');
125126

126127
if (propTypesNode) {
127128
report(context, messages.forbiddenPropType, 'forbiddenPropType', {

lib/rules/forbid-prop-types.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const messages = {
2626
forbiddenPropType: 'Prop type "{{target}}" is forbidden',
2727
};
2828

29+
/** @type { import('eslint').Rule.RuleModule } */
2930
module.exports = {
3031
meta: {
3132
docs: {
@@ -192,7 +193,7 @@ module.exports = {
192193
}
193194
if (node.specifiers.length >= 1) {
194195
const propTypesSpecifier = node.specifiers.find((specifier) => (
195-
specifier.imported && specifier.imported.name === 'PropTypes'
196+
specifier.type === 'ImportSpecifier' && specifier.imported && specifier.imported.name === 'PropTypes'
196197
));
197198
if (propTypesSpecifier) {
198199
propTypesPackageName = propTypesSpecifier.local.name;
@@ -227,13 +228,17 @@ module.exports = {
227228
) {
228229
return;
229230
}
231+
if (node.parent.type !== 'AssignmentExpression') {
232+
return;
233+
}
230234

231235
checkNode(node.parent.right);
232236
},
233237

234238
CallExpression(node) {
235239
if (
236-
node.callee.object
240+
node.callee.type === 'MemberExpression'
241+
&& node.callee.object
237242
&& !isPropTypesPackage(node.callee.object)
238243
&& !propsUtil.isPropTypesDeclaration(node.callee)
239244
) {
@@ -242,7 +247,8 @@ module.exports = {
242247

243248
if (
244249
node.arguments.length > 0
245-
&& (node.callee.name === 'shape' || astUtil.getPropertyName(node.callee) === 'shape')
250+
&& ((node.callee.type === 'Identifier' && node.callee.name === 'shape') || astUtil.getPropertyName(node.callee) === 'shape')
251+
&& node.arguments[0].type === 'ObjectExpression'
246252
) {
247253
checkProperties(node.arguments[0].properties);
248254
}
@@ -267,7 +273,7 @@ module.exports = {
267273

268274
ObjectExpression(node) {
269275
node.properties.forEach((property) => {
270-
if (!property.key) {
276+
if (property.type !== 'Property') {
271277
return;
272278
}
273279

lib/rules/hook-use-state.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const messages = {
2626
suggestMemo: 'Replace useState call with useMemo',
2727
};
2828

29+
/** @type { import('eslint').Rule.RuleModule } */
2930
module.exports = {
3031
meta: {
3132
docs: {

lib/rules/jsx-closing-bracket-location.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const messages = {
2020
bracketLocation: 'The closing bracket must be {{location}}{{details}}',
2121
};
2222

23+
/** @type { import('eslint').Rule.RuleModule } */
2324
module.exports = {
2425
meta: {
2526
docs: {

lib/rules/jsx-curly-spacing.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const messages = {
3535
spaceNeededBefore: 'A space is required before \'{{token}}\'',
3636
};
3737

38+
/** @type { import('eslint').Rule.RuleModule } */
3839
module.exports = {
3940
meta: {
4041
docs: {
@@ -393,7 +394,8 @@ module.exports = {
393394
if (spacing === SPACING.always) {
394395
if (!sourceCode.isSpaceBetweenTokens(first, second)) {
395396
reportRequiredBeginningSpace(node, first);
396-
} else if (!config.allowMultiline && isMultiline(first, second)) {
397+
} else if (!config.allowMultiline && isMultiline(first, second)
398+
) {
397399
reportNoBeginningNewline(node, first, spacing);
398400
}
399401
if (!sourceCode.isSpaceBetweenTokens(penultimate, last)) {
@@ -410,9 +412,10 @@ module.exports = {
410412
reportNoBeginningSpace(node, first);
411413
}
412414
if (isMultiline(penultimate, last)) {
413-
if (!config.allowMultiline) {
414-
reportNoEndingNewline(node, last, spacing);
415+
if (config.allowMultiline) {
416+
return;
415417
}
418+
reportNoEndingNewline(node, last, spacing);
416419
} else if (sourceCode.isSpaceBetweenTokens(penultimate, last)) {
417420
reportNoEndingSpace(node, last);
418421
}

lib/rules/jsx-equals-spacing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const messages = {
2020
needSpaceAfter: 'A space is required after \'=\'',
2121
};
2222

23+
/** @type { import('eslint').Rule.RuleModule } */
2324
module.exports = {
2425
meta: {
2526
docs: {

lib/rules/jsx-fragments.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const messages = {
2828
preferFragment: 'Prefer fragment shorthand over {{react}}.{{fragment}}',
2929
};
3030

31+
/** @type { import('eslint').Rule.RuleModule } */
3132
module.exports = {
3233
meta: {
3334
docs: {
@@ -170,7 +171,7 @@ module.exports = {
170171
ImportDeclaration(node) {
171172
if (node.source && node.source.value === 'react') {
172173
node.specifiers.forEach((spec) => {
173-
if (spec.imported && spec.imported.name === fragmentPragma) {
174+
if (spec.type === "ImportSpecifier" && spec.imported && spec.imported.name === fragmentPragma) {
174175
if (spec.local) {
175176
fragmentNames.add(spec.local.name);
176177
}

0 commit comments

Comments
 (0)