Skip to content

Commit a379060

Browse files
committed
Refactor code-style
1 parent 6f51df6 commit a379060

File tree

14 files changed

+323
-362
lines changed

14 files changed

+323
-362
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
unist-util-assert.js
3+
unist-util-assert.min.js

index.js

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,96 @@
1-
'use strict';
1+
'use strict'
22

33
/* Dependencies. */
4-
var assert = require('assert');
5-
var array = require('x-is-array');
6-
var object = require('x-is-object');
4+
var assert = require('assert')
5+
var array = require('x-is-array')
6+
var object = require('x-is-object')
77

8-
var inspect;
8+
var inspect
99

1010
try {
1111
// eslint-disable-next-line import/no-dynamic-require, no-useless-concat
12-
inspect = require('ut' + 'il').inspect;
13-
} catch (err) { /* Empty. */ }
12+
inspect = require('ut' + 'il').inspect
13+
} catch (err) {
14+
/* Empty. */
15+
}
1416

1517
/* Expose. */
16-
exports = wrap(unist);
17-
module.exports = exports;
18+
exports = wrap(unist)
19+
module.exports = exports
1820

19-
exports.parent = wrap(parent);
20-
exports.text = wrap(text);
21-
exports.void = wrap(empty);
22-
exports.wrap = wrap;
21+
exports.parent = wrap(parent)
22+
exports.text = wrap(text)
23+
exports.void = wrap(empty)
24+
exports.wrap = wrap
2325

2426
/* Identifier to check if a value is seen. */
25-
var ID = '__unist__';
27+
var ID = '__unist__'
2628

2729
/* List of specced properties. */
28-
var defined = ['type', 'value', 'children', 'position'];
30+
var defined = ['type', 'value', 'children', 'position']
2931

3032
/* Wrapper around `Node` which adds the current node
3133
* (and parent, if available), to the message. */
3234
function wrap(fn) {
33-
return wrapped;
35+
return wrapped
3436

3537
function wrapped(node, parent) {
3638
try {
37-
fn(node, parent);
39+
fn(node, parent)
3840
} catch (err) {
3941
if (!err[ID]) {
40-
err[ID] = true;
42+
err[ID] = true
4143

42-
err.message += ': `' + view(node) + '`';
44+
err.message += ': `' + view(node) + '`'
4345

4446
if (parent) {
45-
err.message += ' in `' + view(parent) + '`';
47+
err.message += ' in `' + view(parent) + '`'
4648
}
4749
}
4850

49-
throw err;
51+
throw err
5052
}
5153
}
5254
}
5355

5456
/* Assert. */
5557
function unist(node) {
56-
var type;
57-
var children;
58-
var value;
59-
var key;
60-
var index;
61-
var length;
58+
var type
59+
var children
60+
var value
61+
var key
62+
var index
63+
var length
6264

63-
assert.ok(object(node), 'node should be an object');
65+
assert.ok(object(node), 'node should be an object')
6466

65-
type = node.type;
66-
children = node.children;
67-
value = node.value;
67+
type = node.type
68+
children = node.children
69+
value = node.value
6870

69-
assert.ok('type' in node, 'node should have a type');
70-
assert.equal(typeof type, 'string', '`type` should be a string');
71-
assert.notEqual(type, '', '`type` should not be empty');
71+
assert.ok('type' in node, 'node should have a type')
72+
assert.equal(typeof type, 'string', '`type` should be a string')
73+
assert.notEqual(type, '', '`type` should not be empty')
7274

7375
if (value != null) {
74-
assert.equal(typeof value, 'string', '`value` should be a string');
76+
assert.equal(typeof value, 'string', '`value` should be a string')
7577
}
7678

77-
location(node.position);
79+
location(node.position)
7880

7981
for (key in node) {
8082
if (defined.indexOf(key) === -1) {
81-
vanilla(key, node[key]);
83+
vanilla(key, node[key])
8284
}
8385
}
8486

8587
if (children != null) {
86-
assert.ok(array(children), '`children` should be an array');
87-
index = -1;
88-
length = children.length;
88+
assert.ok(array(children), '`children` should be an array')
89+
index = -1
90+
length = children.length
8991

9092
while (++index < length) {
91-
exports(children[index], node);
93+
exports(children[index], node)
9294
}
9395
}
9496
}
@@ -97,9 +99,9 @@ function unist(node) {
9799
* and re-parsed to the same (deep) value. */
98100
function vanilla(key, value) {
99101
try {
100-
assert.deepEqual(value, JSON.parse(JSON.stringify(value)));
102+
assert.deepEqual(value, JSON.parse(JSON.stringify(value)))
101103
} catch (err) {
102-
assert.fail('', '', 'non-specced property `' + key + '` should be JSON');
104+
assert.fail('', '', 'non-specced property `' + key + '` should be JSON')
103105
}
104106
}
105107

@@ -111,64 +113,67 @@ function view(value) {
111113
/* eslint-disable no-else-return */
112114
/* istanbul ignore else - Browser. */
113115
if (inspect) {
114-
return inspect(value, {colors: false});
116+
return inspect(value, {colors: false})
115117
} else {
116-
return JSON.stringify(value);
118+
return JSON.stringify(value)
117119
}
118120
} catch (err) {
119121
/* istanbul ignore next - Cyclical. */
120-
return String(value);
122+
return String(value)
121123
}
122124
}
123125

124126
/* Assert `node` is a parent node. */
125127
function parent(node) {
126-
unist(node);
128+
unist(node)
127129

128-
assert.equal('value' in node, false, 'parent should not have `value`');
129-
assert.ok('children' in node, 'parent should have `children`');
130+
assert.equal('value' in node, false, 'parent should not have `value`')
131+
assert.ok('children' in node, 'parent should have `children`')
130132
}
131133

132134
/* Assert `node` is a text node. */
133135
function text(node) {
134-
unist(node);
136+
unist(node)
135137

136-
assert.equal('children' in node, false, 'text should not have `children`');
137-
assert.ok('value' in node, 'text should have `value`');
138+
assert.equal('children' in node, false, 'text should not have `children`')
139+
assert.ok('value' in node, 'text should have `value`')
138140
}
139141

140142
/* Assert `node` is a Unist node, but neither parent nor
141143
* text. */
142144
function empty(node) {
143-
unist(node);
145+
unist(node)
144146

145-
assert.equal('value' in node, false, 'void should not have `value`');
146-
assert.equal('children' in node, false, 'void should not have `children`');
147+
assert.equal('value' in node, false, 'void should not have `value`')
148+
assert.equal('children' in node, false, 'void should not have `children`')
147149
}
148150

149151
/* Assert `location` is a Unist Location. */
150152
function location(location) {
151153
if (location != null) {
152-
assert.ok(object(location), '`position` should be an object');
154+
assert.ok(object(location), '`position` should be an object')
153155

154-
position(location.start, 'position.start');
155-
position(location.end, 'position.end');
156+
position(location.start, 'position.start')
157+
position(location.end, 'position.end')
156158
}
157159
}
158160

159161
/* Assert `location` is a Unist Location. */
160162
function position(position, name) {
161163
if (position != null) {
162-
assert.ok(object(position), '`' + name + '` should be an object');
164+
assert.ok(object(position), '`' + name + '` should be an object')
163165

164166
if (position.line != null) {
165-
assert.ok('line' in position, '`' + name + '` should have numeric `line`');
166-
assert.ok(position.line >= 1, '`' + name + '.line` should be gte `1`');
167+
assert.ok('line' in position, '`' + name + '` should have numeric `line`')
168+
assert.ok(position.line >= 1, '`' + name + '.line` should be gte `1`')
167169
}
168170

169171
if (position.column != null) {
170-
assert.ok('column' in position, '`' + name + '` should have numeric `column`');
171-
assert.ok(position.column >= 1, '`' + name + '.column` should be gte `1`');
172+
assert.ok(
173+
'column' in position,
174+
'`' + name + '` should have numeric `column`'
175+
)
176+
assert.ok(position.column >= 1, '`' + name + '.column` should be gte `1`')
172177
}
173178
}
174179
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@
2626
"browserify": "^16.0.0",
2727
"esmangle": "^1.0.1",
2828
"nyc": "^11.0.0",
29+
"prettier": "^1.12.1",
2930
"remark-cli": "^5.0.0",
3031
"remark-preset-wooorm": "^4.0.0",
3132
"tape": "^4.0.0",
3233
"xo": "^0.20.0"
3334
},
3435
"scripts": {
35-
"build-md": "remark . -qfo",
36+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3637
"build-bundle": "browserify index.js -s unistUtilAssert > unist-util-assert.js",
3738
"build-mangle": "esmangle < unist-util-assert.js > unist-util-assert.min.js",
38-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
39-
"lint": "xo",
39+
"build": "npm run build-bundle && npm run build-mangle",
4040
"test-api": "node test",
4141
"test-coverage": "nyc --reporter lcov tape test",
42-
"test": "npm run build && npm run lint && npm run test-coverage"
42+
"test": "npm run format && npm run build && npm run test-coverage"
43+
},
44+
"prettier": {
45+
"tabWidth": 2,
46+
"useTabs": false,
47+
"singleQuote": true,
48+
"bracketSpacing": false,
49+
"semi": false,
50+
"trailingComma": "none"
4351
},
4452
"xo": {
45-
"space": true,
53+
"prettier": true,
4654
"esnext": false,
4755
"rules": {
4856
"guard-for-in": "off",

readme.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ npm install unist-util-assert
1313
## Usage
1414

1515
```javascript
16-
var assert = require('unist-util-assert');
16+
var assert = require('unist-util-assert')
1717

18-
assert({type: 'root', children: []});
19-
assert({type: 'break'});
20-
assert({type: 'element', properties: {}, children: []});
18+
assert({type: 'root', children: []})
19+
assert({type: 'break'})
20+
assert({type: 'element', properties: {}, children: []})
2121
// All OK.
2222

23-
assert({children: []});
23+
assert({children: []})
2424
// AssertionError: node should have a type: `{ children: [] }`
2525

26-
assert.parent({type: 'break'});
26+
assert.parent({type: 'break'})
2727
// AssertionError: parent should have `children`: `{ type: 'break' }`
2828

29-
assert({type: 'element', properties: function () {}});
29+
assert({type: 'element', properties: function() {}})
3030
// AssertionError: non-specced property `properties` should be JSON: `{ type: 'element', properties: [Function] }`
3131

32-
assert.void({type: 'text', value: 'Alpha'});
32+
assert.void({type: 'text', value: 'Alpha'})
3333
// AssertionError: void should not have `value`: `{ type: 'text', value: 'Alpha' }`
3434

35-
assert({type: 'paragraph', children: ['foo']});
35+
assert({type: 'paragraph', children: ['foo']})
3636
// AssertionError: node should be an object: `'foo'` in `{ type: 'paragraph', children: [ 'foo' ] }`
3737
```
3838

test/children.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
'use strict';
1+
'use strict'
22

3-
var test = require('tape');
4-
var assert = require('..');
3+
var test = require('tape')
4+
var assert = require('..')
55

6-
test('children', function (t) {
6+
test('children', function(t) {
77
t.throws(
8-
function () {
9-
assert({type: 'foo', children: {alpha: 'bravo'}});
8+
function() {
9+
assert({type: 'foo', children: {alpha: 'bravo'}})
1010
},
1111
/`children` should be an array: `{ type: 'foo', children: { alpha: 'bravo' } }`$/,
1212
'should throw if given a non-node child in children'
13-
);
13+
)
1414

1515
t.throws(
16-
function () {
17-
assert({type: 'foo', children: ['one']});
16+
function() {
17+
assert({type: 'foo', children: ['one']})
1818
},
1919
/node should be an object: `'one'` in `{ type: 'foo', children: \[ 'one' ] }`$/,
2020
'should throw if given a non-node child in children'
21-
);
21+
)
2222

23-
t.doesNotThrow(
24-
function () {
25-
assert({type: 'parent', children: [{type: 'text', value: 'alpha'}]});
26-
},
27-
'should not throw on vald children'
28-
);
23+
t.doesNotThrow(function() {
24+
assert({type: 'parent', children: [{type: 'text', value: 'alpha'}]})
25+
}, 'should not throw on vald children')
2926

3027
t.throws(
31-
function () {
32-
assert({type: 'foo', children: [{
33-
type: 'bar',
34-
children: ['one']
35-
}]});
28+
function() {
29+
assert({
30+
type: 'foo',
31+
children: [
32+
{
33+
type: 'bar',
34+
children: ['one']
35+
}
36+
]
37+
})
3638
},
3739
/node should be an object: `'one'` in `{ type: 'bar', children: \[ 'one' ] }`$/,
3840
'should throw on invalid descendants'
39-
);
41+
)
4042

41-
t.end();
42-
});
43+
t.end()
44+
})

0 commit comments

Comments
 (0)