Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Fix regression with Babel 6 #158

Merged
merged 2 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions 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 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