From 710085c84d0a0107f9f91f804be91a13db471de7 Mon Sep 17 00:00:00 2001 From: ren-yamanashi Date: Thu, 21 Nov 2024 09:41:04 +0900 Subject: [PATCH] refactor: delete extends types --- src/no-construct-stack-suffix.mts | 24 +++--- src/no-import-private.mts | 18 ++--- src/no-parent-name-construct-id-match.mts | 96 ++++++++++------------- src/pascal-case-construct-id.mts | 70 ++++++++--------- 4 files changed, 96 insertions(+), 112 deletions(-) diff --git a/src/no-construct-stack-suffix.mts b/src/no-construct-stack-suffix.mts index c92a14a..1f82e40 100644 --- a/src/no-construct-stack-suffix.mts +++ b/src/no-construct-stack-suffix.mts @@ -57,8 +57,8 @@ export const noConstructStackSuffix = ESLintUtils.RuleCreator.withoutDocs({ * Validate the constructor body for the parent class * - validate each statement in the constructor body */ -const validateConstructorBody = ( - node: T, +const validateConstructorBody = ( + node: TSESTree.ClassBody, expression: TSESTree.FunctionExpression, context: Context ): void => { @@ -96,8 +96,8 @@ const validateConstructorBody = ( * - Handles BlockStatement, ExpressionStatement, and VariableDeclaration * - Validates construct IDs */ -const traverseStatements = ( - node: T, +const traverseStatements = ( + node: TSESTree.ClassBody, statement: TSESTree.Statement, context: Context ): void => { @@ -128,8 +128,8 @@ const traverseStatements = ( * - Handles different types of statements (Variable, Expression, If, Switch) * - Extracts and validates construct IDs from new expressions */ -const validateStatement = ( - node: T, +const validateStatement = ( + node: TSESTree.ClassBody, body: TSESTree.Statement, context: Context ): void => { @@ -161,8 +161,8 @@ const validateStatement = ( * Validate the `if` statement * - Validate recursively if `if` statements are nested */ -const validateIfStatement = ( - node: T, +const validateIfStatement = ( + node: TSESTree.ClassBody, ifStatement: TSESTree.IfStatement, context: Context ): void => { @@ -173,8 +173,8 @@ const validateIfStatement = ( * Validate the `switch` statement * - Validate recursively if `switch` statements are nested */ -const validateSwitchStatement = ( - node: T, +const validateSwitchStatement = ( + node: TSESTree.ClassBody, switchStatement: TSESTree.SwitchStatement, context: Context ): void => { @@ -188,8 +188,8 @@ const validateSwitchStatement = ( /** * Validate that construct ID does not end with "Construct" or "Stack" */ -const validateConstructId = ( - node: T, +const validateConstructId = ( + node: TSESTree.ClassBody, context: Context, expression: TSESTree.NewExpression ): void => { diff --git a/src/no-import-private.mts b/src/no-import-private.mts index 739cd15..8beaf03 100644 --- a/src/no-import-private.mts +++ b/src/no-import-private.mts @@ -2,15 +2,6 @@ import * as path from "path"; import { Rule } from "eslint"; -/** - * Split the directory path into segments (split at `/`) - * @param dirPath - The directory path to split - * @returns The segments of the directory path - */ -const getDirSegments = (dirPath: string): string[] => { - return dirPath.split(path.sep).filter((segment) => segment !== ""); -}; - /** * Disallow importing modules from private directories at different levels of the hierarchy. * @param context - The rule context provided by ESLint @@ -60,3 +51,12 @@ export const noImportPrivate: Rule.RuleModule = { }; }, }; + +/** + * Split the directory path into segments (split at `/`) + * @param dirPath - The directory path to split + * @returns The segments of the directory path + */ +const getDirSegments = (dirPath: string): string[] => { + return dirPath.split(path.sep).filter((segment) => segment !== ""); +}; diff --git a/src/no-parent-name-construct-id-match.mts b/src/no-parent-name-construct-id-match.mts index 49b5e5e..cfac6af 100644 --- a/src/no-parent-name-construct-id-match.mts +++ b/src/no-parent-name-construct-id-match.mts @@ -9,6 +9,20 @@ import { toPascalCase } from "./utils/convertString.mjs"; type Context = TSESLint.RuleContext<"noParentNameConstructIdMatch", []>; +type ValidateStatementArgs = { + node: TSESTree.ClassBody; + statement: T; + parentClassName: string; + context: Context; +}; + +type ValidateExpressionArgs = { + node: TSESTree.ClassBody; + expression: T; + parentClassName: string; + context: Context; +}; + /** * Enforce that construct IDs does not match the parent construct name. * @param context - The rule context provided by ESLint @@ -65,17 +79,12 @@ export const noParentNameConstructIdMatch = ESLintUtils.RuleCreator.withoutDocs( * Validate the constructor body for the parent class * - validate each statement in the constructor body */ -const validateConstructorBody = ({ +const validateConstructorBody = ({ node, expression, parentClassName, context, -}: { - node: T; - expression: TSESTree.FunctionExpression; - parentClassName: string; - context: Context; -}): void => { +}: ValidateExpressionArgs): void => { for (const statement of expression.body.body) { switch (statement.type) { case AST_NODE_TYPES.VariableDeclaration: { @@ -93,7 +102,7 @@ const validateConstructorBody = ({ if (statement.expression?.type !== AST_NODE_TYPES.NewExpression) break; validateStatement({ node, - body: statement, + statement, parentClassName, context, }); @@ -130,23 +139,18 @@ const validateConstructorBody = ({ * - Handles BlockStatement, ExpressionStatement, and VariableDeclaration * - Validates construct IDs against parent class name */ -const traverseStatements = ({ +const traverseStatements = ({ node, statement, parentClassName, context, -}: { - node: T; - statement: TSESTree.Statement; - parentClassName: string; - context: Context; -}) => { +}: ValidateStatementArgs) => { switch (statement.type) { case AST_NODE_TYPES.BlockStatement: { for (const body of statement.body) { validateStatement({ node, - body, + statement: body, parentClassName, context, }); @@ -158,7 +162,7 @@ const traverseStatements = ({ if (newExpression?.type !== AST_NODE_TYPES.NewExpression) break; validateStatement({ node, - body: statement, + statement, parentClassName, context, }); @@ -183,20 +187,15 @@ const traverseStatements = ({ * - Handles different types of statements (Variable, Expression, If, Switch) * - Extracts and validates construct IDs from new expressions */ -const validateStatement = ({ +const validateStatement = ({ node, - body, + statement, parentClassName, context, -}: { - node: T; - body: TSESTree.Statement; - parentClassName: string; - context: Context; -}): void => { - switch (body.type) { +}: ValidateStatementArgs): void => { + switch (statement.type) { case AST_NODE_TYPES.VariableDeclaration: { - const newExpression = body.declarations[0].init; + const newExpression = statement.declarations[0].init; if (newExpression?.type !== AST_NODE_TYPES.NewExpression) break; validateConstructId({ node, @@ -207,7 +206,7 @@ const validateStatement = ({ break; } case AST_NODE_TYPES.ExpressionStatement: { - const newExpression = body.expression; + const newExpression = statement.expression; if (newExpression?.type !== AST_NODE_TYPES.NewExpression) break; validateConstructId({ node, @@ -220,7 +219,7 @@ const validateStatement = ({ case AST_NODE_TYPES.IfStatement: { validateIfStatement({ node, - ifStatement: body, + statement, parentClassName, context, }); @@ -229,7 +228,7 @@ const validateStatement = ({ case AST_NODE_TYPES.SwitchStatement: { validateSwitchStatement({ node, - switchStatement: body, + statement, parentClassName, context, }); @@ -242,22 +241,17 @@ const validateStatement = ({ * Validate the `if` statement * - Validate recursively if `if` statements are nested */ -const validateIfStatement = ({ +const validateIfStatement = ({ node, - ifStatement, + statement, parentClassName, context, -}: { - node: T; - ifStatement: TSESTree.IfStatement; - parentClassName: string; - context: Context; -}): void => { +}: ValidateStatementArgs): void => { traverseStatements({ node, context, parentClassName, - statement: ifStatement.consequent, + statement: statement.consequent, }); }; @@ -265,19 +259,14 @@ const validateIfStatement = ({ * Validate the `switch` statement * - Validate recursively if `switch` statements are nested */ -const validateSwitchStatement = ({ +const validateSwitchStatement = ({ node, - switchStatement, + statement, parentClassName, context, -}: { - node: T; - switchStatement: TSESTree.SwitchStatement; - parentClassName: string; - context: Context; -}): void => { - for (const statement of switchStatement.cases) { - for (const _consequent of statement.consequent) { +}: ValidateStatementArgs): void => { + for (const caseStatement of statement.cases) { + for (const _consequent of caseStatement.consequent) { traverseStatements({ node, context, @@ -291,17 +280,12 @@ const validateSwitchStatement = ({ /** * Validate that parent construct name and child id do not match */ -const validateConstructId = ({ +const validateConstructId = ({ node, context, expression, parentClassName, -}: { - node: T; - context: Context; - expression: TSESTree.NewExpression; - parentClassName: string; -}): void => { +}: ValidateExpressionArgs): void => { if (expression.arguments.length < 2) return; // NOTE: Treat the second argument as ID diff --git a/src/pascal-case-construct-id.mts b/src/pascal-case-construct-id.mts index 9bfe61e..3db8b1d 100644 --- a/src/pascal-case-construct-id.mts +++ b/src/pascal-case-construct-id.mts @@ -11,6 +11,41 @@ const QUOTE_TYPE = { type QuoteType = (typeof QUOTE_TYPE)[keyof typeof QUOTE_TYPE]; +/** + * Enforce PascalCase for Construct ID. + * @param context - The rule context provided by ESLint + * @returns An object containing the AST visitor functions + * @see {@link https://eslint-cdk-plugin.dev/rules/pascal-case-construct-id} - Documentation + */ +export const pascalCaseConstructId: Rule.RuleModule = { + meta: { + type: "problem", + docs: { + description: "Enforce PascalCase for Construct ID.", + }, + messages: { + pascalCaseConstructId: "Construct ID must be PascalCase.", + }, + schema: [], + fixable: "code", + }, + create(context) { + return { + ExpressionStatement(node) { + if (node.expression.type !== AST_NODE_TYPES.NewExpression) return; + validateConstructId(node, context, node.expression.arguments); + }, + VariableDeclaration(node) { + if (!node.declarations.length) return; + for (const declaration of node.declarations) { + if (declaration.init?.type !== AST_NODE_TYPES.NewExpression) return; + validateConstructId(node, context, declaration.init.arguments); + } + }, + }; + }, +}; + /** * check if the string is PascalCase * @param str - The string to check @@ -57,38 +92,3 @@ const validateConstructId = ( }); } }; - -/** - * Enforce PascalCase for Construct ID. - * @param context - The rule context provided by ESLint - * @returns An object containing the AST visitor functions - * @see {@link https://eslint-cdk-plugin.dev/rules/pascal-case-construct-id} - Documentation - */ -export const pascalCaseConstructId: Rule.RuleModule = { - meta: { - type: "problem", - docs: { - description: "Enforce PascalCase for Construct ID.", - }, - messages: { - pascalCaseConstructId: "Construct ID must be PascalCase.", - }, - schema: [], - fixable: "code", - }, - create(context) { - return { - ExpressionStatement(node) { - if (node.expression.type !== AST_NODE_TYPES.NewExpression) return; - validateConstructId(node, context, node.expression.arguments); - }, - VariableDeclaration(node) { - if (!node.declarations.length) return; - for (const declaration of node.declarations) { - if (declaration.init?.type !== AST_NODE_TYPES.NewExpression) return; - validateConstructId(node, context, declaration.init.arguments); - } - }, - }; - }, -};