Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform CP and observer using prototype extensions #10

Merged
merged 1 commit into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion bin/ember-watson
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Watson = require('../index');
var watson = new Watson();

program
.version('0.0.1')
.version('0.0.2')

program
.command('upgrade-qunit-tests [testsPath]')
Expand All @@ -15,4 +15,12 @@ program
watson.transformQUnitTest(testsPath)
});

program
.command('convert-prototype-extensions [appPath]')
.description('Convert computed properties and observers to not use prototype extensions. appPath defaults to app/')
.action(function(appPath){
appPath = appPath || 'app';
watson.transformPrototypeExtensions(appPath)
});

program.parse(process.argv);
72 changes: 72 additions & 0 deletions lib/formulas/prototype-extension-transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var recast = require('recast');
var builders = recast.types.builders;

function transformExtension(path, type) {
var node = path.node;
var callback = node.callee.object;
var dependencies = node.arguments.slice();

var args = dependencies;
args.push(callback);

var wrappedCallback = builders.callExpression(builders.identifier(type), args);
path.replace(wrappedCallback);
}

function transformComputedProperty(path) {
transformExtension(path, 'Ember.computed');
}

function transformObserver(path) {
transformExtension(path, 'Ember.observer');
}

function isPrototypeExtension(node, name){
return node.callee.type === 'MemberExpression' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === name &&
node.callee.object.type === 'FunctionExpression';
}

function isComputedProperty(node) {
return isPrototypeExtension(node, 'property');
}

function isObserver(node) {
return isPrototypeExtension(node, 'observes');
}

module.exports = function transform(source) {
var sections = {
computedProperties: [],
observers: []
};

var ast = recast.parse(source);

recast.visit(ast, {
visitCallExpression: function(path) {
var node = path.node;

if (isComputedProperty(node)) {
sections.computedProperties.push(path);
}

if (isObserver(node)) {
sections.observers.push(path);
}

this.traverse(path);
}
});

sections.computedProperties.forEach(function(cp) {
transformComputedProperty(cp);
});

sections.observers.forEach(function(observer) {
transformObserver(observer);
});

return recast.print(ast, { tabWidth: 2, quote: 'single' }).code;
};
44 changes: 33 additions & 11 deletions lib/watson.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs');
var path = require('path');

var qunitTransforms = require('./formulas/qunit-transforms');
var prototypeExtensionTransforms = require('./formulas/prototype-extension-transforms');
var walkSync = require('walk-sync');

module.exports = EmberWatson;
Expand All @@ -11,28 +12,49 @@ function EmberWatson() { }

EmberWatson.prototype._transformQUnitTest = qunitTransforms;

EmberWatson.prototype.transformQUnitTest = function(testsPath) {
var paths = walkSync(testsPath);
EmberWatson.prototype.transformQUnitTest = function(path) {
var paths = walkSync(path);
var tests = [];
var wontFix = [];

paths.forEach(function(file) {
if (path.extname(file) === '.js' && file.indexOf('-test.js') > 0) {
tests.push(path.join(testsPath,file));
tests.push(path.join(path,file));
}
});

tests.forEach(function(testFile) {
var source = fs.readFileSync(testFile);
transform(tests, qunitTransforms);
};

EmberWatson.prototype._transformPrototypeExtensions = prototypeExtensionTransforms;


EmberWatson.prototype.transformPrototypeExtensions = function(path) {
var paths = walkSync(path);
var files = [];

paths.forEach(function(file) {
if (path.extname(file) === '.js') {
files.push(path.join(path,file));
}
});

transform(files, qunitTransforms);
};

function transform(files, transformFormula) {
var wontFix = [];

files.forEach(function(file) {
var source = fs.readFileSync(file);
try {
var newSource = this._transformQUnitTest(source);
var newSource = transformFormula(source);
if (source != newSource) {
console.log(chalk.green('Fixed: ', testFile));
console.log(chalk.green('Fixed: ', file));

fs.writeFileSync(testFile, newSource);
fs.writeFileSync(file, newSource);
}
} catch (err) {
wontFix.push(testFile);
wontFix.push(file);
}
}, this);

Expand All @@ -50,4 +72,4 @@ EmberWatson.prototype.transformQUnitTest = function(testsPath) {
}

return true;
};
}
31 changes: 31 additions & 0 deletions tests/fixtures/prototype-extension-files/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Ember from 'ember';

export default Ember.Controller.extend({
hasNoDependency: Ember.computed(function() {
return true;
}),

hasMultipleDependencies: Ember.computed('foo', 'bar', function() {
return this.getProperties('foo', 'bar');
}),

chainedCP: Ember.computed('foo', function() {
return false;
}).volatile().readOnly(),

isNotExtendingPrototype: Ember.computed(function() {
return true;
}),

isNotExtendingPrototypeWithDependencies: Ember.computed('foo', function() {
return this.get('foo');
}),

simpleObserver: Ember.observer('foo', function() {
this.set('bar', true);
}),

chainedObserver: Ember.observer('foo', 'bar', function(property) {
this.set('baz', true);
}).on('init')
});
31 changes: 31 additions & 0 deletions tests/fixtures/prototype-extension-files/old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Ember from 'ember';

export default Ember.Controller.extend({
hasNoDependency: function() {
return true;
}.property(),

hasMultipleDependencies: function() {
return this.getProperties('foo', 'bar');
}.property('foo', 'bar'),

chainedCP: function() {
return false;
}.property('foo').volatile().readOnly(),

isNotExtendingPrototype: Ember.computed(function() {
return true;
}),

isNotExtendingPrototypeWithDependencies: Ember.computed('foo', function() {
return this.get('foo');
}),

simpleObserver: function() {
this.set('bar', true);
}.observes('foo'),

chainedObserver: function(property) {
this.set('baz', true);
}.observes('foo', 'bar').on('init')
});
14 changes: 14 additions & 0 deletions tests/prototype-extension-migrator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var Watson = require('../index.js');
var fs = require('fs');
var astEquality = require('./helpers/ast-equality');
var recast = require('recast');

describe('computed properties and observers extending prototype', function() {
it('makes the correct transformations', function() {
var source = fs.readFileSync('./tests/fixtures/prototype-extension-files/old.js');
var watson = new Watson();
var newSource = watson._transformPrototypeExtensions(source);

astEquality(newSource, fs.readFileSync('./tests/fixtures/prototype-extension-files/new.js'));
});
});