Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
AST_NODE_TYPES.JSXClosingElement,
AST_NODE_TYPES.JSXOpeningElement,
AST_NODE_TYPES.JSXAttribute,
AST_NODE_TYPES.JSXSpreadAttribute,
AST_NODE_TYPES.JSXText,
AST_NODE_TYPES.ExportDefaultDeclaration,
AST_NODE_TYPES.ExportNamedDeclaration,
AST_NODE_TYPES.ExportAllDeclaration,
AST_NODE_TYPES.ExportSpecifier,
AST_NODE_TYPES.ImportDeclaration,
AST_NODE_TYPES.ImportSpecifier,
AST_NODE_TYPES.ImportDefaultSpecifier,
AST_NODE_TYPES.ImportNamespaceSpecifier,
// Class properties aren't yet supported by eslint...
AST_NODE_TYPES.ClassProperty,
// 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,
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/indent';
import * as util from '../util';
type Options = util.InferOptionsTypeFromRule;
type MessageIds = util.InferMessageIdsTypeFromRule;
const KNOWN_NODES = new Set([
// Class properties aren't yet supported by eslint...
AST_NODE_TYPES.ClassProperty,
// 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,
function isClassPropertyWithTypeAnnotation(node: TSESTree.Node): boolean {
return (
node.type === AST_NODE_TYPES.ClassProperty && !!node.typeAnnotation
);
}
function isParentTSReadonlyClassProperty(node: TSESTree.Node): boolean {
if (
node.parent &&
node.parent.type === AST_NODE_TYPES.UnaryExpression &&
['-', '+'].includes(node.parent.operator)
) {
node = node.parent;
}
if (
node.parent &&
node.parent.type === AST_NODE_TYPES.ClassProperty &&
node.parent.readonly
) {
return true;
}
return false;
}
create(context) {
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) {
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;
}
}
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.
*/