Skip to content

Commit 2cf8a4a

Browse files
committed
Refactor code-style
1 parent 309f2a4 commit 2cf8a4a

File tree

3 files changed

+82
-98
lines changed

3 files changed

+82
-98
lines changed

dev/lib/index.js

Lines changed: 63 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ export function fromMarkdown(value, encoding, options) {
2626
}
2727

2828
// Note this compiler only understand complete buffering, not streaming.
29-
function compiler(options) {
30-
var settings = options || {}
31-
var config = configure(
29+
function compiler(options = {}) {
30+
const config = configure(
3231
{
3332
transforms: [],
3433
canContainEols: [
@@ -132,23 +131,19 @@ function compiler(options) {
132131
thematicBreak: closer()
133132
}
134133
},
135-
settings.mdastExtensions || []
134+
options.mdastExtensions || []
136135
)
137136

138-
var data = {}
137+
const data = {}
139138

140139
return compile
141140

142141
function compile(events) {
143-
var tree = {type: 'root', children: []}
144-
var stack = [tree]
145-
var tokenStack = []
146-
var listStack = []
147-
var index = -1
148-
var handler
149-
var listStart
150-
151-
var context = {
142+
let tree = {type: 'root', children: []}
143+
const stack = [tree]
144+
const tokenStack = []
145+
const listStack = []
146+
const context = {
152147
stack,
153148
tokenStack,
154149
config,
@@ -159,6 +154,7 @@ function compiler(options) {
159154
setData,
160155
getData
161156
}
157+
let index = -1
162158

163159
while (++index < events.length) {
164160
// We preprocess lists to add `listItem` tokens, and to infer whether
@@ -170,16 +166,15 @@ function compiler(options) {
170166
if (events[index][0] === 'enter') {
171167
listStack.push(index)
172168
} else {
173-
listStart = listStack.pop(index)
174-
index = prepareList(events, listStart, index)
169+
index = prepareList(events, listStack.pop(index), index)
175170
}
176171
}
177172
}
178173

179174
index = -1
180175

181176
while (++index < events.length) {
182-
handler = config[events[index][0]]
177+
const handler = config[events[index][0]]
183178

184179
if (own.call(handler, events[index][1].type)) {
185180
handler[events[index][1].type].call(
@@ -192,7 +187,7 @@ function compiler(options) {
192187
}
193188
}
194189

195-
if (tokenStack.length) {
190+
if (tokenStack.length > 0) {
196191
throw new Error(
197192
'Cannot close document, a token (`' +
198193
tokenStack[tokenStack.length - 1].type +
@@ -208,10 +203,10 @@ function compiler(options) {
208203
// Figure out `root` position.
209204
tree.position = {
210205
start: point(
211-
events.length ? events[0][1].start : {line: 1, column: 1, offset: 0}
206+
events.length > 0 ? events[0][1].start : {line: 1, column: 1, offset: 0}
212207
),
213208
end: point(
214-
events.length
209+
events.length > 0
215210
? events[events.length - 2][1].end
216211
: {line: 1, column: 1, offset: 0}
217212
)
@@ -226,19 +221,16 @@ function compiler(options) {
226221
}
227222

228223
function prepareList(events, start, length) {
229-
var index = start - 1
230-
var containerBalance = -1
231-
var listSpread = false
232-
var listItem
233-
var tailIndex
234-
var lineIndex
235-
var tailEvent
236-
var event
237-
var firstBlankLineIndex
238-
var atMarker
224+
let index = start - 1
225+
let containerBalance = -1
226+
let listSpread = false
227+
let listItem
228+
let lineIndex
229+
let firstBlankLineIndex
230+
let atMarker
239231

240232
while (++index <= length) {
241-
event = events[index]
233+
const event = events[index]
242234

243235
if (
244236
event[1].type === types.listUnordered ||
@@ -287,11 +279,11 @@ function compiler(options) {
287279
event[1].type === types.listOrdered))
288280
) {
289281
if (listItem) {
290-
tailIndex = index
282+
let tailIndex = index
291283
lineIndex = undefined
292284

293285
while (tailIndex--) {
294-
tailEvent = events[tailIndex]
286+
const tailEvent = events[tailIndex]
295287

296288
if (
297289
tailEvent[1].type === types.lineEnding ||
@@ -399,8 +391,8 @@ function compiler(options) {
399391
}
400392

401393
function exit(token) {
402-
var node = this.stack.pop()
403-
var open = this.tokenStack.pop()
394+
const node = this.stack.pop()
395+
const open = this.tokenStack.pop()
404396

405397
if (!open) {
406398
throw new Error(
@@ -442,7 +434,7 @@ function compiler(options) {
442434

443435
function onenterlistitemvalue(token) {
444436
if (getData('expectingFirstListItemValue')) {
445-
this.stack[this.stack.length - 2].start = parseInt(
437+
this.stack[this.stack.length - 2].start = Number.parseInt(
446438
this.sliceSerialize(token),
447439
constants.numericBaseDecimal
448440
)
@@ -451,12 +443,12 @@ function compiler(options) {
451443
}
452444

453445
function onexitcodefencedfenceinfo() {
454-
var data = this.resume()
446+
const data = this.resume()
455447
this.stack[this.stack.length - 1].lang = data
456448
}
457449

458450
function onexitcodefencedfencemeta() {
459-
var data = this.resume()
451+
const data = this.resume()
460452
this.stack[this.stack.length - 1].meta = data
461453
}
462454

@@ -468,7 +460,7 @@ function compiler(options) {
468460
}
469461

470462
function onexitcodefenced() {
471-
var data = this.resume()
463+
const data = this.resume()
472464

473465
this.stack[this.stack.length - 1].value = data.replace(
474466
/^(\r?\n|\r)|(\r?\n|\r)$/g,
@@ -479,26 +471,26 @@ function compiler(options) {
479471
}
480472

481473
function onexitcodeindented() {
482-
var data = this.resume()
474+
const data = this.resume()
483475
this.stack[this.stack.length - 1].value = data.replace(/(\r?\n|\r)$/g, '')
484476
}
485477

486478
function onexitdefinitionlabelstring(token) {
487479
// Discard label, use the source content instead.
488-
var label = this.resume()
480+
const label = this.resume()
489481
this.stack[this.stack.length - 1].label = label
490482
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
491483
this.sliceSerialize(token)
492484
).toLowerCase()
493485
}
494486

495487
function onexitdefinitiontitlestring() {
496-
var data = this.resume()
488+
const data = this.resume()
497489
this.stack[this.stack.length - 1].title = data
498490
}
499491

500492
function onexitdefinitiondestinationstring() {
501-
var data = this.resume()
493+
const data = this.resume()
502494
this.stack[this.stack.length - 1].url = data
503495
}
504496

@@ -523,8 +515,8 @@ function compiler(options) {
523515
}
524516

525517
function onenterdata(token) {
526-
var siblings = this.stack[this.stack.length - 1].children
527-
var tail = siblings[siblings.length - 1]
518+
const siblings = this.stack[this.stack.length - 1].children
519+
let tail = siblings[siblings.length - 1]
528520

529521
if (!tail || tail.type !== 'text') {
530522
// Add a new text node.
@@ -537,13 +529,13 @@ function compiler(options) {
537529
}
538530

539531
function onexitdata(token) {
540-
var tail = this.stack.pop()
532+
const tail = this.stack.pop()
541533
tail.value += this.sliceSerialize(token)
542534
tail.position.end = point(token.end)
543535
}
544536

545537
function onexitlineending(token) {
546-
var context = this.stack[this.stack.length - 1]
538+
const context = this.stack[this.stack.length - 1]
547539

548540
// If we’re at a hard break, include the line ending in there.
549541
if (getData('atHardBreak')) {
@@ -556,7 +548,7 @@ function compiler(options) {
556548

557549
if (
558550
!getData('setextHeadingSlurpLineEnding') &&
559-
config.canContainEols.indexOf(context.type) > -1
551+
config.canContainEols.includes(context.type)
560552
) {
561553
onenterdata.call(this, token)
562554
onexitdata.call(this, token)
@@ -568,22 +560,22 @@ function compiler(options) {
568560
}
569561

570562
function onexithtmlflow() {
571-
var data = this.resume()
563+
const data = this.resume()
572564
this.stack[this.stack.length - 1].value = data
573565
}
574566

575567
function onexithtmltext() {
576-
var data = this.resume()
568+
const data = this.resume()
577569
this.stack[this.stack.length - 1].value = data
578570
}
579571

580572
function onexitcodetext() {
581-
var data = this.resume()
573+
const data = this.resume()
582574
this.stack[this.stack.length - 1].value = data
583575
}
584576

585577
function onexitlink() {
586-
var context = this.stack[this.stack.length - 1]
578+
const context = this.stack[this.stack.length - 1]
587579

588580
// To do: clean.
589581
if (getData('inReference')) {
@@ -601,7 +593,7 @@ function compiler(options) {
601593
}
602594

603595
function onexitimage() {
604-
var context = this.stack[this.stack.length - 1]
596+
const context = this.stack[this.stack.length - 1]
605597

606598
// To do: clean.
607599
if (getData('inReference')) {
@@ -625,8 +617,8 @@ function compiler(options) {
625617
}
626618

627619
function onexitlabel() {
628-
var fragment = this.stack[this.stack.length - 1]
629-
var value = this.resume()
620+
const fragment = this.stack[this.stack.length - 1]
621+
const value = this.resume()
630622

631623
this.stack[this.stack.length - 1].label = value
632624

@@ -641,12 +633,12 @@ function compiler(options) {
641633
}
642634

643635
function onexitresourcedestinationstring() {
644-
var data = this.resume()
636+
const data = this.resume()
645637
this.stack[this.stack.length - 1].url = data
646638
}
647639

648640
function onexitresourcetitlestring() {
649-
var data = this.resume()
641+
const data = this.resume()
650642
this.stack[this.stack.length - 1].title = data
651643
}
652644

@@ -659,7 +651,7 @@ function compiler(options) {
659651
}
660652

661653
function onexitreferencestring(token) {
662-
var label = this.resume()
654+
const label = this.resume()
663655
this.stack[this.stack.length - 1].label = label
664656
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
665657
this.sliceSerialize(token)
@@ -672,10 +664,9 @@ function compiler(options) {
672664
}
673665

674666
function onexitcharacterreferencevalue(token) {
675-
var data = this.sliceSerialize(token)
676-
var type = getData('characterReferenceType')
677-
var value
678-
var tail
667+
const data = this.sliceSerialize(token)
668+
const type = getData('characterReferenceType')
669+
let value
679670

680671
if (type) {
681672
value = parseNumericCharacterReference(
@@ -689,7 +680,7 @@ function compiler(options) {
689680
value = decodeEntity(data)
690681
}
691682

692-
tail = this.stack.pop()
683+
const tail = this.stack.pop()
693684
tail.value += value
694685
tail.position.end = point(token.end)
695686
}
@@ -792,7 +783,7 @@ function compiler(options) {
792783
}
793784

794785
function configure(config, extensions) {
795-
var index = -1
786+
let index = -1
796787

797788
while (++index < extensions.length) {
798789
extension(config, extensions[index])
@@ -802,16 +793,17 @@ function configure(config, extensions) {
802793
}
803794

804795
function extension(config, extension) {
805-
var key
806-
var left
796+
let key
807797

808798
for (key in extension) {
809-
left = own.call(config, key) ? config[key] : (config[key] = {})
799+
if (own.call(extension, key)) {
800+
const left = own.call(config, key) ? config[key] : (config[key] = {})
810801

811-
if (key === 'canContainEols' || key === 'transforms') {
812-
config[key] = [].concat(left, extension[key])
813-
} else {
814-
Object.assign(left, extension[key])
802+
if (key === 'canContainEols' || key === 'transforms') {
803+
config[key] = [].concat(left, extension[key])
804+
} else {
805+
Object.assign(left, extension[key])
806+
}
815807
}
816808
}
817809
}

package.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"gzip-size-cli": "^5.0.0",
5454
"hast-util-to-html": "^8.0.0",
5555
"mdast-util-to-hast": "^11.0.0",
56-
"micromark-build": "^1.0.0-alpha.1",
56+
"micromark-build": "^1.0.0-alpha.3",
5757
"prettier": "^2.0.0",
5858
"rehype-parse": "^7.0.0",
5959
"rehype-stringify": "^8.0.0",
@@ -81,16 +81,8 @@
8181
"xo": {
8282
"prettier": true,
8383
"rules": {
84-
"no-var": "off",
85-
"prefer-arrow-callback": "off",
8684
"complexity": "off",
87-
"guard-for-in": "off",
88-
"unicorn/prefer-switch": "off",
89-
"unicorn/explicit-length-check": "off",
90-
"unicorn/no-array-callback-reference": "off",
91-
"unicorn/prefer-includes": "off",
92-
"unicorn/prefer-number-properties": "off",
93-
"unicorn/prefer-optional-catch-binding": "off"
85+
"unicorn/prefer-switch": "off"
9486
},
9587
"ignores": [
9688
"types/"

0 commit comments

Comments
 (0)