To extract a shared helper from duplicated code in NestJS, Atlas leverages its semantic search capabilities to identify near-duplicate logic across your project, even when variable names differ. It then guides you through creating a new, tested helper module and replacing each instance of the duplicated code with a call to this new helper, all while integrating direct with your existing `jest` tests, `pnpm` package management, and `prettier` formatting.
How Atlas Finds Duplicated Logic in NestJS Codebases
In 2026, Atlas identifies duplicated logic in NestJS codebases by understanding the semantic meaning of code, not just exact text matches. This capability allows it to surface near-identical implementations across different modules, even if variable names or minor structural elements vary, a task traditional `grep` commands cannot accomplish effectively.
Atlas uses `codebase_search` to perform hybrid semantic and keyword retrieval, powered by AST declarations indexed with tree-sitter. For a NestJS project, this means Atlas can understand the structure of your `@Module` imports, provider scopes, and DTOs. When you ask Atlas to find duplicated logic, it analyzes the behavior you describe, rather than requiring you to provide exact code snippets. For example, if you describe a specific data transformation or validation pattern, Atlas will search across files like `src/modules/users/users.service.ts` and `src/modules/products/products.service.ts` to find all instances that perform that same operation, regardless of whether they use `userDto` or `productDto` as input.
Creating a New NestJS Helper Module with Atlas
Once duplicated logic is identified, creating a new shared helper module in NestJS is a straightforward process with Atlas. The agent drafts the new file, such as `src/common/utils/data-transformer.ts`, and presents a full diff for your review before any changes are written to disk, ensuring you maintain complete control over your codebase in 2026.
After confirming the identified duplicates are genuinely equivalent, you instruct Atlas to `write` the new shared helper. Atlas understands NestJS conventions, so it can suggest appropriate file locations like `src/common/utils/` for shared utilities. For instance, if the duplicated logic involves transforming DTOs, Atlas might create a new `@Injectable()` class or a simple function in `src/common/utils/dto-transformer.ts`. Before writing, Atlas computes a unified diff for the new file and surfaces it for your approval. This permission-gated step ensures that you can review the exact code Atlas proposes, including any necessary imports or exports, before it becomes part of your NestJS project.
Replacing Duplicated Code with Helper Calls in NestJS
Replacing each instance of duplicated code with a call to the new NestJS helper is handled by Atlas using `apply_patch`, ensuring each modification is independently reviewable. This granular approach means you can approve or revert changes one file at a time, such as updating `src/modules/users/users.service.ts` and `src/modules/products/products.service.ts` sequentially, maintaining a clear audit trail in 2026.
Atlas uses `apply_patch` to replace each identified duplicate with a call to your newly created helper. This is not a single, monolithic change; instead, Atlas generates a separate git patch for each file modification. For example, if the helper is `transformData`, Atlas will create one patch to update `src/modules/users/users.service.ts` to call `this.dataTransformer.transformData(...)` and another for `src/modules/products/products.service.ts`. Each patch is presented for your approval, allowing you to review the exact diff and ensure the integration is correct. This method aligns with best practices for code review, making the refactoring process transparent and low-risk.
Ensuring Code Quality and Safety in NestJS Refactoring
Maintaining code quality and ensuring safety during NestJS refactoring is paramount, and Atlas integrates directly with your existing toolchain. After every `apply_patch` operation, Atlas can automatically run your `jest` test suite using `atlas bash pnpm test`, providing immediate feedback on the impact of each change and preventing regressions in 2026.
Atlas prioritizes safety and reviewability throughout the refactoring process. Every Atlas tool call is permission-gated, meaning you explicitly allow, ask, or deny its actions. Before any file is written or modified, Atlas presents a unified diff for your approval. After each `apply_patch` that replaces duplicated code, Atlas can execute `atlas bash pnpm test` to run your `jest` tests. This immediate feedback loop ensures that the refactoring has not introduced any bugs. Furthermore, Atlas can run `prettier --write` across touched providers to maintain consistent formatting. Finally, Atlas snapshots file changes as git patches, allowing for easy rollback if needed, and finishes by using `atlas grep` to confirm no surviving copies of the original duplicated logic remain.
Integrating Atlas with Your NestJS Development Workflow
Integrating Atlas into your NestJS development workflow in 2026 is designed to be direct, working with your existing `nest-cli.json` and `app.module.ts` configurations. Atlas can build its code index with local Ollama embeddings, keeping your proprietary code off third-party servers, and respects your project's specific module and provider structures.
Atlas is terminal-native and designed to complement your existing NestJS setup. It reads your `git` branches, status, and diffs, and can stage and create commits on your behalf. When working with NestJS, Atlas understands `@Module` imports and exports, provider scopes, and DTOs wired to your `ValidationPipe`. This deep understanding allows Atlas to not only find duplicated logic but also to correctly place new helpers and integrate them into your dependency injection container. You can let Atlas build a `Test.createTestingModule` harness for new helper tests, ensuring comprehensive coverage. Atlas's TUI theme system also provides a comfortable development experience, with a charcoal-and-blue default theme and many presets.
Step by step
- 01Ask Atlas to find duplicated logic: Use `atlas codebase_search` to describe the behavior of the duplicated code, rather than exact text, to find near-duplicate implementations across your NestJS project.
- 02Review and confirm duplicates: Read each hit surfaced by Atlas and confirm that the identified code segments are genuinely equivalent and suitable for collapsing into a single NestJS helper.
- 03Create the shared NestJS helper: Instruct `atlas write` to create the new helper file, for example, `src/common/utils/my-helper.ts`. Review the full diff presented by Atlas before approving the file creation.
- 04Replace duplicates with helper calls: Use `atlas apply_patch` to replace each instance of the duplicated code with a call to the new helper. Atlas will generate one patch per file (e.g., `src/modules/feature/feature.service.ts`), allowing for independent review and approval.
- 05Run NestJS tests after each swap: After every `apply_patch`, execute `atlas bash pnpm test` to run your `jest` test suite and immediately verify that the refactoring has not introduced any regressions.
- 06Format touched NestJS files: After all changes are applied, run `atlas bash pnpm prettier --write src/` to ensure all modified NestJS files adhere to your project's formatting standards.
- 07Verify no surviving copies: Conclude the refactoring by running `atlas grep "original duplicated logic snippet"` to confirm that all instances of the old, duplicated code have been successfully removed.
Frequently asked questions
- How does Atlas find duplicated code in NestJS when variable names are different?
- Atlas uses `codebase_search` with hybrid semantic and keyword retrieval, indexing code by AST declarations using tree-sitter. This allows it to understand the underlying logic and behavior of your NestJS code, identifying near-duplicate implementations even if variable names or minor syntax differ, which traditional `grep` cannot do.
- Can Atlas create a new NestJS service or utility class for the helper?
- Yes, Atlas can `write` new files following NestJS conventions. You can instruct it to create a new `@Injectable()` service in `src/common/services/` or a simple utility function in `src/common/utils/`, complete with necessary imports and exports, after reviewing the proposed diff.
- How does Atlas ensure my NestJS application doesn't break during refactoring?
- Atlas ensures safety through permission-gated tool calls, unified diffs for every edit, and integration with your `jest` test runner. After each `apply_patch` operation, Atlas can run `atlas bash pnpm test` to execute your NestJS test suite, providing immediate feedback on any regressions.
- Does Atlas understand NestJS dependency injection and module structure?
- Yes, Atlas is designed to understand NestJS's decorator-driven dependency injection container, `@Module` imports and exports, and provider scopes. This allows it to correctly place new helpers and integrate them into your existing NestJS architecture, for example, by adding them to a module's `providers` array.
- Can I review the changes Atlas makes to my NestJS files?
- Absolutely. Every file edit Atlas proposes, whether creating a new helper or modifying an existing service, is presented as a unified diff for your approval. For replacements, `apply_patch` creates individual git patches per file, making each change independently reviewable and revertible.
- How does Atlas handle code formatting in NestJS projects?
- Atlas integrates with your existing formatting tools. After making changes, you can instruct Atlas to run `atlas bash pnpm prettier --write` across the touched NestJS files or the entire project to ensure all modifications adhere to your `prettier` configuration and maintain consistent code style.
Try Atlas in your terminal
The terminal-native AI coding agent. Free core, single binary.
Install AtlasRelated guides
Extract a Shared Helper from Duplicated Code with Atlas (2026 Workflow)
How to extract a shared helper from duplicated code with Atlas in 2026: codebase_search finds the copies by meaning, write creates the module, apply_patch swaps each call.
Plan a Multi-File Change Before Editing in NestJS with Atlas in 2026
Design and review complex, multi-file changes in your NestJS project using Atlas's plan agent. Get feedback on your strategy before modifying a single line of code, integrating with `jest`, `pnpm`, and `prettier`.
Run Atlas Headless in CI with NestJS in 2026
Automate Atlas sessions in your NestJS CI pipeline for machine-readable output. Learn to configure `atlas run` with `pnpm` and `jest` to integrate AI-driven code changes safely in 2026.
Run the Test Suite and Triage Failures in NestJS with Atlas in 2026
Streamline NestJS test triage in 2026 with Atlas. Turn walls of red `jest` output into prioritized, distinct root causes using `pnpm`, `grep`, and `todowrite`.
Onboard to an unfamiliar codebase in NestJS with Atlas in 2026
Quickly build a mental model of any NestJS codebase using Atlas. Leverage semantic search, AST indexing, and permission-gated tools to understand modules, providers, and DTOs without reading every file.
Diagnose a Hanging or Long-Running Command in NestJS with Atlas in 2026
Quickly diagnose hanging or slow NestJS commands with Atlas in 2026. Learn to distinguish genuinely slow `pnpm` or `jest` runs from those silently blocked on input, and get your NestJS development unstuck.
Migrate a Deprecated API Across Every Callsite in NestJS with Atlas in 2026
Efficiently migrate deprecated APIs across your NestJS codebase using Atlas. Leverage `lsp`, `todowrite`, and `apply_patch` to ensure no callsite is missed, with `jest` and `prettier` integration for a robust workflow.
Audit a NestJS Repository with Parallel Subagents in 2026
Audit your NestJS codebase efficiently in 2026 using Atlas's parallel subagents. Sweep for problems across modules, providers, and DTOs without blowing your context window, leveraging `jest` and `prettier`.