Skip to content

Commit

Permalink
Simplify AST walker and remove "lodash" dependency (#37)
Browse files Browse the repository at this point in the history
* strip-test-selectors: Simplify AST walker

* Replace "lodash" dependency with indexOf() !== -1
  • Loading branch information
Turbo87 authored and marcoow committed Jan 9, 2017
1 parent 836d00b commit 23597d2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* jshint node:true */
'use strict';

var _includes = require('lodash/includes');

module.exports = {
name: 'test-selectors',

Expand All @@ -11,7 +9,7 @@ module.exports = {
var addonOptions = appOptions['ember-test-selectors'] || {};
var environments = addonOptions.environments || ['production'];

if (_includes(environments, registry.app.env)) {
if (environments.indexOf(registry.app.env) !== -1) {
var StripTestSelectorsTransform = require('./strip-test-selectors');

registry.add('htmlbars-ast-plugin', {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"test:strip": "STRIP_TEST_SELECTORS=true ember test"
},
"dependencies": {
"ember-cli-babel": "^5.1.7",
"lodash": "^4.0.0"
"ember-cli-babel": "^5.1.7"
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
Expand Down
15 changes: 2 additions & 13 deletions strip-test-selectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*jshint node:true*/
var _ = require('lodash/lodash');

var TEST_SELECTOR_PREFIX = /data-test-.*/;

function StripTestSelectorsTransform() {
Expand All @@ -12,18 +10,9 @@ StripTestSelectorsTransform.prototype.transform = function(ast) {

walker.visit(ast, function(node) {
if (node.type === 'ElementNode') {
var attributesToDelete = [];
node.attributes.forEach(function(attribute) {
if (TEST_SELECTOR_PREFIX.test(attribute.name)) {
attributesToDelete.push(attribute.name);
}
node.attributes = node.attributes.filter(function(attribute) {
return !TEST_SELECTOR_PREFIX.test(attribute.name);
});

if (attributesToDelete.length > 0) {
_.remove(node.attributes, function(attribute) {
return _.includes(attributesToDelete, attribute.name);
});
}
}
});

Expand Down

0 comments on commit 23597d2

Please sign in to comment.