Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
TSImportEqualsDeclaration(node: TSESTree.TSImportEqualsDeclaration) {
// transform it to an VariableDeclaration
// use VariableDeclaration instead of ImportDeclaration because it's essentially the same thing
const { id, moduleReference } = node;
return rules.VariableDeclaration({
type: AST_NODE_TYPES.VariableDeclaration,
kind: 'const' as 'const',
declarations: [
{
type: AST_NODE_TYPES.VariableDeclarator,
range: [id.range[0], moduleReference.range[1]],
loc: {
start: id.loc.start,
end: moduleReference.loc.end,
},
id: id,
init: {
type: AST_NODE_TYPES.CallExpression,
callee: {
type: AST_NODE_TYPES.Identifier,
name: 'require',
range: [
moduleReference.range[0],
moduleReference.range[0] + 'require'.length,
],
loc: {
const func = stmt.parent;
/* istanbul ignore if */
if (!func) {
throw new Error(
`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
}
// functionDeclaration: function func() {}
if (func.type === AST_NODE_TYPES.FunctionDeclaration) {
return 'function';
}
if (isFunction(func) && func.parent) {
const expr = func.parent;
// arrowfunction or function expr
if (expr.type === AST_NODE_TYPES.VariableDeclarator) {
return 'function';
}
// if it's not a variable, it will be callExpr, we only care about describe
if (expr.type === AST_NODE_TYPES.CallExpression && isDescribe(expr)) {
return DescribeAlias.describe;
}
}
return null;
};
'CallExpression, OptionalCallExpression'(
node: TSESTree.CallExpression | TSESTree.OptionalCallExpression,
): void {
if (
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'require' &&
node.parent &&
(node.parent.type === AST_NODE_TYPES.VariableDeclarator ||
node.parent.type === AST_NODE_TYPES.CallExpression ||
node.parent.type === AST_NODE_TYPES.OptionalCallExpression)
) {
context.report({
node,
messageId: 'noVarReqs',
});
}
},
};
const isPromiseReturnedLater = (
node: TSESTree.Node,
testFunctionBody: TSESTree.Statement[],
) => {
let promiseName;
if (
node.type === AST_NODE_TYPES.ExpressionStatement &&
node.expression.type === AST_NODE_TYPES.CallExpression &&
node.expression.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.expression.callee.object)
) {
promiseName = getAccessorValue(node.expression.callee.object);
} else if (
node.type === AST_NODE_TYPES.VariableDeclarator &&
node.id.type === AST_NODE_TYPES.Identifier
) {
promiseName = node.id.name;
}
const lastLineInTestFunc = testFunctionBody[testFunctionBody.length - 1];
return (
lastLineInTestFunc.type === AST_NODE_TYPES.ReturnStatement &&
lastLineInTestFunc.argument &&
(('name' in lastLineInTestFunc.argument &&
lastLineInTestFunc.argument.name === promiseName) ||
!promiseName)
);
};
export function isVariableDeclarator(
node: TSESTree.Node
): node is TSESTree.VariableDeclarator {
return node.type === AST_NODE_TYPES.VariableDeclarator;
}
AST_NODE_TYPES.ReturnStatement,
AST_NODE_TYPES.SequenceExpression,
AST_NODE_TYPES.SpreadElement,
AST_NODE_TYPES.Super,
AST_NODE_TYPES.SwitchCase,
AST_NODE_TYPES.SwitchStatement,
AST_NODE_TYPES.TaggedTemplateExpression,
AST_NODE_TYPES.TemplateElement,
AST_NODE_TYPES.TemplateLiteral,
AST_NODE_TYPES.ThisExpression,
AST_NODE_TYPES.ThrowStatement,
AST_NODE_TYPES.TryStatement,
AST_NODE_TYPES.UnaryExpression,
AST_NODE_TYPES.UpdateExpression,
AST_NODE_TYPES.VariableDeclaration,
AST_NODE_TYPES.VariableDeclarator,
AST_NODE_TYPES.WhileStatement,
AST_NODE_TYPES.WithStatement,
AST_NODE_TYPES.YieldExpression,
AST_NODE_TYPES.JSXIdentifier,
AST_NODE_TYPES.JSXMemberExpression,
AST_NODE_TYPES.JSXEmptyExpression,
AST_NODE_TYPES.JSXExpressionContainer,
AST_NODE_TYPES.JSXElement,
AST_NODE_TYPES.JSXClosingElement,
AST_NODE_TYPES.JSXOpeningElement,
AST_NODE_TYPES.JSXAttribute,
AST_NODE_TYPES.JSXSpreadAttribute,
AST_NODE_TYPES.JSXText,
AST_NODE_TYPES.ExportDefaultDeclaration,
AST_NODE_TYPES.ExportNamedDeclaration,
AST_NODE_TYPES.ExportAllDeclaration,
function isVariableDeclaratorWithTypeAnnotation(
node: TSESTree.Node,
): boolean {
return (
node.type === AST_NODE_TYPES.VariableDeclarator &&
!!node.id.typeAnnotation
);
}
/*
* Navigate legal ancestors to determine whether this IIFE is outer.
* A "legal ancestor" is an expression or statement that causes the function to get executed immediately.
* For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator.
*/
let statement = node.parent && node.parent.parent;
while (
statement &&
((statement.type === AST_NODE_TYPES.UnaryExpression &&
['!', '~', '+', '-'].includes(statement.operator)) ||
statement.type === AST_NODE_TYPES.AssignmentExpression ||
statement.type === AST_NODE_TYPES.LogicalExpression ||
statement.type === AST_NODE_TYPES.SequenceExpression ||
statement.type === AST_NODE_TYPES.VariableDeclarator)
) {
statement = statement.parent;
}
return (
!!statement &&
(statement.type === AST_NODE_TYPES.ExpressionStatement ||
statement.type === AST_NODE_TYPES.VariableDeclaration) &&
!!statement.parent &&
statement.parent.type === AST_NODE_TYPES.Program
);
}
function isInInitializer(
variable: TSESLint.Scope.Variable,
reference: TSESLint.Scope.Reference,
): boolean {
if (variable.scope !== reference.from) {
return false;
}
let node = variable.identifiers[0].parent;
const location = reference.identifier.range[1];
while (node) {
if (node.type === AST_NODE_TYPES.VariableDeclarator) {
if (isInRange(node.init, location)) {
return true;
}
if (
node.parent &&
node.parent.parent &&
(node.parent.parent.type === AST_NODE_TYPES.ForInStatement ||
node.parent.parent.type === AST_NODE_TYPES.ForOfStatement) &&
isInRange(node.parent.parent.right, location)
) {
return true;
}
break;
} else if (node.type === AST_NODE_TYPES.AssignmentPattern) {
if (isInRange(node.right, location)) {
return true;