Skip to content

Commit 9b73be3

Browse files
committed
Refactor code-style
1 parent 0343f11 commit 9b73be3

File tree

5 files changed

+229
-216
lines changed

5 files changed

+229
-216
lines changed

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
hast-util-to-nlcst.js
3+
hast-util-to-nlcst.min.js

index.js

+100-100
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
'use strict';
1+
'use strict'
22

3-
var vfileLocation = require('vfile-location');
4-
var toString = require('nlcst-to-string');
5-
var position = require('unist-util-position');
6-
var phrasing = require('hast-util-phrasing');
7-
var embedded = require('hast-util-embedded');
8-
var whitespace = require('hast-util-whitespace');
9-
var textContent = require('hast-util-to-string');
10-
var is = require('hast-util-is-element');
3+
var vfileLocation = require('vfile-location')
4+
var toString = require('nlcst-to-string')
5+
var position = require('unist-util-position')
6+
var phrasing = require('hast-util-phrasing')
7+
var embedded = require('hast-util-embedded')
8+
var whitespace = require('hast-util-whitespace')
9+
var textContent = require('hast-util-to-string')
10+
var is = require('hast-util-is-element')
1111

12-
module.exports = toNLCST;
12+
module.exports = toNLCST
1313

1414
/* Elements representing source. */
15-
var SOURCE = ['code'];
16-
var IGNORE = ['script', 'style', 'svg', 'math', 'del'];
17-
var EXPLICIT = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
15+
var SOURCE = ['code']
16+
var IGNORE = ['script', 'style', 'svg', 'math', 'del']
17+
var EXPLICIT = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']
1818

1919
/* Constants. */
2020
var FLOW_ACCEPTING = [
@@ -41,43 +41,43 @@ var FLOW_ACCEPTING = [
4141
'fieldset',
4242
'details',
4343
'dialog'
44-
];
44+
]
4545

4646
/* Transform `tree` to `nlcst`. */
4747
function toNLCST(tree, file, Parser) {
48-
var parser;
49-
var location;
50-
var results;
51-
var doc;
48+
var parser
49+
var location
50+
var results
51+
var doc
5252

5353
/* Warn for invalid parameters. */
5454
if (!tree || !tree.type) {
55-
throw new Error('hast-util-to-nlcst expected node');
55+
throw new Error('hast-util-to-nlcst expected node')
5656
}
5757

5858
if (!file || !file.messages) {
59-
throw new Error('hast-util-to-nlcst expected file');
59+
throw new Error('hast-util-to-nlcst expected file')
6060
}
6161

6262
/* Construct parser. */
6363
if (!Parser) {
64-
throw new Error('hast-util-to-nlcst expected parser');
64+
throw new Error('hast-util-to-nlcst expected parser')
6565
}
6666

6767
if (!position.start(tree).line || !position.start(tree).column) {
68-
throw new Error('hast-util-to-nlcst expected position on nodes');
68+
throw new Error('hast-util-to-nlcst expected position on nodes')
6969
}
7070

71-
location = vfileLocation(file);
72-
doc = String(file);
73-
parser = 'parse' in Parser ? Parser : new Parser();
71+
location = vfileLocation(file)
72+
doc = String(file)
73+
parser = 'parse' in Parser ? Parser : new Parser()
7474

7575
/* Transform HAST into NLCST tokens, and pass these
7676
* into `parser.parse` to insert sentences, paragraphs
7777
* where needed. */
78-
results = [];
78+
results = []
7979

80-
find(tree);
80+
find(tree)
8181

8282
return {
8383
type: 'RootNode',
@@ -86,146 +86,146 @@ function toNLCST(tree, file, Parser) {
8686
start: location.toPosition(0),
8787
end: location.toPosition(doc.length)
8888
}
89-
};
89+
}
9090

9191
function find(node) {
92-
var children = node.children;
92+
var children = node.children
9393

9494
if (node.type === 'root') {
95-
findAll(children);
95+
findAll(children)
9696
} else if (is(node) && !ignored(node)) {
9797
/* Explicit paragraph. */
9898
if (is(node, EXPLICIT)) {
99-
add(node);
100-
/* Slightly simplified version of:
99+
add(node)
100+
/* Slightly simplified version of:
101101
* https://html.spec.whatwg.org/#paragraphs */
102102
} else if (is(node, FLOW_ACCEPTING)) {
103-
implicit(flattenAll(children));
104-
/* Dig deeper. */
103+
implicit(flattenAll(children))
104+
/* Dig deeper. */
105105
} else {
106-
findAll(children);
106+
findAll(children)
107107
}
108108
}
109109
}
110110

111111
function findAll(children) {
112-
var length = children.length;
113-
var index = -1;
112+
var length = children.length
113+
var index = -1
114114

115115
while (++index < length) {
116-
find(children[index]);
116+
find(children[index])
117117
}
118118
}
119119

120120
function flatten(node) {
121121
if (is(node, ['a', 'ins', 'del', 'map'])) {
122-
return flattenAll(node.children);
122+
return flattenAll(node.children)
123123
}
124124

125-
return node;
125+
return node
126126
}
127127

128128
function flattenAll(children) {
129-
var results = [];
130-
var length = children.length;
131-
var index = -1;
129+
var results = []
130+
var length = children.length
131+
var index = -1
132132

133133
while (++index < length) {
134-
results = results.concat(flatten(children[index]));
134+
results = results.concat(flatten(children[index]))
135135
}
136136

137-
return results;
137+
return results
138138
}
139139

140140
function add(node) {
141-
var result = ('length' in node ? all : one)(node);
141+
var result = ('length' in node ? all : one)(node)
142142

143143
if (result.length !== 0) {
144-
results.push(parser.tokenizeParagraph(result));
144+
results.push(parser.tokenizeParagraph(result))
145145
}
146146
}
147147

148148
function implicit(children) {
149-
var length = children.length + 1;
150-
var index = -1;
151-
var viable = false;
152-
var start = -1;
153-
var child;
149+
var length = children.length + 1
150+
var index = -1
151+
var viable = false
152+
var start = -1
153+
var child
154154

155155
while (++index < length) {
156-
child = children[index];
156+
child = children[index]
157157

158158
if (child && phrasing(child)) {
159159
if (start === -1) {
160-
start = index;
160+
start = index
161161
}
162162

163163
if (!viable && !embedded(child) && !whitespace(child)) {
164-
viable = true;
164+
viable = true
165165
}
166166
} else if (child && start === -1) {
167-
find(child);
167+
find(child)
168168
} else {
169-
(viable ? add : findAll)(children.slice(start, index));
169+
;(viable ? add : findAll)(children.slice(start, index))
170170

171171
if (child) {
172-
find(child);
172+
find(child)
173173
}
174174

175-
viable = false;
176-
start = -1;
175+
viable = false
176+
start = -1
177177
}
178178
}
179179
}
180180

181181
/* Convert `node` (HAST) to NLCST. */
182182
function one(node) {
183-
var type = node.type;
184-
var tagName = type === 'element' ? node.tagName : null;
185-
var change;
186-
var replacement;
183+
var type = node.type
184+
var tagName = type === 'element' ? node.tagName : null
185+
var change
186+
var replacement
187187

188188
if (type === 'text') {
189-
change = true;
190-
replacement = parser.tokenize(node.value);
189+
change = true
190+
replacement = parser.tokenize(node.value)
191191
} else if (tagName === 'wbr') {
192-
change = true;
193-
replacement = [parser.tokenizeWhiteSpace(' ')];
192+
change = true
193+
replacement = [parser.tokenizeWhiteSpace(' ')]
194194
} else if (tagName === 'br') {
195-
change = true;
196-
replacement = [parser.tokenizeWhiteSpace('\n')];
195+
change = true
196+
replacement = [parser.tokenizeWhiteSpace('\n')]
197197
} else if (sourced(node)) {
198-
change = true;
199-
replacement = [parser.tokenizeSource(textContent(node))];
198+
change = true
199+
replacement = [parser.tokenizeSource(textContent(node))]
200200
} else if (type === 'root' || !ignored(node)) {
201-
replacement = all(node.children);
201+
replacement = all(node.children)
202202
} else {
203-
return;
203+
return
204204
}
205205

206206
if (!change) {
207-
return replacement;
207+
return replacement
208208
}
209209

210-
return patch(replacement, location, location.toOffset(position.start(node)));
210+
return patch(replacement, location, location.toOffset(position.start(node)))
211211
}
212212

213213
/* Convert all `children` (HAST) to NLCST. */
214214
function all(children) {
215-
var length = children && children.length;
216-
var index = -1;
217-
var result = [];
218-
var child;
215+
var length = children && children.length
216+
var index = -1
217+
var result = []
218+
var child
219219

220220
while (++index < length) {
221-
child = one(children[index]);
221+
child = one(children[index])
222222

223223
if (child) {
224-
result = result.concat(child);
224+
result = result.concat(child)
225225
}
226226
}
227227

228-
return result;
228+
return result
229229
}
230230

231231
/* Patch a position on each node in `nodes`.
@@ -236,41 +236,41 @@ function toNLCST(tree, file, Parser) {
236236
* starting and ending positions can be inferred from their
237237
* content. */
238238
function patch(nodes, location, offset) {
239-
var length = nodes.length;
240-
var index = -1;
241-
var start = offset;
242-
var children;
243-
var node;
244-
var end;
239+
var length = nodes.length
240+
var index = -1
241+
var start = offset
242+
var children
243+
var node
244+
var end
245245

246246
while (++index < length) {
247-
node = nodes[index];
248-
children = node.children;
247+
node = nodes[index]
248+
children = node.children
249249

250250
if (children) {
251-
patch(children, location, start);
251+
patch(children, location, start)
252252
}
253253

254-
end = start + toString(node).length;
254+
end = start + toString(node).length
255255

256256
node.position = {
257257
start: location.toPosition(start),
258258
end: location.toPosition(end)
259-
};
259+
}
260260

261-
start = end;
261+
start = end
262262
}
263263

264-
return nodes;
264+
return nodes
265265
}
266266
}
267267

268268
function sourced(node) {
269-
var props = node.properties;
270-
return is(node) && (is(node, SOURCE) || props.dataNlcst === 'source');
269+
var props = node.properties
270+
return is(node) && (is(node, SOURCE) || props.dataNlcst === 'source')
271271
}
272272

273273
function ignored(node) {
274-
var props = node.properties;
275-
return is(node) && (is(node, IGNORE) || props.dataNlcst === 'ignore');
274+
var props = node.properties
275+
return is(node) && (is(node, IGNORE) || props.dataNlcst === 'ignore')
276276
}

0 commit comments

Comments
 (0)