Skip to content

Commit 2ab6cf8

Browse files
PetrLaskevicmarijnh
authored andcommitted
Docs: Announce both ESM and CommonJS imports are supported, change all examples to ESM imports
1 parent f9da2f3 commit 2ab6cf8

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

acorn-loose/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ placeholders in places where it can't make sense of the input. Depends
4949
on the `acorn` package, because it uses the same tokenizer.
5050

5151
```javascript
52-
var acornLoose = require("acorn-loose");
52+
import * as acornLoose from "acorn-loose";
5353
console.log(acornLoose.parse("1 / * 4 )[2]", {ecmaVersion: 2020}));
5454
```
5555

acorn-walk/README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,29 @@ produce a meaningful state. (An example of a use of state is to track
4747
scope at each point in the tree.)
4848

4949
```js
50-
const acorn = require("acorn")
51-
const walk = require("acorn-walk")
50+
import * as acorn from "acorn";
51+
import * as walk from "acorn-walk";
5252

5353
walk.simple(acorn.parse("let x = 10"), {
5454
Literal(node) {
55-
console.log(`Found a literal: ${node.value}`)
55+
console.log(`Found a literal: ${node.value}`);
5656
}
57-
})
57+
});
5858
```
5959

6060
**ancestor**`(node, visitors, base, state)` does a 'simple' walk over
6161
a tree, building up an array of ancestor nodes (including the current node)
6262
and passing the array to the callbacks as a third parameter.
6363

6464
```js
65-
const acorn = require("acorn")
66-
const walk = require("acorn-walk")
65+
import * as acorn from "acorn";
66+
import * as walk from "acorn-walk";
6767

6868
walk.ancestor(acorn.parse("foo('hi')"), {
6969
Literal(_node, _state, ancestors) {
70-
console.log("This literal's ancestors are:", ancestors.map(n => n.type))
70+
console.log("This literal's ancestors are:", ancestors.map(n => n.type));
7171
}
72-
})
72+
});
7373
```
7474

7575
**recursive**`(node, state, functions, base)` does a 'recursive'
@@ -97,12 +97,12 @@ current node) and passing the array to the callbacks as a third
9797
parameter.
9898

9999
```js
100-
const acorn = require("acorn")
101-
const walk = require("acorn-walk")
100+
import * as acorn from "acorn";
101+
import * as walk from "acorn-walk";
102102

103103
walk.full(acorn.parse("1 + 1"), node => {
104-
console.log(`There's a ${node.type} node at ${node.ch}`)
105-
})
104+
console.log(`There's a ${node.type} node at ${node.ch}`);
105+
});
106106
```
107107

108108
**findNodeAt**`(node, start, end, test, base, state)` tries to locate

acorn/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ git clone https://github.com/acornjs/acorn.git
2626
cd acorn
2727
npm install
2828
```
29+
## Importing acorn
30+
31+
ESM as well as CommonJS is supported for all 3: `acorn`, `acorn-walk` and `acorn-loose`.
32+
33+
ESM example for `acorn`:
34+
35+
```js
36+
import * as acorn from "acorn";
37+
```
38+
39+
CommonJS example for `acorn`:
40+
41+
```js
42+
let acorn = require("acorn");
43+
```
44+
45+
ESM is preferred, as it allows better editor auto-completions by offering TypeScript support.
46+
For this reason, following examples will use ESM imports.
2947

3048
## Interface
3149

@@ -36,7 +54,7 @@ syntax tree object as specified by the [ESTree
3654
spec](https://github.com/estree/estree).
3755

3856
```javascript
39-
let acorn = require("acorn");
57+
import * as acorn from "acorn";
4058
console.log(acorn.parse("1 + 1", {ecmaVersion: 2020}));
4159
```
4260

0 commit comments

Comments
 (0)