Repository: ember-codemods/ember-angle-brackets-codemod (js)

Issue:
`link-to` with conditional model is not converted correctly
Before:
```hbs
{{link-to (t "show") "user" (if linkActor event.actor.id event.user.id)}}
```

Expected:
```hbs
<LinkTo @route="user" @model={{if linkActor event.actor.id event.user.id}}>{{t "show"}}</LinkTo>
```

Actual:
```hbs
<LinkTo @route="user" @query={{hash}}>{{t "show"}}</LinkTo>
```

Pull request description:
`link-to` with conditional model
fixes #156 

Patch:
diff --git a/transforms/angle-brackets/transform.js b/transforms/angle-brackets/transform.js
index 170a39a..73d330d 100755
--- a/transforms/angle-brackets/transform.js
+++ b/transforms/angle-brackets/transform.js
@@ -240,8 +240,19 @@ function transformToAngleBracket(env, fileInfo, config) {
       // eslint-disable-next-line no-unused-vars
       let [_, secondParamInput] = params;
       if (secondParamInput.type === 'SubExpression') {
-        let _queryParam = b.attr('@query', b.mustache(b.path('hash'), [], secondParamInput.hash));
-        attributes = [firstParamOutput, _queryParam];
+        let _queryParamOrModel;
+        if (isQueryParam(secondParamInput)) {
+          _queryParamOrModel = b.attr(
+            '@query',
+            b.mustache(b.path('hash'), [], secondParamInput.hash)
+          );
+        } else {
+          _queryParamOrModel = b.attr(
+            '@model',
+            b.mustache(secondParamInput.path, secondParamInput.params)
+          );
+        }
+        attributes = [firstParamOutput, _queryParamOrModel];
       } else {
         let _modelParam = b.attr('@model', transformModelParams(secondParamInput));
         attributes = [firstParamOutput, _modelParam];
@@ -255,8 +266,15 @@ function transformToAngleBracket(env, fileInfo, config) {
       let _qpParam;
 
       if (hasQueryParamHelper) {
-        _modelsParam = b.attr('@model', transformModelParams(models[0]));
-        _qpParam = b.attr('@query', b.mustache(b.path('hash'), [], models[1].hash));
+        if (models.length < 3) {
+          _modelsParam = b.attr('@model', transformModelParams(models[0]));
+        } else {
+          _modelsParam = b.attr(
+            '@models',
+            b.mustache(b.path('array'), models.slice().splice(0, 2))
+          );
+        }
+        _qpParam = b.attr('@query', b.mustache(b.path('hash'), [], models[models.length - 1].hash));
       } else {
         _modelsParam = b.attr('@models', b.mustache(b.path('array'), models));
       }


Test patch:
diff --git a/transforms/angle-brackets/test.js b/transforms/angle-brackets/test.js
index d92c4a3..fece3aa 100644
--- a/transforms/angle-brackets/test.js
+++ b/transforms/angle-brackets/test.js
@@ -338,12 +338,20 @@ test('link-to', () => {
   let input = `
     {{#link-to "about"}}About Us{{/link-to}}
     {{#link-to this.dynamicRoute}}About Us{{/link-to}}
+    {{#link-to "user" this.first this.second}}Show{{/link-to}}
+    {{#link-to "user" this.first this.second (query-params foo="baz")}}Show{{/link-to}}
+    {{#link-to "user" this.first}}Show{{/link-to}}
+    {{#link-to "user" this.first (query-params foo="baz")}}Show{{/link-to}}
   `;
 
   expect(runTest('link-to.hbs', input)).toMatchInlineSnapshot(`
     "
         <LinkTo @route=\\"about\\">About Us</LinkTo>
         <LinkTo @route={{this.dynamicRoute}}>About Us</LinkTo>
+        <LinkTo @route=\\"user\\" @models={{array this.first this.second}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @models={{array this.first this.second}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @model={{this.first}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @model={{this.first}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
       "
   `);
 });
@@ -374,6 +382,11 @@ test('link-to-inline', () => {
       class="t__em-link"
     }}
     {{link-to (t "show") "flight" event.flight.id class="btn btn-default btn-sm pull-right"}}
+    {{link-to (t "show") "user" (if linkActor event.actor.id event.user.id)}}
+    {{link-to "Show" "user" this.first this.second}}
+    {{link-to "Show" "user" this.first this.second (query-params foo="baz")}}
+    {{link-to "Show" "user" this.first}}
+    {{link-to "Show" "user" this.first (query-params foo="baz")}}
   `;
 
   /**
@@ -388,6 +401,11 @@ test('link-to-inline', () => {
         <LinkTo @route={{this.dynamicPath}} class=\\"tabs__discrete-tab\\" @activeClass=\\"o__selected\\" @current-when=\\"apps.segments\\" data-test-segment-link=\\"segments\\">Segments</LinkTo>
         <LinkTo @route=\\"apps.app.companies.segments.segment\\" @model={{segment}} class=\\"t__em-link\\">{{segment.name}}</LinkTo>
         <LinkTo @route=\\"flight\\" @model={{event.flight.id}} class=\\"btn btn-default btn-sm pull-right\\">{{t \\"show\\"}}</LinkTo>
+        <LinkTo @route=\\"user\\" @model={{if linkActor event.actor.id event.user.id}}>{{t \\"show\\"}}</LinkTo>
+        <LinkTo @route=\\"user\\" @models={{array this.first this.second}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @models={{array this.first this.second}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @model={{this.first}}>Show</LinkTo>
+        <LinkTo @route=\\"user\\" @model={{this.first}} @query={{hash foo=\\"baz\\"}}>Show</LinkTo>
       "
   `);
 });