-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for component data-test-* attributes without values
This adds support for `{{my-component data-test-foo}}`. Basically, this adds a handlebars transform that converts the previous example to `{{my-component data-test-foo=true}}`.
- Loading branch information
1 parent
fa8eaf2
commit fb17788
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-env node */ | ||
var 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) { | ||
var b = this.syntax.builders; | ||
var traverse = this.syntax.traverse; | ||
|
||
traverse(ast, { | ||
MustacheStatement: function(node) { | ||
var testSelectorParams = []; | ||
var otherParams = []; | ||
|
||
node.params.forEach(function(param) { | ||
if (isTestSelectorParam(param)) { | ||
testSelectorParams.push(param); | ||
} else { | ||
otherParams.push(param); | ||
}; | ||
}); | ||
|
||
node.params = otherParams; | ||
|
||
testSelectorParams.forEach(function(param) { | ||
var pair = b.pair(param.original, b.boolean(true)); | ||
node.hash.pairs.push(pair); | ||
}); | ||
} | ||
}); | ||
|
||
return ast; | ||
}; | ||
|
||
module.exports = TransformTestSelectorParamsToHashPairs; |