Skip to content

Commit

Permalink
fix: state init in angular (#1515)
Browse files Browse the repository at this point in the history
* fix state init in angular

* changeset
  • Loading branch information
sidmohanty11 committed Jul 31, 2024
1 parent 0c40cf8 commit 48f5481
Show file tree
Hide file tree
Showing 9 changed files with 780 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-monkeys-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

fix: angular state initialization referencing other states or props
132 changes: 126 additions & 6 deletions packages/core/src/__tests__/__snapshots__/angular.import.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3135,9 +3135,13 @@ import { Component, Input } from \\"@angular/core\\";
export default class MyComponent {
@Input() componentRef;

refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
refToUse = null;

ngOnInit() {
this.refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
}
}

@NgModule({
Expand Down Expand Up @@ -4909,6 +4913,62 @@ export class MyBasicComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > stateInit 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<div>
<div>{{asfas}}</div>
<div>{{someCompute}}</div>
<div>{{someOtherVal}}</div>
<div>{{sf}}</div>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class MyComponent {
@Input() val;

add = function add(a, b) {
return a + b;
};
asfas = \\"asga\\";
subtract() {
return this.someCompute - this.someOtherVal;
}
someCompute = null;
someOtherVal = null;
sf = null;

ngOnInit() {
this.someCompute = this.add(1, 2);

this.someOtherVal = this.val;

this.sf = this.add(this.val, 34);
}
}

@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > styleClassAndCss 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -8809,9 +8869,13 @@ import { Component, Input } from \\"@angular/core\\";
export default class MyComponent {
@Input() componentRef!: any;

refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
refToUse = null;

ngOnInit() {
this.refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
}
}

@NgModule({
Expand Down Expand Up @@ -10704,6 +10768,62 @@ export class MyBasicComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > stateInit 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<div>
<div>{{asfas}}</div>
<div>{{someCompute}}</div>
<div>{{someOtherVal}}</div>
<div>{{sf}}</div>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class MyComponent {
@Input() val!: any;

add = function add(a, b) {
return a + b;
};
asfas = \\"asga\\";
subtract() {
return this.someCompute - this.someOtherVal;
}
someCompute = null;
someOtherVal = null;
sf = null;

ngOnInit() {
this.someCompute = this.add(1, 2);

this.someOtherVal = this.val;

this.sf = this.add(this.val, 34);
}
}

@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: [MyComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > styleClassAndCss 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
134 changes: 128 additions & 6 deletions packages/core/src/__tests__/__snapshots__/angular.mapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3192,9 +3192,13 @@ import { Component, Input } from \\"@angular/core\\";
export default class MyComponent {
@Input() componentRef;

refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
refToUse = null;

ngOnInit() {
this.refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
}
}

@NgModule({
Expand Down Expand Up @@ -5004,6 +5008,63 @@ export class MyBasicComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > stateInit 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<div>
<div>{{asfas}}</div>
<div>{{someCompute}}</div>
<div>{{someOtherVal}}</div>
<div>{{sf}}</div>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class MyComponent {
@Input() val;

add = function add(a, b) {
return a + b;
};
asfas = \\"asga\\";
subtract() {
return this.someCompute - this.someOtherVal;
}
someCompute = null;
someOtherVal = null;
sf = null;

ngOnInit() {
this.someCompute = this.add(1, 2);

this.someOtherVal = this.val;

this.sf = this.add(this.val, 34);
}
}

@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > styleClassAndCss 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -8973,9 +9034,13 @@ import { Component, Input } from \\"@angular/core\\";
export default class MyComponent {
@Input() componentRef!: any;

refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
refToUse = null;

ngOnInit() {
this.refToUse = !(this.componentRef instanceof Function)
? this.componentRef
: null;
}
}

@NgModule({
Expand Down Expand Up @@ -10908,6 +10973,63 @@ export class MyBasicComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > stateInit 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"my-component, MyComponent\\",
template: \`
<div>
<div>{{asfas}}</div>
<div>{{someCompute}}</div>
<div>{{someOtherVal}}</div>
<div>{{sf}}</div>
</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class MyComponent {
@Input() val!: any;

add = function add(a, b) {
return a + b;
};
asfas = \\"asga\\";
subtract() {
return this.someCompute - this.someOtherVal;
}
someCompute = null;
someOtherVal = null;
sf = null;

ngOnInit() {
this.someCompute = this.add(1, 2);

this.someOtherVal = this.val;

this.sf = this.add(this.val, 34);
}
}

@NgModule({
declarations: [MyComponent],
imports: [CommonModule],
exports: [MyComponent],
bootstrap: [SomeOtherComponent],
})
export class MyComponentModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > styleClassAndCss 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Loading

0 comments on commit 48f5481

Please sign in to comment.