Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit 8c63203

Browse files
authored
Merge pull request #105 from bem-sdk/fix/deprecation
Fix deprecation
2 parents aa08471 + d0d9064 commit 8c63203

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

lib/deprecate.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const util = require('util');
2+
3+
const deprecate = require('depd')(require('../package.json').name);
4+
5+
/**
6+
* Logs deprecation messages.
7+
*
8+
* @param {object} obj
9+
* @param {string} deprecateName
10+
* @param {string} newName
11+
*/
12+
module.exports = (obj, deprecateName, newName) => {
13+
const objStr = util.inspect(obj, { depth: 1 });
14+
const message = [
15+
`\`${deprecateName}\` is kept just for compatibility and can be dropped in the future.`,
16+
`Use \`${newName}\` instead in \`${objStr}\` at`
17+
].join(' ');
18+
19+
deprecate(message);
20+
};

lib/entity-name.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const util = require('util');
44

55
const stringifyEntity = require('@bem/naming').stringify;
6-
const deprecate = require('depd')(require('../package.json').name);
76

7+
const deprecate = require('./deprecate');
88
const EntityTypeError = require('./entity-type-error');
99

1010
/**
@@ -29,6 +29,9 @@ class BemEntityName {
2929
throw new EntityTypeError(obj, 'the field `block` is undefined');
3030
}
3131

32+
obj.modName && deprecate(obj, 'modName', 'mod.name');
33+
obj.modVal && deprecate(obj, 'modVal', 'mod.val');
34+
3235
const data = this._data = { block: obj.block };
3336

3437
obj.elem && (data.elem = obj.elem);
@@ -104,7 +107,7 @@ class BemEntityName {
104107
* @deprecated use {@link BemEntityName#mod.name}
105108
*/
106109
get modName() {
107-
deprecate(`modName is kept just for compatibility and can be dropped in the future. Use mod.name instead in ${this.inspect()} at`);
110+
deprecate(this, 'modName', 'mod.name');
108111

109112
return this.mod && this.mod.name;
110113
}
@@ -118,7 +121,7 @@ class BemEntityName {
118121
* @deprecated use {@link BemEntityName#mod.val}
119122
*/
120123
get modVal() {
121-
deprecate(`modVal is kept just for compatibility and can be dropped in the future. Use mod.val instead in ${this.inspect()} at`);
124+
deprecate(this, 'modVal', 'mod.val');
122125

123126
return this.mod && this.mod.val;
124127
}

test/deprecate.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const test = require('ava');
2+
const sinon = require('sinon');
3+
const proxyquire = require('proxyquire');
4+
5+
const BemEntityName = require('../lib/entity-name');
6+
7+
const deprecateSpy = sinon.spy();
8+
const deprecate = proxyquire('../lib/deprecate', {
9+
'depd':() => deprecateSpy
10+
});
11+
12+
test('should deprecate object', t => {
13+
deprecate({ block: 'block' }, 'oldField', 'newField');
14+
15+
const message = [
16+
"`oldField` is kept just for compatibility and can be dropped in the future.",
17+
"Use `newField` instead in `{ block: 'block' }` at"
18+
].join(' ');
19+
20+
t.true(deprecateSpy.calledWith(message));
21+
});
22+
23+
test('should deprecate BemEntityName instance', t => {
24+
deprecate(new BemEntityName({ block: 'block' }), 'oldField', 'newField');
25+
26+
const message = [
27+
"`oldField` is kept just for compatibility and can be dropped in the future.",
28+
"Use `newField` instead in `BemEntityName { block: 'block' }` at"
29+
].join(' ');
30+
31+
t.true(deprecateSpy.calledWith(message));
32+
});

test/to-string.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ test('should use `naming.stringify()` for block', t => {
1414

1515
entityName.toString();
1616

17-
t.truthy(spy.calledWith({ block: 'block' }));
17+
t.true(spy.calledWith({ block: 'block' }));
1818
});
1919

2020
test('should use `naming.stringify()` for elem', t => {
2121
const entityName = new BemEntityName({ block: 'block', elem: 'elem' });
2222

2323
entityName.toString();
2424

25-
t.truthy(spy.calledWith({ block: 'block', elem: 'elem' }));
25+
t.true(spy.calledWith({ block: 'block', elem: 'elem' }));
2626
});
2727

2828
test('should use `naming.stringify()` for block modifier', t => {
2929
const entityName = new BemEntityName({ block: 'block', mod: { name: 'mod', val: 'val' } });
3030

3131
entityName.toString();
3232

33-
t.truthy(spy.calledWith({ block: 'block', mod: { name: 'mod', val: 'val' } }));
33+
t.true(spy.calledWith({ block: 'block', mod: { name: 'mod', val: 'val' } }));
3434
});
3535

3636
test('should use naming.stringify() for element modifier', t => {
3737
const entityName = new BemEntityName({ block: 'block', elem: 'elem', mod: { name: 'mod', val: 'val' } });
3838

3939
entityName.toString();
4040

41-
t.truthy(spy.calledWith({ block: 'block', elem: 'elem', mod: { name: 'mod', val: 'val' } }));
41+
t.true(spy.calledWith({ block: 'block', elem: 'elem', mod: { name: 'mod', val: 'val' } }));
4242
});

0 commit comments

Comments
 (0)