Skip to content

Commit

Permalink
define trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Aug 29, 2023
1 parent 5b9347e commit 95a64a2
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions incubator/@react-native-webapis/scan/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ use swc_ecma_parser::{error::Error, parse_file_as_module, EsConfig, Syntax, TsCo
use swc_ecma_visit::VisitWith;
use swc_ecmascript::ast::{EsVersion, Module};

fn parse_module(fm: &SourceFile, source_path: &Path) -> Result<Module, Error> {
let extension = source_path.extension().expect("Expected a file extension");
parse_file_as_module(
fm,
if extension == "ts" || extension == "tsx" {
Syntax::Typescript(TsConfig {
tsx: extension == "tsx",
decorators: true,
..Default::default()
})
} else {
Syntax::Es(EsConfig {
jsx: true,
decorators: true,
..Default::default()
})
},
EsVersion::latest(),
None,
&mut vec![],
)
trait ParseableModule {
fn parse_file_as_module(&self, source_path: &Path) -> Result<Module, Error>;
}

impl ParseableModule for SourceFile {
fn parse_file_as_module(&self, source_path: &Path) -> Result<Module, Error> {
let extension = source_path.extension().expect("Expected a file extension");
parse_file_as_module(
self,
if extension == "ts" || extension == "tsx" {
Syntax::Typescript(TsConfig {
tsx: extension == "tsx",
decorators: true,
..Default::default()
})
} else {
Syntax::Es(EsConfig {
jsx: true,
decorators: true,
..Default::default()
})
},
EsVersion::latest(),
None,
&mut vec![],
)
}
}

pub fn visit(source_path: &Path) -> HashMap<String, i32> {
Expand All @@ -41,7 +47,7 @@ pub fn visit(source_path: &Path) -> HashMap<String, i32> {
}
};

match parse_module(&fm, &source_path) {
match fm.parse_file_as_module(&source_path) {
Ok(module) => module.visit_with(&mut WebApiVisitor {
usage_map: &mut usage_map,
}),
Expand Down

0 comments on commit 95a64a2

Please sign in to comment.