Skip to content

Commit

Permalink
WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
blimmer committed Sep 21, 2015
1 parent 7629d34 commit 0e74919
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions lib/formulas/destroy-app-transform.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,62 @@
var parseAst = require('../helpers/parse-ast');
var recast = require('recast');
var types = recast.types.namedTypes;
var builders = recast.types.builders;
var isImportFor = require('./helpers/is-import-for');
var parseAst = require('../helpers/parse-ast');
var recast = require('recast');
var types = recast.types.namedTypes;
var builders = recast.types.builders;

function isDestroyAppCall(node) {
var addDefaultImport = require('./helpers/add-default-import');

function isLegacyDestroy(node) {
return types.MemberExpression.check(node.callee) &&
node.callee.object.name === 'Ember' &&
node.callee.property.name === 'run' &&
node.arguments[1].value === 'destroy';
}

function insertDestroyApp(path) {
// TODO: I need to figure out how to properly insert this
// built statement *after* the one I just found (path).
var destroyApp = builders.callExpression(
builders.identifier('destroyApp'),
[]
);
path.insertAfter(destroyApp);
}

module.exports = function transform(source) {
var ast = parseAst(source);
var removeEmberImport = true;
var addDestroyAppImport = false;

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

if(isDestroyAppCall(node)) {
console.dir(path);
insertDestroyApp(path);
var isOldDestroy = isLegacyDestroy(node);
if(isOldDestroy) {
if (addDestroyAppImport !== true) {
addDestroyAppImport = true;
}

path.replace(builders.callExpression(
builders.identifier('destroyApp'),
[]
));
} else if (isEmberCall(path)) {
removeEmberImport = false;
}

this.traverse(path);
},
visitImportDeclaration: function(path) {
// TODO
if (isImportFor('ember', path.node)) {
emberImport = path;
}

this.traverse(path);
}
});

if (addDestroyAppImport) {
addDefaultImport(ast, 'tests/helpers/destroy-app', 'destroyApp');
}

console.log('should remove ember? ' + removeEmberImport);
console.log('ember import? ' + emberImport);
if (removeEmberImport && emberImport) {
emberImport.prune();
}

return recast.print(ast, { tabWidth: 2, quote: 'single' }).code;
};

0 comments on commit 0e74919

Please sign in to comment.