Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function checkParameterPropertyAccessibilityModifier(
node: TSESTree.TSParameterProperty,
): void {
const nodeType = 'parameter property';
// HAS to be an identifier or assignment or TSC will throw
if (
node.parameter.type !== AST_NODE_TYPES.Identifier &&
node.parameter.type !== AST_NODE_TYPES.AssignmentPattern
) {
return;
}
const nodeName =
node.parameter.type === AST_NODE_TYPES.Identifier
? node.parameter.name
: // has to be an Identifier or TSC will throw an error
(node.parameter.left as TSESTree.Identifier).name;
switch (paramPropCheck) {
case 'explicit': {
if (!node.accessibility) {
reportIssue('missingAccessibility', nodeType, node, nodeName);
}
break;
} from 'eslint-utils';
import { TokenOrComment } from './BinarySearchTree';
import { OffsetStorage } from './OffsetStorage';
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,
TSParameterProperty(node): void {
const modifiers = getModifiers(node);
if (!allows.includes(modifiers)) {
// HAS to be an identifier or assignment or TSC will throw
if (
node.parameter.type !== AST_NODE_TYPES.Identifier &&
node.parameter.type !== AST_NODE_TYPES.AssignmentPattern
) {
return;
}
const name =
node.parameter.type === AST_NODE_TYPES.Identifier
? node.parameter.name
: // has to be an Identifier or TSC will throw an error
(node.parameter.left as TSESTree.Identifier).name;
context.report({
node,
messageId: 'noParamProp',
data: {
parameter: name,
},
function checkParameterPropertyAccessibilityModifier(node) {
const nodeType = 'parameter property'
if (isTypeScriptFile(context.getFilename())) {
// HAS to be an identifier or assignment or TSC will throw
if (
node.parameter.type !== AST_NODE_TYPES.Identifier &&
node.parameter.type !== AST_NODE_TYPES.AssignmentPattern
) {
return
}
const nodeName =
node.parameter.type === AST_NODE_TYPES.Identifier
? node.parameter.name
: // has to be an Identifier or TSC will throw an error
node.parameter.left.name
if (!!node.accessibility) {
reportIssue('presentModifier', nodeType, node, nodeName)
}
}
}
function checkParameters(params: TSESTree.Parameter[]): void {
for (const param of params) {
let annotationNode: TSESTree.Node | undefined;
switch (param.type) {
case AST_NODE_TYPES.AssignmentPattern:
annotationNode = param.left;
break;
case AST_NODE_TYPES.TSParameterProperty:
annotationNode = param.parameter;
// Check TS parameter property with default value like `constructor(private param: string = 'something') {}`
if (
annotationNode &&
annotationNode.type === AST_NODE_TYPES.AssignmentPattern
) {
annotationNode = annotationNode.left;
}
break;
default:
annotationNode = param;
break;
}
if (annotationNode !== undefined && !annotationNode.typeAnnotation) {
report(param, getNodeName(param));
}
}
}
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;
}
} else if (SENTINEL_TYPE.test(node.type)) {
break;
}
node = node.parent;
}
return false;
}
export function isAssignmentPattern(
node: TSESTree.Node
): node is TSESTree.AssignmentPattern {
return node.type === AST_NODE_TYPES.AssignmentPattern;
}
function isTSPropertyType(node: TSESTree.Node): boolean {
if (TS_PROPERTY_TYPES.includes(node.type)) {
return true;
}
if (node.type === AST_NODE_TYPES.AssignmentPattern) {
return (
node.parent !== undefined &&
TS_PROPERTY_TYPES.includes(node.parent.type)
);
}
return false;
}
param =>
param.type === AST_NODE_TYPES.AssignmentPattern &&
param.left &&
param.right,
) as TSESTree.AssignmentPattern[]).forEach(param => {
function getParameterNode(
node: TSESTree.TSParameterProperty,
): TSESTree.Identifier | null {
if (node.parameter.type === AST_NODE_TYPES.AssignmentPattern) {
return node.parameter.left as TSESTree.Identifier;
}
if (node.parameter.type === AST_NODE_TYPES.Identifier) {
return node.parameter;
}
return null;
}