Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore(eslint-plugin): fixed no-unnecessary-condition complaints #7837

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions packages/eslint-plugin/src/rules/adjacent-overload-signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export default createRule({
* @returns the name and attribute of the member or null if it's a member not relevant to the rule.
*/
function getMemberMethod(member: TSESTree.Node): Method | null {
if (!member) {
return null;
}

const isStatic = 'static' in member && !!member.static;

switch (member.type) {
Expand Down Expand Up @@ -137,35 +133,33 @@ export default createRule({
function checkBodyForOverloadMethods(node: RuleNode): void {
const members = getMembers(node);

if (members) {
let lastMethod: Method | null = null;
const seenMethods: Method[] = [];
let lastMethod: Method | null = null;
const seenMethods: Method[] = [];

members.forEach(member => {
const method = getMemberMethod(member);
if (method == null) {
lastMethod = null;
return;
}
members.forEach(member => {
const method = getMemberMethod(member);
if (method == null) {
lastMethod = null;
return;
}

const index = seenMethods.findIndex(seenMethod =>
isSameMethod(method, seenMethod),
);
if (index > -1 && !isSameMethod(method, lastMethod)) {
context.report({
node: member,
messageId: 'adjacentSignature',
data: {
name: `${method.static ? 'static ' : ''}${method.name}`,
},
});
} else if (index === -1) {
seenMethods.push(method);
}
const index = seenMethods.findIndex(seenMethod =>
isSameMethod(method, seenMethod),
);
if (index > -1 && !isSameMethod(method, lastMethod)) {
context.report({
node: member,
messageId: 'adjacentSignature',
data: {
name: `${method.static ? 'static ' : ''}${method.name}`,
},
});
} else if (index === -1) {
seenMethods.push(method);
}

lastMethod = method;
});
}
lastMethod = method;
});
}

return {
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-plugin/src/rules/array-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ function isSimpleType(node: TSESTree.Node): boolean {
return true;
case AST_NODE_TYPES.TSTypeReference:
if (
node.typeName &&
node.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeName.name === 'Array'
) {
Expand Down Expand Up @@ -144,7 +143,7 @@ export default createRule<Options, MessageIds>({
* @param node the node to be evaluated.
*/
function getMessageType(node: TSESTree.Node): string {
if (node && isSimpleType(node)) {
if (isSimpleType(node)) {
return sourceCode.getText(node);
}
return 'T';
Expand Down Expand Up @@ -253,7 +252,7 @@ export default createRule<Options, MessageIds>({
const typeParens = typeNeedsParentheses(type);
const parentParens =
readonlyPrefix &&
node.parent?.type === AST_NODE_TYPES.TSArrayType &&
node.parent.type === AST_NODE_TYPES.TSArrayType &&
!isParenthesized(node.parent.elementType, sourceCode);

const start = `${parentParens ? '(' : ''}${readonlyPrefix}${
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-plugin/src/rules/block-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export default createRule<Options, MessageIds>({

// Skip if the node is invalid or empty.
if (
openBrace.type !== AST_TOKEN_TYPES.Punctuator ||
openBrace.value !== '{' ||
closeBrace.type !== AST_TOKEN_TYPES.Punctuator ||
closeBrace.value !== '}' ||
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin/src/rules/class-methods-use-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,10 @@ export default createRule<Options, MessageIds>({
const stackContext = popContext();
if (
stackContext?.member == null ||
stackContext.class == null ||
stackContext.usesThis ||
(ignoreOverrideMethods && stackContext.member.override) ||
(ignoreClassesThatImplementAnInterface &&
stackContext.class.implements != null)
stackContext.class.implements.length)
) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/comma-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default createRule<Options, MessageIds>({
}

const prevToken = tokensAndComments[i - 1];
const nextToken = tokensAndComments[i + 1];
const nextToken = tokensAndComments.at(i + 1);

validateCommaSpacing(
token,
Expand All @@ -192,7 +192,7 @@ export default createRule<Options, MessageIds>({
: prevToken,
(nextToken && isCommaToken(nextToken)) || ignoredTokens.has(token)
? null
: nextToken,
: nextToken ?? null,
);
});
},
Expand Down
54 changes: 26 additions & 28 deletions packages/eslint-plugin/src/rules/consistent-generic-constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,35 @@ export default createRule<Options, MessageIds>({
}
return;
}
if (mode === 'constructor') {
if (lhs?.typeArguments && !rhs.typeArguments) {
const hasParens =
sourceCode.getTokenAfter(rhs.callee)?.value === '(';
const extraComments = new Set(
sourceCode.getCommentsInside(lhs.parent),
);
sourceCode
.getCommentsInside(lhs.typeArguments)
.forEach(c => extraComments.delete(c));
context.report({
node,
messageId: 'preferConstructor',
*fix(fixer) {
yield fixer.remove(lhs.parent);
for (const comment of extraComments) {
yield fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(comment),
);
}

if (lhs?.typeArguments && !rhs.typeArguments) {
const hasParens = sourceCode.getTokenAfter(rhs.callee)?.value === '(';
const extraComments = new Set(
sourceCode.getCommentsInside(lhs.parent),
);
sourceCode
.getCommentsInside(lhs.typeArguments)
.forEach(c => extraComments.delete(c));
context.report({
node,
messageId: 'preferConstructor',
*fix(fixer) {
yield fixer.remove(lhs.parent);
for (const comment of extraComments) {
yield fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeArguments),
sourceCode.getText(comment),
);
if (!hasParens) {
yield fixer.insertTextAfter(rhs.callee, '()');
}
},
});
}
}
yield fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeArguments),
);
if (!hasParens) {
yield fixer.insertTextAfter(rhs.callee, '()');
}
},
});
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ export default createRule<Options, MessageIds>({
return;
}

const [parameter] = member.parameters;

if (!parameter) {
const parameter = member.parameters.at(0);
if (parameter?.type !== AST_NODE_TYPES.Identifier) {
return;
}

if (parameter.type !== AST_NODE_TYPES.Identifier) {
return;
}
const keyType = parameter.typeAnnotation;
if (!keyType) {
return;
Expand Down Expand Up @@ -133,7 +129,7 @@ export default createRule<Options, MessageIds>({
TSInterfaceDeclaration(node): void {
let genericTypes = '';

if (node.typeParameters?.params?.length) {
if (node.typeParameters?.params.length) {
genericTypes = `<${node.typeParameters.params
.map(p => sourceCode.getText(p))
.join(', ')}>`;
Expand All @@ -145,7 +141,7 @@ export default createRule<Options, MessageIds>({
node.id,
`type ${node.id.name}${genericTypes} = `,
';',
!node.extends?.length,
!node.extends.length,
);
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,10 @@ export default createRule<Options, MessageIds>({
return;
}

if (
checkType(node.typeAnnotation) &&
node.expression.type === AST_NODE_TYPES.ObjectExpression
) {
if (checkType(node.typeAnnotation)) {
const suggest: TSESLint.ReportSuggestionArray<MessageIds> = [];
if (
node.parent?.type === AST_NODE_TYPES.VariableDeclarator &&
node.parent.type === AST_NODE_TYPES.VariableDeclarator &&
!node.parent.id.typeAnnotation
) {
const { parent } = node;
Expand Down
16 changes: 7 additions & 9 deletions packages/eslint-plugin/src/rules/consistent-type-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,15 @@ export default createRule({
);
}

if (node.extends) {
node.extends.forEach(heritage => {
const typeIdentifier = sourceCode.getText(heritage);
fixes.push(
fixer.insertTextAfter(node.body, ` & ${typeIdentifier}`),
);
});
}
node.extends.forEach(heritage => {
const typeIdentifier = sourceCode.getText(heritage);
fixes.push(
fixer.insertTextAfter(node.body, ` & ${typeIdentifier}`),
);
});

if (
node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration
node.parent.type === AST_NODE_TYPES.ExportDefaultDeclaration
) {
fixes.push(
fixer.removeRange([node.parent.range[0], node.range[0]]),
Expand Down
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/consistent-type-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ export default createRule<Options, MessageIds>({
): boolean | undefined {
const checker = services.program.getTypeChecker();
const symbol = services.getSymbolAtLocation(specifier.exported);
const aliasedSymbol = checker.getAliasedSymbol(symbol!);
if (!symbol) {
return undefined;
}

const aliasedSymbol = checker.getAliasedSymbol(symbol);

// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (!aliasedSymbol || aliasedSymbol.escapedName === 'unknown') {
if (aliasedSymbol.escapedName === 'unknown') {
return undefined;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ export default createRule<Options, MessageIds>({
* export default Type;
*/
if (
ref.identifier.parent?.type ===
ref.identifier.parent.type ===
AST_NODE_TYPES.ExportSpecifier ||
ref.identifier.parent?.type ===
ref.identifier.parent.type ===
AST_NODE_TYPES.ExportDefaultDeclaration
) {
if (ref.isValueReference && ref.isTypeReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default createRule<Options, MessageIds>({
let funcName;
if (node.id?.name) {
funcName = node.id.name;
} else if (parent) {
} else {
switch (parent.type) {
case AST_NODE_TYPES.VariableDeclarator: {
if (parent.id.type === AST_NODE_TYPES.Identifier) {
Expand Down Expand Up @@ -153,7 +153,6 @@ export default createRule<Options, MessageIds>({
if (
node.type === AST_NODE_TYPES.FunctionDeclaration &&
node.id &&
node.id.type === AST_NODE_TYPES.Identifier &&
!!options.allowedNames.includes(node.id.name)
) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default createRule<Options, MessageIds>({
accessibility: TSESTree.Accessibility,
fixer: TSESLint.RuleFixer,
): TSESLint.RuleFix | null {
if (node?.decorators.length) {
if (node.decorators.length) {
const lastDecorator = node.decorators[node.decorators.length - 1];
const nextToken = sourceCode.getTokenAfter(lastDecorator)!;
return fixer.insertTextBefore(nextToken, `${accessibility} `);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export default createRule<Options, MessageIds>({
while (current) {
if (current.type === AST_NODE_TYPES.ReturnStatement) {
// the parent of a return will always be a block statement, so we can skip over it
current = current.parent?.parent;
current = current.parent.parent;
continue;
}

Expand Down Expand Up @@ -405,11 +405,11 @@ export default createRule<Options, MessageIds>({
node: TSESTree.TSEmptyBodyFunctionExpression,
): void {
const isConstructor =
node.parent?.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.kind === 'constructor';
const isSetAccessor =
(node.parent?.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
node.parent?.type === AST_NODE_TYPES.MethodDefinition) &&
(node.parent.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
node.parent.type === AST_NODE_TYPES.MethodDefinition) &&
node.parent.kind === 'set';
if (!isConstructor && !isSetAccessor && !node.returnType) {
context.report({
Expand Down
Loading