npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-plugin-props-checker

v1.1.0

Published

Ensure that given props exists in React or React Native Components

Downloads

1,527

Readme

ESlint Plugin to Check whether Props exist

Maintainability Test Coverage codecov Master Flow

Introduction

This ESLint plugin is designed to verify that certain components (in React/React Native) have specific props defined. This can be useful to ensure that components have whatever props you want them to have.

If the rules are configured correctly, the plugin will generate an error if a component is missing any of the required props.

The plugin also includes a dependOn feature that allows you to specify that prop validation for a particular component should only be performed if another prop is present. For example, you could use this feature to validate that a testID prop exists only if an onPress prop is also present on a component.

Installation

You'll first need to install ESLint:

npm i eslint --save-dev

Next, install eslint-plugin-props-checker:

npm install eslint-plugin-props-checker --save-dev

Usage

Add props-checker to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
    "plugins": ["props-checker"]
}

Then configure the rules you want to use under the rules section. Once you enable this, it will errors for all the components that we have mentioned.

A sample can be found below.

{
    'rules':
        {
            'props-checker/validator':
                [
                    'error',
                    {
                        props:
                            [
                                { propName: 'testID', components: ['Button', { component: 'Text', dependOn: 'onPress' }] },
                                { propName: 'accessible', components: ['TextInput'] }
                            ]
                    }
                ]
        }
}

According to the above configuration, the plugin will throw an error,

  • if the Button component does not have a testID prop.
  • if the Text component does not have a testID prop only if it has an onPress prop defined.
  • if the TextInput component does not have an accessible prop.

Rule Configurations

You can use the following configurations to customize the plugin. But,

  • props, propName and components are mandatory.
  • components can be a string or an object with component and dependOn properties.

Props: Array of validation objects PropName: Name of the prop to be validated Components: Array of components to be validated against the propName

Features

1. dependOn feature

If you need to check whether a prop exists only if another prop exists, you can use the dependOn feature. This feature allows you to specify that prop validation for a particular component should only be performed if another prop is present.

2. ignoreESLintPropValidation prop feature

If you need to ignore the ESLint prop validation of a component in a specific scenario, you can use the ignoreESLintPropValidation prop. Examples are included below.

##Examples

  1. A warning will be generated instead of an error
{ 'rules': { 'props-checker/validator': ['warn', { props: [{ propName: 'customProp', components: ['CustomComponent'] }] }] } }
  1. Multiple components with multiple dependOn to validate against testID prop.
{
    'rules':
        {
            'props-checker/validator':
                [
                    'error',
                    {
                        props:
                            [
                                { propName: 'testID', components: [{ component: 'Button', dependOn: 'title' }, { component: 'Text', dependOn: 'onPress' }] }
                            ]
                    }
                ]
        }
}
  1. Using ignoreESLintPropValidation prop

In this example, as you can see, CustomComponent must have a customProp property. But, we can ignore the ESLint prop validation by using the ignoreESLintPropValidation prop.

{ 'rules': { 'props-checker/validator': ['error', { props: [{ propName: 'anyProp', components: [ 'AnyComponent'] }] }] } }

If you want to ignore the ESLint prop validation of the AnyComponent in a particular situation/screen, you can use the ignoreESLintPropValidation prop.

<AnyComponent ignoreESLintPropValidation>

ESLint Extends

If this plugin configurations take too much space from your main eslintrc file, you can use the extends feature from eslint to keep these rule settings in another file in another directory. Example can be found here.

Contribution

All PRs are welcome. Please raise an issue before raising a PR.