Skip to content

Importing classes breaks anonymous Livewire components #151

Description

@diboma

When using the PHP Namespace Resolver extension in VSCode, importing a class via Ctrl+Alt+I or the context‑menu option causes incorrect behavior inside a Livewire 4 anonymous component.

Example:

new #[Layout('layouts.admin')] class extends Component {
    use WithPagination;
}

Problems encountered:

  1. The imported class is inserted inside the anonymous class, e.g.:
new class extends Component {
    use WithPagination;
    use Maatwebsite\Excel\Facades\Excel; // should be placed at the top of the file
}
  1. In some cases, the entire new #[Layout(...)] class extends Component { block is removed or corrupted after triggering an import.

It seems the extension does not correctly detect or parse PHP anonymous classes, especially when attributes are present. This makes it unsafe to use the import feature in Livewire single‑file components.

Could support for anonymous classes be improved so imports are added at the file level without modifying or breaking the class expression?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions