Oxc Parser
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
import oxc from './index.js';
const code = 'const url: String = /* 🤨 */ import.meta.url;';
const filename = 'test.tsx';
const result = oxc.parseSync(filename, code);
console.log(result.errors);
console.log(result.program, result.comments);
console.log(result.module);
const ms = result.magicString;
for (const span of result.module.importMetas) {
console.log(ms.getSourceText(span.start, span.end));
console.log(ms.getLineColumnNumber(span.start));
console.log(code.substring(ms.getUtf16ByteOffset(span.start)).startsWith('import.meta.url'));
}