question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional chaining operator on props produces wrong output in angular

See original GitHub issue

I am interested in helping provide a fix!

Yes

Which generators are impacted?

  • All
  • Angular
  • HTML
  • Qwik
  • React
  • React-Native
  • Solid
  • Stencil
  • Svelte
  • Vue
  • Web components

Reproduction case

https://mitosis.builder.io/?outputTab=IYOw5grgNsBOQ%3D%3D%3D&code=JYWwDg9gTgLgBAbzgVwM4FMDKMCGN1wC%2BcAZlBCHAEQACARssADYAm6UAdMBAPQjAwIqYKioBuAFAT0AD0iw4bEjmRN4JZADsAxjG6a4AWQCeAYQqRN6TTAAUYcmFQBKRBLhwo6GMigHb7h5wADwswABuAHyBQSFh4TGxcMAsALwIDhBOAPxcLISJsdpMOKioAHI4IOjpmTkcqBTopiVlBUlBhXDRHR4sEOiocJoQ8ADu0ADWXcE88T1Bs%2FOBzpKEQA%3D

Expected Behaviour

export default function MyComponent(props) {
  const state = useStore({
    value: true,
    visibile: false,
  });

  const someRef = useRef(null);

  return (
    <div
      ref={someRef}
      className={classNames({ 'some-class': state.value })}
    >
      testing
    </div>
  );
}

should produce

@Component({
  selector: "my-component, MyComponent",
  template: `
    <div>
      <div [attr.id]="id" [class]="someClass">does not work</div>
    </div>
  `,
})
export class MyComponent {
  @Input() id: any;
  @Input() someClass: any;
}

Actual Behaviour

This

export default function MyComponent(props) {
  return (
    <div>
      <div
        id={props?.id}
        className={props?.someClass}
        
      >
        does not work
      </div>
    </div>
  );
}

produces

@Component({
  selector: "my-component, MyComponent",
  template: `
    <div>
      <div [attr.id]="props?.id" [class]="props?.someClass">does not work</div>
    </div>
  `,
})
export class MyComponent {}

but props is not defined anywhere.

Additional Information

If you don’t use optional chaining operator ?. works as expected.

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
samijabercommented, Nov 29, 2022

I am in the process of making the code that strips/replaces props. more intelligent, using babel ASTs. So we’ll wait for that to happen before digging into this any further, as it’s a lot priority issue.

1reaction
samijabercommented, Nov 8, 2022

That’s fair. Was mostly pointing out that it’s never technically needed in the Mitosis code.

Fixing this should be as simple as updating this regex check:

https://github.com/BuilderIO/mitosis/blob/a762948e6014450d5b8b80c961b771df4568f847/packages/core/src/helpers/strip-state-and-props-refs.ts#L67-L71

to include an optional ?, e.g. /props(\??)\./

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is wrong with optional chaining and how to fix it - DEV ...
Optional chaining simplifies above and removes a lot of errors by saying that values considered as no value are only two - null...
Read more >
Optional Chaining in JavaScript returns undefined instead of ...
An optional chain does not evaluate to the nullish value on which the property was accessed, but to undefined - just like you...
Read more >
Align with the optional chaining spec · Issue #34385 - GitHub
TL;DR: Angular Templates resolves to null in safe navigation and TypeScript resolves to undefined in optional chaining. Consider the following:.
Read more >
The Beauty of the Optional Chaining Operator (?.) in TypeScript
The OR value in a simple optional chaining expression, triggered when the nullish check fails, is always undefined .
Read more >
Optional Chaining: The ?. Operator in TypeScript
Optional chaining [is] a property access and function invocation operator that short-circuits if the value to access/invoke is nullish. The ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found