Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: delete extends types #53

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/no-construct-stack-suffix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T extends TSESTree.ClassBody>(
node: T,
const validateConstructorBody = (
node: TSESTree.ClassBody,
expression: TSESTree.FunctionExpression,
context: Context
): void => {
Expand Down Expand Up @@ -96,8 +96,8 @@ const validateConstructorBody = <T extends TSESTree.ClassBody>(
* - Handles BlockStatement, ExpressionStatement, and VariableDeclaration
* - Validates construct IDs
*/
const traverseStatements = <T extends TSESTree.ClassBody>(
node: T,
const traverseStatements = (
node: TSESTree.ClassBody,
statement: TSESTree.Statement,
context: Context
): void => {
Expand Down Expand Up @@ -128,8 +128,8 @@ const traverseStatements = <T extends TSESTree.ClassBody>(
* - Handles different types of statements (Variable, Expression, If, Switch)
* - Extracts and validates construct IDs from new expressions
*/
const validateStatement = <T extends TSESTree.ClassBody>(
node: T,
const validateStatement = (
node: TSESTree.ClassBody,
body: TSESTree.Statement,
context: Context
): void => {
Expand Down Expand Up @@ -161,8 +161,8 @@ const validateStatement = <T extends TSESTree.ClassBody>(
* Validate the `if` statement
* - Validate recursively if `if` statements are nested
*/
const validateIfStatement = <T extends TSESTree.ClassBody>(
node: T,
const validateIfStatement = (
node: TSESTree.ClassBody,
ifStatement: TSESTree.IfStatement,
context: Context
): void => {
Expand All @@ -173,8 +173,8 @@ const validateIfStatement = <T extends TSESTree.ClassBody>(
* Validate the `switch` statement
* - Validate recursively if `switch` statements are nested
*/
const validateSwitchStatement = <T extends TSESTree.ClassBody>(
node: T,
const validateSwitchStatement = (
node: TSESTree.ClassBody,
switchStatement: TSESTree.SwitchStatement,
context: Context
): void => {
Expand All @@ -188,8 +188,8 @@ const validateSwitchStatement = <T extends TSESTree.ClassBody>(
/**
* Validate that construct ID does not end with "Construct" or "Stack"
*/
const validateConstructId = <T extends TSESTree.ClassBody>(
node: T,
const validateConstructId = (
node: TSESTree.ClassBody,
context: Context,
expression: TSESTree.NewExpression
): void => {
Expand Down
18 changes: 9 additions & 9 deletions src/no-import-private.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 !== "");
};
96 changes: 40 additions & 56 deletions src/no-parent-name-construct-id-match.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import { toPascalCase } from "./utils/convertString.mjs";

type Context = TSESLint.RuleContext<"noParentNameConstructIdMatch", []>;

type ValidateStatementArgs<T extends TSESTree.Statement> = {
node: TSESTree.ClassBody;
statement: T;
parentClassName: string;
context: Context;
};

type ValidateExpressionArgs<T extends TSESTree.Expression> = {
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
Expand Down Expand Up @@ -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 = <T extends TSESTree.ClassBody>({
const validateConstructorBody = ({
node,
expression,
parentClassName,
context,
}: {
node: T;
expression: TSESTree.FunctionExpression;
parentClassName: string;
context: Context;
}): void => {
}: ValidateExpressionArgs<TSESTree.FunctionExpression>): void => {
for (const statement of expression.body.body) {
switch (statement.type) {
case AST_NODE_TYPES.VariableDeclaration: {
Expand All @@ -93,7 +102,7 @@ const validateConstructorBody = <T extends TSESTree.ClassBody>({
if (statement.expression?.type !== AST_NODE_TYPES.NewExpression) break;
validateStatement({
node,
body: statement,
statement,
parentClassName,
context,
});
Expand Down Expand Up @@ -130,23 +139,18 @@ const validateConstructorBody = <T extends TSESTree.ClassBody>({
* - Handles BlockStatement, ExpressionStatement, and VariableDeclaration
* - Validates construct IDs against parent class name
*/
const traverseStatements = <T extends TSESTree.ClassBody>({
const traverseStatements = ({
node,
statement,
parentClassName,
context,
}: {
node: T;
statement: TSESTree.Statement;
parentClassName: string;
context: Context;
}) => {
}: ValidateStatementArgs<TSESTree.Statement>) => {
switch (statement.type) {
case AST_NODE_TYPES.BlockStatement: {
for (const body of statement.body) {
validateStatement({
node,
body,
statement: body,
parentClassName,
context,
});
Expand All @@ -158,7 +162,7 @@ const traverseStatements = <T extends TSESTree.ClassBody>({
if (newExpression?.type !== AST_NODE_TYPES.NewExpression) break;
validateStatement({
node,
body: statement,
statement,
parentClassName,
context,
});
Expand All @@ -183,20 +187,15 @@ const traverseStatements = <T extends TSESTree.ClassBody>({
* - Handles different types of statements (Variable, Expression, If, Switch)
* - Extracts and validates construct IDs from new expressions
*/
const validateStatement = <T extends TSESTree.ClassBody>({
const validateStatement = ({
node,
body,
statement,
parentClassName,
context,
}: {
node: T;
body: TSESTree.Statement;
parentClassName: string;
context: Context;
}): void => {
switch (body.type) {
}: ValidateStatementArgs<TSESTree.Statement>): 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,
Expand All @@ -207,7 +206,7 @@ const validateStatement = <T extends TSESTree.ClassBody>({
break;
}
case AST_NODE_TYPES.ExpressionStatement: {
const newExpression = body.expression;
const newExpression = statement.expression;
if (newExpression?.type !== AST_NODE_TYPES.NewExpression) break;
validateConstructId({
node,
Expand All @@ -220,7 +219,7 @@ const validateStatement = <T extends TSESTree.ClassBody>({
case AST_NODE_TYPES.IfStatement: {
validateIfStatement({
node,
ifStatement: body,
statement,
parentClassName,
context,
});
Expand All @@ -229,7 +228,7 @@ const validateStatement = <T extends TSESTree.ClassBody>({
case AST_NODE_TYPES.SwitchStatement: {
validateSwitchStatement({
node,
switchStatement: body,
statement,
parentClassName,
context,
});
Expand All @@ -242,42 +241,32 @@ const validateStatement = <T extends TSESTree.ClassBody>({
* Validate the `if` statement
* - Validate recursively if `if` statements are nested
*/
const validateIfStatement = <T extends TSESTree.ClassBody>({
const validateIfStatement = ({
node,
ifStatement,
statement,
parentClassName,
context,
}: {
node: T;
ifStatement: TSESTree.IfStatement;
parentClassName: string;
context: Context;
}): void => {
}: ValidateStatementArgs<TSESTree.IfStatement>): void => {
traverseStatements({
node,
context,
parentClassName,
statement: ifStatement.consequent,
statement: statement.consequent,
});
};

/**
* Validate the `switch` statement
* - Validate recursively if `switch` statements are nested
*/
const validateSwitchStatement = <T extends TSESTree.ClassBody>({
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<TSESTree.SwitchStatement>): void => {
for (const caseStatement of statement.cases) {
for (const _consequent of caseStatement.consequent) {
traverseStatements({
node,
context,
Expand All @@ -291,17 +280,12 @@ const validateSwitchStatement = <T extends TSESTree.ClassBody>({
/**
* Validate that parent construct name and child id do not match
*/
const validateConstructId = <T extends TSESTree.ClassBody>({
const validateConstructId = ({
node,
context,
expression,
parentClassName,
}: {
node: T;
context: Context;
expression: TSESTree.NewExpression;
parentClassName: string;
}): void => {
}: ValidateExpressionArgs<TSESTree.NewExpression>): void => {
if (expression.arguments.length < 2) return;

// NOTE: Treat the second argument as ID
Expand Down
70 changes: 35 additions & 35 deletions src/pascal-case-construct-id.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,38 +92,3 @@ const validateConstructId = <T extends Node>(
});
}
};

/**
* 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);
}
},
};
},
};
Loading