oxc-parser 0.52.0 → 0.54.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 CHANGED
@@ -2,24 +2,58 @@
2
2
 
3
3
  ## Features
4
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.
5
+ ### Fast Mode
8
6
 
9
- ## Caveat
7
+ By default, Oxc parser does not produce semantic errors where symbols and scopes are needed.
10
8
 
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.
9
+ To enable semantic errors, apply the option `showSemanticErrors: true`.
13
10
 
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.
11
+ For example,
12
+
13
+ ```js
14
+ let foo;
15
+ let foo;
16
+ ```
17
+
18
+ Does not produce any errors when `showSemanticErrors` is `false`, which is the default behavior.
19
+
20
+ This mode is best suited for parser plugins, where other parts of your build pipeline has already checked for errors.
21
+
22
+ ### Returns ESM information.
23
+
24
+ It is likely that you are writing a parser plugin that requires ESM information.
25
+
26
+ To avoid walking the AST again, Oxc Parser returns ESM information directly.
27
+
28
+ This information can be used to rewrite import and exports with the help of [`magic-string`](https://www.npmjs.com/package/magic-string),
29
+ without any AST manipulations.
30
+
31
+ ```ts
32
+ export interface EcmaScriptModule {
33
+ /**
34
+ * Has ESM syntax.
35
+ *
36
+ * i.e. `import` and `export` statements, and `import.meta`.
37
+ *
38
+ * Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
39
+ */
40
+ hasModuleSyntax: boolean;
41
+ /** Import statements. */
42
+ staticImports: Array<StaticImport>;
43
+ /** Export statements. */
44
+ staticExports: Array<StaticExport>;
45
+ /** Dynamic import expressions. */
46
+ dynamicImports: Array<DynamicImport>;
47
+ /** Span positions` of `import.meta` */
48
+ importMetas: Array<Span>;
49
+ }
50
+ ```
16
51
 
17
52
  ## API
18
53
 
19
54
  ```javascript
20
55
  import oxc from './index.js';
21
56
 
22
- // The emoji makes the span of `import.meta.url` to be different in UTF8 and UTF16.
23
57
  const code = 'const url: String = /* 🤨 */ import.meta.url;';
24
58
 
25
59
  // File extension is used to determine which dialect to parse source as.
@@ -36,16 +70,4 @@ console.log(result.program, result.comments);
36
70
 
37
71
  // ESM information - imports, exports, `import.meta`s.
38
72
  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;
44
-
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`
50
- }
51
73
  ```
package/bindings.js CHANGED
@@ -370,7 +370,6 @@ if (!nativeBinding) {
370
370
  throw new Error(`Failed to load native binding`)
371
371
  }
372
372
 
373
- module.exports.MagicString = nativeBinding.MagicString
374
373
  module.exports.ParseResult = nativeBinding.ParseResult
375
374
  module.exports.ExportExportNameKind = nativeBinding.ExportExportNameKind
376
375
  module.exports.ExportImportNameKind = nativeBinding.ExportImportNameKind
@@ -378,5 +377,4 @@ module.exports.ExportLocalNameKind = nativeBinding.ExportLocalNameKind
378
377
  module.exports.ImportNameKind = nativeBinding.ImportNameKind
379
378
  module.exports.parseAsync = nativeBinding.parseAsync
380
379
  module.exports.parseSync = nativeBinding.parseSync
381
- module.exports.parseWithoutReturn = nativeBinding.parseWithoutReturn
382
380
  module.exports.Severity = nativeBinding.Severity
package/index.d.ts CHANGED
@@ -2,47 +2,11 @@
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
- hasChanged(): boolean
15
- append(input: string): this
16
- appendLeft(index: number, input: string): this
17
- appendRight(index: number, input: string): this
18
- indent(): this
19
- prepend(input: string): this
20
- prependLeft(index: number, input: string): this
21
- prependRight(index: number, input: string): this
22
- relocate(start: number, end: number, to: number): this
23
- remove(start: number, end: number): this
24
- generateMap(options?: Partial<GenerateDecodedMapOptions>): {
25
- toString: () => string;
26
- toUrl: () => string;
27
- toMap: () => {
28
- file?: string
29
- mappings: string
30
- names: Array<string>
31
- sourceRoot?: string
32
- sources: Array<string>
33
- sourcesContent?: Array<string>
34
- version: number
35
- x_google_ignoreList?: Array<number>
36
- }
37
- }
38
- }
39
-
40
5
  export declare class ParseResult {
41
6
  get program(): import("@oxc-project/types").Program
42
7
  get module(): EcmaScriptModule
43
8
  get comments(): Array<Comment>
44
9
  get errors(): Array<OxcError>
45
- get magicString(): MagicString
46
10
  }
47
11
 
48
12
  export interface Comment {
@@ -136,15 +100,6 @@ export declare const enum ExportLocalNameKind {
136
100
  None = 'None'
137
101
  }
138
102
 
139
- export interface GenerateDecodedMapOptions {
140
- /** The filename of the file containing the original source. */
141
- source?: string
142
- /** Whether to include the original content in the map's `sourcesContent` array. */
143
- includeContent: boolean
144
- /** Whether the mapping should be high-resolution. */
145
- hires: boolean | 'boundary'
146
- }
147
-
148
103
  export interface ImportName {
149
104
  kind: ImportNameKind
150
105
  name?: string
@@ -161,15 +116,6 @@ export declare const enum ImportNameKind {
161
116
  Default = 'Default'
162
117
  }
163
118
 
164
- export interface LineColumn {
165
- line: number
166
- column: number
167
- }
168
-
169
- export interface OverwriteOptions {
170
- contentOnly: boolean
171
- }
172
-
173
119
  export interface OxcError {
174
120
  severity: Severity
175
121
  message: string
@@ -195,49 +141,28 @@ export interface ParserOptions {
195
141
  * (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property
196
142
  * containing the expression inside parentheses.
197
143
  *
198
- * Default: true
144
+ * @default true
199
145
  */
200
146
  preserveParens?: boolean
201
147
  /**
202
- * Default: false
203
- * @experimental Only for internal usage on Rolldown and Vite.
148
+ * Produce semantic errors with an additional AST pass.
149
+ * Semantic errors depend on symbols and scopes, where the parser does not construct.
150
+ * This adds a small performance overhead.
151
+ *
152
+ * @default false
204
153
  */
205
- convertSpanUtf16?: boolean
154
+ showSemanticErrors?: boolean
206
155
  }
207
156
 
208
157
  /** Parse synchronously. */
209
158
  export declare function parseSync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): ParseResult
210
159
 
211
- /**
212
- * Parse without returning anything.
213
- *
214
- * This is for benchmark purposes such as measuring napi communication overhead.
215
- */
216
- export declare function parseWithoutReturn(filename: string, sourceText: string, options?: ParserOptions | undefined | null): void
217
-
218
160
  export declare const enum Severity {
219
161
  Error = 'Error',
220
162
  Warning = 'Warning',
221
163
  Advice = 'Advice'
222
164
  }
223
165
 
224
- export interface SourceMap {
225
- file?: string
226
- mappings: string
227
- names: Array<string>
228
- sourceRoot?: string
229
- sources: Array<string>
230
- sourcesContent?: Array<string>
231
- version: number
232
- x_google_ignoreList?: Array<number>
233
- }
234
-
235
- export interface SourceMapOptions {
236
- includeContent?: boolean
237
- source?: string
238
- hires?: boolean
239
- }
240
-
241
166
  export interface Span {
242
167
  start: number
243
168
  end: number
package/index.js CHANGED
@@ -9,7 +9,7 @@ module.exports.parseWithoutReturn = bindings.parseWithoutReturn;
9
9
  module.exports.Severity = bindings.Severity;
10
10
 
11
11
  function wrap(result) {
12
- let program, module, comments, errors, magicString;
12
+ let program, module, comments, errors;
13
13
  return {
14
14
  get program() {
15
15
  if (!program) {
@@ -48,17 +48,6 @@ function wrap(result) {
48
48
  if (!errors) errors = result.errors;
49
49
  return errors;
50
50
  },
51
- get magicString() {
52
- if (!magicString) magicString = result.magicString;
53
- magicString.generateMap = function generateMap(options) {
54
- return {
55
- toString: () => magicString.toSourcemapString(options),
56
- toUrl: () => magicString.toSourcemapUrl(options),
57
- toMap: () => magicString.toSourcemapObject(options),
58
- };
59
- };
60
- return magicString;
61
- },
62
51
  };
63
52
  }
64
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oxc-parser",
3
- "version": "0.52.0",
3
+ "version": "0.54.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.52.0"
27
+ "@oxc-project/types": "^0.54.0"
28
28
  },
29
29
  "optionalDependencies": {
30
- "@oxc-parser/binding-win32-x64-msvc": "0.52.0",
31
- "@oxc-parser/binding-win32-arm64-msvc": "0.52.0",
32
- "@oxc-parser/binding-linux-x64-gnu": "0.52.0",
33
- "@oxc-parser/binding-linux-arm64-gnu": "0.52.0",
34
- "@oxc-parser/binding-linux-x64-musl": "0.52.0",
35
- "@oxc-parser/binding-linux-arm64-musl": "0.52.0",
36
- "@oxc-parser/binding-darwin-x64": "0.52.0",
37
- "@oxc-parser/binding-darwin-arm64": "0.52.0"
30
+ "@oxc-parser/binding-win32-x64-msvc": "0.54.0",
31
+ "@oxc-parser/binding-win32-arm64-msvc": "0.54.0",
32
+ "@oxc-parser/binding-linux-x64-gnu": "0.54.0",
33
+ "@oxc-parser/binding-linux-arm64-gnu": "0.54.0",
34
+ "@oxc-parser/binding-linux-x64-musl": "0.54.0",
35
+ "@oxc-parser/binding-linux-arm64-musl": "0.54.0",
36
+ "@oxc-parser/binding-darwin-x64": "0.54.0",
37
+ "@oxc-parser/binding-darwin-arm64": "0.54.0"
38
38
  }
39
39
  }