oxc-parser 0.53.0 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -7
- package/bindings.js +2 -1
- package/index.d.ts +49 -4
- package/index.js +82 -2
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -2,20 +2,90 @@
|
|
|
2
2
|
|
|
3
3
|
## Features
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### ESTree
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
The returned JavaScript AST follows the [ESTree](https://github.com/estree/estree) specification.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
The full compiler is needed for such case, as the compiler does an additional semantic pass.
|
|
9
|
+
It is fully aligned with Acorn's AST, and any deviation would be considered a bug.
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
The returned TypeScript AST will conform to (`@typescript-eslint/typescript-estree`)[https://www.npmjs.com/package/@typescript-eslint/typescript-estree] in the near future.
|
|
12
|
+
|
|
13
|
+
### AST Types
|
|
14
|
+
|
|
15
|
+
[@oxc-project/types](https://www.npmjs.com/package/@oxc-project/types) can be used. For example:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Statement } from '@oxc-project/types';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Visitor
|
|
22
|
+
|
|
23
|
+
[oxc-walker](https://www.npmjs.com/package/oxc-walker) or [estree-walker](https://www.npmjs.com/package/estree-walker) can be used.
|
|
24
|
+
|
|
25
|
+
### Fast Mode
|
|
26
|
+
|
|
27
|
+
By default, Oxc parser does not produce semantic errors where symbols and scopes are needed.
|
|
28
|
+
|
|
29
|
+
To enable semantic errors, apply the option `showSemanticErrors: true`.
|
|
30
|
+
|
|
31
|
+
For example,
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
let foo;
|
|
35
|
+
let foo;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Does not produce any errors when `showSemanticErrors` is `false`, which is the default behavior.
|
|
39
|
+
|
|
40
|
+
Fast mode is best suited for parser plugins, where other parts of your build pipeline has already checked for errors.
|
|
41
|
+
|
|
42
|
+
Please note that turning off fast mode incurs a small performance overhead.
|
|
43
|
+
|
|
44
|
+
### ESTree compatibility
|
|
45
|
+
|
|
46
|
+
When parsing JS or JSX files, the AST returned is fully conformant with the
|
|
47
|
+
[ESTree standard](https://github.com/estree/estree).
|
|
48
|
+
|
|
49
|
+
When parsing TS or TSX files, the AST has additional properties related to TypeScript syntax.
|
|
50
|
+
These extra properties are broadly (but not entirely) in line with
|
|
51
|
+
[TypeScript ESLint](https://typescript-eslint.io/packages/parser/)'s AST.
|
|
52
|
+
|
|
53
|
+
If you need all ASTs in the same with-TS-properties format, use the `astType: 'ts'` option.
|
|
54
|
+
|
|
55
|
+
### Returns ESM information.
|
|
56
|
+
|
|
57
|
+
It is likely that you are writing a parser plugin that requires ESM information.
|
|
58
|
+
|
|
59
|
+
To avoid walking the AST again, Oxc Parser returns ESM information directly.
|
|
60
|
+
|
|
61
|
+
This information can be used to rewrite import and exports with the help of [`magic-string`](https://www.npmjs.com/package/magic-string),
|
|
62
|
+
without any AST manipulations.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
export interface EcmaScriptModule {
|
|
66
|
+
/**
|
|
67
|
+
* Has ESM syntax.
|
|
68
|
+
*
|
|
69
|
+
* i.e. `import` and `export` statements, and `import.meta`.
|
|
70
|
+
*
|
|
71
|
+
* Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
|
|
72
|
+
*/
|
|
73
|
+
hasModuleSyntax: boolean;
|
|
74
|
+
/** Import statements. */
|
|
75
|
+
staticImports: Array<StaticImport>;
|
|
76
|
+
/** Export statements. */
|
|
77
|
+
staticExports: Array<StaticExport>;
|
|
78
|
+
/** Dynamic import expressions. */
|
|
79
|
+
dynamicImports: Array<DynamicImport>;
|
|
80
|
+
/** Span positions` of `import.meta` */
|
|
81
|
+
importMetas: Array<Span>;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
14
84
|
|
|
15
85
|
## API
|
|
16
86
|
|
|
17
87
|
```javascript
|
|
18
|
-
import oxc from '
|
|
88
|
+
import oxc from 'oxc-parser';
|
|
19
89
|
|
|
20
90
|
const code = 'const url: String = /* 🤨 */ import.meta.url;';
|
|
21
91
|
|
package/bindings.js
CHANGED
|
@@ -374,8 +374,9 @@ module.exports.ParseResult = nativeBinding.ParseResult
|
|
|
374
374
|
module.exports.ExportExportNameKind = nativeBinding.ExportExportNameKind
|
|
375
375
|
module.exports.ExportImportNameKind = nativeBinding.ExportImportNameKind
|
|
376
376
|
module.exports.ExportLocalNameKind = nativeBinding.ExportLocalNameKind
|
|
377
|
+
module.exports.getBufferOffset = nativeBinding.getBufferOffset
|
|
377
378
|
module.exports.ImportNameKind = nativeBinding.ImportNameKind
|
|
378
379
|
module.exports.parseAsync = nativeBinding.parseAsync
|
|
379
380
|
module.exports.parseSync = nativeBinding.parseSync
|
|
380
|
-
module.exports.
|
|
381
|
+
module.exports.parseSyncRaw = nativeBinding.parseSyncRaw
|
|
381
382
|
module.exports.Severity = nativeBinding.Severity
|
package/index.d.ts
CHANGED
|
@@ -100,6 +100,14 @@ export declare const enum ExportLocalNameKind {
|
|
|
100
100
|
None = 'None'
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Get offset within a `Uint8Array` which is aligned on 4 GiB.
|
|
105
|
+
*
|
|
106
|
+
* Does not check that the offset is within bounds of `buffer`.
|
|
107
|
+
* To ensure it always is, provide a `Uint8Array` of at least 4 GiB size.
|
|
108
|
+
*/
|
|
109
|
+
export declare function getBufferOffset(buffer: Uint8Array): number
|
|
110
|
+
|
|
103
111
|
export interface ImportName {
|
|
104
112
|
kind: ImportNameKind
|
|
105
113
|
name?: string
|
|
@@ -134,6 +142,14 @@ export interface ParserOptions {
|
|
|
134
142
|
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
|
|
135
143
|
/** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */
|
|
136
144
|
lang?: 'js' | 'jsx' | 'ts' | 'tsx'
|
|
145
|
+
/**
|
|
146
|
+
* Return an AST which includes TypeScript-related properties, or excludes them.
|
|
147
|
+
*
|
|
148
|
+
* `'js'` is default for JS / JSX files.
|
|
149
|
+
* `'ts'` is default for TS / TSX files.
|
|
150
|
+
* The type of the file is determined from `lang` option, or extension of provided `filename`.
|
|
151
|
+
*/
|
|
152
|
+
astType?: 'js' | 'ts'
|
|
137
153
|
/**
|
|
138
154
|
* Emit `ParenthesizedExpression` in AST.
|
|
139
155
|
*
|
|
@@ -141,20 +157,49 @@ export interface ParserOptions {
|
|
|
141
157
|
* (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property
|
|
142
158
|
* containing the expression inside parentheses.
|
|
143
159
|
*
|
|
144
|
-
*
|
|
160
|
+
* @default true
|
|
145
161
|
*/
|
|
146
162
|
preserveParens?: boolean
|
|
163
|
+
/**
|
|
164
|
+
* Produce semantic errors with an additional AST pass.
|
|
165
|
+
* Semantic errors depend on symbols and scopes, where the parser does not construct.
|
|
166
|
+
* This adds a small performance overhead.
|
|
167
|
+
*
|
|
168
|
+
* @default false
|
|
169
|
+
*/
|
|
170
|
+
showSemanticErrors?: boolean
|
|
147
171
|
}
|
|
148
172
|
|
|
149
173
|
/** Parse synchronously. */
|
|
150
174
|
export declare function parseSync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): ParseResult
|
|
151
175
|
|
|
152
176
|
/**
|
|
153
|
-
*
|
|
177
|
+
* Parses AST into provided `Uint8Array` buffer.
|
|
178
|
+
*
|
|
179
|
+
* Source text must be written into the start of the buffer, and its length (in UTF-8 bytes)
|
|
180
|
+
* provided as `source_len`.
|
|
181
|
+
*
|
|
182
|
+
* This function will parse the source, and write the AST into the buffer, starting at the end.
|
|
183
|
+
*
|
|
184
|
+
* It also writes to the very end of the buffer the offset of `Program` within the buffer.
|
|
185
|
+
*
|
|
186
|
+
* Caller can deserialize data from the buffer on JS side.
|
|
187
|
+
*
|
|
188
|
+
* # SAFETY
|
|
189
|
+
*
|
|
190
|
+
* Caller must ensure:
|
|
191
|
+
* * Source text is written into start of the buffer.
|
|
192
|
+
* * Source text's UTF-8 byte length is `source_len`.
|
|
193
|
+
* * The 1st `source_len` bytes of the buffer comprises a valid UTF-8 string.
|
|
194
|
+
*
|
|
195
|
+
* If source text is originally a JS string on JS side, and converted to a buffer with
|
|
196
|
+
* `Buffer.from(str)` or `new TextEncoder().encode(str)`, this guarantees it's valid UTF-8.
|
|
197
|
+
*
|
|
198
|
+
* # Panics
|
|
154
199
|
*
|
|
155
|
-
*
|
|
200
|
+
* Panics if source text is too long, or AST takes more memory than is available in the buffer.
|
|
156
201
|
*/
|
|
157
|
-
export declare function
|
|
202
|
+
export declare function parseSyncRaw(filename: string, buffer: Uint8Array, sourceLen: number, options?: ParserOptions | undefined | null): void
|
|
158
203
|
|
|
159
204
|
export declare const enum Severity {
|
|
160
205
|
Error = 'Error',
|
package/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const bindings = require('./bindings.js');
|
|
2
|
+
const deserializeJS = require('./deserialize-js.js');
|
|
3
|
+
const deserializeTS = require('./deserialize-ts.js');
|
|
2
4
|
|
|
3
5
|
module.exports.ParseResult = bindings.ParseResult;
|
|
4
6
|
module.exports.ExportExportNameKind = bindings.ExportExportNameKind;
|
|
@@ -55,6 +57,84 @@ module.exports.parseAsync = async function parseAsync(...args) {
|
|
|
55
57
|
return wrap(await bindings.parseAsync(...args));
|
|
56
58
|
};
|
|
57
59
|
|
|
58
|
-
module.exports.parseSync = function parseSync(
|
|
59
|
-
|
|
60
|
+
module.exports.parseSync = function parseSync(filename, sourceText, options) {
|
|
61
|
+
if (options?.experimentalRawTransfer) {
|
|
62
|
+
return parseSyncRaw(filename, sourceText, options);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return wrap(bindings.parseSync(filename, sourceText, options));
|
|
60
66
|
};
|
|
67
|
+
|
|
68
|
+
let buffer, encoder;
|
|
69
|
+
|
|
70
|
+
function parseSyncRaw(filename, sourceText, options) {
|
|
71
|
+
// Delete `experimentalRawTransfer` option
|
|
72
|
+
let experimentalRawTransfer;
|
|
73
|
+
({ experimentalRawTransfer, ...options } = options);
|
|
74
|
+
|
|
75
|
+
// Create buffer and `TextEncoder`
|
|
76
|
+
if (!buffer) {
|
|
77
|
+
buffer = createBuffer();
|
|
78
|
+
encoder = new TextEncoder();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Write source into start of buffer.
|
|
82
|
+
// `TextEncoder` cannot write into a `Uint8Array` larger than 1 GiB,
|
|
83
|
+
// so create a view into buffer of this size to write into.
|
|
84
|
+
const sourceBuffer = new Uint8Array(buffer.buffer, buffer.byteOffset, ONE_GIB);
|
|
85
|
+
const { read, written: sourceByteLen } = encoder.encodeInto(sourceText, sourceBuffer);
|
|
86
|
+
if (read !== sourceText.length) {
|
|
87
|
+
throw new Error('Failed to write source text into buffer');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Parse
|
|
91
|
+
bindings.parseSyncRaw(filename, buffer, sourceByteLen, options);
|
|
92
|
+
|
|
93
|
+
// Deserialize.
|
|
94
|
+
// We cannot lazily deserialize in the getters, because the buffer might be re-used to parse
|
|
95
|
+
// another file before the getter is called.
|
|
96
|
+
|
|
97
|
+
// (2 * 1024 * 1024 * 1024 - 12)
|
|
98
|
+
const astTypeFlagPos = 2147483636;
|
|
99
|
+
let isJsAst = buffer[astTypeFlagPos] === 0;
|
|
100
|
+
|
|
101
|
+
const data = isJsAst
|
|
102
|
+
? deserializeJS(buffer, sourceText, sourceByteLen)
|
|
103
|
+
: deserializeTS(buffer, sourceText, sourceByteLen);
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
get program() {
|
|
107
|
+
return data.program;
|
|
108
|
+
},
|
|
109
|
+
get module() {
|
|
110
|
+
return data.module;
|
|
111
|
+
},
|
|
112
|
+
get comments() {
|
|
113
|
+
return data.comments;
|
|
114
|
+
},
|
|
115
|
+
get errors() {
|
|
116
|
+
return data.errors;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const ONE_GIB = 1 << 30,
|
|
122
|
+
TWO_GIB = ONE_GIB * 2,
|
|
123
|
+
SIX_GIB = ONE_GIB * 6;
|
|
124
|
+
|
|
125
|
+
// Create a `Uint8Array` which is 2 GiB in size, with its start aligned on 4 GiB.
|
|
126
|
+
//
|
|
127
|
+
// Achieve this by creating a 6 GiB `ArrayBuffer`, getting the offset within it that's aligned to 4 GiB,
|
|
128
|
+
// chopping off that number of bytes from the start, and shortening to 2 GiB.
|
|
129
|
+
//
|
|
130
|
+
// It's always possible to obtain a 2 GiB slice aligned on 4 GiB within a 6 GiB buffer,
|
|
131
|
+
// no matter how the 6 GiB buffer is aligned.
|
|
132
|
+
//
|
|
133
|
+
// Note: On systems with virtual memory, this only consumes 6 GiB of *virtual* memory.
|
|
134
|
+
// It does not consume physical memory until data is actually written to the `Uint8Array`.
|
|
135
|
+
// Physical memory consumed corresponds to the quantity of data actually written.
|
|
136
|
+
function createBuffer() {
|
|
137
|
+
const arrayBuffer = new ArrayBuffer(SIX_GIB);
|
|
138
|
+
const offset = bindings.getBufferOffset(new Uint8Array(arrayBuffer));
|
|
139
|
+
return new Uint8Array(arrayBuffer, offset, TWO_GIB);
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oxc-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.55.0",
|
|
4
4
|
"description": "Oxc Parser Node API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Parser"
|
|
@@ -24,16 +24,16 @@
|
|
|
24
24
|
"bindings.js"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@oxc-project/types": "^0.
|
|
27
|
+
"@oxc-project/types": "^0.55.0"
|
|
28
28
|
},
|
|
29
29
|
"optionalDependencies": {
|
|
30
|
-
"@oxc-parser/binding-win32-x64-msvc": "0.
|
|
31
|
-
"@oxc-parser/binding-win32-arm64-msvc": "0.
|
|
32
|
-
"@oxc-parser/binding-linux-x64-gnu": "0.
|
|
33
|
-
"@oxc-parser/binding-linux-arm64-gnu": "0.
|
|
34
|
-
"@oxc-parser/binding-linux-x64-musl": "0.
|
|
35
|
-
"@oxc-parser/binding-linux-arm64-musl": "0.
|
|
36
|
-
"@oxc-parser/binding-darwin-x64": "0.
|
|
37
|
-
"@oxc-parser/binding-darwin-arm64": "0.
|
|
30
|
+
"@oxc-parser/binding-win32-x64-msvc": "0.55.0",
|
|
31
|
+
"@oxc-parser/binding-win32-arm64-msvc": "0.55.0",
|
|
32
|
+
"@oxc-parser/binding-linux-x64-gnu": "0.55.0",
|
|
33
|
+
"@oxc-parser/binding-linux-arm64-gnu": "0.55.0",
|
|
34
|
+
"@oxc-parser/binding-linux-x64-musl": "0.55.0",
|
|
35
|
+
"@oxc-parser/binding-linux-arm64-musl": "0.55.0",
|
|
36
|
+
"@oxc-parser/binding-darwin-x64": "0.55.0",
|
|
37
|
+
"@oxc-parser/binding-darwin-arm64": "0.55.0"
|
|
38
38
|
}
|
|
39
39
|
}
|