Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #463 from bem/issues/459@support/0.6.x
Browse files Browse the repository at this point in the history
deps: possibility to declare dependence without explicit including item
  • Loading branch information
arikon committed Oct 16, 2013
2 parents 70c2bd4 + 01b4fb9 commit be7ff3f
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 25 deletions.
23 changes: 15 additions & 8 deletions lib/techs/deps.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,15 @@ var Deps = exports.Deps = INHERIT({
add: function(target, depsType, item) {
var items = this.items,
targetKey = target.buildKey(),
itemKey = item.buildKey();
itemKey = item.buildKey(),
itemByKey = items[itemKey];

if(!items[itemKey]) {
if(!itemByKey) {
items[itemKey] = item;
item.include && this.itemsByOrder.push(itemKey);
} else if(!itemByKey.include && item.include) {
// update `include` for existing item in case of adding of new item without `include: false`
itemByKey.include = true;
this.itemsByOrder.push(itemKey);
}

Expand Down Expand Up @@ -300,11 +305,13 @@ var Deps = exports.Deps = INHERIT({
var key = i.buildKey(),
ctxTech = ctx && ctx.item.tech || '';
if(!uniq.hasOwnProperty(key) || !uniq[key].hasOwnProperty(ctxTech)) {
(uniq[key] || (uniq[key] = {}))[ctxTech] = true;
var newCtx = ctx || i;
_this.forEach(fn, uniq, i.mustDeps, newCtx);
fn.call(_this, i, newCtx);
_this.forEach(fn, uniq, i.shouldDeps, newCtx);
if(i.include) { // include only items without `include: false`
(uniq[key] || (uniq[key] = {}))[ctxTech] = true;
var newCtx = ctx || i;
_this.forEach(fn, uniq, i.mustDeps, newCtx);
fn.call(_this, i, newCtx);
_this.forEach(fn, uniq, i.shouldDeps, newCtx);
}
}
}
});
Expand Down Expand Up @@ -364,7 +371,7 @@ var DepsItem = exports.DepsItem = INHERIT({
this.item = {};
this.extendByCtx({ item: item });
this.extendByCtx(ctx);

this.include = item.include !== false;
},

extendByCtx: function(ctx) {
Expand Down
22 changes: 15 additions & 7 deletions lib/techs/v2/deps.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,15 @@ var Deps = exports.Deps = INHERIT({
add: function(target, depsType, item) {
var items = this.items,
targetKey = target.buildKey(),
itemKey = item.buildKey();
itemKey = item.buildKey(),
itemByKey = items[itemKey];

if(!items[itemKey]) {
if(!itemByKey) {
items[itemKey] = item;
item.include && this.itemsByOrder.push(itemKey);
} else if(!itemByKey.include && item.include) {
// update `include` for existing item in case of adding of new item without `include: false`
itemByKey.include = true;
this.itemsByOrder.push(itemKey);
}

Expand Down Expand Up @@ -414,11 +419,13 @@ var Deps = exports.Deps = INHERIT({
var key = i.buildKey(),
ctxTech = ctx && ctx.item.tech || '';
if(!uniq.hasOwnProperty(key) || !uniq[key].hasOwnProperty(ctxTech)) {
(uniq[key] || (uniq[key] = {}))[ctxTech] = true;
var newCtx = ctx || i;
_this.forEach(fn, uniq, i.mustDeps, newCtx);
fn.call(_this, i, newCtx);
_this.forEach(fn, uniq, i.shouldDeps, newCtx);
if(i.include) { // include only items without `include: false`
(uniq[key] || (uniq[key] = {}))[ctxTech] = true;
var newCtx = ctx || i;
_this.forEach(fn, uniq, i.mustDeps, newCtx);
fn.call(_this, i, newCtx);
_this.forEach(fn, uniq, i.shouldDeps, newCtx);
}
}
}
});
Expand Down Expand Up @@ -478,6 +485,7 @@ var DepsItem = exports.DepsItem = INHERIT({
this.item = {};
this.extendByCtx({ item: item });
this.extendByCtx(ctx);
this.include = item.include !== false;
},

extendByCtx: function(ctx) {
Expand Down
55 changes: 45 additions & 10 deletions test/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ describe('Deps', function() {

});

describe('include: false', function() {

it('block', assertDepsParse(
[
{
block: 'b1',
shouldDeps: [
{ block: 'b2', include: false },
{ block: 'b3', include: false }
],
mustDeps: [
{ block: 'b4', include: false },
{ block: 'b5', include: false }
]
},
{ block: 'b3' },
{ block: 'b4' }
],
{ '': { '': [ { block: 'b4' }, { block: 'b1' }, { block: 'b3' } ] } }
));

});

});

describe('serialize:', function() {
Expand All @@ -205,9 +228,13 @@ describe('Deps', function() {

describe('clone', function() {

var deps1 = new Deps().parse([{ block: 'b1', bla: 1 }, 'b2']),
deps2 = deps1.clone(),
var deps1, deps2, deps;

beforeEach(function(){
deps1 = new Deps().parse([{ block: 'b1', bla: 1 }, 'b2']);
deps2 = deps1.clone();
deps = [deps1, deps2];
});

it('.items', function() {
assert.deepEqual(deps[1].items, deps[0].items);
Expand All @@ -221,24 +248,28 @@ describe('Deps', function() {

describe('subtract', function() {

var deps1 = new DEPS.Deps().parse([
var deps1, deps2, deps3;

beforeEach(function() {
deps1 = new DEPS.Deps().parse([
{ block: 'b1' },
{ block: 'b2' },
{ block: 'b3' },
{ block: 'b5' }
]),
]);

deps2 = new DEPS.Deps().parse([
{ block: 'b1' },
{ block: 'b3' },
{ block: 'b4' }
]),
]);

deps3 = new DEPS.Deps().parse([
{ block: 'b5' }
]);

deps1.subtract(deps2).subtract(deps3);
deps1.subtract(deps2).subtract(deps3);
});

it('works correctly', function() {
assert.deepEqual(deps1.serialize(), {
Expand All @@ -252,26 +283,30 @@ describe('Deps', function() {

describe('intersect', function() {

var deps1 = new DEPS.Deps().parse([
var deps1, deps2, deps3;

beforeEach(function() {
deps1 = new DEPS.Deps().parse([
{ block: 'b1' },
{ block: 'b2' },
{ block: 'b3' },
{ block: 'b5' }
]),
]);

deps2 = new DEPS.Deps().parse([
{ block: 'b3' },
{ block: 'b1' },
{ block: 'b4' }
]),
]);

deps3 = new DEPS.Deps().parse([
{ block: 'b3' },
{ block: 'b6' },
{ block: 'b1' }
]);

deps1.intersect(deps2).intersect(deps3);
deps1.intersect(deps2).intersect(deps3);
});

it('works correctly', function() {
assert.deepEqual(deps1.serialize(), {
Expand Down
Loading

0 comments on commit be7ff3f

Please sign in to comment.