Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swc-plugin-angular] Functional APIs (e.g. input()) are broken if a property is declared in the constructor #304

Open
yjaaidi opened this issue Apr 4, 2024 · 2 comments
Labels
enhancement New feature or request swc-angular

Comments

@yjaaidi
Copy link
Member

yjaaidi commented Apr 4, 2024

This doesn't work because the input is not decorated:

class MyCmp {
  title = input();
  constructor(private myService: MyService) {}
}

This happens because this is parsed by SWC as:

class MyCmp {
  title;
  private myService;
  constructor(myService) {
    this.myService = myService;
    this.title = input();
  }
}

We have to detect functional APIs in the constructor.

Meanwhile, the workaround is to use the inject() function which is the approach I'd recommend in general.

@yjaaidi yjaaidi assigned yjaaidi and unassigned yjaaidi Apr 4, 2024
@yjaaidi yjaaidi changed the title Functional APIs (e.g. input()) are broken if a property is declared in the constructor [swc-plugin-angular] Functional APIs (e.g. input()) are broken if a property is declared in the constructor Apr 4, 2024
@edbzn edbzn added enhancement New feature or request swc-angular and removed swc-plugin-angular labels May 27, 2024
@pumano
Copy link
Contributor

pumano commented Sep 2, 2024

got this also: looks like problem in ts config "useDefineClassFields" where by default is true, but need false for proper working (in my case). I got some issue about that swc-project/swc#6985 but when I try to set

.swcrc

{
  "$schema": "https://swc.rs/schema.json",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "transform": {
      "useDefineForClassFields": false,
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "target": "es2022"
  },
  "module": {
    "type": "commonjs"
  }
}

Also I try to add that in swcAngularPreset inside npm package "@swc-angular" but it's not working.

console.error
      NG0303: Can't set value of the 'addresses' input on the 'AddressComponent' component. Make sure that the 'addresses' property is annotated with @Input() or a mapped @Input('addresses') exists.

@pumano
Copy link
Contributor

pumano commented Sep 2, 2024

I prepare unit test (but have to luck to run it due to failed build of lib):

Need to add it to "tests/swc-angular-wide/src/swc-plugin-angular-input.spec.ts" and run.

  it('should bind inputs via setInput', () => {
    @Component({
      standalone: true,
      selector: 'jsc-title',
      template: ` <h1>Hello {{ title() }}!</h1> `,
    })
    class Title {
      title = input<string>();
    }

    const { fixture } = createComponent(Title);
    fixture.componentRef.setInput('title', 'title')
    expect((fixture.componentInstance as Title).title).toBe('title');
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request swc-angular
Projects
None yet
Development

No branches or pull requests

3 participants