Skip to content

Commit 151272d

Browse files
committed
Refactor code-style
1 parent 1e55a6d commit 151272d

File tree

5 files changed

+294
-242
lines changed

5 files changed

+294
-242
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-from-parse5.js
3+
hast-util-from-parse5.min.js

index.js

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

3-
var information = require('property-information');
4-
var camelcase = require('camelcase');
5-
var h = require('hastscript');
6-
var xtend = require('xtend');
7-
var count = require('ccount');
3+
var information = require('property-information')
4+
var camelcase = require('camelcase')
5+
var h = require('hastscript')
6+
var xtend = require('xtend')
7+
var count = require('ccount')
88

9-
module.exports = wrapper;
9+
module.exports = wrapper
1010

11-
var own = {}.hasOwnProperty;
11+
var own = {}.hasOwnProperty
1212

1313
/* Handlers. */
1414
var map = {
@@ -17,83 +17,87 @@ var map = {
1717
'#text': text,
1818
'#comment': comment,
1919
'#documentType': doctype
20-
};
20+
}
2121

2222
/* Wrapper to normalise options. */
2323
function wrapper(ast, options) {
24-
var settings = options || {};
25-
var file;
24+
var settings = options || {}
25+
var file
2626

2727
if (settings.messages) {
28-
file = settings;
29-
settings = {};
28+
file = settings
29+
settings = {}
3030
} else {
31-
file = settings.file;
31+
file = settings.file
3232
}
3333

3434
return transform(ast, {
3535
file: file,
3636
verbose: settings.verbose,
3737
location: false
38-
});
38+
})
3939
}
4040

4141
/* Transform a node. */
4242
function transform(ast, config) {
43-
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element;
44-
var children;
45-
var node;
46-
var pos;
43+
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
44+
var children
45+
var node
46+
var pos
4747

4848
if (ast.childNodes) {
49-
children = nodes(ast.childNodes, config);
49+
children = nodes(ast.childNodes, config)
5050
}
5151

52-
node = fn(ast, children, config);
52+
node = fn(ast, children, config)
5353

5454
if (ast.sourceCodeLocation && config.file) {
55-
pos = location(node, ast.sourceCodeLocation, config.verbose);
55+
pos = location(node, ast.sourceCodeLocation, config.verbose)
5656

5757
if (pos) {
58-
config.location = true;
59-
node.position = pos;
58+
config.location = true
59+
node.position = pos
6060
}
6161
}
6262

63-
return node;
63+
return node
6464
}
6565

6666
/* Transform children. */
6767
function nodes(children, config) {
68-
var length = children.length;
69-
var index = -1;
70-
var result = [];
68+
var length = children.length
69+
var index = -1
70+
var result = []
7171

7272
while (++index < length) {
73-
result[index] = transform(children[index], config);
73+
result[index] = transform(children[index], config)
7474
}
7575

76-
return result;
76+
return result
7777
}
7878

7979
/* Transform a document.
8080
* Stores `ast.quirksMode` in `node.data.quirksMode`. */
8181
function root(ast, children, config) {
82-
var node = {type: 'root', children: children, data: {}};
83-
var doc;
82+
var node = {type: 'root', children: children, data: {}}
83+
var doc
8484

85-
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks';
85+
node.data.quirksMode = ast.mode === 'quirks' || ast.mode === 'limited-quirks'
8686

8787
if (config.file && config.location) {
88-
doc = String(config.file);
88+
doc = String(config.file)
8989

9090
node.position = {
9191
start: {line: 1, column: 1, offset: 0},
92-
end: {line: count(doc, '\n') + 1, column: doc.length - doc.lastIndexOf('\n'), offset: doc.length}
93-
};
92+
end: {
93+
line: count(doc, '\n') + 1,
94+
column: doc.length - doc.lastIndexOf('\n'),
95+
offset: doc.length
96+
}
97+
}
9498
}
9599

96-
return node;
100+
return node
97101
}
98102

99103
/* Transform a doctype. */
@@ -103,77 +107,82 @@ function doctype(ast) {
103107
name: ast.name || '',
104108
public: ast.publicId || null,
105109
system: ast.systemId || null
106-
};
110+
}
107111
}
108112

109113
/* Transform a text. */
110114
function text(ast) {
111-
return {type: 'text', value: ast.value};
115+
return {type: 'text', value: ast.value}
112116
}
113117

114118
/* Transform a comment. */
115119
function comment(ast) {
116-
return {type: 'comment', value: ast.data};
120+
return {type: 'comment', value: ast.data}
117121
}
118122

119123
/* Transform an element. */
120124
function element(ast, children, config) {
121-
var props = {};
122-
var values = ast.attrs;
123-
var length = values.length;
124-
var index = -1;
125-
var attr;
126-
var node;
127-
var pos;
128-
var start;
129-
var end;
125+
var props = {}
126+
var values = ast.attrs
127+
var length = values.length
128+
var index = -1
129+
var attr
130+
var node
131+
var pos
132+
var start
133+
var end
130134

131135
while (++index < length) {
132-
attr = values[index];
133-
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value;
136+
attr = values[index]
137+
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value
134138
}
135139

136-
node = h(ast.tagName, props, children);
140+
node = h(ast.tagName, props, children)
137141

138142
if (ast.nodeName === 'template' && 'content' in ast) {
139-
pos = ast.sourceCodeLocation;
140-
start = pos && pos.startTag && position(pos.startTag).end;
141-
end = pos && pos.endTag && position(pos.endTag).start;
143+
pos = ast.sourceCodeLocation
144+
start = pos && pos.startTag && position(pos.startTag).end
145+
end = pos && pos.endTag && position(pos.endTag).start
142146

143-
node.content = transform(ast.content, config);
147+
node.content = transform(ast.content, config)
144148

145149
if ((start || end) && config.file) {
146-
node.content.position = {start: start, end: end};
150+
node.content.position = {start: start, end: end}
147151
}
148152
}
149153

150-
return node;
154+
return node
151155
}
152156

153157
/* Create clean positional information. */
154158
function location(node, location, verbose) {
155-
var pos = position(location);
156-
var reference;
157-
var values;
158-
var props;
159-
var prop;
160-
var name;
159+
var pos = position(location)
160+
var reference
161+
var values
162+
var props
163+
var prop
164+
var name
161165

162166
if (node.type === 'element') {
163-
reference = node.children[node.children.length - 1];
167+
reference = node.children[node.children.length - 1]
164168

165169
/* Unclosed with children (upstream: https://github.com/inikulin/parse5/issues/109) */
166-
if (!location.endTag && reference && reference.position && reference.position.end) {
167-
pos.end = xtend(reference.position.end);
170+
if (
171+
!location.endTag &&
172+
reference &&
173+
reference.position &&
174+
reference.position.end
175+
) {
176+
pos.end = xtend(reference.position.end)
168177
}
169178

170179
if (verbose) {
171-
values = location.attrs;
172-
props = {};
180+
values = location.attrs
181+
props = {}
173182

174183
for (prop in values) {
175-
name = (information(prop) || {}).propertyName || camelcase(prop);
176-
props[name] = position(values[prop]);
184+
name = (information(prop) || {}).propertyName || camelcase(prop)
185+
props[name] = position(values[prop])
177186
}
178187

179188
node.data = {
@@ -182,19 +191,27 @@ function location(node, location, verbose) {
182191
closing: location.endTag ? position(location.endTag) : null,
183192
properties: props
184193
}
185-
};
194+
}
186195
}
187196
}
188197

189-
return pos;
198+
return pos
190199
}
191200

192201
function position(loc) {
193-
var start = point({line: loc.startLine, column: loc.startCol, offset: loc.startOffset});
194-
var end = point({line: loc.endLine, column: loc.endCol, offset: loc.endOffset});
195-
return start || end ? {start: start, end: end} : null;
202+
var start = point({
203+
line: loc.startLine,
204+
column: loc.startCol,
205+
offset: loc.startOffset
206+
})
207+
var end = point({
208+
line: loc.endLine,
209+
column: loc.endCol,
210+
offset: loc.endOffset
211+
})
212+
return start || end ? {start: start, end: end} : null
196213
}
197214

198215
function point(point) {
199-
return point.line && point.column ? point : null;
216+
return point.line && point.column ? point : null
200217
}

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"not": "^0.1.0",
3636
"nyc": "^12.0.0",
3737
"parse5": "^5.0.0",
38+
"prettier": "^1.13.5",
3839
"remark-cli": "^5.0.0",
3940
"remark-preset-wooorm": "^4.0.0",
4041
"tape": "^4.0.0",
@@ -43,17 +44,24 @@
4344
"xo": "^0.21.0"
4445
},
4546
"scripts": {
46-
"build-md": "remark . -qfo",
47+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
4748
"build-bundle": "browserify index.js --bare -s hastUtilFromParse5 -g [ babelify --presets [ \"babel-preset-env\" ] ] > hast-util-from-parse5.js",
4849
"build-mangle": "esmangle hast-util-from-parse5.js > hast-util-from-parse5.min.js",
49-
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
50-
"lint": "xo",
50+
"build": "npm run build-bundle && npm run build-mangle",
5151
"test-api": "node test",
5252
"test-coverage": "nyc --reporter lcov tape test",
53-
"test": "npm run build && npm run lint && npm run test-coverage"
53+
"test": "npm run format && npm run build && npm run test-coverage"
54+
},
55+
"prettier": {
56+
"tabWidth": 2,
57+
"useTabs": false,
58+
"singleQuote": true,
59+
"bracketSpacing": false,
60+
"semi": false,
61+
"trailingComma": "none"
5462
},
5563
"xo": {
56-
"space": true,
64+
"prettier": true,
5765
"esnext": false,
5866
"rules": {
5967
"guard-for-in": "off"

readme.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ Say we have the following file, `example.html`:
2121
And our script, `example.js`, looks as follows:
2222

2323
```javascript
24-
var vfile = require('to-vfile');
25-
var parse5 = require('parse5');
26-
var inspect = require('unist-util-inspect');
27-
var fromParse5 = require('hast-util-from-parse5');
24+
var vfile = require('to-vfile')
25+
var parse5 = require('parse5')
26+
var inspect = require('unist-util-inspect')
27+
var fromParse5 = require('hast-util-from-parse5')
2828

29-
var doc = vfile.readSync('example.html');
30-
var ast = parse5.parse(String(doc), {sourceCodeLocationInfo: true});
31-
var hast = fromParse5(ast, doc);
29+
var doc = vfile.readSync('example.html')
30+
var ast = parse5.parse(String(doc), {sourceCodeLocationInfo: true})
31+
var hast = fromParse5(ast, doc)
3232

33-
console.log(inspect(hast));
33+
console.log(inspect(hast))
3434
```
3535

3636
Now, running `node example` yields:

0 commit comments

Comments
 (0)