oxc-parser 0.39.0 → 0.40.1
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 +42 -11
- package/bindings.js +3 -0
- package/index.d.ts +77 -7
- package/index.js +36 -6
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -1,20 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Oxc Parser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Features
|
|
4
|
+
|
|
5
|
+
- Returns ESM information.
|
|
6
|
+
- Built-in `magic-string` on the Rust side exposed through N-API.
|
|
7
|
+
- "clever" approach to overcome the Rust UTF8 vs JavaScript UTF16 length problem.
|
|
8
|
+
|
|
9
|
+
## Caveat
|
|
10
|
+
|
|
11
|
+
The parser alone does not fully check for syntax errors that are associated with semantic data (symbols and scopes).
|
|
12
|
+
The full compiler is needed for such case, as the compiler does an additional semantic pass.
|
|
13
|
+
|
|
14
|
+
With this caveat, `oxc-parser` is best suited for parser plugins,
|
|
15
|
+
where you need quick access to ESM information, as well as fast `magic-string` operations.
|
|
16
|
+
|
|
17
|
+
## API
|
|
4
18
|
|
|
5
19
|
```javascript
|
|
6
|
-
import
|
|
7
|
-
import oxc from 'oxc-parser';
|
|
20
|
+
import oxc from './index.js';
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
|
|
22
|
+
// The emoji makes the span of `import.meta.url` to be different in UTF8 and UTF16.
|
|
23
|
+
const code = 'const url: String = /* 🤨 */ import.meta.url;';
|
|
24
|
+
|
|
25
|
+
// File extension is used to determine which dialect to parse source as.
|
|
11
26
|
const filename = 'test.tsx';
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
28
|
+
const result = oxc.parseSync(filename, code);
|
|
29
|
+
// or `await oxc.parseAsync(filename, code)`
|
|
30
|
+
|
|
31
|
+
// An array of errors, if any.
|
|
32
|
+
console.log(result.errors);
|
|
33
|
+
|
|
34
|
+
// AST and comments.
|
|
35
|
+
console.log(result.program, result.comments);
|
|
36
|
+
|
|
37
|
+
// ESM information - imports, exports, `import.meta`s.
|
|
38
|
+
console.log(result.module);
|
|
39
|
+
|
|
40
|
+
// A `magic-string` instance for accessing and manipulating the source text.
|
|
41
|
+
// All returned spans are in UTF8 offsets, which cannot be used directly on our JavaScript.
|
|
42
|
+
// JavaScript string lengths are in UTF16 offsets.
|
|
43
|
+
const ms = result.magicString;
|
|
15
44
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
45
|
+
for (const span of result.module.importMetas) {
|
|
46
|
+
// Extra methods for access the source text through spans with UTF8 offsets.
|
|
47
|
+
console.log(ms.getSourceText(span.start, span.end)); // prints `import.meta`
|
|
48
|
+
console.log(ms.getLineColumnNumber(span.start)); // prints `{ line: 0, column: 20 }`
|
|
49
|
+
console.log(code.substring(ms.getUtf16ByteOffset(span.start)).startsWith('import.meta.url')); // prints `true`
|
|
19
50
|
}
|
|
20
51
|
```
|
package/bindings.js
CHANGED
|
@@ -361,6 +361,8 @@ if (!nativeBinding) {
|
|
|
361
361
|
throw new Error(`Failed to load native binding`)
|
|
362
362
|
}
|
|
363
363
|
|
|
364
|
+
module.exports.MagicString = nativeBinding.MagicString
|
|
365
|
+
module.exports.ParseResult = nativeBinding.ParseResult
|
|
364
366
|
module.exports.ExportExportNameKind = nativeBinding.ExportExportNameKind
|
|
365
367
|
module.exports.ExportImportNameKind = nativeBinding.ExportImportNameKind
|
|
366
368
|
module.exports.ExportLocalNameKind = nativeBinding.ExportLocalNameKind
|
|
@@ -368,3 +370,4 @@ module.exports.ImportNameKind = nativeBinding.ImportNameKind
|
|
|
368
370
|
module.exports.parseAsync = nativeBinding.parseAsync
|
|
369
371
|
module.exports.parseSync = nativeBinding.parseSync
|
|
370
372
|
module.exports.parseWithoutReturn = nativeBinding.parseWithoutReturn
|
|
373
|
+
module.exports.Severity = nativeBinding.Severity
|
package/index.d.ts
CHANGED
|
@@ -2,6 +2,34 @@
|
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
|
|
4
4
|
export * from '@oxc-project/types';
|
|
5
|
+
export declare class MagicString {
|
|
6
|
+
/** Get source text from utf8 offset. */
|
|
7
|
+
getSourceText(start: number, end: number): string
|
|
8
|
+
/** Get 0-based line and column number from utf8 offset. */
|
|
9
|
+
getLineColumnNumber(offset: number): LineColumn
|
|
10
|
+
/** Get UTF16 byte offset from UTF8 byte offset. */
|
|
11
|
+
getUtf16ByteOffset(offset: number): number
|
|
12
|
+
length(): number
|
|
13
|
+
toString(): string
|
|
14
|
+
append(input: string): this
|
|
15
|
+
appendLeft(index: number, input: string): this
|
|
16
|
+
appendRight(index: number, input: string): this
|
|
17
|
+
indent(): this
|
|
18
|
+
prepend(input: string): this
|
|
19
|
+
prependLeft(index: number, input: string): this
|
|
20
|
+
prependRight(index: number, input: string): this
|
|
21
|
+
relocate(start: number, end: number, to: number): this
|
|
22
|
+
remove(start: number, end: number): this
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export declare class ParseResult {
|
|
26
|
+
get program(): import("@oxc-project/types").Program
|
|
27
|
+
get module(): EcmaScriptModule
|
|
28
|
+
get comments(): Array<Comment>
|
|
29
|
+
get errors(): Array<OxcError>
|
|
30
|
+
get magicString(): MagicString
|
|
31
|
+
}
|
|
32
|
+
|
|
5
33
|
export interface Comment {
|
|
6
34
|
type: 'Line' | 'Block'
|
|
7
35
|
value: string
|
|
@@ -10,10 +38,26 @@ export interface Comment {
|
|
|
10
38
|
}
|
|
11
39
|
|
|
12
40
|
export interface EcmaScriptModule {
|
|
41
|
+
/**
|
|
42
|
+
* Has ESM syntax.
|
|
43
|
+
*
|
|
44
|
+
* i.e. `import` and `export` statements, and `import.meta`.
|
|
45
|
+
*
|
|
46
|
+
* Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
|
|
47
|
+
*/
|
|
48
|
+
hasModuleSyntax: boolean
|
|
13
49
|
/** Import Statements. */
|
|
14
50
|
staticImports: Array<StaticImport>
|
|
15
51
|
/** Export Statements. */
|
|
16
52
|
staticExports: Array<StaticExport>
|
|
53
|
+
/** Span positions` of `import.meta` */
|
|
54
|
+
importMetas: Array<Span>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ErrorLabel {
|
|
58
|
+
message?: string
|
|
59
|
+
start: number
|
|
60
|
+
end: number
|
|
17
61
|
}
|
|
18
62
|
|
|
19
63
|
export interface ExportExportName {
|
|
@@ -85,6 +129,22 @@ export declare const enum ImportNameKind {
|
|
|
85
129
|
Default = 'Default'
|
|
86
130
|
}
|
|
87
131
|
|
|
132
|
+
export interface LineColumn {
|
|
133
|
+
line: number
|
|
134
|
+
column: number
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface OverwriteOptions {
|
|
138
|
+
contentOnly: boolean
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface OxcError {
|
|
142
|
+
severity: Severity
|
|
143
|
+
message: string
|
|
144
|
+
labels: Array<ErrorLabel>
|
|
145
|
+
helpMessage?: string
|
|
146
|
+
}
|
|
147
|
+
|
|
88
148
|
/**
|
|
89
149
|
* Parse asynchronously.
|
|
90
150
|
*
|
|
@@ -92,13 +152,6 @@ export declare const enum ImportNameKind {
|
|
|
92
152
|
*/
|
|
93
153
|
export declare function parseAsync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): Promise<ParseResult>
|
|
94
154
|
|
|
95
|
-
export interface ParseResult {
|
|
96
|
-
program: import("@oxc-project/types").Program
|
|
97
|
-
module: EcmaScriptModule
|
|
98
|
-
comments: Array<Comment>
|
|
99
|
-
errors: Array<string>
|
|
100
|
-
}
|
|
101
|
-
|
|
102
155
|
export interface ParserOptions {
|
|
103
156
|
sourceType?: 'script' | 'module' | 'unambiguous' | undefined
|
|
104
157
|
/** Treat the source text as `js`, `jsx`, `ts`, or `tsx`. */
|
|
@@ -125,6 +178,23 @@ export declare function parseSync(filename: string, sourceText: string, options?
|
|
|
125
178
|
*/
|
|
126
179
|
export declare function parseWithoutReturn(filename: string, sourceText: string, options?: ParserOptions | undefined | null): void
|
|
127
180
|
|
|
181
|
+
export declare const enum Severity {
|
|
182
|
+
Error = 'Error',
|
|
183
|
+
Warning = 'Warning',
|
|
184
|
+
Advice = 'Advice'
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface SourceMapOptions {
|
|
188
|
+
includeContent?: boolean
|
|
189
|
+
source?: string
|
|
190
|
+
hires?: boolean
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface Span {
|
|
194
|
+
start: number
|
|
195
|
+
end: number
|
|
196
|
+
}
|
|
197
|
+
|
|
128
198
|
export interface StaticExport {
|
|
129
199
|
start: number
|
|
130
200
|
end: number
|
package/index.js
CHANGED
|
@@ -1,14 +1,44 @@
|
|
|
1
1
|
const bindings = require('./bindings.js');
|
|
2
2
|
|
|
3
|
+
module.exports.MagicString = bindings.MagicString;
|
|
4
|
+
module.exports.ParseResult = bindings.ParseResult;
|
|
5
|
+
module.exports.ExportExportNameKind = bindings.ExportExportNameKind;
|
|
6
|
+
module.exports.ExportImportNameKind = bindings.ExportImportNameKind;
|
|
7
|
+
module.exports.ExportLocalNameKind = bindings.ExportLocalNameKind;
|
|
8
|
+
module.exports.ImportNameKind = bindings.ImportNameKind;
|
|
3
9
|
module.exports.parseWithoutReturn = bindings.parseWithoutReturn;
|
|
10
|
+
module.exports.Severity = bindings.Severity;
|
|
11
|
+
|
|
12
|
+
function wrap(result) {
|
|
13
|
+
let program, module, comments, errors, magicString;
|
|
14
|
+
return {
|
|
15
|
+
get program() {
|
|
16
|
+
if (!program) program = JSON.parse(result.program);
|
|
17
|
+
return program;
|
|
18
|
+
},
|
|
19
|
+
get module() {
|
|
20
|
+
if (!module) module = result.module;
|
|
21
|
+
return module;
|
|
22
|
+
},
|
|
23
|
+
get comments() {
|
|
24
|
+
if (!comments) comments = result.comments;
|
|
25
|
+
return comments;
|
|
26
|
+
},
|
|
27
|
+
get errors() {
|
|
28
|
+
if (!errors) errors = result.errors;
|
|
29
|
+
return errors;
|
|
30
|
+
},
|
|
31
|
+
get magicString() {
|
|
32
|
+
if (!magicString) magicString = result.magicString;
|
|
33
|
+
return magicString;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
4
37
|
|
|
5
38
|
module.exports.parseAsync = async function parseAsync(...args) {
|
|
6
|
-
|
|
7
|
-
result.program = JSON.parse(result.program);
|
|
8
|
-
return result;
|
|
39
|
+
return wrap(await bindings.parseAsync(...args));
|
|
9
40
|
};
|
|
41
|
+
|
|
10
42
|
module.exports.parseSync = function parseSync(...args) {
|
|
11
|
-
|
|
12
|
-
result.program = JSON.parse(result.program);
|
|
13
|
-
return result;
|
|
43
|
+
return wrap(bindings.parseSync(...args));
|
|
14
44
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oxc-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.1",
|
|
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.40.1"
|
|
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.40.1",
|
|
31
|
+
"@oxc-parser/binding-win32-arm64-msvc": "0.40.1",
|
|
32
|
+
"@oxc-parser/binding-linux-x64-gnu": "0.40.1",
|
|
33
|
+
"@oxc-parser/binding-linux-arm64-gnu": "0.40.1",
|
|
34
|
+
"@oxc-parser/binding-linux-x64-musl": "0.40.1",
|
|
35
|
+
"@oxc-parser/binding-linux-arm64-musl": "0.40.1",
|
|
36
|
+
"@oxc-parser/binding-darwin-x64": "0.40.1",
|
|
37
|
+
"@oxc-parser/binding-darwin-arm64": "0.40.1"
|
|
38
38
|
}
|
|
39
39
|
}
|