Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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';
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'constructor';
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
return node.value && functionExpressions.includes(node.value.type)
? 'method'
: 'field';
case AST_NODE_TYPES.TSPropertySignature:
return 'field';
case AST_NODE_TYPES.TSIndexSignature:
return 'signature';
default:
return null;
}
}
// ts keywords
AST_NODE_TYPES.TSAbstractKeyword,
AST_NODE_TYPES.TSAnyKeyword,
AST_NODE_TYPES.TSBooleanKeyword,
AST_NODE_TYPES.TSNeverKeyword,
AST_NODE_TYPES.TSNumberKeyword,
AST_NODE_TYPES.TSStringKeyword,
AST_NODE_TYPES.TSSymbolKeyword,
AST_NODE_TYPES.TSUndefinedKeyword,
AST_NODE_TYPES.TSUnknownKeyword,
AST_NODE_TYPES.TSVoidKeyword,
AST_NODE_TYPES.TSNullKeyword,
// ts specific nodes we want to support
AST_NODE_TYPES.TSAbstractClassProperty,
AST_NODE_TYPES.TSAbstractMethodDefinition,
AST_NODE_TYPES.TSArrayType,
AST_NODE_TYPES.TSAsExpression,
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConditionalType,
AST_NODE_TYPES.TSConstructorType,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSDeclareFunction,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSEnumDeclaration,
AST_NODE_TYPES.TSEnumMember,
AST_NODE_TYPES.TSExportAssignment,
AST_NODE_TYPES.TSExternalModuleReference,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSImportType,
AST_NODE_TYPES.TSIndexedAccessType,
create(context, [options]) {
const rules = baseRule.create(context);
const TS_PROPERTY_TYPES = [
AST_NODE_TYPES.TSPropertySignature,
AST_NODE_TYPES.ClassProperty,
AST_NODE_TYPES.TSParameterProperty,
AST_NODE_TYPES.TSAbstractClassProperty,
];
const genericType = options.genericType;
const properties = options.properties;
const allow =
options.allow?.map(entry => ({
name: entry,
regex: new RegExp(entry),
})) ?? [];
/**
* Checks if a string contains an underscore and isn't all upper-case
* @param name The string to check.
*/
function isUnderscored(name: string): boolean {
// if there's an underscore, it might be A_CONSTANT, which is okay
// ts keywords
AST_NODE_TYPES.TSAbstractKeyword,
AST_NODE_TYPES.TSAnyKeyword,
AST_NODE_TYPES.TSBooleanKeyword,
AST_NODE_TYPES.TSNeverKeyword,
AST_NODE_TYPES.TSNumberKeyword,
AST_NODE_TYPES.TSStringKeyword,
AST_NODE_TYPES.TSSymbolKeyword,
AST_NODE_TYPES.TSUndefinedKeyword,
AST_NODE_TYPES.TSUnknownKeyword,
AST_NODE_TYPES.TSVoidKeyword,
AST_NODE_TYPES.TSNullKeyword,
// ts specific nodes we want to support
AST_NODE_TYPES.TSAbstractClassProperty,
AST_NODE_TYPES.TSAbstractMethodDefinition,
AST_NODE_TYPES.TSArrayType,
AST_NODE_TYPES.TSAsExpression,
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConditionalType,
AST_NODE_TYPES.TSConstructorType,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSDeclareFunction,
AST_NODE_TYPES.TSEmptyBodyFunctionExpression,
AST_NODE_TYPES.TSEnumDeclaration,
AST_NODE_TYPES.TSEnumMember,
AST_NODE_TYPES.TSExportAssignment,
AST_NODE_TYPES.TSExternalModuleReference,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSImportType,
AST_NODE_TYPES.TSIndexedAccessType,
| TSESTree.TSParameterProperty,
): Set {
const modifiers = new Set();
if (node.accessibility) {
modifiers.add(Modifiers[node.accessibility]);
} else {
modifiers.add(Modifiers.public);
}
if (node.static) {
modifiers.add(Modifiers.static);
}
if ('readonly' in node && node.readonly) {
modifiers.add(Modifiers.readonly);
}
if (
node.type === AST_NODE_TYPES.TSAbstractClassProperty ||
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition
) {
modifiers.add(Modifiers.abstract);
}
return modifiers;
}
const rules = baseRule.create(context);
const checkForSemicolon = rules.ExpressionStatement as TSESLint.RuleFunction<
TSESTree.Node
>;
/*
The following nodes are handled by the member-delimiter-style rule
AST_NODE_TYPES.TSCallSignatureDeclaration,
AST_NODE_TYPES.TSConstructSignatureDeclaration,
AST_NODE_TYPES.TSIndexSignature,
AST_NODE_TYPES.TSMethodSignature,
AST_NODE_TYPES.TSPropertySignature,
*/
const nodesToCheck = [
AST_NODE_TYPES.ClassProperty,
AST_NODE_TYPES.TSAbstractClassProperty,
AST_NODE_TYPES.TSAbstractMethodDefinition,
AST_NODE_TYPES.TSDeclareFunction,
AST_NODE_TYPES.TSExportAssignment,
AST_NODE_TYPES.TSImportEqualsDeclaration,
AST_NODE_TYPES.TSTypeAliasDeclaration,
].reduce((acc, node) => {
acc[node as string] = checkForSemicolon;
return acc;
}, {});
return {
...rules,
...nodesToCheck,
ExportDefaultDeclaration(node): void {
if (node.declaration.type !== AST_NODE_TYPES.TSInterfaceDeclaration) {
rules.ExportDefaultDeclaration(node);
function isAllowedAsNonBacktick(node: TSESTree.Literal): boolean {
const parent = node.parent;
switch (parent?.type) {
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSModuleDeclaration:
case AST_NODE_TYPES.TSLiteralType:
return true;
case AST_NODE_TYPES.TSEnumMember:
return node === parent.id;
case AST_NODE_TYPES.TSAbstractClassProperty:
case AST_NODE_TYPES.ClassProperty:
return node === parent.key;
default:
return false;
}
}