diff --git a/lib/ember-migrator.js b/lib/ember-migrator.js index 3452426..182c940 100644 --- a/lib/ember-migrator.js +++ b/lib/ember-migrator.js @@ -211,7 +211,8 @@ EmberMigrator.prototype.splitFile = function(filePath) { var astBody = ast.program.body; // Cache of ast nodes that are not directly exported so need to be appended // at the end of the splitting process - var nonExportNodes = []; + var nonExportNodesPrefix = []; + var nonExportNodesSuffix = []; // Keep track of exports we have split this file into var typedExports = []; // For some reason the first nodes leading comments are separated from the node @@ -331,13 +332,25 @@ EmberMigrator.prototype.splitFile = function(filePath) { // Any other code that is not a global class assignment on root app will // be remembered and at the end of splitting we will append these to the // first typedExport or create a new typed export if none was created - nonExportNodes.push(node); + if (typedExports.length === 0) { + nonExportNodesPrefix.push(node); + } else { + nonExportNodesSuffix.push(node); + } } }, this); - // Append any remaining non-export nodes to either first export or new export - nonExportNodes.forEach(function (node) { + // Append any nonExportNodes[Suffix/Prefix] non-export nodes to either first export or new export. + nonExportNodesPrefix.reverse().forEach(function (node) { + if (typedExports.length === 0) { + var newFilePath = helper.dasherizePath(filePath); + addTypedNode(node, newFilePath); + } else { + typedExports[0].astNodes.unshift(node); + } + }); + nonExportNodesSuffix.forEach(function (node) { if (typedExports.length === 0) { var newFilePath = string.dasherize(filePath); addTypedNode(node, newFilePath); @@ -387,7 +400,16 @@ EmberMigrator.prototype.convertFile = function(typedExport){ code = code.replace(/\n;\n/, '\n'); code = code.replace(new RegExp(this.rootAppName + "\\.", 'g'), ''); + + // Ensure 'use strict' is on top of file. code = code.replace(/Em\./g, 'Ember.'); + var isStrictRegularExpression = /\n'use strict';\n/g; + var hasIsStrict = code.match(isStrictRegularExpression, ''); + if (hasIsStrict) { + code = code.replace(isStrictRegularExpression, '\n'); + code = '\'use strict\';\n\n' + code; + } + // For any module imported that used to be a window global Object.keys(this.classesFilepathMap).forEach(function(name) { var module = this.classesFilepathMap[name];