Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
);
// Remove overload definition to avoid confusion of no-redeclare rule.
const { defs, identifiers } = upperScope.set.get(id.name)!;
for (let i = 0; i < defs.length; ++i) {
const def = defs[i];
if (
def.type === 'FunctionName' &&
def.node.type === AST_NODE_TYPES.TSDeclareFunction
) {
defs.splice(i, 1);
identifiers.splice(i, 1);
break;
}
}
} else if (type === AST_NODE_TYPES.FunctionExpression && id) {
scopeManager.__nestFunctionExpressionNameScope(node);
}
// Open the function scope.
scopeManager.__nestFunctionScope(node, this.isInnerMethodDefinition);
const innerScope = this.currentScope();
// Process the type parameters
this.visit(typeParameters);
// Process parameter declarations.
for (let i = 0; i < params.length; ++i) {
this.visitPattern(
params[i],
{ processRightHandNodes: true },
(pattern, info) => {
create(context, [options]) {
const sourceCode = context.getSourceCode();
const functionExpressions = [
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.ArrowFunctionExpression,
];
/**
* Gets the node type.
* @param node the node to be evaluated.
*/
function getNodeType(
node: TSESTree.ClassElement | TSESTree.TypeElement,
): string | null {
// TODO: add missing TSCallSignatureDeclaration
switch (node.type) {
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
return node.kind;
case AST_NODE_TYPES.TSMethodSignature:
export function isFunctionExpressionLike(
node: TSESTree.Node
): node is TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression {
return (
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression
);
}
MethodDefinition(node): void {
if (
node.value &&
node.value.type === AST_NODE_TYPES.FunctionExpression &&
node.value.body &&
checkAccessibility(node) &&
checkParams(node)
) {
rules.MethodDefinition(node);
}
},
};
if (
(current.type === AST_NODE_TYPES.ConditionalExpression ||
current.type === AST_NODE_TYPES.DoWhileStatement ||
current.type === AST_NODE_TYPES.IfStatement ||
current.type === AST_NODE_TYPES.ForStatement ||
current.type === AST_NODE_TYPES.WhileStatement) &&
parents.has(current.test)
) {
return true;
}
if (
[
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionExpression,
].includes(current.type)
) {
/**
* This is a weird situation like:
* `if (() => a || b) {}`
* `if (function () { return a || b }) {}`
*/
return false;
}
current = current.parent;
}
return false;
}
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
type MessageIds = 'preferReadonly';
type Options = [
{
onlyInlineLambdas?: boolean;
},
];
const functionScopeBoundaries = [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.MethodDefinition,
].join(', ');
export default util.createRule({
name: 'prefer-readonly',
meta: {
docs: {
description:
"Requires that private members are marked as `readonly` if they're never modified outside of the constructor",
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
preferReadonly:
export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression;
'BlockStatement, ClassBody'(
node: TSESTree.BlockStatement | TSESTree.ClassBody,
) {
let blockIndentLevel;
if (node.parent && isOuterIIFE(node.parent)) {
blockIndentLevel = options.outerIIFEBody;
} else if (
node.parent &&
(node.parent.type === AST_NODE_TYPES.FunctionExpression ||
node.parent.type === AST_NODE_TYPES.ArrowFunctionExpression)
) {
blockIndentLevel = options.FunctionExpression.body;
} else if (
node.parent &&
node.parent.type === AST_NODE_TYPES.FunctionDeclaration
) {
blockIndentLevel = options.FunctionDeclaration.body;
} else {
blockIndentLevel = 1;
}
/*
* For blocks that aren't lone statements, ensure that the opening curly brace
* is aligned with the parent.
*/
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,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.IfStatement,
AST_NODE_TYPES.Literal,
AST_NODE_TYPES.LabeledStatement,
AST_NODE_TYPES.LogicalExpression,
AST_NODE_TYPES.MemberExpression,
AST_NODE_TYPES.MetaProperty,
AST_NODE_TYPES.MethodDefinition,
AST_NODE_TYPES.NewExpression,
AST_NODE_TYPES.ObjectExpression,
AST_NODE_TYPES.ObjectPattern,
AST_NODE_TYPES.Program,
AST_NODE_TYPES.Property,
AST_NODE_TYPES.RestElement,
AST_NODE_TYPES.ReturnStatement,
AST_NODE_TYPES.SequenceExpression,
function isNodeValidFunction(node: TSESTree.Node): boolean {
return [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSCallSignatureDeclaration,
].includes(node.type);
}