React Component & Prop Usage Scanner

1.2.0 · active · verified Sun Apr 19

react-scanner is a static analysis tool designed to extract React component and prop usage from source code, supporting both JavaScript and TypeScript. It operates by crawling a specified directory, identifying relevant files, and then parsing them to build a detailed JSON report of component instances and their associated prop values. The current stable version is 1.2.0, with a history of regular updates indicating active maintenance and feature development. Key differentiators include its static analysis approach, eliminating the need for runtime instrumentation, robust TypeScript support, and a flexible architecture that allows for custom processors to transform the raw JSON output into actionable insights, such as component usage counts or prop value distributions. It is primarily used for understanding the adoption and utilization patterns of design system components within a codebase.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic use of `react-scanner` to scan a temporary React project and print the raw JSON report. It shows how to configure the scanner with basic options like `rootDir`, `getComponentName`, and `getPropValue` to analyze component and prop usage.

import { mkdir, writeFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import scan from 'react-scanner';

async function runScanner() {
  const tmpDir = join(process.cwd(), 'tmp-scanner-test');
  await mkdir(tmpDir, { recursive: true });

  const jsxContent = `
    import React from 'react';
    import { Button, Card } from './components';

    function App() {
      return (
        <div>
          <Button label="Click Me" onClick={() => console.log('clicked')} />
          <Card title="Hello" description="This is a test card" />
          <Button label="Submit" variant="primary" />
        </div>
      );
    }
    export default App;
  `;
  await writeFile(join(tmpDir, 'App.jsx'), jsxContent);

  const componentsContent = `
    import React from 'react';
    export const Button = ({ label, onClick, variant }) => <button>{label}</button>;
    export const Card = ({ title, description }) => <div><h3>{title}</h3><p>{description}</p></div>;
  `;
  await writeFile(join(tmpDir, 'components.js'), componentsContent);

  const config = {
    rootDir: tmpDir,
    excludedPaths: [],
    processors: [],
    getComponentName: ({ local }) => local,
    getPropValue: ({ value }) => value && value.type === 'JSXExpressionContainer' ? '(Identifier)' : value
  };

  try {
    console.log('Scanning React components...');
    const report = await scan(config);
    console.log('Generated Report:\n', JSON.stringify(report, null, 2));
  } catch (error) {
    console.error('Error during scanning:', error);
  } finally {
    await rm(tmpDir, { recursive: true, force: true });
    console.log(`Cleaned up temporary directory: ${tmpDir}`);
  }
}

runScanner();

view raw JSON →