Repository: abuiles/ember-watson (js)

Issue:
interest in adding other transforms like es6.properties.shorthand ?
https://github.com/stefanpenner/es6.properties.shorthand/blob/master/index.js

```js
var a = {
  foo: function() { }
};
```

becomes:


```js
var a = {
  foo() { }
};
```


Pull request:
Add a methodify formula.
Fix #14

Patch:
diff --git a/addon/commands/index.js b/addon/commands/index.js
index 8011cad..198368f 100644
--- a/addon/commands/index.js
+++ b/addon/commands/index.js
@@ -5,5 +5,6 @@ module.exports = {
   'watson:convert-prototype-extensions': require('./convert-prototype-extensions'),
   'watson:convert-ember-data-model-lookups': require('./convert-ember-data-model-lookups'),
   'watson:convert-ember-data-async-false-relationships': require('./convert-ember-data-async-false-relationships'),
+  'watson:methodify': require('./methodify'),
   'watson:convert-resource-router-mapping': require('./convert-resource-router-mapping')
 };
diff --git a/addon/commands/methodify.js b/addon/commands/methodify.js
new file mode 100644
index 0000000..e83a2bc
--- /dev/null
+++ b/addon/commands/methodify.js
@@ -0,0 +1,17 @@
+'use strict';
+
+var Watson = require('../../index');
+var watson = new Watson();
+
+module.exports = {
+  name: 'watson:methodify',
+  description: 'Convert methods to new ES6 syntax.',
+  works: 'insideProject',
+  anonymousOptions: [
+    '<path>'
+  ],
+  run: function(commandOptions, rawArgs) {
+    var path = rawArgs || 'app';
+    watson.transformMethodify(path);
+  }
+};
diff --git a/lib/cli.js b/lib/cli.js
index d387a37..d911dca 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -48,6 +48,14 @@ program
     watson.transformResourceRouterMapping(routerPath);
   });
 
+program
+  .command('methodify [path]')
+  .description('Convert methods to new ES6 syntax')
+  .action(function(path) {
+    path = path || 'app';
+    watson.transformMethodify(path);
+  });
+
 module.exports = function init(args) {
   program.parse(args);
 };
diff --git a/lib/formulas/methodify.js b/lib/formulas/methodify.js
new file mode 100644
index 0000000..1243ead
--- /dev/null
+++ b/lib/formulas/methodify.js
@@ -0,0 +1,30 @@
+var recast      = require('recast');
+var builders    = recast.types.builders;
+var types       = recast.types.namedTypes;
+var isImportFor = require('./helpers/is-import-for');
+
+function isNotES6Method(node) {
+  return types.Property.check(node) &&
+         types.FunctionExpression.check(node.value) &&
+         !node.method;
+}
+
+function makeES6Method(path) {
+  var node = path.node;
+  node.method = true;
+}
+
+module.exports = function transform(source) {
+  var ast = recast.parse(source);
+
+  recast.visit(ast, {
+    visitProperty: function(path) {
+      if(isNotES6Method(path.node)) {
+        makeES6Method(path);
+      }
+      this.traverse(path);
+    }
+  });
+
+  return recast.print(ast, { tabWidth: 2, quote: 'single' }).code;
+};
diff --git a/lib/watson.js b/lib/watson.js
index 5025646..b322cdc 100644
--- a/lib/watson.js
+++ b/lib/watson.js
@@ -8,6 +8,7 @@ var prototypeExtensionTransforms = require('./formulas/prototype-extension-trans
 var transformEmberDataModelLookups = require('./formulas/ember-data-model-lookup-transform');
 var transformEmberDataAsyncFalseRelationships = require('./formulas/ember-data-async-false-relationships-transforms');
 var transformResourceRouterMapping = require('./formulas/resource-router-mapping');
+var transformMethodify = require('./formulas/methodify');
 
 
 module.exports = EmberWatson;
@@ -48,6 +49,13 @@ EmberWatson.prototype.transformPrototypeExtensions = function(rootPath) {
   transform(files, this._transformPrototypeExtensions);
 };
 
+EmberWatson.prototype._transformMethodify = transformMethodify;
+EmberWatson.prototype.transformMethodify = function(rootPath) {
+  var files = findFiles(rootPath, '.js');
+
+  transform(files, this._transformMethodify);
+};
+
 EmberWatson.prototype._transformResourceRouterMapping = transformResourceRouterMapping;
 
 EmberWatson.prototype.transformResourceRouterMapping = function(routerPath) {


Test patch:
diff --git a/tests/cli-test.js b/tests/cli-test.js
index 7b2233e..412ea80 100644
--- a/tests/cli-test.js
+++ b/tests/cli-test.js
@@ -20,6 +20,10 @@ var commands = {
     method: 'transformEmberDataAsyncFalseRelationships',
     path: 'app'
   },
+  'methodify': {
+    method: 'transformMethodify',
+    path: 'app'
+  },
   'convert-resource-router-mapping': {
     method: 'transformResourceRouterMapping',
     path: 'app/router.js'
diff --git a/tests/fixtures/methodify/object-new.js b/tests/fixtures/methodify/object-new.js
new file mode 100644
index 0000000..984d8f5
--- /dev/null
+++ b/tests/fixtures/methodify/object-new.js
@@ -0,0 +1,10 @@
+var myObject = {
+  property: 'first',
+  method(some, args) {
+    return some + args;
+  },
+  otherProperty: 'second',
+  otherMethod() {
+    console.log('in otherMethod');
+  },
+};
diff --git a/tests/fixtures/methodify/object-old.js b/tests/fixtures/methodify/object-old.js
new file mode 100644
index 0000000..6a63e54
--- /dev/null
+++ b/tests/fixtures/methodify/object-old.js
@@ -0,0 +1,10 @@
+var myObject = {
+  property: 'first',
+  method: function(some, args) {
+    return some + args;
+  },
+  otherProperty: 'second',
+  otherMethod: function() {
+    console.log('in otherMethod');
+  },
+};
diff --git a/tests/methodify-test.js b/tests/methodify-test.js
new file mode 100644
index 0000000..a7e03f8
--- /dev/null
+++ b/tests/methodify-test.js
@@ -0,0 +1,22 @@
+'use strict';
+
+var Watson      = require('../index.js');
+var fs          = require('fs');
+var astEquality = require('./helpers/ast-equality');
+
+describe('convert methods to ES6 syntax', function() {
+  var baseDir = './tests/fixtures/methodify';
+  var watson;
+
+  beforeEach(function() {
+    watson = new Watson();
+  });
+
+  it('converts to ES6 method syntax', function() {
+    var source = fs.readFileSync(baseDir + '/object-old.js');
+    var newSource = watson._transformMethodify(source);
+    var expectedNewSource = fs.readFileSync(baseDir + '/object-new.js');
+
+    astEquality(newSource, expectedNewSource);
+  });
+});