Skip to content

Commit

Permalink
Fix regression with Babel 6
Browse files Browse the repository at this point in the history
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 rollup#155, fixes rollup#156.
  • Loading branch information
nicolo-ribaudo committed Aug 14, 2017
1 parent 3cc04d9 commit 38711a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/helperPlugin.js
Original file line number Diff line number Diff line change
@@ -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'));
}
}
};
Expand Down
23 changes: 20 additions & 3 deletions test/helperPlugin.js
Original file line number Diff line number Diff line change
@@ -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"));
}
}
};
Expand Down

0 comments on commit 38711a9

Please sign in to comment.