Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TSAsExpression(node: TSESTree.TSAsExpression) {
// transform it to a BinaryExpression
return rules['BinaryExpression, LogicalExpression']({
type: AST_NODE_TYPES.BinaryExpression,
operator: 'as',
left: node.expression,
// the first typeAnnotation includes the as token
right: node.typeAnnotation as any,
// location data
parent: node.parent,
range: node.range,
loc: node.loc,
});
},
TSConditionalType(node: TSESTree.TSConditionalType) {
// transform it to a ConditionalExpression
return rules.ConditionalExpression({
type: AST_NODE_TYPES.ConditionalExpression,
test: {
type: AST_NODE_TYPES.BinaryExpression,
operator: 'extends',
left: node.checkType as any,
right: node.extendsType as any,
// location data
range: [node.checkType.range[0], node.extendsType.range[1]],
loc: {
start: node.checkType.loc.start,
end: node.extendsType.loc.end,
},
},
consequent: node.trueType as any,
alternate: node.falseType as any,
// location data
parent: node.parent,
// makes the rule think it should skip the left or right
if (util.isTypeAssertion(node.left)) {
return rule({
...node,
left: {
...node.left,
type: AST_NODE_TYPES.BinaryExpression as any,
},
});
}
if (util.isTypeAssertion(node.right)) {
return rule({
...node,
right: {
...node.right,
type: AST_NODE_TYPES.BinaryExpression as any,
},
});
}
return rule(node);
}
function callExp(
const GLOBAL_LINEBREAK_REGEX = /\r\n|[\r\n\u2028\u2029]/gu;
const WHITESPACE_REGEX = /\s*$/u;
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const KNOWN_NODES = new Set([
AST_NODE_TYPES.AssignmentExpression,
AST_NODE_TYPES.AssignmentPattern,
AST_NODE_TYPES.ArrayExpression,
AST_NODE_TYPES.ArrayPattern,
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.AwaitExpression,
AST_NODE_TYPES.BlockStatement,
AST_NODE_TYPES.BinaryExpression,
AST_NODE_TYPES.BreakStatement,
AST_NODE_TYPES.CallExpression,
AST_NODE_TYPES.CatchClause,
AST_NODE_TYPES.ClassBody,
AST_NODE_TYPES.ClassDeclaration,
AST_NODE_TYPES.ClassExpression,
AST_NODE_TYPES.ConditionalExpression,
AST_NODE_TYPES.ContinueStatement,
AST_NODE_TYPES.DoWhileStatement,
AST_NODE_TYPES.DebuggerStatement,
AST_NODE_TYPES.EmptyStatement,
AST_NODE_TYPES.ExpressionStatement,
AST_NODE_TYPES.ForStatement,
AST_NODE_TYPES.ForInStatement,
AST_NODE_TYPES.ForOfStatement,
AST_NODE_TYPES.FunctionDeclaration,
switch (node.type) {
case AST_NODE_TYPES.UpdateExpression:
// x++ or ++x
return (
node.operator === '++' && isMatchingIdentifier(node.argument, name)
);
case AST_NODE_TYPES.AssignmentExpression:
if (isMatchingIdentifier(node.left, name)) {
if (node.operator === '+=') {
// x += 1
return isLiteral(node.right, 1);
} else if (node.operator === '=') {
// x = x + 1 or x = 1 + x
const expr = node.right;
return (
expr.type === AST_NODE_TYPES.BinaryExpression &&
expr.operator === '+' &&
((isMatchingIdentifier(expr.left, name) &&
isLiteral(expr.right, 1)) ||
(isLiteral(expr.left, 1) &&
isMatchingIdentifier(expr.right, name)))
);
}
}
}
return false;
}
CallExpression(node: TSESTree.CallExpression) {
if (!(isDescribe(node) || isTestCase(node)) || !node.arguments.length) {
return;
}
const [argument] = getJestFunctionArguments(node);
if (!isStringNode(argument)) {
if (
argument.type === AST_NODE_TYPES.BinaryExpression &&
doesBinaryExpressionContainStringNode(argument)
) {
return;
}
if (
argument.type !== AST_NODE_TYPES.TemplateLiteral &&
!(ignoreTypeOfDescribeName && isDescribe(node))
) {
context.report({
messageId: 'titleMustBeString',
loc: argument.loc,
});
}
return;
case AST_NODE_TYPES.OptionalCallExpression:
return parent.callee === node;
case AST_NODE_TYPES.ConditionalExpression:
return parent.test === node;
case AST_NODE_TYPES.LogicalExpression:
return parent.operator !== '||';
case AST_NODE_TYPES.TaggedTemplateExpression:
return parent.tag === node;
case AST_NODE_TYPES.UnaryExpression:
return parent.operator === 'typeof';
case AST_NODE_TYPES.BinaryExpression:
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);
case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
return isSafeUse(parent);
}
return false;
}
allowIdentifier &&
(node.type === AST_NODE_TYPES.Identifier ||
node.type === AST_NODE_TYPES.ThisExpression)
) {
return true;
}
/*
special case for the following, where we only want the left
- foo !== null
- foo != null
- foo !== undefined
- foo != undefined
*/
if (
node.type === AST_NODE_TYPES.BinaryExpression &&
['!==', '!='].includes(node.operator) &&
isValidChainTarget(node.left, allowIdentifier)
) {
if (
node.right.type === AST_NODE_TYPES.Identifier &&
node.right.name === 'undefined'
) {
return true;
}
if (
node.right.type === AST_NODE_TYPES.Literal &&
node.right.value === null
) {
return true;
}
}
function isLessThanLengthExpression(
node: TSESTree.Node | null,
name: string,
): TSESTree.Expression | null {
if (
node !== null &&
node.type === AST_NODE_TYPES.BinaryExpression &&
node.operator === '<' &&
isMatchingIdentifier(node.left, name) &&
(node.right.type === AST_NODE_TYPES.MemberExpression ||
node.right.type === AST_NODE_TYPES.OptionalMemberExpression) &&
isMatchingIdentifier(node.right.property, 'length')
) {
return node.right.object;
}
return null;
}