Skip to content

Commit fa82f99

Browse files
committed
parse real file
1 parent af7dd52 commit fa82f99

File tree

3 files changed

+55
-49
lines changed

3 files changed

+55
-49
lines changed

acorn.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
'use strict'
22

33
const acorn = require('acorn')
4+
const file = require('fs').readFileSync('./node_modules/express/lib/request.js', 'utf8')
45

5-
const code = `async function test () {
6-
const foo = globalThis.query()
6+
// console.log('hello')
7+
acorn.parse(file)
78

8-
return await foo
9-
}
9+
// const code = `async function test () {
10+
// const foo = globalThis.query()
1011

11-
foo()`
12+
// return await foo
13+
// }
1214

13-
const ast = acorn.parse(code, { ecmaVersion: 2020 })
15+
// foo()`
1416

15-
console.log(ast.body)
17+
// const ast = acorn.parse(code, { ecmaVersion: 2020 })
1618

17-
const before = code.slice(0, 24)
18-
const body = code.slice(24, 78)
19-
const after = code.slice(78)
19+
// console.log(ast.body)
2020

21-
const prefix = 'const ctx = {};return tracingChannel(\'test\').tracePromise(ctx, async () => {'
22-
const suffix = '})'
21+
// const before = code.slice(0, 24)
22+
// const body = code.slice(24, 78)
23+
// const after = code.slice(78)
2324

24-
const patched = before + prefix + body + suffix + after
25+
// const prefix = 'const ctx = {};return tracingChannel(\'test\').tracePromise(ctx, async () => {'
26+
// const suffix = '})'
2527

26-
console.log(patched)
27-
console.log(acorn.parse(patched))
28+
// const patched = before + prefix + body + suffix + after
29+
30+
// console.log(patched)
31+
// console.log(acorn.parse(patched))

oxc.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
'use strict'
22

33
const { parseSync, Visitor } = require('oxc-parser')
4+
const code = require('fs').readFileSync('./node_modules/express/lib/request.js', 'utf8')
45

5-
const code = 'const url: String = /* 🤨 */ import.meta.url;'
6+
// const code = 'const url: String = /* 🤨 */ import.meta.url;'
67

78
// File extension is used to determine which dialect to parse source as.
8-
const filename = 'test.tsx'
9+
const filename = 'request.js'
910

1011
const result = parseSync(filename, code)
1112
// or `await parseAsync(filename, code)`
1213

1314
// An array of errors, if any.
1415
console.log(result.errors)
1516

16-
// AST and comments.
17-
console.log(result.program, result.comments)
17+
// // AST and comments.
18+
// console.log(result.program, result.comments)
1819

19-
// ESM information - imports, exports, `import.meta`s.
20-
console.log(result.module)
20+
// // ESM information - imports, exports, `import.meta`s.
21+
// console.log(result.module)
2122

2223
// Visit the AST
23-
const visitations = []
24-
25-
const visitor = new Visitor({
26-
VariableDeclaration (decl) {
27-
visitations.push(`enter ${decl.kind}`)
28-
},
29-
'VariableDeclaration:exit' (decl) {
30-
visitations.push(`exit ${decl.kind}`)
31-
},
32-
Identifier (ident) {
33-
visitations.push(ident.name)
34-
},
35-
})
36-
37-
visitor.visit(result.program)
24+
// const visitations = []
25+
26+
// const visitor = new Visitor({
27+
// VariableDeclaration (decl) {
28+
// visitations.push(`enter ${decl.kind}`)
29+
// },
30+
// 'VariableDeclaration:exit' (decl) {
31+
// visitations.push(`exit ${decl.kind}`)
32+
// },
33+
// Identifier (ident) {
34+
// visitations.push(ident.name)
35+
// },
36+
// })
37+
38+
// visitor.visit(result.program)
3839

3940
// Logs: [ 'enter const', 'url', 'String', 'import', 'meta', 'url', 'exit const' ]
40-
console.log(visitations)
41+
// console.log(visitations)

swc.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@
33
const swc = require('@swc/core')
44

55
async function parseCode () {
6-
const code = `
7-
import { useState } from 'react';
6+
const code = require('fs').readFileSync('./node_modules/express/lib/request.js', 'utf8')
7+
// const code = `
8+
// import { useState } from 'react';
89

9-
function MyComponent() {
10-
const [count, setCount] = useState(0);
10+
// function MyComponent() {
11+
// const [count, setCount] = useState(0);
1112

12-
const increment = () => {
13-
setCount(count + 1);
14-
};
13+
// const increment = () => {
14+
// setCount(count + 1);
15+
// };
1516

16-
return 'test';
17-
}
18-
`
17+
// return 'test';
18+
// }
19+
// `
1920

2021
try {
2122
const ast = await swc.parse(code, {
2223
syntax: 'ecmascript',
23-
isModule: true,
24+
isModule: false,
2425
comments: false,
2526
})
2627

27-
console.log(JSON.stringify(ast, null, 2))
28+
// console.log(JSON.stringify(ast, null, 2))
2829
} catch (error) {
2930
console.error('Error parsing code:', error)
3031
}

0 commit comments

Comments
 (0)