Skip to content

Commit dc9155e

Browse files
committed
Refactor code-style
1 parent 13b0ff7 commit dc9155e

File tree

4 files changed

+32
-46
lines changed

4 files changed

+32
-46
lines changed

index.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {visitParents} from 'unist-util-visit-parents'
3636
import {convertElement} from 'hast-util-is-element'
3737
import escape from 'escape-string-regexp'
3838

39-
var own = {}.hasOwnProperty
39+
const own = {}.hasOwnProperty
4040

4141
export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
4242

@@ -48,9 +48,9 @@ export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
4848
*/
4949
export function findAndReplace(tree, find, replace, options) {
5050
/** @type {Options} */
51-
var settings
51+
let settings
5252
/** @type {FindAndReplaceSchema|FindAndReplaceList} */
53-
var schema
53+
let schema
5454

5555
if (typeof find === 'string' || find instanceof RegExp) {
5656
// @ts-expect-error don’t expect options twice.
@@ -66,9 +66,9 @@ export function findAndReplace(tree, find, replace, options) {
6666
settings = {}
6767
}
6868

69-
var ignored = convertElement(settings.ignore || defaultIgnore)
70-
var pairs = toPairs(schema)
71-
var pairIndex = -1
69+
const ignored = convertElement(settings.ignore || defaultIgnore)
70+
const pairs = toPairs(schema)
71+
let pairIndex = -1
7272

7373
while (++pairIndex < pairs.length) {
7474
visitParents(tree, 'text', visitor)
@@ -78,15 +78,12 @@ export function findAndReplace(tree, find, replace, options) {
7878

7979
/** @type {import('unist-util-visit-parents').Visitor<Text>} */
8080
function visitor(node, parents) {
81-
var index = -1
81+
let index = -1
8282
/** @type {Parent} */
83-
var parent
84-
/** @type {Parent} */
85-
var grandparent
83+
let grandparent
8684

8785
while (++index < parents.length) {
88-
// @ts-expect-error hast vs. unist parent.
89-
parent = parents[index]
86+
const parent = parents[index]
9087

9188
if (
9289
ignored(
@@ -99,6 +96,7 @@ export function findAndReplace(tree, find, replace, options) {
9996
return
10097
}
10198

99+
// @ts-expect-error hast vs. unist parent.
102100
grandparent = parent
103101
}
104102

@@ -111,27 +109,23 @@ export function findAndReplace(tree, find, replace, options) {
111109
* @returns {VisitorResult}
112110
*/
113111
function handler(node, parent) {
114-
var find = pairs[pairIndex][0]
115-
var replace = pairs[pairIndex][1]
112+
const find = pairs[pairIndex][0]
113+
const replace = pairs[pairIndex][1]
114+
let start = 0
115+
let index = parent.children.indexOf(node)
116116
/** @type {Array.<Content>} */
117-
var nodes = []
118-
var start = 0
119-
var index = parent.children.indexOf(node)
117+
let nodes = []
120118
/** @type {number} */
121-
var position
122-
/** @type {RegExpMatchArray} */
123-
var match
124-
/** @type {Array.<Content>|Content|string|false|undefined|null} */
125-
var value
119+
let position
126120

127121
find.lastIndex = 0
128122

129-
match = find.exec(node.value)
123+
let match = find.exec(node.value)
130124

131125
while (match) {
132126
position = match.index
133127
// @ts-expect-error this is perfectly fine, typescript.
134-
value = replace(...match, {index: match.index, input: match.input})
128+
let value = replace(...match, {index: match.index, input: match.input})
135129

136130
if (typeof value === 'string' && value.length > 0) {
137131
value = {type: 'text', value}
@@ -176,24 +170,26 @@ export function findAndReplace(tree, find, replace, options) {
176170
* @returns {Pairs}
177171
*/
178172
function toPairs(schema) {
179-
var index = -1
180173
/** @type {Pairs} */
181-
var result = []
182-
/** @type {string} */
183-
var key
174+
const result = []
184175

185176
if (typeof schema !== 'object') {
186177
throw new TypeError('Expected array or object as schema')
187178
}
188179

189180
if (Array.isArray(schema)) {
181+
let index = -1
182+
190183
while (++index < schema.length) {
191184
result.push([
192185
toExpression(schema[index][0]),
193186
toFunction(schema[index][1])
194187
])
195188
}
196189
} else {
190+
/** @type {string} */
191+
let key
192+
197193
for (key in schema) {
198194
if (own.call(schema, key)) {
199195
result.push([toExpression(key), toFunction(schema[key])])
@@ -217,11 +213,5 @@ function toExpression(find) {
217213
* @returns {ReplaceFunction}
218214
*/
219215
function toFunction(replace) {
220-
return typeof replace === 'function' ? replace : returner
221-
222-
/** @type {ReplaceFunction} */
223-
function returner() {
224-
// @ts-expect-error it’s a string.
225-
return replace
226-
}
216+
return typeof replace === 'function' ? replace : () => replace
227217
}

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@
6666
"trailingComma": "none"
6767
},
6868
"xo": {
69-
"prettier": true,
70-
"rules": {
71-
"no-var": "off",
72-
"prefer-arrow-callback": "off"
73-
}
69+
"prettier": true
7470
},
7571
"remarkConfig": {
7672
"plugins": [

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {h} from 'hastscript'
2828
import {inspect} from 'unist-util-inspect'
2929
import {findAndReplace} from 'hast-util-find-and-replace'
3030

31-
var tree = h('p', [
31+
const tree = h('p', [
3232
'Some ',
3333
h('em', 'emphasis'),
3434
', ',

test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import test from 'tape'
22
import {h} from 'hastscript'
33
import {findAndReplace} from './index.js'
44

5-
test('findAndReplace', function (t) {
5+
test('findAndReplace', (t) => {
66
t.throws(
7-
function () {
7+
() => {
88
// @ts-expect-error runtime.
99
findAndReplace(create(), true)
1010
},
@@ -44,7 +44,7 @@ test('findAndReplace', function (t) {
4444
findAndReplace(
4545
create(),
4646
/em(\w+)is/,
47-
function (/** @type {string} */ _, /** @type {string} */ $1) {
47+
(/** @type {string} */ _, /** @type {string} */ $1) => {
4848
return '[' + $1 + ']'
4949
}
5050
),
@@ -61,7 +61,7 @@ test('findAndReplace', function (t) {
6161
)
6262

6363
t.deepEqual(
64-
findAndReplace(create(), 'emphasis', function () {
64+
findAndReplace(create(), 'emphasis', () => {
6565
return h('a', h('b', 'c'))
6666
}),
6767
h('p', [
@@ -233,7 +233,7 @@ test('findAndReplace', function (t) {
233233
)
234234

235235
t.deepEqual(
236-
findAndReplace(create(), 'and', function () {
236+
findAndReplace(create(), 'and', () => {
237237
return h('script', 'alert(1)')
238238
}),
239239
h('p', [

0 commit comments

Comments
 (0)