Skip to content

Commit 0e3e56c

Browse files
committed
Extract helper-fixing codemod to a separate file
1 parent 079e447 commit 0e3e56c

3 files changed

Lines changed: 48 additions & 22 deletions

File tree

lib/models/file-info.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var isTypeInSingleTypeCollection = require('../utils/is-type-in-single-type-coll
77
var defaultTypeForCollection = require('../utils/default-type-for-collection');
88
var calculateCollectionInfo = require('../utils/calculate-collection-info');
99
var importDeclarationsTransform = require('../transforms/import-declarations');
10+
var renameImportHelperTransform = require('../transforms/rename-helper-import');
1011
var inflection = require('inflection');
1112
var PodsSupport = require('../utils/pods-support');
1213
var typeForPodFile = PodsSupport.typeForPodFile;
@@ -176,6 +177,14 @@ var FileInfo = CoreObject.extend({
176177
},
177178
{ jscodeshift });
178179

180+
// Fixes "import { helper } from '@ember/component/helper'"
181+
// to "import { helper as buildHelper } from '@ember/component/helper'"
182+
newContents = renameImportHelperTransform(
183+
{
184+
source: newContents
185+
},
186+
{ jscodeshift });
187+
179188
var fullPath = path.join(this.projectRoot, this.sourceRelativePath);
180189

181190
fse.writeFileSync(fullPath, newContents, { encoding: 'utf-8' });

lib/transforms/import-declarations.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,6 @@ function transformer(file, api) {
55

66
var root = j(file.source);
77

8-
var foundHelper;
9-
10-
root.find(j.ImportDeclaration, {
11-
source: { value: '@ember/component/helper' },
12-
}).filter(path => {
13-
return path.value.specifiers[0].local.name === 'helper';
14-
})
15-
.forEach(function(path) {
16-
foundHelper = true;
17-
var specifier = j.importSpecifier(j.identifier('helper'), j.identifier('buildHelper'));
18-
19-
path.value.specifiers[0] = specifier;
20-
});
21-
22-
if (foundHelper) {
23-
root.find(j.CallExpression, {
24-
callee: { name: 'helper' }
25-
}).forEach(path => {
26-
path.value.callee.name = 'buildHelper';
27-
});
28-
}
29-
308
root.find(j.ImportDeclaration)
319
.find(j.Literal)
3210
.forEach(function(path) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function transformer(file, api) {
2+
var j = api.jscodeshift;
3+
4+
var foundHelper = false;
5+
6+
var root = j(file.source);
7+
8+
// Replaces:
9+
// import { helper } from '@ember/component/helper';
10+
// With:
11+
// import { helper as buildHelper } from '@ember/component/helper';
12+
root.find(j.ImportDeclaration, {
13+
source: { value: '@ember/component/helper' },
14+
}).filter(path => {
15+
return path.value.specifiers[0].local.name === 'helper';
16+
})
17+
.forEach(function(path) {
18+
foundHelper = true;
19+
var specifier = j.importSpecifier(
20+
j.identifier('helper'),
21+
j.identifier('buildHelper')
22+
);
23+
24+
path.value.specifiers[0] = specifier;
25+
});
26+
27+
// Replace helper(...) with buildHelper(...)
28+
if (foundHelper) {
29+
root.find(j.CallExpression, {
30+
callee: { name: 'helper' }
31+
}).forEach(path => {
32+
path.value.callee.name = 'buildHelper';
33+
});
34+
}
35+
36+
return root.toSource();
37+
}
38+
39+
module.exports = transformer;

0 commit comments

Comments
 (0)