# AUR0157

## Warning Message

`AUR0157: Binding command <command-name> has already been registered.`

Where `<command-name>` is the name of the binding command (e.g., `trigger`, `for`, `bind`) that was attempted to be registered again.

## Description

This warning indicates that an attempt was made to register a binding command with a specific name, but another binding command with the same name was already registered in the application's configuration. Binding commands define how specific binding syntaxes (like `.bind`, `.trigger`, `.for`) are processed by the template compiler. Each command name must be unique.

**Note:** This is typically logged as a warning. The application might continue, but only the first registration for the given command name will be effective. Overwriting built-in commands or duplicating custom commands can lead to unpredictable compilation and runtime binding behavior.

## Cause

1. **Duplicate Custom Command Registration:** Calling `Aurelia.register()` or a similar registration method multiple times with the same custom binding command class or different classes using the same command name via the `@bindingCommand` decorator or configuration object.
2. **Attempting to Overwrite Built-in Commands:** Defining a custom binding command with a name that conflicts with a standard Aurelia command (e.g., `bind`, `trigger`, `for`, `one-time`, `to-view`, `from-view`, `two-way`, `capture`). While possible, this is generally discouraged unless you fully understand the implications.
3. **Naming Collision:** Accidentally defining two different custom binding command classes that use the same command name.

## Solution

1. **Ensure Unique Names:** Choose a unique name for each custom binding command. Avoid using names of built-in commands unless intentionally replacing core functionality (use with caution).
2. **Register Once:** Ensure each binding command (especially custom ones) is registered only once. Review your application's registration configuration, typically in `main.ts` or where `Aurelia.register(...)` is called with binding command configurations.
3. **Refactor/Rename:** If you have two distinct custom commands that ended up with the same name, rename one of them.

## Example

```typescript
// my-custom-command.ts
import { bindingCommand, type BindingCommandInstance, type BindingInfo, type ExpressionParser, type IAttrMapper, type BindingInstruction, PropertyBindingInstruction } from 'aurelia';

// Correct: Defining a custom command
@bindingCommand('my-cmd')
export class MyCustomBindingCommand implements BindingCommandInstance {
  public readonly type = 'bind'; // Or other valid type
  build(info: BindingInfo, parser: ExpressionParser, mapper: IAttrMapper): BindingInstruction {
    // Command logic...
    // Example: return new PropertyBindingInstruction(info.expr, info.attr.target);
    return null! // Placeholder
  }
}

// another-command.ts - Incorrect: Uses the same name
import { bindingCommand, type BindingCommandInstance } from 'aurelia';
@bindingCommand('my-cmd') // Causes AUR0157 if both are registered
export class AnotherCustomBindingCommand implements BindingCommandInstance {
  // ... command logic ...
  public readonly type = 'bind';
  build(info: BindingInfo, parser: ExpressionParser, mapper: IAttrMapper): BindingInstruction { return null!; }
}

// trigger-command-override.ts - Discouraged: Overwriting built-in
import { bindingCommand, type BindingCommandInstance } from 'aurelia';
@bindingCommand('trigger') // Causes AUR0157, overwrites standard .trigger
export class TriggerOverrideCommand implements BindingCommandInstance {
  // ... command logic ...
  public readonly type = 'trigger';
  build(info: BindingInfo, parser: ExpressionParser, mapper: IAttrMapper): BindingInstruction { return null!; }
}


// --- main.ts ---
import { Aurelia, BindingCommand } from 'aurelia';
import { MyCustomBindingCommand } from './my-custom-command';
import { AnotherCustomBindingCommand } from './another-command';
// import { TriggerOverrideCommand } from './trigger-command-override';

// Incorrect: Attempting to register two commands with the same name 'my-cmd'
// Aurelia.register(
//   BindingCommand.define('my-cmd', MyCustomBindingCommand),
//   BindingCommand.define('my-cmd', AnotherCustomBindingCommand)
// ) /* ... */;

// Correct: Register only one command for the name 'my-cmd'
Aurelia.register(
  BindingCommand.define('my-cmd', MyCustomBindingCommand)
) /* ... */;

// Discouraged: Overwriting trigger (will log AUR0157)
// Aurelia.register(
//   BindingCommand.define('trigger', TriggerOverrideCommand)
// ) /* ... */;

```

## Debugging Tips

* Search your project for `@bindingCommand('<command-name>')` or `BindingCommand.define('<command-name>', ...)` usages for the command name mentioned in the warning.
* Review your main configuration/registration file (`main.ts` or similar) to ensure commands are registered only once.
* Compare your custom command names against the list of built-in Aurelia binding commands to avoid unintentional conflicts.
