123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * @fileOverview Enforce a defaultProps definition for every prop that is not a required prop.
- * @author Vitor Balocco
- */
- 'use strict';
- const Components = require('../util/Components');
- const docsUrl = require('../util/docsUrl');
- const astUtil = require('../util/ast');
- // ------------------------------------------------------------------------------
- // Rule Definition
- // ------------------------------------------------------------------------------
- module.exports = {
- meta: {
- docs: {
- description: 'Enforce a defaultProps definition for every prop that is not a required prop.',
- category: 'Best Practices',
- url: docsUrl('require-default-props')
- },
- messages: {
- noDefaultWithRequired: 'propType "{{name}}" is required and should not have a defaultProps declaration.',
- shouldHaveDefault: 'propType "{{name}}" is not required, but has no corresponding defaultProps declaration.'
- },
- schema: [{
- type: 'object',
- properties: {
- forbidDefaultForRequired: {
- type: 'boolean'
- },
- ignoreFunctionalComponents: {
- type: 'boolean'
- }
- },
- additionalProperties: false
- }]
- },
- create: Components.detect((context, components) => {
- const configuration = context.options[0] || {};
- const forbidDefaultForRequired = configuration.forbidDefaultForRequired || false;
- const ignoreFunctionalComponents = configuration.ignoreFunctionalComponents || false;
- /**
- * Reports all propTypes passed in that don't have a defaultProps counterpart.
- * @param {Object[]} propTypes List of propTypes to check.
- * @param {Object} defaultProps Object of defaultProps to check. Keys are the props names.
- * @return {void}
- */
- function reportPropTypesWithoutDefault(propTypes, defaultProps) {
- // If this defaultProps is "unresolved", then we should ignore this component and not report
- // any errors for it, to avoid false-positives with e.g. external defaultProps declarations or spread operators.
- if (defaultProps === 'unresolved') {
- return;
- }
- Object.keys(propTypes).forEach((propName) => {
- const prop = propTypes[propName];
- if (prop.isRequired) {
- if (forbidDefaultForRequired && defaultProps[propName]) {
- context.report({
- node: prop.node,
- messageId: 'noDefaultWithRequired',
- data: {name: propName}
- });
- }
- return;
- }
- if (defaultProps[propName]) {
- return;
- }
- context.report({
- node: prop.node,
- messageId: 'shouldHaveDefault',
- data: {name: propName}
- });
- });
- }
- // --------------------------------------------------------------------------
- // Public API
- // --------------------------------------------------------------------------
- return {
- 'Program:exit'() {
- const list = components.list();
- Object.keys(list).filter((component) => {
- if (ignoreFunctionalComponents
- && (astUtil.isFunction(list[component].node) || astUtil.isFunctionLikeExpression(list[component].node))) {
- return false;
- }
- return list[component].declaredPropTypes;
- }).forEach((component) => {
- reportPropTypesWithoutDefault(
- list[component].declaredPropTypes,
- list[component].defaultProps || {}
- );
- });
- }
- };
- })
- };
|