Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Sep 19, 2023
1 parent 05b9728 commit b2b7fa6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions incubator/@react-native-webapis/scan/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions incubator/@react-native-webapis/scan/src/ignored_dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions incubator/@react-native-webapis/scan/src/web_api_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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()) {
Expand Down

0 comments on commit b2b7fa6

Please sign in to comment.