Angular Workspace Symbols: Defining and Resolving Custom Symbols
Angular workspace symbols allow you to define reusable components, directives, pipes, or services across your project, improving code organization and reducing duplication. This challenge focuses on creating a simple system for defining and resolving these symbols within an Angular application, enabling developers to easily import and use them. Successfully completing this challenge will demonstrate understanding of Angular's module system and how to manage custom symbols.
Problem Description
You are tasked with building a basic workspace symbol resolution system. The system should allow you to define symbols (represented as strings) and associate them with specific module paths (also strings). When a symbol is requested, the system should return the corresponding module path. The system should handle cases where a symbol is not defined and provide a default behavior.
What needs to be achieved:
- Create a
WorkspaceSymbolsServicethat manages a mapping of symbols to module paths. - Implement methods to define a new symbol and retrieve the module path associated with a given symbol.
- Handle the case where a symbol is not found gracefully.
Key Requirements:
- The
WorkspaceSymbolsServiceshould store symbols and module paths in a private data structure (e.g., a JavaScript object or Map). - The
defineSymbolmethod should allow you to associate a symbol with a module path. - The
getModulePathForSymbolmethod should return the module path for a given symbol. - If a symbol is not found,
getModulePathForSymbolshould returnnull. - The service should be injectable into Angular components or other services.
Expected Behavior:
- Defining a symbol and then retrieving it should return the correct module path.
- Retrieving a non-existent symbol should return
null. - Multiple calls to
getModulePathForSymbolwith the same symbol should consistently return the same module path.
Edge Cases to Consider:
- What happens if you try to define a symbol that already exists? (Overwrite the existing path or throw an error?) For this challenge, overwriting is acceptable.
- What happens if the module path is an empty string? (Allow it or throw an error?) Allowing an empty string is acceptable.
- What happens if the symbol is an empty string? (Allow it or throw an error?) Allowing an empty string is acceptable.
Examples
Example 1:
Input:
- defineSymbol('MyComponent', 'src/app/components/my-component/my-component.module.ts')
- getModulePathForSymbol('MyComponent')
Output: 'src/app/components/my-component/my-component.module.ts'
Explanation: The symbol 'MyComponent' is defined and its associated module path is returned.
Example 2:
Input:
- defineSymbol('MyService', 'src/app/services/my-service.module.ts')
- getModulePathForSymbol('NonExistentSymbol')
Output: null
Explanation: The symbol 'NonExistentSymbol' is not defined, so null is returned.
Example 3: (Edge Case)
Input:
- defineSymbol('MyComponent', 'src/app/components/my-component/my-component.module.ts')
- defineSymbol('MyComponent', 'src/app/components/another-component/my-component.module.ts')
- getModulePathForSymbol('MyComponent')
Output: 'src/app/components/another-component/my-component.module.ts'
Explanation: The symbol 'MyComponent' is redefined with a new module path. The latest definition is returned.
Constraints
- All module paths and symbols are strings.
- The
WorkspaceSymbolsServiceshould be designed for use within an Angular application. - Performance is not a primary concern for this challenge; focus on correctness and clarity.
- The service should be thread-safe (although this is less critical in a typical Angular application).
Notes
- Consider using a
Mapfor storing the symbol-to-module path mapping for efficient lookups. - Think about how you would handle errors or invalid input (e.g., non-string module paths). For this challenge, you can assume valid input.
- This is a simplified model of workspace symbols. Real-world implementations often involve more complex features like symbol resolution precedence and dependency management.
- Focus on creating a clean and well-documented service.