-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(napi/parser): add
getUtf16ByteOffset
API; update README (#7772)
- Loading branch information
Showing
4 changed files
with
58 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,51 @@ | ||
# The JavaScript Oxidation Compiler | ||
# Oxc Parser | ||
|
||
See `index.d.ts` for `parseSync` and `parseAsync` API. | ||
## Features | ||
|
||
- Returns ESM information. | ||
- Built-in `magic-string` on the Rust side exposed through N-API. | ||
- "clever" approach to overcome the Rust UTF8 vs JavaScript UTF16 length problem. | ||
|
||
## Caveat | ||
|
||
The parser alone does not fully check for syntax errors that are associated with semantic data (symbols and scopes). | ||
The full compiler is needed for such case, as the compiler does an additional semantic pass. | ||
|
||
With this caveat, `oxc-parser` is best suited for parser plugins, | ||
where you need quick access to ESM information, as well as fast `magic-string` operations. | ||
|
||
## API | ||
|
||
```javascript | ||
import assert from 'assert'; | ||
import oxc from 'oxc-parser'; | ||
import oxc from './index.js'; | ||
|
||
const sourceText = "let foo: Foo = 'foo';"; | ||
// Filename extension is used to determine which dialect to parse source as. | ||
// The emoji makes the span of `import.meta.url` to be different in UTF8 and UTF16. | ||
const code = 'const url: String = /* 🤨 */ import.meta.url;'; | ||
|
||
// File extension is used to determine which dialect to parse source as. | ||
const filename = 'test.tsx'; | ||
|
||
test(oxc.parseSync(filename, sourceText, options)); | ||
test(await oxc.parseAsync(filename, sourceText, options)); | ||
const result = oxc.parseSync(filename, code); | ||
// or `await oxc.parseAsnyc(filename, code)` | ||
|
||
// An array of errors, if any. | ||
console.log(result.errors); | ||
|
||
// AST and comments. | ||
console.log(result.program, result.comments); | ||
|
||
// ESM information - imports, exports, `import.meta`s. | ||
console.log(result.module); | ||
|
||
// A `magic-string` instance for accessing and manipulating the source text. | ||
// All returned spans are in UTF8 offsets, which cannot be used directly on our JavaScript. | ||
// JavaScript string lengths are in UTF16 offsets. | ||
const ms = result.magicString; | ||
|
||
function test(ret) { | ||
assert(ret.program.body.length == 1); | ||
assert(ret.errors.length == 0); | ||
for (const span of result.module.importMetas) { | ||
// Extra methods for access the source text through spans with UTF8 offsets. | ||
console.log(ms.getSourceText(span.start, span.end)); // prints `import.meta` | ||
console.log(ms.getLineColumnNumber(span.start)); // prints `{ line: 0, column: 20 }` | ||
console.log(code.substring(ms.getUtf16ByteOffset(span.start)).startsWith('import.meta.url')); // prints `true` | ||
} | ||
``` |