Skip to content

Commit 09ab3e6

Browse files
committed
Refactor code-style
1 parent 1e39182 commit 09ab3e6

File tree

18 files changed

+194
-173
lines changed

18 files changed

+194
-173
lines changed

.prettierignore

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

index.js

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,78 @@
1-
'use strict';
1+
'use strict'
22

3-
/* Expose. */
4-
module.exports = zone;
3+
var commentMarker = require('mdast-comment-marker')
4+
var visit = require('unist-util-visit')
55

6-
/* Dependencies. */
7-
var commentMarker = require('mdast-comment-marker');
8-
var visit = require('unist-util-visit');
6+
module.exports = zone
97

10-
/* Methods. */
11-
var splice = [].splice;
8+
var splice = [].splice
129

13-
/* Run factory. */
1410
function zone(node, name, callback) {
15-
var nodes = [];
16-
var start = null;
17-
var scope = null;
18-
var level = 0;
19-
var position;
11+
var nodes = []
12+
var start = null
13+
var scope = null
14+
var level = 0
15+
var position
2016

21-
visit(node, gather);
17+
visit(node, gather)
2218

2319
/* Gather one dimensional zones. */
2420
function gather(node, index, parent) {
25-
var type = test(node);
21+
var type = test(node)
2622

2723
if (scope && parent === scope) {
2824
if (type === 'start') {
29-
level++;
25+
level++
3026
}
3127

3228
if (type === 'end') {
33-
level--;
29+
level--
3430
}
3531

3632
if (type === 'end' && level === 0) {
3733
nodes = callback(start, nodes, node, {
3834
start: index - nodes.length - 1,
3935
end: index,
4036
parent: scope
41-
});
37+
})
4238

4339
if (nodes) {
44-
splice.apply(scope.children, [position, index - position + 1].concat(nodes));
40+
splice.apply(
41+
scope.children,
42+
[position, index - position + 1].concat(nodes)
43+
)
4544
}
4645

47-
start = null;
48-
scope = null;
49-
position = null;
50-
nodes = [];
46+
start = null
47+
scope = null
48+
position = null
49+
nodes = []
5150
} else {
52-
nodes.push(node);
51+
nodes.push(node)
5352
}
5453
}
5554

5655
if (!scope && type === 'start') {
57-
level = 1;
58-
position = index;
59-
start = node;
60-
scope = parent;
56+
level = 1
57+
position = index
58+
start = node
59+
scope = parent
6160
}
6261
}
6362

6463
/* Test if `node` matches the bound settings. */
6564
function test(node) {
66-
var marker = commentMarker(node);
67-
var attributes;
68-
var head;
65+
var marker = commentMarker(node)
66+
var attributes
67+
var head
6968

7069
if (!marker || marker.name !== name) {
71-
return null;
70+
return null
7271
}
7372

74-
attributes = marker.attributes;
75-
head = attributes.match(/(start|end)\b/);
73+
attributes = marker.attributes
74+
head = attributes.match(/(start|end)\b/)
7675

77-
return head ? head[0] : null;
76+
return head ? head[0] : null
7877
}
7978
}

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"is-hidden": "^1.0.1",
3131
"negate": "^1.0.0",
3232
"nyc": "^12.0.0",
33+
"prettier": "^1.14.2",
3334
"remark": "^9.0.0",
3435
"remark-cli": "^5.0.0",
3536
"remark-preset-wooorm": "^4.0.0",
@@ -38,23 +39,30 @@
3839
"xo": "^0.22.0"
3940
},
4041
"scripts": {
41-
"build-md": "remark . -qfo",
42+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
4243
"build-bundle": "browserify . -s mdastZone > mdast-zone.js",
4344
"build-mangle": "browserify . -s mdastZone -p tinyify > mdast-zone.min.js",
44-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
45-
"lint": "xo",
46-
"test-api": "node test/index.js",
45+
"build": "npm run build-bundle && npm run build-mangle",
46+
"test-api": "node test",
4747
"test-coverage": "nyc --reporter lcov tape test/index.js",
48-
"test": "npm run build && npm run lint && npm run test-coverage"
48+
"test": "npm run format && npm run build && npm run test-coverage"
4949
},
5050
"nyc": {
5151
"check-coverage": true,
5252
"lines": 100,
5353
"functions": 100,
5454
"branches": 100
5555
},
56+
"prettier": {
57+
"tabWidth": 2,
58+
"useTabs": false,
59+
"singleQuote": true,
60+
"bracketSpacing": false,
61+
"semi": false,
62+
"trailingComma": "none"
63+
},
5664
"xo": {
57-
"space": true,
65+
"prettier": true,
5866
"esnext": false,
5967
"ignores": [
6068
"mdast-zone.js"

readme.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@ Foo
2626
And our script, `example.js`, looks as follows:
2727

2828
```javascript
29-
var vfile = require('to-vfile');
30-
var remark = require('remark');
31-
var zone = require('mdast-zone');
29+
var vfile = require('to-vfile')
30+
var remark = require('remark')
31+
var zone = require('mdast-zone')
3232

3333
remark()
3434
.use(plugin)
35-
.process(vfile.readSync('example.md'), function (err, file) {
36-
if (err) throw err;
37-
console.log(String(file));
38-
});
35+
.process(vfile.readSync('example.md'), function(err, file) {
36+
if (err) throw err
37+
console.log(String(file))
38+
})
3939

4040
function plugin() {
41-
return transformer;
42-
function transformer(tree) {
43-
zone(tree, 'foo', mutate);
41+
return transform
42+
43+
function transform(tree) {
44+
zone(tree, 'foo', mutate)
4445
}
46+
4547
function mutate(start, nodes, end) {
4648
return [
4749
start,
4850
{type: 'paragraph', children: [{type: 'text', value: 'Bar'}]},
4951
end
50-
];
52+
]
5153
}
5254
}
5355
```

test/fixtures.js

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

33
/* eslint-disable import/no-dynamic-require */
44

5-
var fs = require('fs');
6-
var path = require('path');
7-
var negate = require('negate');
8-
var hidden = require('is-hidden');
5+
var fs = require('fs')
6+
var path = require('path')
7+
var negate = require('negate')
8+
var hidden = require('is-hidden')
99

10-
var root = path.join(__dirname, 'fixtures');
10+
var root = path.join(__dirname, 'fixtures')
1111

12-
module.exports = fs.readdirSync(root)
12+
module.exports = fs
13+
.readdirSync(root)
1314
.filter(negate(hidden))
14-
.map(function (basename) {
15-
var output = path.join(root, basename, 'output.md');
15+
.map(map)
1616

17-
return {
18-
name: basename,
19-
input: fs.readFileSync(path.join(root, basename, 'input.md'), 'utf8'),
20-
output: fs.existsSync(output) ? fs.readFileSync(output, 'utf8') : null,
21-
test: require(path.join(root, basename, 'index.js'))
22-
};
23-
});
17+
function map(basename) {
18+
var output = null
19+
20+
try {
21+
output = fs.readFileSync(path.join(root, basename, 'output.md'))
22+
} catch (err) {}
23+
24+
return {
25+
name: basename,
26+
input: fs.readFileSync(path.join(root, basename, 'input.md')),
27+
output: output,
28+
test: require(path.join(root, basename))
29+
}
30+
}

test/fixtures/mismatched/index.js

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

3-
module.exports = assertion;
3+
module.exports = assertion
44

55
function assertion(t, zone, tree) {
6-
zone(tree, 'foo', handle);
6+
zone(tree, 'foo', handle)
77

88
function handle() {
9-
throw new Error('Should not be invoked');
9+
throw new Error('Should not be invoked')
1010
}
1111
}

test/fixtures/nodes/index.js

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

3-
module.exports = assertion;
3+
module.exports = assertion
44

55
function assertion(t, zone, tree) {
6-
var count = 0;
6+
var count = 0
77

8-
zone(tree, 'foo', handle);
8+
t.test('nodes', function(st) {
9+
st.plan(6)
910

10-
function handle(start, nodes) {
11-
t.test('range-children', function (st) {
12-
st.equal(nodes.length, 1);
13-
st.equal(nodes[0].type, 'paragraph');
14-
st.equal(nodes[0].children.length, 1);
15-
st.equal(nodes[0].children[0].type, 'text');
16-
st.equal(nodes[0].children[0].value, 'Foo.');
11+
zone(tree, 'foo', handle)
1712

18-
count++;
19-
st.equal(count, 1);
20-
21-
st.end();
22-
});
23-
}
13+
function handle(start, nodes) {
14+
st.equal(nodes.length, 1)
15+
st.equal(nodes[0].type, 'paragraph')
16+
st.equal(nodes[0].children.length, 1)
17+
st.equal(nodes[0].children[0].type, 'text')
18+
st.equal(nodes[0].children[0].value, 'Foo.')
19+
st.equal(++count, 1)
20+
}
21+
})
2422
}

test/fixtures/non-marker/index.js

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

3-
module.exports = assertion;
3+
module.exports = assertion
44

55
function assertion(t, zone, tree) {
6-
zone(tree, 'foo', handle);
6+
zone(tree, 'foo', handle)
77

88
function handle() {
9-
throw new Error('Should not be invoked');
9+
throw new Error('Should not be invoked')
1010
}
1111
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
module.exports = assertion
4+
5+
function assertion(t, zone, tree) {
6+
var count = 0
7+
8+
t.test('range-children', function(st) {
9+
st.plan(3)
10+
11+
zone(tree, 'foo', handle)
12+
13+
function handle(start, nodes) {
14+
st.equal(nodes.length, 1)
15+
st.equal(nodes[0].type, 'blockquote')
16+
st.equal(++count, 1)
17+
}
18+
})
19+
}
File renamed without changes.

0 commit comments

Comments
 (0)