From cfb712ca4c1551385af12f145788cbc2d474d1a7 Mon Sep 17 00:00:00 2001 From: Raido Kuli Date: Fri, 7 Apr 2017 14:50:00 +0300 Subject: [PATCH] fix: strip data-test-* attributes without explicit value from production build (#88) * Components with positional params failure if not full attribute definition * Filter MustacheStatement and BlockStatement params for data-test-* with no value * tests: Fix assertion message * strip-test-selectors: Replace isNotTestSelector() with isTestSelector() --- strip-test-selectors.js | 12 ++++++++-- .../app/components/print-test-attributes.js | 11 +++++++++ .../components/print-test-attributes.hbs | 1 + ...ta-test-attributes-from-components-test.js | 24 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tests/dummy/app/components/print-test-attributes.js diff --git a/strip-test-selectors.js b/strip-test-selectors.js index cb00248d..28e94ec4 100644 --- a/strip-test-selectors.js +++ b/strip-test-selectors.js @@ -4,6 +4,10 @@ let TEST_SELECTOR_PREFIX = /data-test-.*/; +function isTestSelector(attribute) { + return TEST_SELECTOR_PREFIX.test(attribute); +} + function StripTestSelectorsTransform() { this.syntax = null; } @@ -14,11 +18,15 @@ StripTestSelectorsTransform.prototype.transform = function(ast) { walker.visit(ast, function(node) { if (node.type === 'ElementNode') { node.attributes = node.attributes.filter(function(attribute) { - return !TEST_SELECTOR_PREFIX.test(attribute.name); + return !isTestSelector(attribute.name); }); } else if (node.type === 'MustacheStatement' || node.type === 'BlockStatement') { + node.params = node.params.filter(function(param) { + return !isTestSelector(param.original); + }); + node.hash.pairs = node.hash.pairs.filter(function(pair) { - return !TEST_SELECTOR_PREFIX.test(pair.key); + return !isTestSelector(pair.key); }); } }); diff --git a/tests/dummy/app/components/print-test-attributes.js b/tests/dummy/app/components/print-test-attributes.js new file mode 100644 index 00000000..0e5bb2f1 --- /dev/null +++ b/tests/dummy/app/components/print-test-attributes.js @@ -0,0 +1,11 @@ +import Ember from 'ember'; + +const { Component } = Ember; + +const component = Component.extend(); + +component.reopenClass({ + positionalParams: 'params' +}); + +export default component; diff --git a/tests/dummy/app/templates/components/print-test-attributes.hbs b/tests/dummy/app/templates/components/print-test-attributes.hbs index 990e1941..60bf5127 100644 --- a/tests/dummy/app/templates/components/print-test-attributes.hbs +++ b/tests/dummy/app/templates/components/print-test-attributes.hbs @@ -2,3 +2,4 @@
{{data-test-second}}
{{data-non-test}}
{{data-test}}
+
{{params.length}}
\ No newline at end of file diff --git a/tests/integration/strip-data-test-attributes-from-components-test.js b/tests/integration/strip-data-test-attributes-from-components-test.js index 43e59e90..aed650b3 100644 --- a/tests/integration/strip-data-test-attributes-from-components-test.js +++ b/tests/integration/strip-data-test-attributes-from-components-test.js @@ -9,6 +9,30 @@ moduleForComponent('print-test-attributes', 'StripTestSelectorsTransform plugin' if (config.stripTestSelectors) { + test('it strips data-test-* attributes from components with single positional params', function(assert) { + this.render(hbs`{{print-test-attributes data-test-should-not-be}}`); + + assert.equal(this.$('.data-test-positional-params').text(), 0, 'there should be no params'); + }); + + test('it strips data-test-* attributes from components with positional params data-test-* as first param', function(assert) { + this.render(hbs`{{print-test-attributes data-test-should-not-be "param1"}}`); + + assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param'); + }); + + test('it strips data-test-* attributes from components with multiple positional params', function(assert) { + this.render(hbs`{{print-test-attributes "param1" data-test-should-not-be}}`); + + assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param'); + }); + + test('it strips data-test-* attributes from components with block and multiple positional params', function(assert) { + this.render(hbs`{{#print-test-attributes "param1" data-test-should-not-be}}{{/print-test-attributes}}`); + + assert.equal(this.$('.data-test-positional-params').text(), 1, 'there should be only one param'); + }); + test('it strips data-test-* attributes from components', function(assert) { this.render(hbs`{{print-test-attributes data-test-first="foobar"}}`);