Skip to content

Commit fa311dd

Browse files
committed
Refactor code-style
1 parent 3c88de9 commit fa311dd

File tree

6 files changed

+44
-53
lines changed

6 files changed

+44
-53
lines changed

color-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var color = false
1+
export const color = false

color.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export var color = true
1+
export const color = true

index.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
import {color} from './color.js'
1111

1212
/* c8 ignore next */
13-
export var inspect = color ? inspectColor : inspectNoColor
13+
export const inspect = color ? inspectColor : inspectNoColor
1414

15-
var own = {}.hasOwnProperty
15+
const own = {}.hasOwnProperty
1616

17-
var bold = ansiColor(1, 22)
18-
var dim = ansiColor(2, 22)
19-
var yellow = ansiColor(33, 39)
20-
var green = ansiColor(32, 39)
17+
const bold = ansiColor(1, 22)
18+
const dim = ansiColor(2, 22)
19+
const yellow = ansiColor(33, 39)
20+
const green = ansiColor(32, 39)
2121

2222
// ANSI color regex.
2323
/* eslint-disable no-control-regex */
24-
var colorExpression =
24+
const colorExpression =
2525
/(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
2626
/* eslint-enable no-control-regex */
2727

@@ -43,11 +43,9 @@ export function inspectNoColor(node, options) {
4343
* @param {InspectOptions} [options]
4444
* @returns {string}
4545
*/
46-
export function inspectColor(tree, options) {
47-
var positions =
48-
!options ||
49-
options.showPositions === null ||
50-
options.showPositions === undefined
46+
export function inspectColor(tree, options = {}) {
47+
const positions =
48+
options.showPositions === null || options.showPositions === undefined
5149
? true
5250
: options.showPositions
5351

@@ -86,8 +84,8 @@ export function inspectColor(tree, options) {
8684
*/
8785
function inspectNodes(nodes) {
8886
/** @type {Array.<string>} */
89-
var result = []
90-
var index = -1
87+
const result = []
88+
let index = -1
9189

9290
while (++index < nodes.length) {
9391
result.push(
@@ -108,21 +106,20 @@ export function inspectColor(tree, options) {
108106
* @param {Object.<string, unknown>} object
109107
* @returns {string}
110108
*/
109+
// eslint-disable-next-line complexity
111110
function inspectFields(object) {
112111
/** @type {Array.<string>} */
113-
var result = []
112+
const result = []
114113
/** @type {string} */
115-
var key
116-
/** @type {unknown} */
117-
var value
118-
/** @type {string} */
119-
var formatted
114+
let key
120115

121116
for (key in object) {
122117
/* c8 ignore next 1 */
123118
if (!own.call(object, key)) continue
124119

125-
value = object[key]
120+
const value = object[key]
121+
/** @type {string} */
122+
let formatted
126123

127124
if (
128125
value === undefined ||
@@ -182,11 +179,11 @@ export function inspectColor(tree, options) {
182179
* @returns {string}
183180
*/
184181
function inspectTree(node) {
185-
var result = [formatNode(node)]
182+
const result = [formatNode(node)]
186183
// @ts-expect-error: looks like a record.
187-
var fields = inspectFields(node)
184+
const fields = inspectFields(node)
188185
// @ts-ignore looks like a parent.
189-
var content = inspectNodes(node.children || [])
186+
const content = inspectNodes(node.children || [])
190187
if (fields) result.push(fields)
191188
if (content) result.push(content)
192189
return result.join('\n')
@@ -199,11 +196,11 @@ export function inspectColor(tree, options) {
199196
* @returns {string}
200197
*/
201198
function formatNode(node) {
202-
var result = [bold(node.type)]
203-
/** @type {string} */
199+
const result = [bold(node.type)]
200+
/** @type {string|undefined} */
204201
// @ts-expect-error: might be available.
205-
var kind = node.tagName || node.name
206-
var position = positions ? stringifyPosition(node.position) : ''
202+
const kind = node.tagName || node.name
203+
const position = positions ? stringifyPosition(node.position) : ''
207204

208205
if (typeof kind === 'string') {
209206
result.push('<', kind, '>')
@@ -234,8 +231,8 @@ export function inspectColor(tree, options) {
234231
* @returns {string}
235232
*/
236233
function indent(value, indentation, ignoreFirst) {
237-
var lines = value.split('\n')
238-
var index = ignoreFirst ? 0 : -1
234+
const lines = value.split('\n')
235+
let index = ignoreFirst ? 0 : -1
239236

240237
if (!value) return value
241238

@@ -253,13 +250,13 @@ function indent(value, indentation, ignoreFirst) {
253250
function stringifyPosition(value) {
254251
/** @type {Position} */
255252
// @ts-ignore
256-
var position = value || {}
253+
const position = value || {}
257254
/** @type {Array.<string>} */
258-
var result = []
255+
const result = []
259256
/** @type {Array.<string>} */
260-
var positions = []
257+
const positions = []
261258
/** @type {Array.<string>} */
262-
var offsets = []
259+
const offsets = []
263260

264261
point(position.start)
265262
point(position.end)

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,7 @@
8080
"trailingComma": "none"
8181
},
8282
"xo": {
83-
"prettier": true,
84-
"rules": {
85-
"complexity": "off",
86-
"import/no-mutable-exports": "off",
87-
"no-var": "off",
88-
"prefer-arrow-callback": "off"
89-
}
83+
"prettier": true
9084
},
9185
"remarkConfig": {
9286
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ npm install unist-util-inspect
2424
## Use
2525

2626
```js
27-
var u = require('unist-builder')
28-
var inspect = require('unist-util-inspect')
27+
import {u} from 'unist-builder'
28+
import {inspect} from 'unist-util-inspect'
2929

30-
var tree = u('root', [
30+
const tree = u('root', [
3131
u('literal', '1'),
3232
u('parent', [
3333
u('void', {id: 'a'}),

test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import retext from 'retext'
1010
import {fromXml} from 'xast-util-from-xml'
1111
import {inspect, inspectColor, inspectNoColor} from './index.js'
1212

13-
var chalkEnabled = new chalk.Instance({level: 1})
13+
const chalkEnabled = new chalk.Instance({level: 1})
1414

15-
var paragraph = 'Some simple text. Other “sentence”.'
15+
const paragraph = 'Some simple text. Other “sentence”.'
1616

17-
test('inspect', function (t) {
17+
test('inspect', (t) => {
1818
t.equal(typeof inspect, 'function', 'should be a `function`')
1919

2020
t.end()
2121
})
2222

23-
test('inspect()', function (t) {
23+
test('inspect()', (t) => {
2424
t.equal(
2525
strip(inspect(retext().parse(paragraph))),
2626
[
@@ -56,7 +56,7 @@ test('inspect()', function (t) {
5656
'should work with a list of nodes'
5757
)
5858

59-
t.test('should work on non-nodes', function (st) {
59+
t.test('should work on non-nodes', (st) => {
6060
st.equal(strip(inspect('foo')), '"foo"')
6161
st.equal(strip(inspect(null)), 'null')
6262
st.equal(strip(inspect(Number.NaN)), 'null')
@@ -330,7 +330,7 @@ test('inspect()', function (t) {
330330
t.end()
331331
})
332332

333-
test('inspectNoColor()', function (t) {
333+
test('inspectNoColor()', (t) => {
334334
t.equal(
335335
inspectNoColor(retext().parse(paragraph).children[0].children[0]),
336336
[
@@ -351,7 +351,7 @@ test('inspectNoColor()', function (t) {
351351
t.end()
352352
})
353353

354-
test('inspectColor()', function (t) {
354+
test('inspectColor()', (t) => {
355355
t.equal(
356356
inspectColor(retext().parse(paragraph).children[0].children[0]),
357357
[

0 commit comments

Comments
 (0)