Repository: abuiles/ember-watson
Pull request #80 (2015-09-02 07:37:56)

Check whether it is an expression before accessing the expression properties
Need to check whether the node is a `expression` before getting the `expression properties`

Found while testing the issue #79 

Feel free to reject the PR if not needed.


Patch:
diff --git a/lib/formulas/resource-router-mapping.js b/lib/formulas/resource-router-mapping.js
index 64b1b21..5fd12a1 100644
--- a/lib/formulas/resource-router-mapping.js
+++ b/lib/formulas/resource-router-mapping.js
@@ -30,9 +30,11 @@ function traverseExpressions(routes) {
     if (isResourceRoute(route)) {
       transformResourceToRoute(route);
     }
-    var lastArgument = route.expression.arguments[route.expression.arguments.length-1];
-    if (types.FunctionExpression.check(lastArgument)) {
-      traverseExpressions(lastArgument.body.body);
+    if (route.expression && types.CallExpression.check(route.expression)) {
+      var lastArgument = route.expression.arguments[route.expression.arguments.length-1];
+      if (types.FunctionExpression.check(lastArgument)) {
+        traverseExpressions(lastArgument.body.body);
+      }
     }
   });
 }


Test patch:
diff --git a/tests/fixtures/resource-router-mapping/new-ember-cli-conditional-sample.js b/tests/fixtures/resource-router-mapping/new-ember-cli-conditional-sample.js
new file mode 100644
index 0000000..5e12aa0
--- /dev/null
+++ b/tests/fixtures/resource-router-mapping/new-ember-cli-conditional-sample.js
@@ -0,0 +1,27 @@
+import Ember from 'ember';
+import config from './config/environment';
+
+let AdminUser = true;
+
+var Router = Ember.Router.extend({
+  location: config.locationType
+});
+
+Router.map(function () {
+  this.route('items', {
+    resetNamespace: true
+  }, function () {
+    this.route('item_details', {
+      path: '/:item_id',
+      resetNamespace: true
+    }, function () {
+      if (AdminUser) {
+        this.route('create_subitem', {
+          path: '/new_sub_item'
+        });
+      }
+    });
+  });
+});
+
+export default Router;
diff --git a/tests/fixtures/resource-router-mapping/old-ember-cli-conditional-sample.js b/tests/fixtures/resource-router-mapping/old-ember-cli-conditional-sample.js
new file mode 100644
index 0000000..c9a510c
--- /dev/null
+++ b/tests/fixtures/resource-router-mapping/old-ember-cli-conditional-sample.js
@@ -0,0 +1,24 @@
+import Ember from 'ember';
+import config from './config/environment';
+
+let AdminUser = true;
+
+var Router = Ember.Router.extend({
+  location: config.locationType
+});
+
+Router.map(function () {
+  this.resource('items', function () {
+    this.resource('item_details', {
+      path: '/:item_id'
+    }, function () {
+      if (AdminUser) {
+        this.route('create_subitem', {
+          path: '/new_sub_item'
+        });
+      }
+    });
+  });
+});
+
+export default Router;
diff --git a/tests/resource-route-mapping-test.js b/tests/resource-route-mapping-test.js
index eabdb20..fb9a425 100644
--- a/tests/resource-route-mapping-test.js
+++ b/tests/resource-route-mapping-test.js
@@ -13,7 +13,7 @@ describe('Resource router mapping', function() {
   });
 
   describe('convert resource to route with resetNamespace option', function() {
-    it('transoform simple resource', function() {
+    it('transform simple resource', function() {
       var source            = fs.readFileSync(baseDir + '/old-foos-route.js');
       var newSource         = watson._transformResourceRouterMapping(source);
       var expectedNewSource = fs.readFileSync(baseDir + '/new-foos-route.js');
@@ -44,5 +44,13 @@ describe('Resource router mapping', function() {
 
       astEquality(newSource, expectedNewSource);
     });
+
+    it('transform conditional routing in ember-cli file format', function() {
+      var source            = fs.readFileSync(baseDir + '/old-ember-cli-conditional-sample.js');
+      var newSource         = watson._transformResourceRouterMapping(source);
+      var expectedNewSource = fs.readFileSync(baseDir + '/new-ember-cli-conditional-sample.js');
+
+      astEquality(newSource, expectedNewSource);
+    });
   });
 });