feat(napi/parser): reexport @oxc-project/types (#7214)

closes #7207
This commit is contained in:
Boshen 2024-11-09 17:24:07 +08:00 committed by GitHub
parent 1fcd70932d
commit 24c585a20a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 75 additions and 36 deletions

4
napi/parser/header.js Normal file
View file

@ -0,0 +1,4 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export * from '@oxc-project/types';

View file

@ -1,5 +1,7 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export * from '@oxc-project/types';
export interface Comment {
type: 'Line' | 'Block'
value: string
@ -7,7 +9,27 @@ export interface Comment {
end: number
}
export interface ExportSpecifier {
export interface ModuleLexer {
imports: Array<ModuleLexerImportSpecifier>
exports: Array<ModuleLexerExportSpecifier>
/**
* ESM syntax detection
*
* The use of ESM syntax: import / export statements and `import.meta`
*/
hasModuleSyntax: boolean
/** Facade modules that only use import / export syntax */
facade: boolean
}
/**
* # Panics
*
* * Tokio crashes
*/
export declare function moduleLexerAsync(sourceText: string, options?: ParserOptions | undefined | null): Promise<ModuleLexer>
export interface ModuleLexerExportSpecifier {
/** Exported name */
n: string
/** Local name, or undefined. */
@ -22,7 +44,7 @@ export interface ExportSpecifier {
le?: number
}
export interface ImportSpecifier {
export interface ModuleLexerImportSpecifier {
/**
* Module name
*
@ -54,26 +76,6 @@ export interface ImportSpecifier {
a: number
}
export interface ModuleLexer {
imports: Array<ImportSpecifier>
exports: Array<ExportSpecifier>
/**
* ESM syntax detection
*
* The use of ESM syntax: import / export statements and `import.meta`
*/
hasModuleSyntax: boolean
/** Facade modules that only use import / export syntax */
facade: boolean
}
/**
* # Panics
*
* * Tokio crashes
*/
export declare function moduleLexerAsync(sourceText: string, options?: ParserOptions | undefined | null): Promise<ModuleLexer>
/**
* Outputs the list of exports and locations of import specifiers,
* including dynamic import and import meta handling.

View file

@ -3,10 +3,7 @@
"private": true,
"scripts": {
"build": "napi build --platform --release --js bindings.js",
"test": "vitest run ./test"
},
"engines": {
"node": ">=14.*"
"test": "vitest --typecheck run ./test"
},
"napi": {
"binaryName": "parser",
@ -19,6 +16,10 @@
"aarch64-unknown-linux-musl",
"x86_64-apple-darwin",
"aarch64-apple-darwin"
]
],
"dtsHeaderFile": "header.js"
},
"dependencies": {
"@oxc-project/types": "workspace:^"
}
}

View file

@ -7,7 +7,7 @@ use oxc_module_lexer::ImportType;
use crate::{parse, ParserOptions};
#[napi(object)]
pub struct ImportSpecifier {
pub struct ModuleLexerImportSpecifier {
/// Module name
///
/// To handle escape sequences in specifier strings, the .n field of imported specifiers will be provided where possible.
@ -40,7 +40,7 @@ pub struct ImportSpecifier {
}
#[napi(object)]
pub struct ExportSpecifier {
pub struct ModuleLexerExportSpecifier {
/// Exported name
pub n: String,
@ -60,7 +60,7 @@ pub struct ExportSpecifier {
pub le: Option<u32>,
}
impl<'a> From<oxc_module_lexer::ImportSpecifier<'a>> for ImportSpecifier {
impl<'a> From<oxc_module_lexer::ImportSpecifier<'a>> for ModuleLexerImportSpecifier {
#[allow(clippy::cast_lossless)]
fn from(i: oxc_module_lexer::ImportSpecifier) -> Self {
Self {
@ -80,7 +80,7 @@ impl<'a> From<oxc_module_lexer::ImportSpecifier<'a>> for ImportSpecifier {
}
}
impl<'a> From<oxc_module_lexer::ExportSpecifier<'a>> for ExportSpecifier {
impl<'a> From<oxc_module_lexer::ExportSpecifier<'a>> for ModuleLexerExportSpecifier {
fn from(e: oxc_module_lexer::ExportSpecifier) -> Self {
Self {
n: e.n.to_string(),
@ -95,9 +95,9 @@ impl<'a> From<oxc_module_lexer::ExportSpecifier<'a>> for ExportSpecifier {
#[napi(object)]
pub struct ModuleLexer {
pub imports: Vec<ImportSpecifier>,
pub imports: Vec<ModuleLexerImportSpecifier>,
pub exports: Vec<ExportSpecifier>,
pub exports: Vec<ModuleLexerExportSpecifier>,
/// ESM syntax detection
///
@ -113,8 +113,8 @@ fn module_lexer(source_text: &str, options: &ParserOptions) -> ModuleLexer {
let allocator = Allocator::default();
let ret = parse(&allocator, source_text, options);
let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program);
let imports = module_lexer.imports.into_iter().map(ImportSpecifier::from).collect();
let exports = module_lexer.exports.into_iter().map(ExportSpecifier::from).collect();
let imports = module_lexer.imports.into_iter().map(ModuleLexerImportSpecifier::from).collect();
let exports = module_lexer.exports.into_iter().map(ModuleLexerExportSpecifier::from).collect();
ModuleLexer {
imports,
exports,

View file

@ -0,0 +1,13 @@
import { assertType, describe, it } from 'vitest';
import type { Statement } from '../index';
import * as oxc from '../index';
describe('parse', () => {
const code = '/* comment */ foo';
it('checks type', async () => {
const ret = oxc.parseSync(code);
assertType<Statement>(ret.program.body[0]);
});
});

View file

@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "Preserve",
"moduleResolution": "Bundler",
"target": "ESNext"
}
}

View file

@ -11,6 +11,7 @@
},
"devDependencies": {
"@napi-rs/cli": "catalog:",
"typescript": "catalog:",
"vitest": "catalog:"
}
}

View file

@ -9,6 +9,9 @@ catalogs:
'@napi-rs/cli':
specifier: 3.0.0-alpha.61
version: 3.0.0-alpha.61
typescript:
specifier: 5.6.3
version: 5.6.3
vitest:
specifier: 2.1.2
version: 2.1.2
@ -20,6 +23,9 @@ importers:
'@napi-rs/cli':
specifier: 'catalog:'
version: 3.0.0-alpha.61(@emnapi/runtime@1.2.0)
typescript:
specifier: 'catalog:'
version: 5.6.3
vitest:
specifier: 'catalog:'
version: 2.1.2(@types/node@22.9.0)
@ -54,7 +60,11 @@ importers:
napi/minify: {}
napi/parser: {}
napi/parser:
dependencies:
'@oxc-project/types':
specifier: workspace:^
version: link:../../npm/oxc-types
napi/transform: {}

View file

@ -10,3 +10,4 @@ packages:
catalog:
"@napi-rs/cli": 3.0.0-alpha.61
"vitest": 2.1.2
"typescript": 5.6.3