From 95a64a29991c58f215e998846566e4d5799a7d71 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 24 Aug 2023 09:32:20 +0200 Subject: [PATCH] define trait --- .../@react-native-webapis/scan/src/visitor.rs | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/incubator/@react-native-webapis/scan/src/visitor.rs b/incubator/@react-native-webapis/scan/src/visitor.rs index 18d5c0ca5b..cf7e9f5369 100644 --- a/incubator/@react-native-webapis/scan/src/visitor.rs +++ b/incubator/@react-native-webapis/scan/src/visitor.rs @@ -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 { - 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; +} + +impl ParseableModule for SourceFile { + fn parse_file_as_module(&self, source_path: &Path) -> Result { + 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 { @@ -41,7 +47,7 @@ pub fn visit(source_path: &Path) -> HashMap { } }; - 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, }),