Skip to content

Commit 8b3540c

Browse files
committed
Refactor code-style
1 parent da70db4 commit 8b3540c

File tree

5 files changed

+283
-232
lines changed

5 files changed

+283
-232
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-inspect.js
3+
unist-util-inspect.min.js

index.js

Lines changed: 75 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,222 +1,207 @@
1-
'use strict';
1+
'use strict'
22

3-
var isEmpty = require('is-empty');
3+
var isEmpty = require('is-empty')
44

55
/* Detect color support. */
66

7-
var color = true;
7+
var color = true
88

99
try {
10-
color = 'inspect' in require('util');
10+
color = 'inspect' in require('util')
1111
} catch (err) {
1212
/* istanbul ignore next - browser */
13-
color = false;
13+
color = false
1414
}
1515

16-
module.exports = color ? inspect : /* istanbul ignore next */ noColor;
16+
module.exports = color ? inspect : /* istanbul ignore next */ noColor
1717

18-
inspect.color = inspect;
19-
noColor.color = inspect;
20-
inspect.noColor = noColor;
21-
noColor.noColor = noColor;
18+
inspect.color = inspect
19+
noColor.color = inspect
20+
inspect.noColor = noColor
21+
noColor.noColor = noColor
2222

23-
var dim = ansiColor(2, 22);
24-
var yellow = ansiColor(33, 39);
25-
var green = ansiColor(32, 39);
23+
var dim = ansiColor(2, 22)
24+
var yellow = ansiColor(33, 39)
25+
var green = ansiColor(32, 39)
2626

2727
/* Define ANSII color removal functionality. */
2828
var COLOR_EXPRESSION = new RegExp(
2929
'(?:' +
3030
'(?:\\u001b\\[)|' +
3131
'\\u009b' +
32-
')' +
33-
'(?:' +
32+
')' +
33+
'(?:' +
3434
'(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m]' +
35-
')|' +
36-
'\\u001b[A-M]',
35+
')|' +
36+
'\\u001b[A-M]',
3737
'g'
38-
);
39-
40-
/* Constants. */
41-
var CHAR_HORIZONTAL_LINE = '─';
42-
var CHAR_VERTICAL_LINE = '│';
43-
var CHAR_SPLIT = '└';
44-
var CHAR_CONTINUE_AND_SPLIT = '├';
45-
var CONTINUE = CHAR_CONTINUE_AND_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
46-
var STOP = CHAR_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
38+
)
4739

4840
/* Standard keys defined by unist:
4941
* https://github.com/syntax-tree/unist.
50-
* We don‘t include `data` though. */
51-
var ignore = [
52-
'type',
53-
'value',
54-
'children',
55-
'position'
56-
];
42+
* We don’t ignore `data` though. */
43+
var ignore = ['type', 'value', 'children', 'position']
5744

5845
/* Inspects a node, without using color. */
5946
function noColor(node, pad) {
60-
return stripColor(inspect(node, pad));
47+
return stripColor(inspect(node, pad))
6148
}
6249

6350
/* Inspects a node. */
6451
function inspect(node, pad) {
65-
var result;
66-
var children;
67-
var index;
68-
var length;
52+
var result
53+
var children
54+
var index
55+
var length
6956

7057
if (node && Boolean(node.length) && typeof node !== 'string') {
71-
length = node.length;
72-
index = -1;
73-
result = [];
58+
length = node.length
59+
index = -1
60+
result = []
7461

7562
while (++index < length) {
76-
result[index] = inspect(node[index]);
63+
result[index] = inspect(node[index])
7764
}
7865

79-
return result.join('\n');
66+
return result.join('\n')
8067
}
8168

8269
if (!node || !node.type) {
83-
return String(node);
70+
return String(node)
8471
}
8572

86-
result = [formatNode(node)];
87-
children = node.children;
88-
length = children && children.length;
89-
index = -1;
73+
result = [formatNode(node)]
74+
children = node.children
75+
length = children && children.length
76+
index = -1
9077

9178
if (!length) {
92-
return result[0];
79+
return result[0]
9380
}
9481

9582
if (!pad || typeof pad === 'number') {
96-
pad = '';
83+
pad = ''
9784
}
9885

9986
while (++index < length) {
100-
node = children[index];
87+
node = children[index]
10188

10289
if (index === length - 1) {
103-
result.push(formatNesting(pad + STOP) + inspect(node, pad + ' '));
90+
result.push(formatNesting(pad + '└─ ') + inspect(node, pad + ' '))
10491
} else {
105-
result.push(formatNesting(pad + CONTINUE) + inspect(node, pad + CHAR_VERTICAL_LINE + ' '));
92+
result.push(formatNesting(pad + '├─ ') + inspect(node, pad + '│ '))
10693
}
10794
}
10895

109-
return result.join('\n');
96+
return result.join('\n')
11097
}
11198

11299
/* Colored nesting formatter. */
113100
function formatNesting(value) {
114-
return dim(value);
101+
return dim(value)
115102
}
116103

117104
/* Compile a single position. */
118105
function compile(pos) {
119-
var values = [];
106+
var values = []
120107

121108
if (!pos) {
122-
return null;
109+
return null
123110
}
124111

125-
values = [
126-
[pos.line || 1, pos.column || 1].join(':')
127-
];
112+
values = [[pos.line || 1, pos.column || 1].join(':')]
128113

129114
if ('offset' in pos) {
130-
values.push(String(pos.offset || 0));
115+
values.push(String(pos.offset || 0))
131116
}
132117

133-
return values;
118+
return values
134119
}
135120

136121
/* Compile a location. */
137122
function stringify(start, end) {
138-
var values = [];
139-
var positions = [];
140-
var offsets = [];
123+
var values = []
124+
var positions = []
125+
var offsets = []
141126

142-
add(start);
143-
add(end);
127+
add(start)
128+
add(end)
144129

145130
if (positions.length !== 0) {
146-
values.push(positions.join('-'));
131+
values.push(positions.join('-'))
147132
}
148133

149134
if (offsets.length !== 0) {
150-
values.push(offsets.join('-'));
135+
values.push(offsets.join('-'))
151136
}
152137

153-
return values.join(', ');
138+
return values.join(', ')
154139

155140
/* Add a position. */
156141
function add(position) {
157-
var tuple = compile(position);
142+
var tuple = compile(position)
158143

159144
if (tuple) {
160-
positions.push(tuple[0]);
145+
positions.push(tuple[0])
161146

162147
if (tuple[1]) {
163-
offsets.push(tuple[1]);
148+
offsets.push(tuple[1])
164149
}
165150
}
166151
}
167152
}
168153

169154
/* Colored node formatter. */
170155
function formatNode(node) {
171-
var log = node.type;
172-
var location = node.position || {};
173-
var position = stringify(location.start, location.end);
174-
var key;
175-
var values = [];
176-
var value;
156+
var log = node.type
157+
var location = node.position || {}
158+
var position = stringify(location.start, location.end)
159+
var key
160+
var values = []
161+
var value
177162

178163
if (node.children) {
179-
log += dim('[') + yellow(node.children.length) + dim(']');
164+
log += dim('[') + yellow(node.children.length) + dim(']')
180165
} else if (typeof node.value === 'string') {
181-
log += dim(': ') + green(JSON.stringify(node.value));
166+
log += dim(': ') + green(JSON.stringify(node.value))
182167
}
183168

184169
if (position) {
185-
log += ' (' + position + ')';
170+
log += ' (' + position + ')'
186171
}
187172

188173
for (key in node) {
189-
value = node[key];
174+
value = node[key]
190175

191176
if (
192177
ignore.indexOf(key) !== -1 ||
193178
value === null ||
194179
value === undefined ||
195180
(typeof value === 'object' && isEmpty(value))
196181
) {
197-
continue;
182+
continue
198183
}
199184

200-
values.push('[' + key + '=' + JSON.stringify(value) + ']');
185+
values.push('[' + key + '=' + JSON.stringify(value) + ']')
201186
}
202187

203188
if (values.length !== 0) {
204-
log += ' ' + values.join('');
189+
log += ' ' + values.join('')
205190
}
206191

207-
return log;
192+
return log
208193
}
209194

210195
/* Remove ANSI colour from `value`. */
211196
function stripColor(value) {
212-
return value.replace(COLOR_EXPRESSION, '');
197+
return value.replace(COLOR_EXPRESSION, '')
213198
}
214199

215200
/* Factory to wrap values in ANSI colours. */
216201
function ansiColor(open, close) {
217-
return color;
202+
return color
218203

219204
function color(value) {
220-
return '\u001b[' + open + 'm' + value + '\u001b[' + close + 'm';
205+
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
221206
}
222207
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"chalk": "^2.3.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
"retext": "^5.0.0",
@@ -34,17 +35,24 @@
3435
"xo": "^0.20.0"
3536
},
3637
"scripts": {
37-
"build-md": "remark . -qfo",
38+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
3839
"build-bundle": "browserify index.js --bare -s unistUtilInspect > unist-util-inspect.js",
3940
"build-mangle": "esmangle unist-util-inspect.js > unist-util-inspect.min.js",
40-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
41-
"lint": "xo",
41+
"build": "npm run build-bundle && npm run build-mangle",
4242
"test-api": "node test",
4343
"test-coverage": "nyc --reporter lcov tape test.js",
44-
"test": "npm run build && npm run lint && npm run test-coverage"
44+
"test": "npm run format && npm run build && npm run test-coverage"
45+
},
46+
"prettier": {
47+
"tabWidth": 2,
48+
"useTabs": false,
49+
"singleQuote": true,
50+
"bracketSpacing": false,
51+
"semi": false,
52+
"trailingComma": "none"
4553
},
4654
"xo": {
47-
"space": true,
55+
"prettier": true,
4856
"esnext": false,
4957
"rules": {
5058
"guard-for-in": "off",

readme.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ npm install unist-util-inspect
1313
## Usage
1414

1515
```javascript
16-
var unified = require('unified');
17-
var inspect = require('unist-util-inspect');
18-
var parse = require('rehype-parse');
16+
var unified = require('unified')
17+
var inspect = require('unist-util-inspect')
18+
var parse = require('rehype-parse')
1919

20-
var tree = unified().use(parse).parse('<h2>Hello, world!</h2>');
20+
var tree = unified()
21+
.use(parse)
22+
.parse('<h2>Hello, world!</h2>')
2123

22-
console.log(inspect(tree));
24+
console.log(inspect(tree))
2325
```
2426

2527
Yields:

0 commit comments

Comments
 (0)