Skip to content

Commit

Permalink
chore: enabled no-else-return internally (#7340)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jul 28, 2023
1 parent 54ef459 commit 1b8e0bc
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 265 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -122,6 +122,7 @@ module.exports = {
null: 'never',
},
],
'no-else-return': 'error',
'no-mixed-operators': 'error',
'no-console': 'error',
'no-process-exit': 'error',
Expand Down
31 changes: 14 additions & 17 deletions packages/eslint-plugin/src/rules/consistent-type-imports.ts
Expand Up @@ -296,25 +296,22 @@ export default util.createRule<Options, MessageIds>({
messageId: 'aImportInDecoMeta',
data: { typeImports },
};
} else {
return {
messageId: 'aImportIsOnlyTypes',
data: { typeImports },
};
}
} else {
if (isTypeImport) {
return {
messageId: 'someImportsInDecoMeta',
data: { typeImports }, // typeImports are all the value specifiers that are in the type position
};
} else {
return {
messageId: 'someImportsAreOnlyTypes',
data: { typeImports }, // typeImports are all the type specifiers in the value position
};
}
return {
messageId: 'aImportIsOnlyTypes',
data: { typeImports },
};
}
if (isTypeImport) {
return {
messageId: 'someImportsInDecoMeta',
data: { typeImports }, // typeImports are all the value specifiers that are in the type position
};
}
return {
messageId: 'someImportsAreOnlyTypes',
data: { typeImports }, // typeImports are all the type specifiers in the value position
};
})();

context.report({
Expand Down
27 changes: 13 additions & 14 deletions packages/eslint-plugin/src/rules/indent.ts
Expand Up @@ -159,21 +159,20 @@ export default util.createRule<Options, MessageIds>({
type,
...base,
} as TSESTree.Property;
} else {
return {
type,
accessibility: undefined,
declare: false,
decorators: [],
definite: false,
optional: false,
override: false,
readonly: false,
static: false,
typeAnnotation: undefined,
...base,
} as TSESTree.PropertyDefinition;
}
return {
type,
accessibility: undefined,
declare: false,
decorators: [],
definite: false,
optional: false,
override: false,
readonly: false,
static: false,
typeAnnotation: undefined,
...base,
} as TSESTree.PropertyDefinition;
}

return Object.assign({}, rules, {
Expand Down
21 changes: 9 additions & 12 deletions packages/eslint-plugin/src/rules/key-spacing.ts
Expand Up @@ -143,12 +143,11 @@ export default util.createRule<Options, MessageIds>({
typeAnnotation.range[0] - difference,
typeAnnotation.range[0],
]);
} else {
return fixer.insertTextBefore(
typeAnnotation,
' '.repeat(-difference),
);
}
return fixer.insertTextBefore(
typeAnnotation,
' '.repeat(-difference),
);
},
data: {
computed: '',
Expand Down Expand Up @@ -177,12 +176,11 @@ export default util.createRule<Options, MessageIds>({
typeAnnotation.typeAnnotation.range[0] - difference,
typeAnnotation.typeAnnotation.range[0],
]);
} else {
return fixer.insertTextBefore(
typeAnnotation.typeAnnotation,
' '.repeat(-difference),
);
}
return fixer.insertTextBefore(
typeAnnotation.typeAnnotation,
' '.repeat(-difference),
);
},
data: {
computed: '',
Expand Down Expand Up @@ -314,9 +312,8 @@ export default util.createRule<Options, MessageIds>({
toCheck.range[0] - difference,
toCheck.range[0],
]);
} else {
return fixer.insertTextBefore(toCheck, ' '.repeat(-difference));
}
return fixer.insertTextBefore(toCheck, ' '.repeat(-difference));
},
data: {
computed: '',
Expand Down
48 changes: 20 additions & 28 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
Expand Up @@ -114,15 +114,14 @@ export default util.createRule<Options, MessageId>({
);
if (isHigherPrecedenceThanUnary(tsNode)) {
return fixer.insertTextBefore(node, 'void ');
} else {
return [
fixer.insertTextBefore(node, 'void ('),
fixer.insertTextAfterRange(
[expression.range[1], expression.range[1]],
')',
),
];
}
return [
fixer.insertTextBefore(node, 'void ('),
fixer.insertTextAfterRange(
[expression.range[1], expression.range[1]],
')',
),
];
},
},
],
Expand Down Expand Up @@ -151,15 +150,14 @@ export default util.createRule<Options, MessageId>({
);
if (isHigherPrecedenceThanUnary(tsNode)) {
return fixer.insertTextBefore(node, 'await ');
} else {
return [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfterRange(
[expression.range[1], expression.range[1]],
')',
),
];
}
return [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfterRange(
[expression.range[1], expression.range[1]],
')',
),
];
},
},
],
Expand Down Expand Up @@ -240,18 +238,16 @@ export default util.createRule<Options, MessageId>({
if (catchRejectionHandler) {
if (isValidRejectionHandler(catchRejectionHandler)) {
return { isUnhandled: false };
} else {
return { isUnhandled: true, nonFunctionHandler: true };
}
return { isUnhandled: true, nonFunctionHandler: true };
}

const thenRejectionHandler = getRejectionHandlerFromThenCall(node);
if (thenRejectionHandler) {
if (isValidRejectionHandler(thenRejectionHandler)) {
return { isUnhandled: false };
} else {
return { isUnhandled: true, nonFunctionHandler: true };
}
return { isUnhandled: true, nonFunctionHandler: true };
}

// `x.finally()` is transparent to resolution of the promise, so check `x`.
Expand All @@ -269,9 +265,8 @@ export default util.createRule<Options, MessageId>({
const alternateResult = isUnhandledPromise(checker, node.alternate);
if (alternateResult.isUnhandled) {
return alternateResult;
} else {
return isUnhandledPromise(checker, node.consequent);
}
return isUnhandledPromise(checker, node.consequent);
} else if (
node.type === AST_NODE_TYPES.MemberExpression ||
node.type === AST_NODE_TYPES.Identifier ||
Expand All @@ -285,9 +280,8 @@ export default util.createRule<Options, MessageId>({
const leftResult = isUnhandledPromise(checker, node.left);
if (leftResult.isUnhandled) {
return leftResult;
} else {
return isUnhandledPromise(checker, node.right);
}
return isUnhandledPromise(checker, node.right);
}

// We conservatively return false for all other types of expressions because
Expand Down Expand Up @@ -365,9 +359,8 @@ function getRejectionHandlerFromCatchCall(
expression.arguments.length >= 1
) {
return expression.arguments[0];
} else {
return undefined;
}
return undefined;
}

function getRejectionHandlerFromThenCall(
Expand All @@ -380,9 +373,8 @@ function getRejectionHandlerFromThenCall(
expression.arguments.length >= 2
) {
return expression.arguments[1];
} else {
return undefined;
}
return undefined;
}

function getObjectFromFinallyCall(
Expand Down
7 changes: 3 additions & 4 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -537,11 +537,10 @@ export default util.createRule<Options, MessageIds>({
line: identifier.loc.start.line,
column: identifier.loc.start.column + 1,
};
} else {
return {
global: true,
};
}
return {
global: true,
};
}

/**
Expand Down
Expand Up @@ -50,11 +50,8 @@ export default util.createRule({
}
}
return false;
} else {
return (
(type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0
);
}
return (type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
};

const sameTypeWithoutNullish = (
Expand Down
Expand Up @@ -333,16 +333,15 @@ function getFixer(
left: sourceCode.getText(lastOperand.node.left),
right: unaryOperator + newCode,
};
} else {
const unaryOperator =
lastOperand.node.left.type === AST_NODE_TYPES.UnaryExpression
? lastOperand.node.left.operator + ' '
: '';
return {
left: unaryOperator + newCode,
right: sourceCode.getText(lastOperand.node.right),
};
}
const unaryOperator =
lastOperand.node.left.type === AST_NODE_TYPES.UnaryExpression
? lastOperand.node.left.operator + ' '
: '';
return {
left: unaryOperator + newCode,
right: sourceCode.getText(lastOperand.node.right),
};
})();

newCode = `${left} ${operator} ${right}`;
Expand Down
Expand Up @@ -144,13 +144,12 @@ export function gatherLogicalOperands(
comparedValue: comparedValueRight,
isYoda: false,
};
} else {
return {
comparedExpression: operand.right,
comparedValue: getComparisonValueType(operand.left),
isYoda: true,
};
}
return {
comparedExpression: operand.right,
comparedValue: getComparisonValueType(operand.left),
isYoda: true,
};
})();

if (comparedValue === ComparisonValueType.UndefinedStringLiteral) {
Expand Down
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/rules/return-await.ts
Expand Up @@ -156,12 +156,11 @@ export default util.createRule({
): TSESLint.RuleFix | TSESLint.RuleFix[] {
if (isHighPrecendence) {
return fixer.insertTextBefore(node, 'await ');
} else {
return [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfter(node, ')'),
];
}
return [
fixer.insertTextBefore(node, 'await ('),
fixer.insertTextAfter(node, ')'),
];
}

function isHigherPrecedenceThanAwait(node: ts.Node): boolean {
Expand Down
6 changes: 2 additions & 4 deletions packages/eslint-plugin/src/rules/type-annotation-spacing.ts
Expand Up @@ -76,9 +76,8 @@ function getIdentifierRules(
return rules.variable;
} else if (isFunctionOrFunctionType(scope)) {
return rules.parameter;
} else {
return rules.colon;
}
return rules.colon;
}

function getRules(
Expand All @@ -95,9 +94,8 @@ function getRules(
return rules.property;
} else if (isFunction(scope)) {
return rules.returnType;
} else {
return rules.colon;
}
return rules.colon;
}

export default util.createRule<Options, MessageIds>({
Expand Down
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/util/misc.ts
Expand Up @@ -135,12 +135,11 @@ function getNameFromMember(
type: MemberNameType.Quoted,
name: `"${name}"`,
};
} else {
return {
type: MemberNameType.Normal,
name,
};
}
return {
type: MemberNameType.Normal,
name,
};
}

return {
Expand Down
3 changes: 1 addition & 2 deletions packages/type-utils/src/predicates.ts
Expand Up @@ -26,9 +26,8 @@ export function isNullableType(

if (allowUndefined) {
return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
} else {
return (flags & ts.TypeFlags.Null) !== 0;
}
return (flags & ts.TypeFlags.Null) !== 0;
}

/**
Expand Down

0 comments on commit 1b8e0bc

Please sign in to comment.