Skip to content

Commit 1a84b06

Browse files
committed
Use ESM
1 parent 7bdbd61 commit 1a84b06

File tree

6 files changed

+29
-33
lines changed

6 files changed

+29
-33
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
'use strict'
2-
3-
module.exports = attachComments
4-
51
var push = [].push
62

7-
function attachComments(tree, comments) {
3+
export function attachComments(tree, comments) {
84
walk(tree, {comments: comments.concat().sort(compare), index: 0})
95
return tree
106
}

package.json

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
"contributors": [
2525
"Titus Wormer <[email protected]> (https://wooorm.com)"
2626
],
27+
"sideEffects": false,
28+
"type": "module",
29+
"main": "index.js",
2730
"files": [
2831
"index.js"
2932
],
3033
"devDependencies": {
3134
"acorn": "^8.0.0",
35+
"c8": "^7.0.0",
3236
"estree-walker": "^2.0.0",
33-
"nyc": "^15.0.0",
3437
"prettier": "^2.0.0",
3538
"recast": "^0.20.0",
3639
"remark-cli": "^9.0.0",
@@ -40,8 +43,8 @@
4043
},
4144
"scripts": {
4245
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
43-
"test-api": "node test",
44-
"test-coverage": "nyc --reporter lcov tape test.js",
46+
"test-api": "node test.js",
47+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
4548
"test": "npm run format && npm run test-coverage"
4649
},
4750
"prettier": {
@@ -54,20 +57,15 @@
5457
},
5558
"xo": {
5659
"prettier": true,
57-
"esnext": false,
5860
"rules": {
61+
"no-var": "off",
62+
"prefer-arrow-callback": "off",
5963
"guard-for-in": "off",
6064
"max-depth": "off",
6165
"unicorn/explicit-length-check": "off",
6266
"unicorn/prefer-number-properties": "off"
6367
}
6468
},
65-
"nyc": {
66-
"check-coverage": true,
67-
"lines": 100,
68-
"functions": 100,
69-
"branches": 100
70-
},
7169
"remarkConfig": {
7270
"plugins": [
7371
"preset-wooorm"

readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ tree.
1414

1515
## Install
1616

17+
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
18+
instead of `require`d.
19+
1720
[npm][]:
1821

1922
```sh
@@ -31,9 +34,9 @@ Say we have this weird `code`:
3134
And our script, `example.js`, looks as follows:
3235

3336
```js
34-
var acorn = require('acorn')
35-
var recast = require('recast')
36-
var attachComments = require('estree-util-attach-comments')
37+
import * as acorn from 'acorn'
38+
import recast from 'recast'
39+
import {attachComments} from 'estree-util-attach-comments'
3740

3841
var comments = []
3942
var tree = acorn.parse(code, {ecmaVersion: 2020, onComment: comments})
@@ -68,6 +71,9 @@ And, some of these weird comments are off, but they’re pretty close.
6871

6972
## API
7073

74+
This package exports the following identifiers: `attachComment`.
75+
There is no default export.
76+
7177
### `attachComment(tree, comments)`
7278

7379
Attach semistandard estree comment nodes to the tree.

test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var acorn = require('acorn')
5-
var recast = require('recast')
6-
var walk = require('estree-walker').walk
7-
var attachComments = require('.')
1+
import test from 'tape'
2+
import {parse as acornParse} from 'acorn'
3+
import recast from 'recast'
4+
import {walk} from 'estree-walker'
5+
import {attachComments} from './index.js'
86

97
test('estree-attach-comments (recast)', function (t) {
108
t.equal(
@@ -75,7 +73,7 @@ test('estree-attach-comments (recast)', function (t) {
7573
)
7674

7775
var comments = []
78-
var tree = acorn.parse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
76+
var tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
7977
ecmaVersion: 2020,
8078
onComment: comments
8179
})
@@ -89,7 +87,7 @@ test('estree-attach-comments (recast)', function (t) {
8987
)
9088

9189
comments = []
92-
tree = acorn.parse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
90+
tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
9391
ecmaVersion: 2020,
9492
onComment: comments
9593
})
@@ -103,7 +101,7 @@ test('estree-attach-comments (recast)', function (t) {
103101
)
104102

105103
comments = []
106-
tree = acorn.parse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
104+
tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
107105
ecmaVersion: 2020,
108106
ranges: true,
109107
onComment: comments
@@ -118,7 +116,7 @@ test('estree-attach-comments (recast)', function (t) {
118116
)
119117

120118
comments = []
121-
tree = acorn.parse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
119+
tree = acornParse('/* 1 */ a /* 2 */ + /* 3 */ 1', {
122120
ecmaVersion: 2020,
123121
locations: true,
124122
onComment: comments
@@ -137,13 +135,13 @@ test('estree-attach-comments (recast)', function (t) {
137135

138136
function parse(doc) {
139137
var comments = []
140-
var tree = acorn.parse(doc, {ecmaVersion: 2020, onComment: comments})
138+
var tree = acornParse(doc, {ecmaVersion: 2020, onComment: comments})
141139
return [tree, comments]
142140
}
143141

144142
function removePositions(value) {
145143
walk(value, {
146-
enter: function (node) {
144+
enter(node) {
147145
delete node.start
148146
delete node.end
149147
}

0 commit comments

Comments
 (0)