https://github.com/moroshko/react-scanner
Extract React components and props usage from code.
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (7.0%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Extract React components and props usage from code.
Basic Info
Statistics
- Stars: 643
- Watchers: 9
- Forks: 45
- Open Issues: 25
- Releases: 10
Topics
Metadata Files
README.md
react-scanner
react-scanner statically analyzes the given code (TypeScript supported) and extracts React components and props usage.
First, it crawls the given directory and compiles a list of files to be scanned. Then, it scans every file and extracts rendered components and their props into a JSON report.
For example, let's say we have the following index.js file:
```jsx import React from "react"; import ReactDOM from "react-dom"; import { BasisProvider, defaultTheme, Container, Text, Link as BasisLink, } from "basis";
function App() {
return (
ReactDOM.render(
Running react-scanner on it will create the following JSON report:
Click to see it
```json { "BasisProvider": { "instances": [ { "importInfo": { "imported": "BasisProvider", "local": "BasisProvider", "moduleName": "basis" }, "props": { "theme": "(Identifier)" }, "propsSpread": false, "location": { "file": "/path/to/index.js", "start": { "line": 13, "column": 5 } } } ] }, "Container": { "instances": [ { "importInfo": { "imported": "Container", "local": "Container", "moduleName": "basis" }, "props": { "margin": "4", "hasBreakpointWidth": null }, "propsSpread": false, "location": { "file": "/path/to/index.js", "start": { "line": 14, "column": 7 } } } ] }, "Text": { "instances": [ { "importInfo": { "imported": "Text", "local": "Text", "moduleName": "basis" }, "props": { "textStyle": "subtitle2" }, "propsSpread": false, "location": { "file": "/path/to/index.js", "start": { "line": 15, "column": 9 } } }, { "importInfo": { "imported": "Text", "local": "Text", "moduleName": "basis" }, "props": { "margin": "4 0 0 0" }, "propsSpread": false, "location": { "file": "/path/to/index.js", "start": { "line": 18, "column": 9 } } } ] }, "Link": { "instances": [ { "importInfo": { "imported": "Link", "local": "BasisLink", "moduleName": "basis" }, "props": { "href": "https://github.com/moroshko/react-scanner", "newTab": null }, "propsSpread": false, "location": { "file": "/path/to/index.js", "start": { "line": 20, "column": 11 } } } ] } } ```This raw JSON report is used then to generate something that is useful to you. For example, you might want to know:
- How often a cetrain component is used in your design system? (see
count-componentsprocessor) - How often a certain prop in a given component is used? (see
count-components-and-propsprocessor) - Looking at some prop in a given component, what's the distribution of values used? (e.g. you might consider deprecating a certain value)
Once you have the result you are interested in, you can write it to a file or simply log it to the console.
Installation
npm install --save-dev react-scanner
Usage
npx react-scanner -c /path/to/react-scanner.config.js
Config file
Everything that react-scanner does is controlled by a config file.
The config file can be located anywhere and it must export an object like this:
js
module.exports = {
crawlFrom: "./src",
includeSubComponents: true,
importedFrom: "basis",
};
Running react-scanner with this config would output something like this to the console:
json
{
"Text": {
"instances": 17,
"props": {
"margin": 6,
"color": 4,
"textStyle": 1
}
},
"Button": {
"instances": 10,
"props": {
"width": 10,
"variant": 5,
"type": 3
}
},
"Footer": {
"instances": 1,
"props": {}
}
}
Running programmatically
It is also possible to run the scanner programmatically. In this case, the config options should be passed directly to the run function.
```js import scanner from "react-scanner";
const output = await scanner.run(config); ```
Config options
Here are all the available config options:
| Option | Type | Description |
| ---------------------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| rootDir | string | The path to the root directory of your project.
If using a config file, this defaults to the config directory. |
| crawlFrom | string | The path of the directory to start crawling from.
Absolute or relative to the config file location. |
| exclude | array or function | Each array item should be a string or a regex. When crawling, if directory name matches exactly the string item or matches the regex item, it will be excluded from crawling.
For more complex scenarios, exclude can be a a function that accepts a directory name and should return true if the directory should be excluded from crawling. |
| globs | array | Only files matching these globs will be scanned. See here for glob syntax.
Default: ["**/!(*.test\|*.spec).@(js\|ts)?(x)"] |
| components | object | Components to report. Omit to report all components. |
| includeSubComponents | boolean | Whether to report subcomponents or not.
When false, Footer will be reported, but Footer.Content will not.
When true, Footer.Content will be reported, as well as Footer.Content.Legal, etc.
Default: false |
| importedFrom | string or regex | Before reporting a component, we'll check if it's imported from a module name matching importedFrom and, only if there is a match, the component will be reported.
When omitted, this check is bypassed. |
| getComponentName | function | This function is called to determine the component name to be used in the report based on the import declaration.
Default: ({ imported, local, moduleName, importType }) => imported \|\| local |
| getPropValue | function | Customize reporting for non-trivial prop values. See Customizing prop values treatment |
| processors | array | See Processors.
Default: ["count-components-and-props"] |
Processors
Scanning the files results in a JSON report. Add processors to tell react-scanner what to do with this report.
Built-in processors
react-scanner comes with some ready to use processors.
To use a built-in processor, simply specify its name as a string, e.g.:
processors: ["count-components"]
You can also use a tuple form to pass options to a built-in processor, e.g.:
processors: [
["count-components", { outputTo: "/path/to/my-report.json" }]
]
All the built-in processors support the following options:
| Option | Type | Description |
| ---------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------- |
| outputTo | string | Where to output the result.
Absolute or relative to the root directory.
When omitted, the result is printed out to the console. |
Here are the built-in processors that react-scanner comes with:
count-components
Example output:
json
{
"Text": 10,
"Button": 5,
"Link": 3
}
count-components-and-props
Example output:
json
{
"Text": {
"instances": 17,
"props": {
"margin": 6,
"color": 4,
"textStyle": 1
}
},
"Button": {
"instances": 10,
"props": {
"width": 10,
"variant": 4,
"type": 2
}
},
"Footer": {
"instances": 1,
"props": {}
}
}
raw-report
Example output:
json
{
"Text": {
"instances": [
{
"props": {
"textStyle": "subtitle2"
},
"propsSpread": false,
"location": {
"file": "/path/to/file",
"start": {
"line": 9,
"column": 9
}
}
},
{
"props": {
"margin": "4 0 0 0"
},
"propsSpread": false,
"location": {
"file": "/path/to/file",
"start": {
"line": 12,
"column": 9
}
}
}
]
},
"Link": {
"instances": [
{
"props": {
"href": "https://github.com/moroshko/react-scanner",
"newTab": null
},
"propsSpread": false,
"location": {
"file": "/path/to/file",
"start": {
"line": 14,
"column": 11
}
}
}
]
},
"Container": {
"instances": [
{
"props": {
"margin": "4",
"hasBreakpointWidth": null
},
"propsSpread": false,
"location": {
"file": "/path/to/file",
"start": {
"line": 8,
"column": 7
}
}
}
]
}
}
Custom processors
We saw above that built-in processors come in the form of a string or a tuple.
Custom processors are functions, and can be asynchronous!
If the processor function returns a Promise, it will be awaited before the next processor kicks in. This way, you can use previous processors results in your processor function.
Here is an example of taking the output of the built-in count-components-and-props processor and sending it to your storage solution.
processors: [
"count-components-and-props",
({ prevResult }) => {
return axios.post("/my/storage/solution", prevResult);
}
]
Processor functions receive an object with the following keys in it:
| Key | Type | Description |
| ----------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| report | object | The raw JSON report. |
| prevResults | array | Previous processors results. |
| prevResult | any | The last item in prevResults. Just for convenience. |
| forEachComponent | function | Helper function to recursively traverse the raw JSON report. The function you pass in is called for every component in the report, and it gets an object with componentName and component in it. Check the implementation of count-components-and-props for a usage example. |
| sortObjectKeysByValue | function | Helper function that sorts object keys by some function of the value. Check the implementation of count-components-and-props for a usage example. |
| output | function | Helper function that outputs the given data. Its first parameter is the data you want to output. The second parameter is the destination. When the second parameter is omitted, it outputs to the console. To output to the file system, pass an absolute path or a relative path to the config file location. |
Customizing prop values treatment
When a primitive (strings, numbers, booleans, etc...) is passed as a prop value into a component, the raw report will display this literal value. However, when expressions or variables are passed as a prop value into a component, the raw report will display the AST type. In some instances, we may want to see the actual expression that was passed in.
getPropValue
Using the getPropValue configuration parameter makes this possible.
typescript
type IGetPropValue = {
/** The AST node */
node: Node,
componentName: string,
propName: string,
/** Pass the node back into this method for default handling of the prop value */
defaultGetPropValue: (node: Node) => string
}
getPropValue({ node, componentName, propName, defaultGetPropValue }: IGetPropValue): string
Example
If we were building out a design system, and wanted to see all the variations of a style prop that we passed into an Input component, we could do something like this:
```javascript const escodegen = require("escodegen-wallaby");
getPropValue: ({ node, propName, componentName, defaultGetPropValue }) => { if (componentName === "Input" && propName === "style") { if (node.type === "JSXExpressionContainer") { return escodegen.generate(node.expression); } else { return escodegen.generate(node); } } else { return defaultGetPropValue(node); } }; ```
License
MIT
Owner
- Name: Misha Moroshko
- Login: moroshko
- Kind: user
- Location: Melbourne, Australia
- Website: https://www.moroshko.me
- Twitter: moroshko
- Repositories: 121
- Profile: https://github.com/moroshko
I build products that make humans happier. Previously Front End engineer @facebook. Now, reimagining live experiences at https://muso.live
GitHub Events
Total
- Issues event: 3
- Watch event: 60
- Issue comment event: 3
- Push event: 2
- Create event: 1
Last Year
- Issues event: 3
- Watch event: 60
- Issue comment event: 3
- Push event: 2
- Create event: 1
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Misha Moroshko | m****o@g****m | 43 |
| Renovate Bot | b****t@r****m | 26 |
| Vlad Kosinov | 4****v | 3 |
| Mihkel Eidast | m****t@g****m | 2 |
| renovate[bot] | 2****] | 1 |
| dependabot[bot] | 4****] | 1 |
| Mike Beach | m****1@s****k | 1 |
| Luis Gil | l****l@b****m | 1 |
| Janey2022 | 1****2 | 1 |
| Gerard Brull | g****s@g****m | 1 |
| Douglas Eggleton | d****n | 1 |
| Brendan Drew | b****w@a****m | 1 |
| Andrew Tam | a****w@a****m | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 31
- Total pull requests: 49
- Average time to close issues: about 1 month
- Average time to close pull requests: about 1 month
- Total issue authors: 29
- Total pull request authors: 13
- Average comments per issue: 1.52
- Average comments per pull request: 0.67
- Merged pull requests: 38
- Bot issues: 2
- Bot pull requests: 36
Past Year
- Issues: 5
- Pull requests: 2
- Average time to close issues: N/A
- Average time to close pull requests: 3 days
- Issue authors: 5
- Pull request authors: 2
- Average comments per issue: 0.6
- Average comments per pull request: 0.5
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 1
Top Authors
Issue Authors
- mihkeleidast (2)
- renovate[bot] (2)
- durgapavaniB (1)
- zdarovka (1)
- jpt (1)
- JDansercoer (1)
- ethange7 (1)
- yakunins (1)
- thedeviousdev (1)
- mattyb (1)
- alexandra-lim (1)
- sapegin (1)
- megob56 (1)
- douglaseggleton (1)
- mrmartan (1)
Pull Request Authors
- renovate[bot] (31)
- dependabot[bot] (6)
- mihkeleidast (3)
- mikbeach (2)
- Janey2022 (2)
- poteirard (2)
- jake-chapman-mark43 (1)
- Luigisa (1)
- vladkosinov (1)
- smol-honk (1)
- douglaseggleton (1)
- drewbrend (1)
- andrewmtam (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- 237 dependencies
- c8 7.11.0 development
- escodegen-wallaby 1.6.34 development
- eslint 8.13.0 development
- eslint-plugin-import 2.26.0 development
- execa 5.0.0 development
- husky 7.0.4 development
- prettier 2.6.2 development
- uvu 0.5.3 development
- watchlist 0.3.1 development
- @typescript-eslint/typescript-estree 5.20.0
- astray 1.1.1
- dlv 1.1.3
- dset 3.1.1
- fdir 5.2.0
- is-plain-object 5.0.0
- picomatch 2.3.1
- sade 1.8.1
- typescript 4.6.3
- actions/checkout v2 composite
- actions/setup-node v2 composite