Hone logo
Hone
Problems

Angular Component Reference Finder

Finding all references to a specific Angular component within a project can be a tedious and error-prone task, especially in larger codebases. This challenge asks you to implement a function that analyzes Angular code (represented as strings) and identifies all locations where a given component is used, whether as a template tag, imported module, or injected dependency. This is a crucial tool for refactoring, debugging, and understanding component dependencies.

Problem Description

You are tasked with creating a TypeScript function findComponentReferences that takes the name of an Angular component and an array of code strings (representing files in your Angular project) as input. The function should return an array of objects, where each object represents a reference to the component. Each object should contain the following properties:

  • filePath: The path to the file where the reference was found.
  • line: The line number where the reference was found (1-indexed).
  • type: A string indicating the type of reference. Possible values are:
    • template: The component is used as a template tag (e.g., <app-my-component>).
    • import: The component is imported into the file (e.g., import { MyComponent } from './my-component.component';).
    • dependencyInjection: The component is injected as a dependency (e.g., constructor(private myComponent: MyComponent) {}).

The function should handle various scenarios, including:

  • Component names with hyphens (e.g., app-my-component).
  • Import statements with different syntax (e.g., import { MyComponent } from './my-component.component'; or import * as fromMyComponent from './my-component.component';).
  • Dependency injection using different aliases (e.g., constructor(private myComponent: MyComponent) {} or constructor(public component: MyComponent) {}).
  • Files containing no references to the component.

Examples

Example 1:

Input:
componentName = "MyComponent"
codeFiles = [
  "import { MyComponent } from './my-component.component';\n\n<app-my-component></app-my-component>",
  "export class AnotherComponent {\n  constructor(private myComponent: MyComponent) {}\n}"
]
Output:
[
  { filePath: "file0", line: 1, type: "import" },
  { filePath: "file0", line: 3, type: "template" },
  { filePath: "file1", line: 3, type: "dependencyInjection" }
]
Explanation: The component "MyComponent" is imported in file0 on line 1, used as a template on line 3, and injected as a dependency in file1 on line 3.  File paths are represented as "file0", "file1", etc. based on their index in the `codeFiles` array.

Example 2:

Input:
componentName = "AppHeaderComponent"
codeFiles = [
  "import { AppHeaderComponent } from '../components/app-header.component';\n\n<div></div>",
  "export class AppComponent {}\n"
]
Output:
[
  { filePath: "file0", line: 1, type: "import" }
]
Explanation: The component "AppHeaderComponent" is imported in file0 on line 1, but not used as a template or dependency. File1 contains no references.

Example 3: (Edge Case - Component Name with Hyphens)

Input:
componentName = "app-my-component"
codeFiles = [
  "<app-my-component></app-my-component>",
  "export class SomeComponent {}\n"
]
Output:
[
  { filePath: "file0", line: 1, type: "template" }
]
Explanation: The component "app-my-component" is used as a template in file0 on line 1.

Constraints

  • componentName: A string representing the name of the Angular component to search for.
  • codeFiles: An array of strings, where each string represents the content of a file in the Angular project.
  • The maximum length of each code file string is 10,000 characters.
  • The function should return an array of reference objects. If no references are found, return an empty array.
  • Line numbers should be 1-indexed.
  • Performance: The function should be reasonably efficient for projects with a moderate number of files (up to 100).

Notes

  • You can use regular expressions to search for the component name in the code files. However, be mindful of potential false positives.
  • Consider the different ways an Angular component can be referenced (template tag, import statement, dependency injection).
  • The provided examples are illustrative and may not cover all possible scenarios. Think about edge cases and potential variations in Angular code.
  • Focus on accuracy and clarity of the code. Error handling is not required for this challenge.
  • Assume the input code files are valid TypeScript/HTML. You don't need to validate the code itself.
  • File paths are represented as "file0", "file1", etc. based on their index in the codeFiles array.
Loading editor...
typescript