Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { TokenInfo } from './TokenInfo';
import { createRule, ExcludeKeys, RequireKeys } from '../../util';
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,
function getConfigForFunction(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): FuncOption {
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
// Always ignore non-async functions and arrow functions without parens, e.g. async foo => bar
if (
node.async &&
isOpeningParenToken(sourceCode.getFirstToken(node, { skip: 1 })!)
) {
return overrideConfig.asyncArrow ?? baseConfig;
}
} else if (isNamedFunction(node)) {
return overrideConfig.named ?? baseConfig;
// `generator-star-spacing` should warn anonymous generators. E.g. `function* () {}`
} else if (!node.generator) {
return overrideConfig.anonymous ?? baseConfig;
}
return 'ignore';
function getLocEnd(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): TSESTree.LineAndColumnData {
/* highlight `=>` */
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
return sourceCode.getTokenBefore(
node.body,
token =>
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=>',
)!.loc.end;
}
return sourceCode.getTokenBefore(node.body!)!.loc.end;
}
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:
return 'method';
'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.
*/
if (node.parent && !STATEMENT_LIST_PARENTS.has(node.parent.type)) {
parents.add(current);
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;
}
import { typeIsOrHasBaseType } from '../util';
import {
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',
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
);
}
export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression;