Skip to content

Commit

Permalink
refactor: 🎨 add file_name parameter for parseCode
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Mar 22, 2023
1 parent d2c4ed8 commit 2b23b18
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type SimpleExportSpecifier = {
local: string;
};

export declare function parseCode(code: string): Declaration[];
export declare function parseCode(code: string, fileName?: string | undefined | null): Declaration[];

export declare function parseFiles(files: string[]): Promise<Record<string, Declaration[]>>;

Expand Down
2 changes: 1 addition & 1 deletion src/extract_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn extract_module_imports(module: &mut Module) -> Vec<DeclareType> {
ExportSpecifier::Default(default_specifier) => {
//todo: swc does not support export x from 'xx'
specifiers.push(SimpleExportSpecifier::DefaultExport(ExportDefaultName {
exported: default_specifier.exported.to_string(),
exported: default_specifier.exported.sym.to_string(),
}));
}
// eg export * as foo from 'bar'
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::collections::HashMap;
use std::sync::Arc;

#[napi]
fn parse_code(code: String) -> Result<Value> {
let import_map = extract_from_code(code)?;
fn parse_code(code: String, file_name: Option<String>) -> Result<Value> {
let import_map = extract_from_code(code, file_name)?;

let value =
serde_json::to_value(&import_map).map_err(|_| anyhow!("serialize import declares error"))?;
Expand Down Expand Up @@ -88,8 +88,8 @@ async fn parse_files_async(files: Vec<String>) -> Result<HashMap<String, Vec<Dec
Ok(file_imports)
}

fn extract_from_code(code: String) -> Result<Vec<DeclareType>> {
let mut module = parse_code_to_module(code)?;
fn extract_from_code(code: String, file_name: Option<String>) -> Result<Vec<DeclareType>> {
let mut module = parse_code_to_module(code, file_name)?;
Ok(extract_module_imports(&mut module))
}

Expand Down
19 changes: 11 additions & 8 deletions src/module_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,21 @@ pub fn parse_file_to_module(file_path: &String) -> Result<Module> {
Ok(module)
}

pub fn parse_code_to_module(code: String) -> Result<Module> {
pub fn parse_code_to_module(code: String, file_name: Option<String>) -> Result<Module> {
let cm: Lrc<SourceMap> = Default::default();
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

let fm = cm.new_source_file(FileName::Custom("_.tsx".to_string()), code);
let default_file_name = "_.tsx".to_string();

let file_name: String = if let Some(path) = file_name {
path
} else {
default_file_name.clone()
};

let fm = cm.new_source_file(FileName::Custom(default_file_name), code);
let lexer = Lexer::new(
// We want to parse ecmascript
Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
}),
// EsVersion defaults to es5
path_to_syntax(Path::new(&file_name)),
Default::default(),
StringInput::from(&*fm),
None,
Expand Down

0 comments on commit 2b23b18

Please sign in to comment.