Skip to content

Commit 03cee9c

Browse files
committed
Refactor code-style
1 parent fe9c8cb commit 03cee9c

File tree

3 files changed

+53
-60
lines changed

3 files changed

+53
-60
lines changed

index.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import flow from 'mdast-util-to-markdown/lib/util/container-flow.js'
66
import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
77
import checkQuote from 'mdast-util-to-markdown/lib/util/check-quote.js'
88

9-
var eol = /\r?\n|\r/g
9+
const eol = /\r?\n|\r/g
1010

1111
mdxElement.peek = peekElement
1212

@@ -100,7 +100,7 @@ function enterMdxJsxTag(token) {
100100
}
101101

102102
function enterMdxJsxTagClosingMarker(token) {
103-
if (!this.getData('mdxJsxTagStack').length) {
103+
if (this.getData('mdxJsxTagStack').length === 0) {
104104
throw new VFileMessage(
105105
'Unexpected closing slash `/` in tag, expected an open tag first',
106106
{start: token.start, end: token.end},
@@ -164,7 +164,7 @@ function enterMdxJsxTagExpressionAttribute(token) {
164164
}
165165

166166
function exitMdxJsxTagExpressionAttribute(token) {
167-
var attributes = this.getData('mdxJsxTag').attributes
167+
const attributes = this.getData('mdxJsxTag').attributes
168168
attributes[attributes.length - 1].value = this.resume()
169169

170170
if (token.estree) {
@@ -173,24 +173,24 @@ function exitMdxJsxTagExpressionAttribute(token) {
173173
}
174174

175175
function exitMdxJsxTagAttributeNamePrimary(token) {
176-
var attributes = this.getData('mdxJsxTag').attributes
176+
const attributes = this.getData('mdxJsxTag').attributes
177177
attributes[attributes.length - 1].name = this.sliceSerialize(token)
178178
}
179179

180180
function exitMdxJsxTagAttributeNameLocal(token) {
181-
var attributes = this.getData('mdxJsxTag').attributes
181+
const attributes = this.getData('mdxJsxTag').attributes
182182
attributes[attributes.length - 1].name += ':' + this.sliceSerialize(token)
183183
}
184184

185185
function exitMdxJsxTagAttributeValueLiteral() {
186-
var attributes = this.getData('mdxJsxTag').attributes
186+
const attributes = this.getData('mdxJsxTag').attributes
187187
attributes[attributes.length - 1].value = parseEntities(this.resume(), {
188188
nonTerminated: false
189189
})
190190
}
191191

192192
function exitMdxJsxTagAttributeValueExpression(token) {
193-
var attributes = this.getData('mdxJsxTag').attributes
193+
const attributes = this.getData('mdxJsxTag').attributes
194194

195195
attributes[attributes.length - 1].value = {
196196
type: 'mdxJsxAttributeValueExpression',
@@ -207,9 +207,9 @@ function exitMdxJsxTagSelfClosingMarker() {
207207
}
208208

209209
function exitMdxJsxTag(token) {
210-
var tag = this.getData('mdxJsxTag')
211-
var stack = this.getData('mdxJsxTagStack')
212-
var tail = stack[stack.length - 1]
210+
const tag = this.getData('mdxJsxTag')
211+
const stack = this.getData('mdxJsxTagStack')
212+
const tail = stack[stack.length - 1]
213213

214214
if (tag.close && tail.name !== tag.name) {
215215
throw new VFileMessage(
@@ -260,17 +260,17 @@ function serializeAbbreviatedTag(tag) {
260260

261261
// eslint-disable-next-line complexity
262262
function mdxElement(node, _, context) {
263-
var selfClosing = node.name && (!node.children || !node.children.length)
264-
var quote = checkQuote(context)
265-
var exit = context.enter(node.type)
266-
var index = -1
267-
var attributes = []
268-
var attribute
269-
var result
270-
var value
263+
const selfClosing =
264+
node.name && (!node.children || node.children.length === 0)
265+
const quote = checkQuote(context)
266+
const exit = context.enter(node.type)
267+
let index = -1
268+
const attributes = []
269+
let attribute
270+
let result
271271

272272
// None.
273-
if (node.attributes && node.attributes.length) {
273+
if (node.attributes && node.attributes.length > 0) {
274274
if (!node.name) {
275275
throw new Error('Cannot serialize fragment w/ attributes')
276276
}
@@ -287,7 +287,7 @@ function mdxElement(node, _, context) {
287287

288288
result =
289289
attribute.name +
290-
(attribute.value == null
290+
(attribute.value === undefined || attribute.value === null
291291
? ''
292292
: '=' +
293293
(typeof attribute.value === 'object'
@@ -301,18 +301,18 @@ function mdxElement(node, _, context) {
301301
}
302302
}
303303

304-
value =
304+
const value =
305305
'<' +
306306
(node.name || '') +
307307
(node.type === 'mdxJsxFlowElement' && attributes.length > 1
308308
? // Flow w/ multiple attributes.
309309
'\n' + indent(attributes.join('\n')) + '\n'
310-
: attributes.length // Text or flow w/ a single attribute.
310+
: attributes.length > 0 // Text or flow w/ a single attribute.
311311
? ' ' + dedentStart(indent(attributes.join(' ')))
312312
: '') +
313313
(selfClosing ? '/' : '') +
314314
'>' +
315-
(node.children && node.children.length
315+
(node.children && node.children.length > 0
316316
? node.type === 'mdxJsxFlowElement'
317317
? '\n' + indent(flow(node, context)) + '\n'
318318
: phrasing(node, context, {before: '<', after: '>'})
@@ -332,9 +332,9 @@ function dedentStart(value) {
332332
}
333333

334334
function indent(value) {
335-
var result = []
336-
var start = 0
337-
var match
335+
const result = []
336+
let start = 0
337+
let match
338338

339339
while ((match = eol.exec(value))) {
340340
one(value.slice(start, match.index))

package.json

+1-8
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@
6666
"trailingComma": "none"
6767
},
6868
"xo": {
69-
"prettier": true,
70-
"rules": {
71-
"no-var": "off",
72-
"prefer-arrow-callback": "off",
73-
"eqeqeq": "off",
74-
"no-eq-null": "off",
75-
"unicorn/explicit-length-check": "off"
76-
}
69+
"prettier": true
7770
},
7871
"remarkConfig": {
7972
"plugins": [

test.js

+26-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {removePosition} from 'unist-util-remove-position'
66
import mdxJsx from 'micromark-extension-mdx-jsx'
77
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from './index.js'
88

9-
test('markdown -> mdast', function (t) {
9+
test('markdown -> mdast', (t) => {
1010
t.deepEqual(
1111
fromMarkdown('<a />', {
1212
extensions: [mdxJsx()],
@@ -152,7 +152,7 @@ test('markdown -> mdast', function (t) {
152152
)
153153

154154
t.throws(
155-
function () {
155+
() => {
156156
fromMarkdown('a <b> c', {
157157
extensions: [mdxJsx()],
158158
mdastExtensions: [mdxJsxFromMarkdown]
@@ -163,7 +163,7 @@ test('markdown -> mdast', function (t) {
163163
)
164164

165165
t.throws(
166-
function () {
166+
() => {
167167
fromMarkdown('<a>', {
168168
extensions: [mdxJsx()],
169169
mdastExtensions: [mdxJsxFromMarkdown]
@@ -418,7 +418,7 @@ test('markdown -> mdast', function (t) {
418418
)
419419

420420
t.throws(
421-
function () {
421+
() => {
422422
fromMarkdown('a <b {1 + 1} /> c', {
423423
extensions: [mdxJsx({acorn})],
424424
mdastExtensions: [mdxJsxFromMarkdown]
@@ -429,7 +429,7 @@ test('markdown -> mdast', function (t) {
429429
)
430430

431431
t.throws(
432-
function () {
432+
() => {
433433
fromMarkdown('a <b c={?} /> d', {
434434
extensions: [mdxJsx({acorn})],
435435
mdastExtensions: [mdxJsxFromMarkdown]
@@ -440,7 +440,7 @@ test('markdown -> mdast', function (t) {
440440
)
441441

442442
t.throws(
443-
function () {
443+
() => {
444444
fromMarkdown('a < \t>b</>', {
445445
extensions: [mdxJsx({acorn})],
446446
mdastExtensions: [mdxJsxFromMarkdown]
@@ -648,7 +648,7 @@ test('markdown -> mdast', function (t) {
648648
)
649649

650650
t.throws(
651-
function () {
651+
() => {
652652
fromMarkdown('a </> c', {
653653
extensions: [mdxJsx()],
654654
mdastExtensions: [mdxJsxFromMarkdown]
@@ -659,7 +659,7 @@ test('markdown -> mdast', function (t) {
659659
)
660660

661661
t.throws(
662-
function () {
662+
() => {
663663
fromMarkdown('</>', {
664664
extensions: [mdxJsx()],
665665
mdastExtensions: [mdxJsxFromMarkdown]
@@ -670,7 +670,7 @@ test('markdown -> mdast', function (t) {
670670
)
671671

672672
t.throws(
673-
function () {
673+
() => {
674674
fromMarkdown('a <></b>', {
675675
extensions: [mdxJsx()],
676676
mdastExtensions: [mdxJsxFromMarkdown]
@@ -680,7 +680,7 @@ test('markdown -> mdast', function (t) {
680680
'should crash on mismatched tags (1)'
681681
)
682682
t.throws(
683-
function () {
683+
() => {
684684
fromMarkdown('a <b></>', {
685685
extensions: [mdxJsx()],
686686
mdastExtensions: [mdxJsxFromMarkdown]
@@ -690,7 +690,7 @@ test('markdown -> mdast', function (t) {
690690
'should crash on mismatched tags (2)'
691691
)
692692
t.throws(
693-
function () {
693+
() => {
694694
fromMarkdown('a <a.b></a>', {
695695
extensions: [mdxJsx()],
696696
mdastExtensions: [mdxJsxFromMarkdown]
@@ -700,7 +700,7 @@ test('markdown -> mdast', function (t) {
700700
'should crash on mismatched tags (3)'
701701
)
702702
t.throws(
703-
function () {
703+
() => {
704704
fromMarkdown('a <a></a.b>', {
705705
extensions: [mdxJsx()],
706706
mdastExtensions: [mdxJsxFromMarkdown]
@@ -710,7 +710,7 @@ test('markdown -> mdast', function (t) {
710710
'should crash on mismatched tags (4)'
711711
)
712712
t.throws(
713-
function () {
713+
() => {
714714
fromMarkdown('a <a.b></a.c>', {
715715
extensions: [mdxJsx()],
716716
mdastExtensions: [mdxJsxFromMarkdown]
@@ -720,7 +720,7 @@ test('markdown -> mdast', function (t) {
720720
'should crash on mismatched tags (5)'
721721
)
722722
t.throws(
723-
function () {
723+
() => {
724724
fromMarkdown('a <a:b></a>', {
725725
extensions: [mdxJsx()],
726726
mdastExtensions: [mdxJsxFromMarkdown]
@@ -730,7 +730,7 @@ test('markdown -> mdast', function (t) {
730730
'should crash on mismatched tags (6)'
731731
)
732732
t.throws(
733-
function () {
733+
() => {
734734
fromMarkdown('a <a></a:b>', {
735735
extensions: [mdxJsx()],
736736
mdastExtensions: [mdxJsxFromMarkdown]
@@ -740,7 +740,7 @@ test('markdown -> mdast', function (t) {
740740
'should crash on mismatched tags (7)'
741741
)
742742
t.throws(
743-
function () {
743+
() => {
744744
fromMarkdown('a <a:b></a:c>', {
745745
extensions: [mdxJsx()],
746746
mdastExtensions: [mdxJsxFromMarkdown]
@@ -750,7 +750,7 @@ test('markdown -> mdast', function (t) {
750750
'should crash on mismatched tags (8)'
751751
)
752752
t.throws(
753-
function () {
753+
() => {
754754
fromMarkdown('a <a:b></a.b>', {
755755
extensions: [mdxJsx()],
756756
mdastExtensions: [mdxJsxFromMarkdown]
@@ -761,7 +761,7 @@ test('markdown -> mdast', function (t) {
761761
)
762762

763763
t.throws(
764-
function () {
764+
() => {
765765
fromMarkdown('<a>b</a/>', {
766766
extensions: [mdxJsx()],
767767
mdastExtensions: [mdxJsxFromMarkdown]
@@ -772,7 +772,7 @@ test('markdown -> mdast', function (t) {
772772
)
773773

774774
t.throws(
775-
function () {
775+
() => {
776776
fromMarkdown('<a>b</a b>', {
777777
extensions: [mdxJsx()],
778778
mdastExtensions: [mdxJsxFromMarkdown]
@@ -904,7 +904,7 @@ test('markdown -> mdast', function (t) {
904904
)
905905

906906
t.throws(
907-
function () {
907+
() => {
908908
fromMarkdown('a *open <b> close* </b> c.', {
909909
extensions: [mdxJsx()],
910910
mdastExtensions: [mdxJsxFromMarkdown]
@@ -915,7 +915,7 @@ test('markdown -> mdast', function (t) {
915915
)
916916

917917
t.throws(
918-
function () {
918+
() => {
919919
fromMarkdown('a **open <b> close** </b> c.', {
920920
extensions: [mdxJsx()],
921921
mdastExtensions: [mdxJsxFromMarkdown]
@@ -926,7 +926,7 @@ test('markdown -> mdast', function (t) {
926926
)
927927

928928
t.throws(
929-
function () {
929+
() => {
930930
fromMarkdown('a [open <b> close](c) </b> d.', {
931931
extensions: [mdxJsx()],
932932
mdastExtensions: [mdxJsxFromMarkdown]
@@ -937,7 +937,7 @@ test('markdown -> mdast', function (t) {
937937
)
938938

939939
t.throws(
940-
function () {
940+
() => {
941941
fromMarkdown('a ![open <b> close](c) </b> d.', {
942942
extensions: [mdxJsx()],
943943
mdastExtensions: [mdxJsxFromMarkdown]
@@ -1246,7 +1246,7 @@ test('markdown -> mdast', function (t) {
12461246
t.end()
12471247
})
12481248

1249-
test('mdast -> markdown', function (t) {
1249+
test('mdast -> markdown', (t) => {
12501250
t.deepEqual(
12511251
toMarkdown({type: 'mdxJsxFlowElement'}, {extensions: [mdxJsxToMarkdown]}),
12521252
'<></>\n',
@@ -1288,7 +1288,7 @@ test('mdast -> markdown', function (t) {
12881288
)
12891289

12901290
t.throws(
1291-
function () {
1291+
() => {
12921292
toMarkdown(
12931293
{
12941294
type: 'mdxJsxFlowElement',
@@ -1371,7 +1371,7 @@ test('mdast -> markdown', function (t) {
13711371
)
13721372

13731373
t.throws(
1374-
function () {
1374+
() => {
13751375
toMarkdown(
13761376
{
13771377
type: 'mdxJsxFlowElement',

0 commit comments

Comments
 (0)