From 99a7530c73d062d57db152857e6c586b3b2821f7 Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Mon, 30 Mar 2020 12:57:12 +0100 Subject: [PATCH 1/2] Add tests for arrow functions in AMD wrappers --- test/fixtures/amd.after.js | 32 ++++++++++++++++++++++++++++- test/fixtures/amd.before.js | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/test/fixtures/amd.after.js b/test/fixtures/amd.after.js index 54294ad..be40c33 100644 --- a/test/fixtures/amd.after.js +++ b/test/fixtures/amd.after.js @@ -28,4 +28,34 @@ import moduleA from 'a'; import moduleB from 'b'; import moduleC from 'c'; import 'd'; -console.log('xyz'); \ No newline at end of file +console.log('xyz'); +console.log('c'); +export default {}; +export default {}; +console.log('last statement'); +console.log('d'); +import 'd'; +console.log('dont bother with d'); +import d from 'd'; +console.log('I need you D', d.isUsed()); +export default d.e; + +export default { + e: '123' +}; + +import 'e'; + +export default { + e: 'e is ignored' +}; + +import e from 'e'; + +export default { + e: `e is ${e.isUsed()}` +}; + +export default { + f: '123' +}; \ No newline at end of file diff --git a/test/fixtures/amd.before.js b/test/fixtures/amd.before.js index 2a10966..18d624c 100644 --- a/test/fixtures/amd.before.js +++ b/test/fixtures/amd.before.js @@ -41,3 +41,44 @@ define(['a', 'b', 'c', 'd'], function(moduleA, moduleB, moduleC) { console.log('xyz'); }); +define(() => { + console.log('c'); +}); + +define(() => { + return {}; +}); + +define(() => { + return {}; + console.log('last statement'); +}); + +define([], () => { + console.log('d'); +}); + +define(['d'], () => { + console.log('dont bother with d'); +}); + +define(['d'], (d) => { + console.log('I need you D', d.isUsed()); + return d.e; +}); + +define([], () => ({ + e: '123' +})); + +define(['e'], () => ({ + e: 'e is ignored' +})); + +define(['e'], (e) => ({ + e: `e is ${e.isUsed()}` +})); + +define(() => ({ + f: '123' +})); From ebad5bc647f9fab373b2253530a9a2a034953ceb Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Mon, 30 Mar 2020 12:57:25 +0100 Subject: [PATCH 2/2] Handle arrow functions in AMD wrappers --- transforms/amd.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/transforms/amd.js b/transforms/amd.js index fe15553..49013fa 100644 --- a/transforms/amd.js +++ b/transforms/amd.js @@ -9,6 +9,16 @@ module.exports = function(file, api, options) { var root = j(file.source); var leadingComment = root.find(j.Program).get('body', 0).node.leadingComments; + /** + * Get factory function whether arrow expression or block statement + * @param factory - Function body AST + */ + function getFactoryBody(factory) { + return factory.body.type === 'BlockStatement' + ? factory.body.body + : [j.returnStatement(factory.body)] + } + /** * Convert an `return` to `export default`. * @param body - Function body AST (Array) @@ -39,7 +49,7 @@ module.exports = function(file, api, options) { if (p.value.arguments.length === 1) { // convert `return` statement to `export default` - body = returnToExport(p.value.arguments[0].body.body); + body = returnToExport(getFactoryBody(p.value.arguments[0])); return j(p.parent).replaceWith(body); } @@ -55,7 +65,7 @@ module.exports = function(file, api, options) { }); // add the body after the import statements - Array.prototype.push.apply(importStatements, p.value.arguments[1].body.body); + Array.prototype.push.apply(importStatements, getFactoryBody(p.value.arguments[1])); // add any comments at the top importStatements[0].comments = comments;