-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |