Skip to content

Commit ebc02bd

Browse files
committed
wip
1 parent 8b60f25 commit ebc02bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+519
-344
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"ignoreConstructors": false,
2626
"avoidQuotes": false, // this is the override vs airbnb
2727
}],
28-
"max-len": [2, 140, {
28+
"max-len": [0, 140, {
2929
"ignoreStrings": true,
3030
"ignoreTemplateLiterals": true,
3131
"ignoreComments": true,

lib/rules/boolean-prop-naming.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
/**
150150
* Returns the name of the given node (prop)
151151
* @param {RuleProp} node The node we're getting the name of
152-
* @returns {string}
152+
* @returns {string | undefined}
153153
*/
154154
function getPropName(node) {
155155
// Due to this bug https://github.com/babel/babel-eslint/issues/307
@@ -185,7 +185,7 @@ module.exports = {
185185
function regularCheck(prop) {
186186
const propKey = getPropKey(prop);
187187
return (
188-
propKey
188+
!!propKey
189189
&& propTypeNames.indexOf(propKey) >= 0
190190
&& rule.test(getPropName(prop)) === false
191191
);
@@ -265,7 +265,7 @@ module.exports = {
265265
args.filter((arg) => arg.type === 'ObjectExpression').forEach((object) => validatePropNaming(node, object.properties));
266266
}
267267

268-
/** @type {(component: Components.Component) => GenericFlowNode} */
268+
/** @type {(component: Components.Component) => (GenericFlowNode | undefined)} */
269269
function getComponentTypeAnnotation(component) {
270270
// If this is a functional component that uses a global type, check it
271271
if (

lib/rules/button-has-type.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = {
9393
}
9494
}
9595

96+
/** @param {ASTNode} node @param {ASTNode} expression */
9697
function checkExpression(node, expression) {
9798
switch (expression.type) {
9899
case 'Literal':
@@ -115,8 +116,9 @@ module.exports = {
115116
}
116117

117118
return {
119+
/** @param {JSXElement} node */
118120
JSXElement(node) {
119-
if (node.openingElement.name.name !== 'button') {
121+
if ('name' in node.openingElement.name && node.openingElement.name.name !== 'button') {
120122
return;
121123
}
122124

@@ -135,6 +137,8 @@ module.exports = {
135137
const propValue = getLiteralPropValue(typeProp);
136138
checkValue(node, propValue);
137139
},
140+
141+
/** @param {CallExpression} node */
138142
CallExpression(node) {
139143
if (!isCreateElement(context, node) || node.arguments.length < 1) {
140144
return;

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55

66
'use strict';
77

8-
const ASTUtils = require('jsx-ast-utils');
8+
const { elementType } = require('jsx-ast-utils');
99

1010
const isCreateElement = require('../util/isCreateElement');
1111
const report = require('../util/report');
1212
const docsUrl = require('../util/docsUrl');
1313

14-
const messages = {
14+
const messages = /** @type {const} */ ({
1515
missingProperty: '`checked` should be used with either `onChange` or `readOnly`.',
1616
exclusiveCheckedAttribute: 'Use either `checked` or `defaultChecked`, but not both.',
17-
};
17+
});
1818

1919
const targetPropSet = new Set(['checked', 'onChange', 'readOnly', 'defaultChecked']);
2020

21-
const defaultOptions = {
21+
const defaultOptions = /** @type {const} */ ({
2222
ignoreMissingProperties: false,
2323
ignoreExclusiveCheckedAttribute: false,
24-
};
24+
});
2525

2626
/**
2727
* @param {object[]} properties
@@ -63,6 +63,7 @@ module.exports = {
6363
create(context) {
6464
const options = { ...defaultOptions, ...context.options[0] };
6565

66+
/** @param {JSXOpeningElement | CallExpression} node */
6667
function reportMissingProperty(node) {
6768
report(
6869
context,
@@ -72,6 +73,7 @@ module.exports = {
7273
);
7374
}
7475

76+
/** @param {JSXOpeningElement | CallExpression} node */
7577
function reportExclusiveCheckedAttribute(node) {
7678
report(
7779
context,
@@ -82,11 +84,10 @@ module.exports = {
8284
}
8385

8486
/**
85-
* @param {ASTNode} node
87+
* @param {JSXOpeningElement | CallExpression} node
8688
* @param {Set<string>} propSet
87-
* @returns {void}
8889
*/
89-
const checkAttributesAndReport = (node, propSet) => {
90+
function checkAttributesAndReport(node, propSet) {
9091
if (!propSet.has('checked')) {
9192
return;
9293
}
@@ -101,17 +102,19 @@ module.exports = {
101102
) {
102103
reportMissingProperty(node);
103104
}
104-
};
105+
}
105106

106107
return {
108+
/** @param {JSXOpeningElement} node */
107109
JSXOpeningElement(node) {
108-
if (ASTUtils.elementType(node) !== 'input') {
110+
if (elementType(node) !== 'input') {
109111
return;
110112
}
111113

112114
const propSet = extractTargetProps(node.attributes, 'name');
113115
checkAttributesAndReport(node, propSet);
114116
},
117+
115118
CallExpression(node) {
116119
if (!isCreateElement(context, node)) {
117120
return;

lib/rules/forbid-dom-props.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ const report = require('../util/report');
1212
// Constants
1313
// ------------------------------------------------------------------------------
1414

15+
/** @typedef {{ disallowList: null | string[], message: null | string }} ForbidConfig */
16+
17+
/** @type {(string | { propName: string, disallowedFor: string[], message: string })[]} */
1518
const DEFAULTS = [];
1619

1720
// ------------------------------------------------------------------------------
1821
// Rule Definition
1922
// ------------------------------------------------------------------------------
2023

2124
/**
22-
* @param {Map<string, object>} forbidMap // { disallowList: null | string[], message: null | string }
23-
* @param {string} prop
25+
* @param {Map<string, ForbidConfig>} forbidMap
26+
* @param {string | JSXIdentifier} prop
2427
* @param {string} tagName
2528
* @returns {boolean}
2629
*/
2730
function isForbidden(forbidMap, prop, tagName) {
28-
const options = forbidMap.get(prop);
29-
return options && (
31+
const options = typeof prop === 'string' && forbidMap.get(prop);
32+
return !!options && (
3033
typeof tagName === 'undefined'
3134
|| !options.disallowList
3235
|| options.disallowList.indexOf(tagName) !== -1
@@ -86,7 +89,8 @@ module.exports = {
8689

8790
create(context) {
8891
const configuration = context.options[0] || {};
89-
const forbid = new Map((configuration.forbid || DEFAULTS).map((value) => {
92+
/** @type {Map<string, ForbidConfig>} */
93+
const forbid = new Map(/** @type {typeof DEFAULTS} */ (configuration.forbid || DEFAULTS).map((value) => {
9094
const propName = typeof value === 'string' ? value : value.propName;
9195
return [propName, {
9296
disallowList: typeof value === 'string' ? null : (value.disallowedFor || null),
@@ -95,8 +99,9 @@ module.exports = {
9599
}));
96100

97101
return {
102+
/** @param {JSXAttribute} node */
98103
JSXAttribute(node) {
99-
const tag = node.parent.name.name;
104+
const tag = 'name' in node.parent.name && node.parent.name.name;
100105
if (!(tag && typeof tag === 'string' && tag[0] !== tag[0].toUpperCase())) {
101106
// This is a Component, not a DOM node, so exit.
102107
return;
@@ -108,9 +113,9 @@ module.exports = {
108113
return;
109114
}
110115

111-
const customMessage = forbid.get(prop).message;
116+
const customMessage = typeof prop === 'string' && forbid.get(prop).message;
112117

113-
report(context, customMessage || messages.propIsForbidden, !customMessage && 'propIsForbidden', {
118+
report(context, customMessage || messages.propIsForbidden, customMessage ? 'propIsForbidden' : '', {
114119
node,
115120
data: {
116121
prop,

lib/rules/forbid-prop-types.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ module.exports = {
7171
const configuration = context.options[0] || {};
7272
const checkContextTypes = configuration.checkContextTypes || false;
7373
const checkChildContextTypes = configuration.checkChildContextTypes || false;
74+
/** @type {string | null} */
7475
let propTypesPackageName = null;
76+
/** @type {string | null} */
7577
let reactPackageName = null;
7678
let isForeignPropTypesPackage = false;
7779

@@ -223,6 +225,7 @@ module.exports = {
223225
}
224226
},
225227

228+
/** @param {ClassProperty | PropertyDefinition} node */
226229
'ClassProperty, PropertyDefinition'(node) {
227230
if (
228231
!isPropTypesDeclaration(node)

lib/rules/forward-ref-uses-ref.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
'use strict';
66

7-
const { isMemberExpression } = require('../util/ast');
8-
const { isParenthesized } = require('../util/ast');
7+
const {
8+
isMemberExpression,
9+
isParenthesized,
10+
} = require('../util/ast');
911
const docsUrl = require('../util/docsUrl');
1012
const report = require('../util/report');
13+
const { getText } = require('../util/eslint');
1114

1215
// ------------------------------------------------------------------------------
1316
// Rule Definition
@@ -18,7 +21,7 @@ const report = require('../util/report');
1821
* @returns {boolean} If the node represents the identifier `forwardRef`.
1922
*/
2023
function isForwardRefIdentifier(node) {
21-
return node.type === 'Identifier' && node.name === 'forwardRef';
24+
return node.type === 'Identifier' && 'name' in node && node.name === 'forwardRef';
2225
}
2326

2427
/**
@@ -28,6 +31,7 @@ function isForwardRefIdentifier(node) {
2831
function isForwardRefCall(node) {
2932
return (
3033
node.type === 'CallExpression'
34+
&& 'callee' in node
3135
&& (
3236
isForwardRefIdentifier(node.callee)
3337
|| (isMemberExpression(node.callee) && isForwardRefIdentifier(node.callee.property))
@@ -57,9 +61,8 @@ module.exports = {
5761
},
5862

5963
create(context) {
60-
const sourceCode = context.getSourceCode();
61-
6264
return {
65+
/** @param {FunctionExpression | ArrowFunctionExpression} node */
6366
'FunctionExpression, ArrowFunctionExpression'(node) {
6467
if (!isForwardRefCall(node.parent)) {
6568
return;
@@ -70,7 +73,7 @@ module.exports = {
7073
node,
7174
suggest: [
7275
{
73-
messageId: 'addRefParameter',
76+
messageId: messages.addRefParameter,
7477
fix(fixer) {
7578
const param = node.params[0];
7679
// If using shorthand arrow function syntax, add parentheses around the new parameter pair
@@ -82,9 +85,9 @@ module.exports = {
8285
},
8386
},
8487
{
85-
messageId: 'removeForwardRef',
88+
messageId: messages.removeForwardRef,
8689
fix(fixer) {
87-
return fixer.replaceText(node.parent, sourceCode.getText(node));
90+
return fixer.replaceText(node.parent, getText(context, node));
8891
},
8992
},
9093
],

0 commit comments

Comments
 (0)