Skip to content
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 #55

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ module.exports = {
plugin: StripTestSelectorsTransform,
baseDir: function() { return __dirname; }
});
} else {
var TransformTestSelectorParamsToHashPairs = require('./transform-test-selector-params-to-hash-pairs');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think? this is where this should be but I'm definitely not sure.


registry.add('htmlbars-ast-plugin', {
name: 'transform-test-selector-params-to-hash-pairs',
plugin: TransformTestSelectorParamsToHashPairs,
baseDir: function() { return __dirname; }
});
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ if (!config.stripTestSelectors) {
assert.equal(find('.test6').find('div[data-non-test]').length, 0, 'data-non-test does not exists');
});

}
test('it binds data-test-* attributes without values on components', function (assert) {
assert.equal(find('.test7').find('div[data-test-without-value]').length, 1, 'data-test-without-value exists');
});

}
2 changes: 2 additions & 0 deletions tests/dummy/app/templates/bind-test.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
<div class="test5">{{data-test-component data-test="foo"}}</div>

<div class="test6">{{data-test-component data-non-test="foo"}}</div>

<div class="test7">{{data-test-component data-test-without-value}}</div>
42 changes: 42 additions & 0 deletions transform-test-selector-params-to-hash-pairs.js
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;