Skip to content

Commit b620bfc

Browse files
committed
Refactor code-style
1 parent 6d49736 commit b620bfc

File tree

11 files changed

+330
-281
lines changed

11 files changed

+330
-281
lines changed

.prettierignore

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

index.js

Lines changed: 81 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1-
'use strict';
2-
3-
var xtend = require('xtend');
4-
var toH = require('hast-to-hyperscript');
5-
var NS = require('web-namespaces');
6-
var zwitch = require('zwitch');
7-
var mapz = require('mapz');
8-
9-
module.exports = transform;
10-
11-
var own = {}.hasOwnProperty;
12-
var one = zwitch('type');
13-
var all = mapz(one, {key: 'children', indices: false});
14-
15-
var customProps = ['__location', 'childNodes', 'content', 'parentNode', 'namespaceURI'];
16-
17-
one.handlers.root = root;
18-
one.handlers.element = element;
19-
one.handlers.text = text;
20-
one.handlers.comment = comment;
21-
one.handlers.doctype = doctype;
1+
'use strict'
2+
3+
var xtend = require('xtend')
4+
var toH = require('hast-to-hyperscript')
5+
var NS = require('web-namespaces')
6+
var zwitch = require('zwitch')
7+
var mapz = require('mapz')
8+
9+
module.exports = transform
10+
11+
var own = {}.hasOwnProperty
12+
var one = zwitch('type')
13+
var all = mapz(one, {key: 'children', indices: false})
14+
15+
var customProps = [
16+
'__location',
17+
'childNodes',
18+
'content',
19+
'parentNode',
20+
'namespaceURI'
21+
]
22+
23+
one.handlers.root = root
24+
one.handlers.element = element
25+
one.handlers.text = text
26+
one.handlers.comment = comment
27+
one.handlers.doctype = doctype
2228

2329
/* Map of tag-names starting new namespaces. */
2430
var namespaces = {
2531
math: NS.mathml,
2632
svg: NS.svg
27-
};
33+
}
2834

2935
/* Map of attributes with namespaces. */
3036
var attributeSpaces = {
@@ -40,58 +46,62 @@ var attributeSpaces = {
4046
'xml:space': {prefix: 'xml', name: 'space', namespace: NS.xml},
4147
xmlns: {prefix: '', name: 'xmlns', namespace: NS.xmlns},
4248
'xmlns:xlink': {prefix: 'xmlns', name: 'xlink', namespace: NS.xmlns}
43-
};
49+
}
4450

4551
/* Transform a tree from HAST to Parse5’s AST. */
4652
function transform(tree) {
47-
return patch(one(tree), null, NS.html);
53+
return patch(one(tree), null, NS.html)
4854
}
4955

5056
function root(node) {
51-
var data = node.data || {};
52-
var qs = own.call(data, 'quirksMode') ? Boolean(data.quirksMode) : false;
57+
var data = node.data || {}
58+
var qs = own.call(data, 'quirksMode') ? Boolean(data.quirksMode) : false
5359

5460
return {
5561
nodeName: '#document',
5662
mode: qs ? 'quirks' : 'no-quirks',
5763
childNodes: all(node)
58-
};
64+
}
5965
}
6066

6167
function element(node) {
62-
var shallow = xtend(node);
68+
var shallow = xtend(node)
6369

64-
shallow.children = [];
70+
shallow.children = []
6571

66-
return toH(function (name, attrs) {
67-
var values = [];
68-
var content;
69-
var value;
70-
var key;
72+
return toH(function(name, attrs) {
73+
var values = []
74+
var content
75+
var value
76+
var key
7177

7278
for (key in attrs) {
73-
value = {name: key, value: attrs[key]};
79+
value = {name: key, value: attrs[key]}
7480

7581
if (own.call(attributeSpaces, key)) {
76-
value = xtend(value, attributeSpaces[key]);
82+
value = xtend(value, attributeSpaces[key])
7783
}
7884

79-
values.push(value);
85+
values.push(value)
8086
}
8187

8288
if (name === 'template') {
83-
content = transform(shallow.content);
84-
delete content.mode;
85-
content.nodeName = '#document-fragment';
89+
content = transform(shallow.content)
90+
delete content.mode
91+
content.nodeName = '#document-fragment'
8692
}
8793

88-
return wrap(node, {
89-
nodeName: node.tagName,
90-
tagName: node.tagName,
91-
attrs: values,
92-
childNodes: node.children ? all(node) : []
93-
}, content);
94-
}, shallow);
94+
return wrap(
95+
node,
96+
{
97+
nodeName: node.tagName,
98+
tagName: node.tagName,
99+
attrs: values,
100+
childNodes: node.children ? all(node) : []
101+
},
102+
content
103+
)
104+
}, shallow)
95105
}
96106

97107
function doctype(node) {
@@ -100,21 +110,21 @@ function doctype(node) {
100110
name: node.name,
101111
publicId: node.public || null,
102112
systemId: node.system || null
103-
});
113+
})
104114
}
105115

106116
function text(node) {
107117
return wrap(node, {
108118
nodeName: '#text',
109119
value: node.value
110-
});
120+
})
111121
}
112122

113123
function comment(node) {
114124
return wrap(node, {
115125
nodeName: '#comment',
116126
data: node.value
117-
});
127+
})
118128
}
119129

120130
/* Patch position. */
@@ -125,62 +135,62 @@ function wrap(node, ast, content) {
125135
col: node.position.start.column,
126136
startOffset: node.position.start.offset,
127137
endOffset: node.position.end.offset
128-
};
138+
}
129139
}
130140

131141
if (content) {
132-
ast.content = content;
142+
ast.content = content
133143
}
134144

135-
return ast;
145+
return ast
136146
}
137147

138148
/* Patch a tree recursively, by adding namespaces
139149
* and parent references where needed. */
140150
function patch(node, parent, ns) {
141-
var location = node.__location;
142-
var children = node.childNodes;
143-
var name = node.tagName;
144-
var replacement = {};
145-
var length;
146-
var index;
147-
var key;
151+
var location = node.__location
152+
var children = node.childNodes
153+
var name = node.tagName
154+
var replacement = {}
155+
var length
156+
var index
157+
var key
148158

149159
for (key in node) {
150160
if (customProps.indexOf(key) === -1) {
151-
replacement[key] = node[key];
161+
replacement[key] = node[key]
152162
}
153163
}
154164

155165
if (own.call(namespaces, name)) {
156-
ns = namespaces[name];
166+
ns = namespaces[name]
157167
}
158168

159169
if (own.call(replacement, 'tagName')) {
160-
replacement.namespaceURI = ns;
170+
replacement.namespaceURI = ns
161171
}
162172

163173
if (children) {
164-
replacement.childNodes = children;
165-
length = children.length;
166-
index = -1;
174+
replacement.childNodes = children
175+
length = children.length
176+
index = -1
167177

168178
while (++index < length) {
169-
children[index] = patch(children[index], replacement, ns);
179+
children[index] = patch(children[index], replacement, ns)
170180
}
171181
}
172182

173183
if (name === 'template') {
174-
replacement.content = patch(node.content, null, ns);
184+
replacement.content = patch(node.content, null, ns)
175185
}
176186

177187
if (parent) {
178-
replacement.parentNode = parent;
188+
replacement.parentNode = parent
179189
}
180190

181191
if (location) {
182-
replacement.__location = location;
192+
replacement.__location = location
183193
}
184194

185-
return replacement;
195+
return replacement
186196
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,31 @@
3030
"esmangle": "^1.0.1",
3131
"nyc": "^12.0.0",
3232
"parse5": "^3.0.0",
33+
"prettier": "^1.13.5",
3334
"remark-cli": "^5.0.0",
3435
"remark-preset-wooorm": "^4.0.0",
3536
"tape": "^4.0.0",
3637
"xo": "^0.21.0"
3738
},
3839
"scripts": {
39-
"build-md": "remark . --quiet --frail --output",
40+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
4041
"build-bundle": "browserify index.js --bare -s hastUtilToParse5 > hast-util-to-parse5.js",
4142
"build-mangle": "esmangle hast-util-to-parse5.js > hast-util-to-parse5.min.js",
42-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
43-
"lint": "xo",
43+
"build": "npm run build-bundle && npm run build-mangle",
4444
"test-api": "node test",
4545
"test-coverage": "nyc --reporter lcov tape test",
46-
"test": "npm run build && npm run lint && npm run test-coverage"
46+
"test": "npm run format && npm run build && npm run test-coverage"
47+
},
48+
"prettier": {
49+
"tabWidth": 2,
50+
"useTabs": false,
51+
"singleQuote": true,
52+
"bracketSpacing": false,
53+
"semi": false,
54+
"trailingComma": "none"
4755
},
4856
"xo": {
49-
"space": true,
57+
"prettier": true,
5058
"esnext": false,
5159
"rules": {
5260
"guard-for-in": "off"

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ npm install hast-util-to-parse5
1616
## Usage
1717

1818
```javascript
19-
var toParse5 = require('hast-util-to-parse5');
19+
var toParse5 = require('hast-util-to-parse5')
2020

2121
var ast = toParse5({
2222
type: 'element',
2323
tagName: 'h1',
2424
properties: {},
2525
children: [{type: 'text', value: 'World!'}]
26-
});
26+
})
2727

28-
console.log(ast);
28+
console.log(ast)
2929
```
3030

3131
Yields:

test/comment.js

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

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

7-
test('comment', function (t) {
8-
var node = parse5.parseFragment('<!--Alpha-->');
7+
test('comment', function(t) {
8+
var node = parse5.parseFragment('<!--Alpha-->')
99

10-
node = node.childNodes[0];
11-
delete node.parentNode;
10+
node = node.childNodes[0]
11+
delete node.parentNode
1212

1313
t.deepEqual(
1414
toParse5({
@@ -17,7 +17,7 @@ test('comment', function (t) {
1717
}),
1818
node,
1919
'should transform comments'
20-
);
20+
)
2121

22-
t.end();
23-
});
22+
t.end()
23+
})

0 commit comments

Comments
 (0)