Skip to content

Commit

Permalink
Fix regression with Babel 6 (#158)
Browse files Browse the repository at this point in the history
* 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.

* Run tests using both Babel 7 and Babel 6
  • Loading branch information
nicolo-ribaudo authored and Andarist committed Apr 11, 2020
1 parent 993ff36 commit 31ed861
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
16 changes: 16 additions & 0 deletions packages/rollup-plugin-babel/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 19 additions & 2 deletions packages/rollup-plugin-babel/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 packages/rollup-plugin-babel/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 31ed861

Please sign in to comment.