forked from mainmatter/ember-test-selectors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform-test-selector-params-to-hash-pairs.js
49 lines (37 loc) · 1.14 KB
/
transform-test-selector-params-to-hash-pairs.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
'use strict';
/* eslint-env node */
let TEST_SELECTOR_PREFIX = /data-test-.*/;
function TransformTestSelectorParamsToHashPairs() {
this.syntax = null;
}
function isTestSelectorParam(param) {
return param.type === 'PathExpression'
&& TEST_SELECTOR_PREFIX.test(param.original);
}
TransformTestSelectorParamsToHashPairs.prototype.transform = function(ast) {
let b = this.syntax.builders;
let walker = new this.syntax.Walker();
walker.visit(ast, function(node) {
if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') {
if ('sexpr' in node) {
node = node.sexpr;
}
let testSelectorParams = [];
let otherParams = [];
node.params.forEach(function(param) {
if (isTestSelectorParam(param)) {
testSelectorParams.push(param);
} else {
otherParams.push(param);
}
});
node.params = otherParams;
testSelectorParams.forEach(function(param) {
let pair = b.pair(param.original, b.boolean(true));
node.hash.pairs.push(pair);
});
}
});
return ast;
};
module.exports = TransformTestSelectorParamsToHashPairs;