Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSDoc type annotations #3731

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/rules/jsx-closing-bracket-location.js
Expand Up @@ -196,9 +196,11 @@ module.exports = {
*/
function getTokensLocations(node) {
const sourceCode = context.getSourceCode();
// @ts-ignore
// The types differ between ASTNode and ESTree.Node.
// @ts-expect-error
ljharb marked this conversation as resolved.
Show resolved Hide resolved
const opening = sourceCode.getFirstToken(node).loc.start;
// @ts-ignore
// The types differ between ASTNode and ESTree.Node.
// @ts-expect-error
const closing = sourceCode.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start;
const tag = sourceCode.getFirstToken(node.name).loc.start;
let lastProp;
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/jsx-equals-spacing.js
Expand Up @@ -62,9 +62,10 @@ module.exports = {

const sourceCode = context.getSourceCode();
const equalToken = sourceCode.getTokenAfter(attrNode.name);
// @ts-ignore
if (equalToken.type !== 'Punctuator') {
return;
}
const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
// @ts-ignore
const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);

if (config === 'never') {
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/jsx-indent-props.js
Expand Up @@ -141,7 +141,8 @@ module.exports = {
* @return {Number} Indent
*/
function getNodeIndent(node) {
// @ts-ignore
// The types differ between ASTNode and ESTree.Node.
// @ts-expect-error
let src = context.getSourceCode().getText(node, node.loc.start.column + extraColumnStart);
const lines = src.split('\n');
src = lines[0];
Expand Down
30 changes: 12 additions & 18 deletions lib/rules/no-access-state-in-setstate.js
Expand Up @@ -107,8 +107,8 @@ module.exports = {
&& node.object.type === 'ThisExpression'
&& isClassComponent()
) {
/** @type {import("eslint").Rule.Node} */
let current = node;
// @ts-ignore
while (current.type !== 'Program') {
// Reporting if this.state is directly within this.setState
if (isFirstArgumentInSetStateCall(current, node)) {
Expand All @@ -119,56 +119,51 @@ module.exports = {
}

// Storing all functions and methods that contains this.state
// @ts-ignore
if (current.type === 'MethodDefinition') {
if (current.type === 'MethodDefinition' && current.key.type === 'Identifier') {
methods.push({
// @ts-ignore
methodName: current.key.name,
node,
});
break;
// @ts-ignore
} else if (current.type === 'FunctionExpression' && current.parent.key) {
} else if (
current.type === 'FunctionExpression'
&& (current.parent.type === 'Property' || current.parent.type === 'MethodDefinition')
&& current.parent.key.type === 'Identifier'
&& current.parent.key
) {
methods.push({
// @ts-ignore
methodName: current.parent.key.name,
node,
});
break;
}

// Storing all variables containing this.state
// @ts-ignore
if (current.type === 'VariableDeclarator') {
if (current.type === 'VariableDeclarator' && current.id.type === 'Identifier') {
vars.push({
node,
scope: context.getScope(),
// @ts-ignore
variableName: current.id.name,
});
break;
}

// @ts-ignore
current = current.parent;
}
}
},

Identifier(node) {
// Checks if the identifier is a variable within an object
/** @type {import("eslint").Rule.Node} */
let current = node;
while (current.parent.type === 'BinaryExpression') {
// @ts-ignore
current = current.parent;
}
if (
// @ts-ignore
current.parent.value === current
// @ts-ignore
|| current.parent.object === current
(current.parent.type === 'Property' && current.parent.value === current)
|| (current.parent.type === 'MemberExpression' && current.parent.object === current)
) {
// @ts-ignore
while (current.type !== 'Program') {
if (isFirstArgumentInSetStateCall(current, node)) {
vars
Expand All @@ -179,7 +174,6 @@ module.exports = {
});
});
}
// @ts-ignore
current = current.parent;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-danger-with-children.js
Expand Up @@ -129,8 +129,8 @@ module.exports = {
let props = node.arguments[1];

if (props.type === 'Identifier') {
// @ts-ignore
const variable = variableUtil.variablesInScope(context).find((item) => item.name === props.name);
const name = props.name;
const variable = variableUtil.variablesInScope(context).find((item) => item.name === name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
props = variable.defs[0].node.init;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/no-deprecated.js
Expand Up @@ -236,14 +236,13 @@ module.exports = {

VariableDeclarator(node) {
const reactModuleName = getReactModuleName(node);
// @ts-ignore
const isRequire = node.init && node.init.callee && node.init.callee.name === 'require';
const isReactRequire = node.init
// @ts-ignore
const isRequire = node.init.type === 'CallExpression'
&& node.init.callee && node.init.callee.type === 'Identifier'
&& node.init.callee.name === 'require';
const isReactRequire = node.init.type === 'CallExpression'
&& node.init.arguments
// @ts-ignore
&& node.init.arguments.length
// @ts-ignore
&& node.init.arguments[0].type === 'Literal'
&& typeof MODULES[node.init.arguments[0].value] !== 'undefined';
const isDestructuring = node.id && node.id.type === 'ObjectPattern';

Expand Down
1 change: 0 additions & 1 deletion lib/rules/no-unused-state.js
Expand Up @@ -111,7 +111,6 @@ module.exports = {

let scope = context.getScope();
while (scope) {
// @ts-ignore
const parent = scope.block && scope.block.parent;
if (
parent
Expand Down