Skip to content

Jhonatanmizu/snuts.js-core

Repository files navigation

@snutsjs/core

License: GPL v3

🎯 Goal

@snutsjs/core aims to be a robust and extensible static analysis tool designed to identify and report common "smells" or anti-patterns in JavaScript and TypeScript test files. By integrating with your development workflow, it helps maintain high-quality, readable, and effective test suites.

πŸ“¦ Installation

npm install @snutsjs/core

πŸš€ Library Usage (Extension-Friendly)

import { DetectorRunner, detectors } from "@snutsjs/core";

const detectorInstances = Object.values(detectors).map((DetectorClass) => new DetectorClass());
const runner = new DetectorRunner(detectorInstances);

const smells = await runner.run("/absolute/path/to/example.test.ts");
console.log(smells);

πŸ‘€ Runtime Watcher Entry

Use the runtime subpath when you want the side-effectful watcher behavior:

import "@snutsjs/core/runtime/watch";

The root package import is side-effect free and safe for VS Code extension integration.

CLI Usage

npx @snutsjs/core watch .

You can also point to a specific directory:

npx @snutsjs/core watch src

πŸ“ Project Structure

β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ ast/                # AST (Abstract Syntax Tree) related services for parsing and querying
β”‚   β”‚   β”œβ”€β”€ ast.service.test.ts
β”‚   β”‚   └── ast.service.ts
β”‚   β”œβ”€β”€ core/               # Core logic for detector runner and file watching
β”‚   β”‚   β”œβ”€β”€ detector.interface.ts
β”‚   β”‚   β”œβ”€β”€ detector-runner.test.ts
β”‚   β”‚   β”œβ”€β”€ detector-runner.ts
β”‚   β”‚   β”œβ”€β”€ watcher.test.ts
β”‚   β”‚   └── watcher.ts
β”‚   β”œβ”€β”€ detectors/          # Collection of predefined test smell detectors
β”‚   β”‚   β”œβ”€β”€ anonymousTestLogic.ts
β”‚   β”‚   β”œβ”€β”€ anonymousTestLogic.test.ts
β”‚   β”‚   β”œβ”€β”€ commentsOnlyTestLogic.test.ts
β”‚   β”‚   β”œβ”€β”€ commentsOnlyTestLogic.ts
β”‚   β”‚   β”œβ”€β”€ conditionalTestLogic.test.ts
β”‚   β”‚   β”œβ”€β”€ conditionalTestLogic.ts
β”‚   β”‚   β”œβ”€β”€ identicalDescriptionTestLogic.test.ts
β”‚   β”‚   β”œβ”€β”€ identicalDescriptionTestLogic.ts
β”‚   β”‚   β”œβ”€β”€ index.ts
β”‚   β”‚   β”œβ”€β”€ overcommentedTestLogic.test.ts
β”‚   β”‚   └── overcommentedTestLogic.ts
β”‚   β”œβ”€β”€ runtime/
β”‚   β”‚   └── watch.ts
β”‚   β”œβ”€β”€ shared/             # Shared utilities, constants, and plugins
β”‚   β”‚   β”œβ”€β”€ aliases/        # Module aliases configuration
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”œβ”€β”€ constants.ts
β”‚   β”‚   β”œβ”€β”€ logger/         # Logging utility
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   └── plugins/        # Plugin system for extensibility
β”‚   β”‚       └── index.ts
β”‚   β”œβ”€β”€ test/               # Internal testing utilities and builders
β”‚   β”‚   └── builders/
β”‚   β”‚       └── astNodeBuilder.ts
β”‚   └── index.ts            # Main entry point for the library
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .prettierignore
β”œβ”€β”€ .prettierrc.json
β”œβ”€β”€ eslint.config.js
β”œβ”€β”€ jest.config.js
β”œβ”€β”€ LICENSE
β”œβ”€β”€ package.json
β”œβ”€β”€ README.md
β”œβ”€β”€ tsconfig.json
β”œβ”€β”€ tsconfig.test.json
└── yarn.lock

License

This project is licensed under the GPL-3.0 License. See the LICENSE file for details.


✨ Features

  • πŸ”§ Static Analysis: Identifies anti-patterns in test files using AST parsing.
  • πŸ’¬ Extensible Detectors: Easily add new test smell detection logic.
  • πŸ› οΈ Real-time File Watching: Monitors your codebase for changes and re-runs detectors automatically.
  • πŸ§ͺ Jest Testing: Integrated testing setup for robust development.
  • πŸ’‘ TypeScript Support: Built with TypeScript for type safety and improved developer experience.
  • πŸš€ CLI Tool: Command-line interface for easy interaction.

πŸ”Œ Core Libraries

βš›οΈ Core

  • TypeScript: Primary language for the project.
  • @babel/parser: Used for parsing JavaScript/TypeScript code into an AST.
  • @babel/types: Utilities for working with Babel AST nodes.
  • esquery: Powerful tool for querying ASTs with CSS-like selectors.
  • chokidar: File system watcher for real-time monitoring of file changes.
  • commander: Node.js command-line interfaces made easy.

πŸ§ͺ Testing

  • Jest: JavaScript testing framework.
  • ts-jest: TypeScript preprocessor for Jest.

πŸš€ Getting Started

πŸ”§ Prerequisites

To run this project, you will need:

▢️ Run Project

  1. Clone this repository to your local machine.

  2. Install the project dependencies:

    yarn install
  3. To start watching your files for smelly tests, run:

    yarn start

    @snutsjs/core will automatically watch all files in the selected directory and its subdirectories and report findings.

πŸ“š Build and Validate

yarn lint
yarn test
yarn typecheck
yarn build
npm pack --dry-run

🧾 Release Automation (Changesets)

Create a changeset:

yarn changeset

Version packages and changelog:

yarn version-packages

Publish to npm:

yarn release

GitHub Actions workflows are configured to run CI on pull requests and publish through Changesets on merges to main.


πŸ§‘β€πŸ’» Contributing

Want to contribute? Here's how you can help:

  1. Create a new branch for your changes:
    git checkout -b feature/your-feature-name
  2. Implement your changes and commit them with a meaningful message (e.g., :sparkles: feat: Your message here):
    git commit -m "feat: Add new detector for unused imports"
  3. Push your branch to the remote repository:
    git push origin feature/your-feature-name
  4. Open a pull request and request a code review.

πŸ” Git Workflow

Common Commands:

  • Create a new branch:
    git checkout -b your-branch-name
  • Switch to a branch:
    git checkout branch-name
  • Commit your changes:
    git commit -m "Your commit message"
  • Push changes to remote:
    git push
  • Pull updates from remote:
    git pull

πŸ“š Development Setup

🧭 Path Aliases

This project uses path aliases for cleaner imports. Instead of relative paths like ../../../shared/constants, you can use:

// Before
import { MY_CONSTANT } from "../../../shared/constants";

// After
import { MY_CONSTANT } from "@/shared/constants";

Path aliases are configured in:

  • tsconfig.json - For TypeScript resolution
  • babel.config.js - For Babel transpilation (if applicable)
  • jest.config.js - For testing with Jest

πŸ§ͺ Troubleshooting

TypeScript and Aliases Issues

If VS Code or Jest doesn't recognize path aliases:

  • Restart TypeScript server: Ctrl+Shift+P β†’ "TypeScript: Restart TS Server"
  • Ensure tsconfig.json has correct baseUrl and paths configurations.
  • Run yarn tsc --noEmit to verify TypeScript configuration.

File Watcher Issues

If the file watcher (chokidar) doesn't seem to be picking up changes:

  • Ensure you are running yarn start from the project's root directory.
  • Check for any system-level file watch limits (e.g., fs.inotify.max_user_watches on Linux) that might be preventing chokidar from functioning correctly in large projects.

About

core library of snuts

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors