-
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.
Merge pull request #57 from Turbo87/add-support-for-component-test-se…
…lectors Add support for component data-test-* attributes without values
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
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 walker = new this.syntax.Walker(); | ||
|
||
walker.visit(ast, function(node) { | ||
if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { | ||
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; |