-
Notifications
You must be signed in to change notification settings - Fork 3
/
file-name-plugin.js
113 lines (95 loc) · 2.9 KB
/
file-name-plugin.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
/* eslint-env node */
const locationAttribute = 'l';
const blockHelpers = ['each', 'each-in', 'let', 'with', 'if', 'unless'];
const undasherizedComponents = ['input', 'component'];
module.exports = function ({ appOrAddonIndex, fileIndex, files }) {
return class Plugin {
constructor(env) {
if (env.moduleName) {
files[fileIndex] = env.moduleName;
this.fileHash = `${appOrAddonIndex}:${fileIndex}`;
fileIndex++;
}
}
transform(ast) {
let { syntax, fileHash } = this;
let { builders } = syntax;
let startLoc = ast.loc ? ast.loc.start : {};
if (startLoc.line !== 1 || startLoc.column !== 0) {
return ast;
}
let visitor = {
MustacheStatement: {
enter(node) {
let {
path: { type, original },
hash,
params,
} = node;
let isComponent =
type === 'PathExpression' &&
(undasherizedComponents.includes(original) ||
original.includes('-')) &&
(params.length > 0 || hash.pairs.length > 0);
if (!isComponent) {
return;
}
if (fileHash) {
let startLocation = node.loc.start;
let file = `${fileHash}:${startLocation.line}:${startLocation.column}`;
let attrNode = builders.pair(
locationAttribute,
builders.string(file)
);
hash.pairs.push(attrNode);
}
return node;
},
},
ElementNode: {
enter(node) {
if (node.tag.startsWith(':')) {
return node;
}
if (fileHash) {
let startLocation = node.loc.start;
let file = `${fileHash}:${startLocation.line}:${startLocation.column}`;
let attrNode = builders.attr(
locationAttribute,
builders.text(file)
);
node.attributes.push(attrNode);
}
return node;
},
},
BlockStatement: {
enter(node) {
let {
path: { type, original },
hash,
} = node;
let isComponent =
type === 'PathExpression' && !blockHelpers.includes(original);
if (!isComponent) {
return;
}
if (fileHash) {
let startLocation = node.loc.start;
let file = `${fileHash}:${startLocation.line}:${startLocation.column}`;
let attrNode = builders.pair(
locationAttribute,
builders.string(file)
);
hash.pairs.push(attrNode);
}
return node;
},
},
};
syntax.traverse(ast, visitor);
return ast;
}
};
};