How depcheck/depcheck works
Main Components
• CLI Interface – Represented by files such as src/cli.js and the entry point in src/index.js. This component handles user input, processes command-line arguments, and triggers the dependency analysis flow.
• Core Analysis Engine – The main orchestrator that ties in parsers, detectors, and special modules. This is essentially the logic in src/index.js that uses configuration files (like .depcheckrc) and utilities.
• Detector Modules – Located in src/detector, these modules (e.g., exportDeclaration.js, requireCallExpression.js, and others) implement various rules to detect how dependencies are referenced in the code.
• Parser Modules – Found in src/parser, these handle different syntaxes: ES6, ES7, JSX, CoffeeScript, TypeScript, GraphQL, Sass, Vue, etc. They analyze the files to extract dependency information.
• Special Modules – Under src/special, these address non-standard or framework-specific dependency declarations (e.g., babel.js, eslint.js, gatsby.js, webpack.js, etc.). They clarify dependencies used in configuration or build environments.
• Utility and Configuration Components – In src/utils, modules like configuration-reader.js and parser.js interplay with the file system, handle exceptions, and support configuration via external files.
• Test and Build Infrastructure – Although not part of the runtime flow, test directories and build scripts (e.g., under bin and build) indicate how the project is structured for development, continuous integration, and deployment.
Relationships & Data Flow
• The CLI then calls the core analysis engine, which reads the project’s configuration (possibly merging CLI overrides with .depcheckrc configuration) using utility modules.
• The core engine iterates over the source files, distributing them to appropriate parser modules based on file patterns (syntax types). The parsers return parsed data on dependency usage.
• Parsed data is then fed into a series of detectors that analyze import/require statements and other patterns (such as express view engine usage, grunt tasks, etc.) to determine dependency usage.
• Special modules augment this process by addressing dependencies in contextual cases like Babel configurations or ESLint setups.
• Finally, the results are aggregated (including unused dependencies, missing declarations, and detailed usage maps) and formatted for output (either neatly to the console or as JSON).
Architectural and Design Patterns
• Modularity – Each type of analysis (parsing, detection, handling specials) is split into its own module or set of modules, making it easy to plug new language supports or frameworks.
• Plugin/Pluggable Design – Depcheck’s “pluggable” design (as mentioned in the README with a reference to the pluggable design document) encourages adding or replacing parsers, detectors, and special handlers without modifying the core logic.
• Utility Abstraction – Helpers for file operations, configuration reading, and exception management are encapsulated in the utils folder, reinforcing a clean, layered design.