Repository navigation
Expand file tree
/
Copy pathbabel-plugin.js
More file actions
60 lines (56 loc) · 1.82 KB
/
Copy pathbabel-plugin.js
File metadata and controls
60 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { basename, extname } = require('path');
module.exports = function addTOCNames({ types: t }) {
return {
name: 'add-template-only-names-for-inspector',
visitor: {
CallExpression(path, state) {
let calleePath = path.get('callee');
if (!calleePath.isIdentifier()) {
return;
}
if (
calleePath.referencesImport(
'@ember/component/template-only',
'default',
)
) {
let params = path.node.arguments;
let assignment = path.parentPath.parentPath.node;
let filename = state.filename
.replace(state.opts.root, '')
.replace(/\\/g, '/')
.replace(/template.hbs$/, '')
.replace(/component\.(js|ts)$/, '')
.replace(/index\.(js|ts)$/, '')
.replace(/\/$/, '');
let rootName = basename(filename).slice(
0,
-extname(filename).length || undefined, // undefined -> same as slice(0)
);
let assignmentName = t.identifier('undefined');
if (
assignment.type === 'AssignmentExpression' &&
assignment.left.type === 'Identifier'
) {
assignmentName = t.stringLiteral(
rootName + ':' + assignment.left.name,
);
}
if (
assignment.type === 'VariableDeclarator' &&
assignment.id.type === 'Identifier'
) {
assignmentName = t.stringLiteral(
rootName + ':' + assignment.id.name,
);
}
if (assignment.type === 'ExportDefaultDeclaration') {
assignmentName = t.stringLiteral(rootName);
}
params.length = 0;
params.push(t.identifier('undefined'), assignmentName);
}
},
},
};
};