Skip to content

Commit bca7cdc

Browse files
committed
Refactor code-style
1 parent 645e60f commit bca7cdc

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
lines changed

index.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
* @property {boolean} trailing
1212
*/
1313

14+
const own = {}.hasOwnProperty
15+
1416
/**
1517
* Attach semistandard estree comment nodes to the tree.
1618
*
1719
* @param {EstreeNode} tree
1820
* @param {EstreeComment[]} [comments]
1921
*/
2022
export function attachComments(tree, comments) {
21-
var list = (comments || []).concat().sort(compare)
22-
if (list.length) walk(tree, {comments: list, index: 0})
23+
const list = (comments || []).concat().sort(compare)
24+
if (list.length > 0) walk(tree, {comments: list, index: 0})
2325
return tree
2426
}
2527

@@ -30,38 +32,37 @@ export function attachComments(tree, comments) {
3032
* @param {State} state
3133
*/
3234
function walk(node, state) {
33-
/** @type {EstreeNode[]} */
34-
var children = []
35-
/** @type {EstreeComment[]} */
36-
var comments = []
37-
/** @type {string} */
38-
var key
39-
/** @type {EstreeNode|EstreeNode[]} */
40-
var value
41-
/** @type {number} */
42-
var index
43-
4435
// Done, we can quit.
4536
if (state.index === state.comments.length) {
4637
return
4738
}
4839

40+
/** @type {EstreeNode[]} */
41+
const children = []
42+
/** @type {EstreeComment[]} */
43+
const comments = []
44+
/** @type {string} */
45+
let key
46+
4947
// Find all children of `node`
5048
for (key in node) {
51-
value = node[key]
52-
53-
// Ignore comments.
54-
if (value && typeof value === 'object' && key !== 'comments') {
55-
if (Array.isArray(value)) {
56-
index = -1
57-
58-
while (++index < value.length) {
59-
if (value[index] && typeof value[index].type === 'string') {
60-
children.push(value[index])
49+
if (own.call(node, key)) {
50+
/** @type {EstreeNode|EstreeNode[]} */
51+
const value = node[key]
52+
53+
// Ignore comments.
54+
if (value && typeof value === 'object' && key !== 'comments') {
55+
if (Array.isArray(value)) {
56+
let index = -1
57+
58+
while (++index < value.length) {
59+
if (value[index] && typeof value[index].type === 'string') {
60+
children.push(value[index])
61+
}
6162
}
63+
} else if (typeof value.type === 'string') {
64+
children.push(value)
6265
}
63-
} else if (typeof value.type === 'string') {
64-
children.push(value)
6566
}
6667
}
6768
}
@@ -72,7 +73,7 @@ function walk(node, state) {
7273
// Initial comments.
7374
comments.push(...slice(state, node, false, {leading: true, trailing: false}))
7475

75-
index = -1
76+
let index = -1
7677

7778
while (++index < children.length) {
7879
walk(children[index], state)
@@ -82,11 +83,11 @@ function walk(node, state) {
8283
comments.push(
8384
...slice(state, node, true, {
8485
leading: false,
85-
trailing: Boolean(children.length)
86+
trailing: children.length > 0
8687
})
8788
)
8889

89-
if (comments.length) {
90+
if (comments.length > 0) {
9091
// @ts-expect-error, yes, because they’re nonstandard.
9192
node.comments = comments
9293
}
@@ -100,7 +101,7 @@ function walk(node, state) {
100101
*/
101102
function slice(state, node, compareEnd, fields) {
102103
/** @type {EstreeComment[]} */
103-
var result = []
104+
const result = []
104105

105106
while (
106107
state.comments[state.index] &&
@@ -119,7 +120,7 @@ function slice(state, node, compareEnd, fields) {
119120
* @returns {number}
120121
*/
121122
function compare(left, right, compareEnd) {
122-
var field = compareEnd ? 'end' : 'start'
123+
const field = compareEnd ? 'end' : 'start'
123124

124125
// Offsets.
125126
if (left.range && right.range) {
@@ -141,5 +142,5 @@ function compare(left, right, compareEnd) {
141142
return left.start - right[field]
142143
}
143144

144-
return NaN
145+
return Number.NaN
145146
}

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@
7070
"xo": {
7171
"prettier": true,
7272
"rules": {
73-
"no-var": "off",
74-
"prefer-arrow-callback": "off",
75-
"guard-for-in": "off",
76-
"max-depth": "off",
77-
"unicorn/explicit-length-check": "off",
78-
"unicorn/prefer-number-properties": "off"
73+
"max-depth": "off"
7974
}
8075
},
8176
"remarkConfig": {

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import * as acorn from 'acorn'
4545
import recast from 'recast'
4646
import {attachComments} from 'estree-util-attach-comments'
4747

48-
var comments = []
49-
var tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
48+
const comments = []
49+
const tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
5050

5151
attachComments(tree, comments)
5252

test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {attachComments} from './index.js'
1010
* @typedef {import('estree').Comment} EstreeComment
1111
*/
1212

13-
test('estree-attach-comments (recast)', function (t) {
13+
test('estree-attach-comments (recast)', (t) => {
1414
t.equal(
1515
recast.print(attachComments(...parse(''))).code,
1616
'',
@@ -79,10 +79,10 @@ test('estree-attach-comments (recast)', function (t) {
7979
)
8080

8181
/** @type {EstreeComment[]} */
82-
var comments = []
82+
let comments = []
8383
/** @type {EstreeProgram} */
8484
// @ts-expect-error
85-
var tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
85+
let tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
8686
ecmaVersion: 2020,
8787
// @ts-expect-error
8888
onComment: comments
@@ -170,10 +170,10 @@ test('estree-attach-comments (recast)', function (t) {
170170
*/
171171
function parse(doc) {
172172
/** @type {EstreeComment[]} */
173-
var comments = []
173+
const comments = []
174174
/** @type {EstreeProgram} */
175175
// @ts-expect-error
176-
var tree = acornParse(doc, {ecmaVersion: 2020, onComment: comments})
176+
const tree = acornParse(doc, {ecmaVersion: 2020, onComment: comments})
177177
return [tree, comments]
178178
}
179179

@@ -188,7 +188,7 @@ function removePositions(value) {
188188
/**
189189
* @param {EstreeNode} node
190190
*/
191-
function (node) {
191+
(node) => {
192192
// @ts-expect-error they most certainly exist.
193193
delete node.start
194194
// @ts-expect-error they most certainly exist.

0 commit comments

Comments
 (0)