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 a808b7a
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/formulas/destroy-app-transform.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
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 addDestroyAppImport = false;

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'),
[]
));
}

this.traverse(path);
},
visitImportDeclaration: function(path) {
// TODO
this.traverse(path);
}
});

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

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

0 comments on commit a808b7a

Please sign in to comment.