diff --git a/incubator/@react-native-webapis/scan/README.md b/incubator/@react-native-webapis/scan/README.md new file mode 100644 index 000000000..abcdd8ba2 --- /dev/null +++ b/incubator/@react-native-webapis/scan/README.md @@ -0,0 +1,31 @@ +# @react-native-webapis/scan + +[![Build](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml/badge.svg)](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml) +[![npm version](https://img.shields.io/npm/v/@react-native-webapis/scan)](https://www.npmjs.com/package/@react-native-webapis/scan) + +`@react-native-webapis/scan` is a tool for scanning your code bases for +potential uses of [Web APIs](https://developer.mozilla.org/en-US/docs/Web/API). + +## Build + +1. [Install Rust](https://www.rust-lang.org/learn/get-started) +2. Build: `cargo build --release` + +A binary will be output at `target/release/scan`. Make not of this location, or +copy the binary somewhere that you can easily access later. + +## Usage + +```sh +./scan +``` + +This is currently a very simple tool. It takes no arguments and simply scans all +`.js` and `.ts` files it finds in the current working directory. For each file +it finds, it tries to parse using [SWC](https://swc.rs/). Once parsed, it will +try to count all references to `navigator.*` and any identifiers listed in +`src/web_apis.rs`. Files or directories that are listed in `src/ignored_dirs.rs` +are skipped. + +You can see examples of output in `merge.mjs`. This is a script we use to +aggregate the output from multiple repositories. diff --git a/incubator/@react-native-webapis/scan/src/ignored_dirs.rs b/incubator/@react-native-webapis/scan/src/ignored_dirs.rs index 390d4bc90..dba617f91 100644 --- a/incubator/@react-native-webapis/scan/src/ignored_dirs.rs +++ b/incubator/@react-native-webapis/scan/src/ignored_dirs.rs @@ -12,6 +12,7 @@ pub static IGNORED_DIRECTORIES: &'static [&str] = &[ "@pnpm", "@react-native", "@react-native-community", + "@react-native-mac", "@react-native-webapis", "@react-native-windows", "@rnx-kit", diff --git a/incubator/@react-native-webapis/scan/src/web_api_visitor.rs b/incubator/@react-native-webapis/scan/src/web_api_visitor.rs index f42095a7a..cad3ae4cf 100644 --- a/incubator/@react-native-webapis/scan/src/web_api_visitor.rs +++ b/incubator/@react-native-webapis/scan/src/web_api_visitor.rs @@ -21,6 +21,7 @@ impl<'a> WebApiVisitor<'a> { } impl<'a> Visit for WebApiVisitor<'a> { + /// Count all references to `navigator.*` fn visit_member_expr(&mut self, member: &MemberExpr) { if let Expr::Ident(ident) = member.obj.as_ref() { if ident.as_ref() == "navigator" { @@ -39,6 +40,7 @@ impl<'a> Visit for WebApiVisitor<'a> { } } + /// Count all uses of identifiers that found in `WEB_APIS` fn visit_new_expr(&mut self, expr: &NewExpr) { if let Expr::Ident(ident) = expr.callee.as_ref() { if let Ok(..) = WEB_APIS.binary_search(&ident.as_ref()) {