Skip to content

Commit 9ec1580

Browse files
committed
Fix linting
1 parent f97dd84 commit 9ec1580

File tree

4 files changed

+85
-67
lines changed

4 files changed

+85
-67
lines changed

example/example.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {remark} from 'remark'
1+
import {remark} from 'remark';
22
import {is} from 'unist-util-is';
3-
import replaceAllBetween from '../all.js';
3+
import replaceAllBetween from '..';
44

55
const markdown = `
66
# Hello World!
@@ -59,8 +59,8 @@ asdf
5959
* @returns {Section[]}
6060
*/
6161
const getSections = (allChildren, headerIndexes) =>
62-
headerIndexes.map((_, i, arr) => {
63-
let [startIndex, endIndex] = [arr[i], arr[i + 1]];
62+
headerIndexes.map((_, i, array) => {
63+
let [startIndex, endIndex] = [array[i], array[i + 1]];
6464
const header = allChildren[startIndex];
6565
startIndex += 1;
6666

@@ -84,36 +84,30 @@ const plugin = () => (tree) => {
8484
};
8585

8686
// Get lists between `start` and `end`
87-
replaceAllBetween(tree, start, end, list => {
87+
replaceAllBetween(tree, start, end, (list) => {
8888
// Remove both `tabs` mention
89-
// list.shift();
90-
// list.pop()
91-
92-
console.log({list});
89+
list.shift();
90+
list.pop();
9391

9492
const headerIndexes = list
9593
.map((node, i) => is(node, {type: 'heading'}) && i)
96-
.filter(Number.isInteger)
97-
98-
return [{
99-
type: 'html',
100-
value: headerIndexes.toString()
101-
}]
102-
103-
// const sections = getSections(list, headerIndexes);
104-
//
105-
// const tabNodes = sections.map(section => {
106-
// return {
107-
// type: 'tab',
108-
// heading: section.heading,
109-
// children: section.range
110-
// }
111-
// })
112-
//
113-
// return [{
114-
// type: 'tabList',
115-
// children: tabNodes
116-
// }]
94+
.filter(Number.isInteger);
95+
96+
const sections = getSections(list, headerIndexes);
97+
98+
const tabNodes = sections.map((section) => {
99+
return {
100+
type: 'html',
101+
value: section.range
102+
};
103+
});
104+
105+
return [
106+
{
107+
type: 'element',
108+
children: tabNodes
109+
}
110+
];
117111
});
118112

119113
// Return new tree

index.js

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,50 @@ function replaceAllBetween(parent, start, end, func) {
99

1010
const {children} = parent;
1111

12-
const isStart = node => is(node, start);
13-
const isEnd = node => is(node, end);
12+
const isStart = (node) => is(node, start);
13+
const isEnd = (node) => is(node, end);
1414

1515
/**
1616
* @type {Array<{start: number, end: number}>}
1717
*/
18-
const ranges = children.reduce((prev, child, i) => {
19-
const lastPrev = prev[prev.length - 1];
18+
const ranges = children.reduce((previous, child, i) => {
19+
const lastPrevious = previous[previous.length - 1];
2020
if (isStart(children[i])) {
21-
if (lastPrev && lastPrev.end === undefined) {
22-
console.error('Attempted to start a replacement before the first one was cleaned up')
23-
throw new Error('Attempted to start a replacement before the first one was cleaned up');
21+
if (lastPrevious && lastPrevious.end === undefined) {
22+
console.error(
23+
'Attempted to start a replacement before the first one was cleaned up'
24+
);
25+
throw new Error(
26+
'Attempted to start a replacement before the first one was cleaned up'
27+
);
2428
}
25-
prev.push({
29+
30+
previous.push({
2631
start: i
2732
});
28-
return prev;
33+
return previous;
2934
}
35+
3036
if (isEnd(children[i])) {
31-
if (!lastPrev || (lastPrev && lastPrev.start === undefined)) {
32-
console.error('Attempted to end a replacement before finding the start', i, lastPrev)
33-
throw new Error('Attempted to end a replacement before finding the start');
37+
if (!lastPrevious || (lastPrevious && lastPrevious.start === undefined)) {
38+
console.error(
39+
'Attempted to end a replacement before finding the start',
40+
i,
41+
lastPrevious
42+
);
43+
throw new Error(
44+
'Attempted to end a replacement before finding the start'
45+
);
3446
}
35-
lastPrev.end = i;
47+
48+
lastPrevious.end = i;
3649
}
37-
return prev;
50+
51+
return previous;
3852
}, []);
3953

4054
if (!ranges[ranges.length - 1].end) {
41-
console.error('No ending value was found')
55+
console.error('No ending value was found');
4256
throw new Error('No ending value was found');
4357
}
4458

@@ -54,7 +68,11 @@ function replaceAllBetween(parent, start, end, func) {
5468

5569
const changedArray = func(replaced);
5670

57-
const diff = children.splice(offsetStart, offsetEnd - offsetStart + 1, ...changedArray);
71+
const diff = children.splice(
72+
offsetStart,
73+
offsetEnd - offsetStart + 1,
74+
...changedArray
75+
);
5876

5977
offset -= diff.length - changedArray.length;
6078

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"xo": {
5353
"prettier": true,
5454
"rules": {
55-
"unicorn/prefer-number-properties": "off"
55+
"unicorn/prefer-number-properties": "off",
56+
"unicorn/no-reduce": "off",
57+
"unicorn/no-fn-reference-in-iterator": "off"
5658
}
5759
},
5860
"prettier": {

test.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,38 @@ test('unist-util-find-all-between', (test) => {
1313
}, 'Should fail without parent node');
1414

1515
test.throws(() => {
16-
findAllBetween({type: 'foo', children: [
17-
{type: 'start'},
18-
{type: 'start'}
19-
]}, {type: 'start'}, {type: 'end'});
16+
findAllBetween(
17+
{type: 'foo', children: [{type: 'start'}, {type: 'start'}]},
18+
{type: 'start'},
19+
{type: 'end'}
20+
);
2021
}, 'Tried getting a new start instead of ending');
2122

2223
test.throws(() => {
23-
findAllBetween({type: 'foo', children: [
24-
{type: 'start'},
25-
{type: 'end'},
26-
{type: 'start'}
27-
]}, {type: 'start'}, {type: 'end'});
24+
findAllBetween(
25+
{
26+
type: 'foo',
27+
children: [{type: 'start'}, {type: 'end'}, {type: 'start'}]
28+
},
29+
{type: 'start'},
30+
{type: 'end'}
31+
);
2832
}, 'No ending value was found');
2933

3034
test.throws(() => {
31-
findAllBetween({type: 'foo', children: [
32-
{type: 'end'},
33-
{type: 'start'}
34-
]}, {type: 'start'}, {type: 'end'});
35+
findAllBetween(
36+
{type: 'foo', children: [{type: 'end'}, {type: 'start'}]},
37+
{type: 'start'},
38+
{type: 'end'}
39+
);
3540
}, 'Attempted to end a replacement before finding the start');
3641

3742
test.throws(() => {
38-
findAllBetween({type: 'foo', children: [
39-
{type: 'start'},
40-
{type: 'end'},
41-
{type: 'end'},
42-
]}, {type: 'start'}, {type: 'end'});
43+
findAllBetween(
44+
{type: 'foo', children: [{type: 'start'}, {type: 'end'}, {type: 'end'}]},
45+
{type: 'start'},
46+
{type: 'end'}
47+
);
4348
}, 'Attempted to end a replacement before finding the start');
4449

4550
test.doesNotThrow(() => {
@@ -102,7 +107,7 @@ test('unist-util-find-all-between', (test) => {
102107
},
103108
{
104109
type: 'baz'
105-
},
110+
}
106111
]
107112
},
108113
{type: 'foo'},
@@ -125,6 +130,5 @@ test('unist-util-find-all-between', (test) => {
125130
);
126131
}, 'Replaces two matches with new output');
127132

128-
129133
test.end();
130134
});

0 commit comments

Comments
 (0)