From 38711a9d0792eeaf7d5874e9d30c0d007a468e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 14 Aug 2017 14:06:14 +0200 Subject: [PATCH 1/2] Fix regression with Babel 6 Since in Babel 6 it is not supported to replace the argument of a default export declaration with an expression, use a custom "replaceWith" function which handles also that case. Fixes #155, fixes #156. --- src/helperPlugin.js | 21 +++++++++++++++++++-- test/helperPlugin.js | 23 ++++++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/helperPlugin.js b/src/helperPlugin.js index 6c367cd..481cab7 100644 --- a/src/helperPlugin.js +++ b/src/helperPlugin.js @@ -1,8 +1,25 @@ -export default function importHelperPlugin () { +export default function importHelperPlugin ({ types: t }) { + /** + * This function is needed because of a bug in Babel 6.x, which prevents the + * declaration of an ExportDefaultDeclaration to be replaced with an + * expression. + * That bug has been fixed in Babel 7. + */ + function replaceWith (path, replacement) { + if ( + path.parentPath.isExportDefaultDeclaration() && + t.isExpression(replacement) + ) { + path.parentPath.replaceWith(t.exportDefaultDeclaration(replacement)); + } else { + path.replaceWith(replacement); + } + } + return { visitor: { ClassDeclaration (path, state) { - path.replaceWith(state.file.addHelper('classCallCheck')); + replaceWith(path, state.file.addHelper('classCallCheck')); } } }; diff --git a/test/helperPlugin.js b/test/helperPlugin.js index 4fd937b..95cb7c6 100644 --- a/test/helperPlugin.js +++ b/test/helperPlugin.js @@ -1,8 +1,25 @@ -module.exports = function importHelperPlugin () { +module.exports = function importHelperPlugin({ types: t }) { + /** + * This function is needed because of a bug in Babel 6.x, which prevents the + * declaration of an ExportDefaultDeclaration to be replaced with an + * expression. + * That bug has been fixed in Babel 7. + */ + function replaceWith(path, replacement) { + if ( + path.parentPath.isExportDefaultDeclaration() && + t.isExpression(replacement) + ) { + path.parentPath.replaceWith(t.exportDefaultDeclaration(replacement)); + } else { + path.replaceWith(replacement); + } + } + return { visitor: { - ClassDeclaration (path, state) { - path.replaceWith(state.file.addHelper('classCallCheck')); + ClassDeclaration(path, state) { + replaceWith(path, state.file.addHelper("classCallCheck")); } } }; From bc5a4052f094b949ed64fb91b2a38c3fa9679362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 14 Aug 2017 14:16:27 +0200 Subject: [PATCH 2/2] Run tests using both Babel 7 and Babel 6 --- appveyor.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index 7c98234..381a48f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -22,6 +22,22 @@ build: off test_script: - node --version && npm --version + - echo -- BABEL 7 -- + - npm test + - echo Replacing Babel 7 with Babel 6 + - npm remove + babel-core + babel-plugin-external-helpers + babel-plugin-transform-decorators + babel-plugin-transform-runtime + babel-preset-es2015 + - npm install + babel-core@6 + babel-plugin-external-helpers@6 + babel-plugin-transform-decorators@6 + babel-plugin-transform-runtime@6 + babel-preset-es2015@6 + - echo -- BABEL 6 -- - npm test matrix: