Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function checkRedundantParentheses(
sourceCode: SourceCode,
node: estree.Node,
context: Rule.RuleContext,
) {
const parenthesesPairsAroundNode = getParenthesesPairsAround(sourceCode, node, node);
const parent = getParent(context);
// Ignore parentheses pair from the parent node
if (!!parent && isInParentNodeParentheses(node, parent)) {
parenthesesPairsAroundNode.pop();
}
// One pair of parentheses is allowed for readability purposes
parenthesesPairsAroundNode.shift();
parenthesesPairsAroundNode.forEach(parentheses => {
context.report({
message: toEncodedMessage(`Remove these useless parentheses.`, [
parentheses.closingParenthesis,
]),
loc: parentheses.openingParenthesis.loc,
});
});
function getSymbol(
id: estree.Identifier,
services: RequiredParserServices,
context: Rule.RuleContext,
tc: tsTypes.TypeChecker,
) {
let symbol: tsTypes.Symbol | undefined;
const tsId = services.esTreeNodeToTSNodeMap.get(id as TSESTree.Node) as tsTypes.Identifier;
const parent = services.esTreeNodeToTSNodeMap.get(getParent(
context,
) as TSESTree.Node) as tsTypes.Node;
if (parent.kind === ts.SyntaxKind.BindingElement) {
symbol = tc.getTypeAtLocation(parent.parent).getProperty(tsId.text);
} else if (
(isPropertyAssignment(parent) && parent.name === tsId) ||
(isShorthandPropertyAssignment(parent) && parent.name === tsId)
) {
try {
symbol = tc.getPropertySymbolOfDestructuringAssignment(tsId);
} catch (e) {
// do nothing, we are in object literal, not destructuring
// no obvious easy way to check that in advance
}
} else {
symbol = tc.getSymbolAtLocation(tsId);
function isAllowedCallbacks(context: Rule.RuleContext) {
const parent = getParent(context);
if (parent && parent.type === "CallExpression") {
const callee = parent.callee;
if (callee.type === "MemberExpression") {
return (
callee.property.type === "Identifier" && allowedCallbacks.includes(callee.property.name)
);
}
}
return false;
}
function raiseIssue(context: Rule.RuleContext): void {
const deleteKeyword = context.getSourceCode().getFirstToken(getParent(context)!);
context.report({
message: `Remove this use of "delete".`,
loc: deleteKeyword!.loc,
});
}
function isElseIf(node: estree.IfStatement, context: Rule.RuleContext) {
const parent = getParent(context);
return parent && parent.type === "IfStatement" && parent.alternate === node;
}
CallExpression: (node: estree.Node) => {
const call = node as estree.CallExpression;
const callee = call.callee;
if (callee.type === "MemberExpression") {
const parent = getParent(context);
if (parent && parent.type === "ExpressionStatement") {
const methodName = context.getSourceCode().getText(callee.property);
const objectType = services.program
.getTypeChecker()
.getTypeAtLocation(
services.esTreeNodeToTSNodeMap.get(callee.object as TSESTree.Node),
);
if (
!hasSideEffect(methodName, objectType, services) &&
!isReplaceWithCallback(methodName, call.arguments)
) {
context.report({
message: message(methodName),
node,
});
}