-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for component data-test-* attributes without values #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-env node */ | ||
var TEST_SELECTOR_PREFIX = /data-test-.*/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have such a regex already in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the problem is that one uses ES6 modules and the other CommonJS, so reducing duplication there might not be quite as easy |
||
|
||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we import this from elsewhere so we don't have to define it at several locations? We also might want to make it configurable in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, but I'd like to do that in a separate PR since we already have quite a bit of duplication around that regex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍