Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with helpers where we re-export the default as a named export #69

Merged
merged 2 commits into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 27 additions & 4 deletions lib/transforms/import-declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,31 @@ var path_utils = require('../utils/path');
function transformer(file, api) {
var j = api.jscodeshift;

return j(file.source)
.find(j.ImportDeclaration)
let root = j(file.source);

let foundHelper;

root.find(j.ImportDeclaration, {
source: { value: '@ember/component/helper' },
}).filter(path => {
return path.value.specifiers[0].local.name === 'helper';
})
.forEach(function(path) {
foundHelper = true;
let specifier = j.importSpecifier(j.identifier('helper'), j.identifier('buildHelper'));

path.value.specifiers[0] = specifier;
});

if (foundHelper) {
root.find(j.CallExpression, {
callee: { name: 'helper' }
}).forEach(path => {
path.value.callee.name = 'buildHelper';
});
}

root.find(j.ImportDeclaration)
.find(j.Literal)
.forEach(function(path) {
var importPath = path.value.value + '.js';
Expand Down Expand Up @@ -55,9 +78,9 @@ function transformer(file, api) {
// remove extension
newImportPath = newImportPath.slice(0, -targetFileInfo.ext.length);
j(path).replaceWith(j.literal(newImportPath));
})
.toSource();
});

return root.toSource();
}

module.exports = transformer;
4 changes: 2 additions & 2 deletions test/fixtures/classic-named-exports/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
'app.js': 'export default App',
'router.js': 'export default Router',
helpers: {
'titleize.js': 'export default helper(function() { });',
'capitalize.js': 'export default helper(function() { });'
'titleize.js': 'import { helper } from \'@ember/component/helper\';\nexport default helper(function() { });',
'capitalize.js': 'import { helper } from \'@ember/component/helper\';\nexport default helper(function() { });'
}
},

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/classic-named-exports/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module.exports = {
'router.js': 'export default Router',
ui: {
components: {
'titleize.js': 'export const helper = helper(function() { });',
'titleize.js': 'import { helper as buildHelper } from \'@ember/component/helper\';\nexport const helper = buildHelper(function() { });',
'capitalize': {
'helper.js': 'export default helper(function() { });',
'helper.js': 'import { helper as buildHelper } from \'@ember/component/helper\';\nexport default buildHelper(function() { });',
'helper-integration-test.js': '"capitalize helper test"'
}
}
Expand Down