Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function isAllowedEmptyConstructor(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent?.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
return (
// allow protected constructors
(accessibility === 'protected' && isAllowedProtectedConstructors) ||
// allow private constructors
(accessibility === 'private' && isAllowedPrivateConstructors) ||
// allow constructors which have parameter properties
hasParameterProperties(node)
);
}
return false;
}
function isNamedFunction(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): boolean {
if (node.id) {
return true;
}
const parent = node.parent!;
return (
parent.type === AST_NODE_TYPES.MethodDefinition ||
(parent.type === AST_NODE_TYPES.Property &&
(parent.kind === 'get' || parent.kind === 'set' || parent.method))
);
}
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:
"Member '{{name}}' is never reassigned; mark it as `readonly`.",
export function isMethodDefinition(
node: TSESTree.Node
): node is TSESTree.MethodDefinition {
return node.type === AST_NODE_TYPES.MethodDefinition;
}
const returnType = checker.getReturnTypeOfSignature(signatures[0]);
if (
!util.containsAllTypesByName(
returnType,
allowAny!,
allAllowedPromiseNames,
)
) {
return;
}
if (
node.parent &&
(node.parent.type === AST_NODE_TYPES.Property ||
node.parent.type === AST_NODE_TYPES.MethodDefinition) &&
(node.parent.kind === 'get' || node.parent.kind === 'set')
) {
return;
}
context.report({
messageId: 'missingAsync',
node,
});
}
function getMemberName(
node: TSESTree.ClassElement | TSESTree.TypeElement,
): string | null {
switch (node.type) {
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
return util.getNameFromMember(node, sourceCode);
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
return node.kind === 'constructor'
? 'constructor'
: util.getNameFromMember(node, sourceCode);
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'new';
case AST_NODE_TYPES.TSIndexSignature:
return util.getNameFromIndexSignature(node);
default:
return null;
}
}
function isConstructor(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor'
);
}
if (!member.declaration) {
return null;
}
return getMemberName(member.declaration);
}
case AST_NODE_TYPES.TSDeclareFunction:
case AST_NODE_TYPES.FunctionDeclaration:
return member.id && member.id.name;
case AST_NODE_TYPES.TSMethodSignature:
return util.getNameFromMember(member, sourceCode);
case AST_NODE_TYPES.TSCallSignatureDeclaration:
return 'call';
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'new';
case AST_NODE_TYPES.MethodDefinition:
return util.getNameFromMember(member, sourceCode);
}
return null;
}
function isConstructor(node: TSESTree.Node | undefined): boolean {
return (
!!node &&
node.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor'
);
}