Skip to content

Commit ed7f3b7

Browse files
willmendesnetoTheLarkInn
authored andcommitted
refactor(DependenciesBlockVariable): upgrade to ES6 (webpack#3772)
1 parent 4729286 commit ed7f3b7

2 files changed

Lines changed: 135 additions & 32 deletions

File tree

‎lib/DependenciesBlockVariable.js‎

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,45 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var ReplaceSource = require("webpack-sources").ReplaceSource;
6-
var RawSource = require("webpack-sources").RawSource;
5+
"use strict";
76

8-
function DependenciesBlockVariable(name, expression, dependencies) {
9-
this.name = name;
10-
this.expression = expression;
11-
this.dependencies = dependencies || [];
12-
}
13-
module.exports = DependenciesBlockVariable;
7+
const ReplaceSource = require("webpack-sources").ReplaceSource;
8+
const RawSource = require("webpack-sources").RawSource;
9+
10+
class DependenciesBlockVariable {
11+
constructor(name, expression, dependencies) {
12+
this.name = name;
13+
this.expression = expression;
14+
this.dependencies = dependencies || [];
15+
}
1416

15-
DependenciesBlockVariable.prototype.updateHash = function(hash) {
16-
hash.update(this.name);
17-
hash.update(this.expression);
18-
this.dependencies.forEach(function(d) {
19-
d.updateHash(hash);
20-
});
21-
};
17+
updateHash(hash) {
18+
hash.update(this.name);
19+
hash.update(this.expression);
20+
this.dependencies.forEach(d => {
21+
d.updateHash(hash);
22+
});
23+
}
2224

23-
DependenciesBlockVariable.prototype.expressionSource = function(dependencyTemplates, outputOptions, requestShortener) {
24-
var source = new ReplaceSource(new RawSource(this.expression));
25-
this.dependencies.forEach(function(dep) {
26-
var template = dependencyTemplates.get(dep.constructor);
27-
if(!template) throw new Error("No template for dependency: " + dep.constructor.name);
28-
template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
29-
});
30-
return source;
31-
};
25+
expressionSource(dependencyTemplates, outputOptions, requestShortener) {
26+
const source = new ReplaceSource(new RawSource(this.expression));
27+
this.dependencies.forEach(dep => {
28+
const template = dependencyTemplates.get(dep.constructor);
29+
if(!template) throw new Error(`No template for dependency: ${dep.constructor.name}`);
30+
template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
31+
});
32+
return source;
33+
}
3234

33-
DependenciesBlockVariable.prototype.disconnect = function() {
34-
this.dependencies.forEach(function(d) {
35-
d.disconnect();
36-
});
37-
};
35+
disconnect() {
36+
this.dependencies.forEach(d => {
37+
d.disconnect();
38+
});
39+
}
3840

39-
DependenciesBlockVariable.prototype.hasDependencies = function() {
40-
return this.dependencies.length > 0;
41-
};
41+
hasDependencies() {
42+
return this.dependencies.length > 0;
43+
}
44+
}
45+
46+
module.exports = DependenciesBlockVariable;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
var should = require("should");
2+
var sinon = require("sinon");
3+
var DependenciesBlockVariable = require("../lib/DependenciesBlockVariable");
4+
5+
describe("DependenciesBlockVariable", function() {
6+
var DependenciesBlockVariableInstance,
7+
dependencyMock,
8+
sandbox;
9+
10+
before(function() {
11+
sandbox = sinon.sandbox.create();
12+
dependencyMock = {
13+
constructor: {
14+
name: "DependencyMock"
15+
},
16+
disconnect: sandbox.spy(),
17+
updateHash: sandbox.spy()
18+
};
19+
DependenciesBlockVariableInstance = new DependenciesBlockVariable(
20+
"dependencies-name",
21+
"expression", [dependencyMock]);
22+
});
23+
24+
afterEach(function() {
25+
sandbox.restore();
26+
});
27+
28+
describe("hasDependencies", function() {
29+
it("returns `true` if has dependencies", function() {
30+
should(DependenciesBlockVariableInstance.hasDependencies()).be.true();
31+
});
32+
});
33+
34+
describe("disconnect", function() {
35+
it("trigger dependencies disconnection", function() {
36+
DependenciesBlockVariableInstance.disconnect();
37+
should(dependencyMock.disconnect.calledOnce).be.true();
38+
});
39+
});
40+
41+
describe("updateHash", function() {
42+
var hash;
43+
before(function() {
44+
hash = {
45+
update: sandbox.spy()
46+
};
47+
DependenciesBlockVariableInstance.updateHash(hash);
48+
});
49+
50+
it("should update hash dependencies with name", function() {
51+
should(hash.update.calledWith("dependencies-name")).be.true();
52+
});
53+
54+
it("should update hash dependencies with expression", function() {
55+
should(hash.update.calledWith("expression")).be.true();
56+
});
57+
58+
it("should update hash inside dependencies", function() {
59+
should(dependencyMock.updateHash.calledOnce).be.true();
60+
});
61+
});
62+
63+
describe("expressionSource", function() {
64+
var dependencyTemplates,
65+
applyMock;
66+
67+
before(function() {
68+
applyMock = sandbox.spy();
69+
});
70+
71+
it("aplies information inside dependency templates", function() {
72+
dependencyTemplates = {
73+
get: function() {
74+
return {
75+
apply: applyMock
76+
};
77+
}
78+
};
79+
DependenciesBlockVariableInstance.expressionSource(
80+
dependencyTemplates, {}, {}
81+
);
82+
should(applyMock.calledOnce).be.true();
83+
});
84+
85+
it("aplies information inside dependency templates", function() {
86+
dependencyTemplates = {
87+
get: function() {
88+
return false;
89+
}
90+
};
91+
should(function() {
92+
DependenciesBlockVariableInstance.expressionSource(
93+
dependencyTemplates, {}, {}
94+
)
95+
}).throw("No template for dependency: DependencyMock");
96+
})
97+
});
98+
});

0 commit comments

Comments
 (0)