Skip to content

Commit 20cc1f2

Browse files
committed
Refactor code-style
1 parent 06a84bf commit 20cc1f2

File tree

5 files changed

+93
-81
lines changed

5 files changed

+93
-81
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-source.js
3+
unist-util-source.min.js

index.js

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

3-
var location = require('vfile-location');
3+
var location = require('vfile-location')
44

5-
module.exports = source;
5+
module.exports = source
66

77
function source(value, file) {
8-
var doc = String(file);
9-
var loc = location(file);
10-
var val = value && value.position ? value.position : value || {};
11-
var start;
12-
var end;
13-
var indents;
14-
var indent;
15-
var lines;
16-
var length;
17-
var index;
18-
19-
start = loc.toOffset(val.start);
20-
end = loc.toOffset(val.end);
21-
indents = val.indent || [];
8+
var doc = String(file)
9+
var loc = location(file)
10+
var val = value && value.position ? value.position : value || {}
11+
var start
12+
var end
13+
var indents
14+
var indent
15+
var lines
16+
var length
17+
var index
18+
19+
start = loc.toOffset(val.start)
20+
end = loc.toOffset(val.end)
21+
indents = val.indent || []
2222

2323
if (start === -1 || end === -1) {
24-
return null;
24+
return null
2525
}
2626

27-
lines = doc.slice(start, end).split('\n');
28-
length = lines.length;
29-
index = 0;
27+
lines = doc.slice(start, end).split('\n')
28+
length = lines.length
29+
index = 0
3030

3131
while (++index < length) {
32-
indent = indents[index - 1];
33-
lines[index] = lines[index].slice(indent ? indent - 1 : 0);
32+
indent = indents[index - 1]
33+
lines[index] = lines[index].slice(indent ? indent - 1 : 0)
3434
}
3535

36-
return lines.join('\n');
36+
return lines.join('\n')
3737
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,38 @@
2525
"browserify": "^16.0.0",
2626
"esmangle": "^1.0.1",
2727
"nyc": "^11.0.0",
28+
"prettier": "^1.12.1",
2829
"remark": "^9.0.0",
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 --bare -s unistUtilSource > unist-util-source.js",
3738
"build-mangle": "esmangle < unist-util-source.js > unist-util-source.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.js",
42-
"test": "npm run build && npm run lint && npm run test-coverage"
42+
"test": "npm run format && npm run build && npm run test-coverage"
4343
},
4444
"nyc": {
4545
"check-coverage": true,
4646
"lines": 100,
4747
"functions": 100,
4848
"branches": 100
4949
},
50+
"prettier": {
51+
"tabWidth": 2,
52+
"useTabs": false,
53+
"singleQuote": true,
54+
"bracketSpacing": false,
55+
"semi": false,
56+
"trailingComma": "none"
57+
},
5058
"xo": {
51-
"space": true,
59+
"prettier": true,
5260
"esnext": false,
5361
"rules": {
5462
"guard-for-in": "off",

readme.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ Say we have the following file, `example.md`:
2323
And our script, `example.js`, looks as follows:
2424

2525
```javascript
26-
var vfile = require('to-vfile');
27-
var unified = require('unified');
28-
var parse = require('remark-parse');
29-
var source = require('unist-util-source');
30-
31-
var file = vfile.readSync('example.md');
32-
var tree = unified().use(parse).parse(file);
33-
34-
var list = tree.children[0].children[0];
35-
console.log(source(list, file));
26+
var vfile = require('to-vfile')
27+
var unified = require('unified')
28+
var parse = require('remark-parse')
29+
var source = require('unist-util-source')
30+
31+
var file = vfile.readSync('example.md')
32+
var tree = unified()
33+
.use(parse)
34+
.parse(file)
35+
36+
var list = tree.children[0].children[0]
37+
console.log(source(list, file))
3638
```
3739

3840
Now, running `node example` yields:

test.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
1-
'use strict';
1+
'use strict'
22

3-
var test = require('tape');
4-
var remark = require('remark');
5-
var source = require('.');
3+
var test = require('tape')
4+
var remark = require('remark')
5+
var source = require('.')
66

7-
test('unist-util-source', function (t) {
8-
t.plan(10);
7+
test('unist-util-source', function(t) {
8+
t.plan(10)
99

10-
remark().use(function () {
11-
return transformer;
12-
function transformer(tree, file) {
13-
var node = tree;
10+
remark()
11+
.use(function() {
12+
return transformer
13+
function transformer(tree, file) {
14+
var node = tree
1415

15-
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.');
16+
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.')
1617

17-
/* Blockquote. */
18-
node = tree.children[0];
19-
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.');
18+
/* Blockquote. */
19+
node = tree.children[0]
20+
t.equal(source(node, file), '> + **[Hello](./example)**\n> world.')
2021

21-
/* List. */
22-
node = node.children[0];
23-
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.');
22+
/* List. */
23+
node = node.children[0]
24+
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.')
2425

25-
/* List-item. */
26-
node = node.children[0];
27-
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.');
26+
/* List-item. */
27+
node = node.children[0]
28+
t.equal(source(node, file), '+ **[Hello](./example)**\nworld.')
2829

29-
/* Paragraph. */
30-
node = node.children[0];
31-
t.equal(source(node, file), '**[Hello](./example)**\nworld.');
30+
/* Paragraph. */
31+
node = node.children[0]
32+
t.equal(source(node, file), '**[Hello](./example)**\nworld.')
3233

33-
/* Strong. */
34-
node = node.children[0];
35-
t.equal(source(node, file), '**[Hello](./example)**');
34+
/* Strong. */
35+
node = node.children[0]
36+
t.equal(source(node, file), '**[Hello](./example)**')
3637

37-
/* Link. */
38-
node = node.children[0];
39-
t.equal(source(node, file), '[Hello](./example)');
38+
/* Link. */
39+
node = node.children[0]
40+
t.equal(source(node, file), '[Hello](./example)')
4041

41-
/* Text. */
42-
node = node.children[0];
43-
t.equal(source(node, file), 'Hello');
42+
/* Text. */
43+
node = node.children[0]
44+
t.equal(source(node, file), 'Hello')
4445

45-
/* Generated. */
46-
t.equal(source({type: node.type, value: node.value}, file), null);
46+
/* Generated. */
47+
t.equal(source({type: node.type, value: node.value}, file), null)
4748

48-
/* Missing. */
49-
t.equal(source(null, file), null);
50-
}
51-
}).processSync([
52-
'> + **[Hello](./example)**',
53-
'> world.'
54-
].join('\n'));
55-
});
49+
/* Missing. */
50+
t.equal(source(null, file), null)
51+
}
52+
})
53+
.processSync(['> + **[Hello](./example)**', '> world.'].join('\n'))
54+
})

0 commit comments

Comments
 (0)