{"size": 4597, "ext": "js", "max_stars_repo_path": "lib/rules/no-typos.js", "max_stars_repo_name": "infernojs/eslint-plugin-inferno", "max_stars_repo_stars_event_min_datetime": "2016-12-07T15:34:57.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-29T17:44:38.000Z", "max_issues_repo_path": "lib/rules/no-typos.js", "max_issues_repo_name": "infernojs/eslint-plugin-inferno", "max_issues_repo_issues_event_min_datetime": "2017-02-08T06:51:17.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-21T12:35:09.000Z", "max_forks_repo_path": "lib/rules/no-typos.js", "max_forks_repo_name": "infernojs/eslint-plugin-inferno", "max_forks_repo_forks_event_min_datetime": "2017-02-07T13:27:51.000Z", "max_forks_repo_forks_event_max_datetime": "2019-06-12T10:47:13.000Z"}
{"max_stars_count": 16, "max_issues_count": 14, "max_forks_count": 9, "avg_line_length": 29.8506493506, "max_line_length": 119, "alphanum_fraction": 0.5860343702}
/**
 * @fileoverview Prevent common casing typos
 */

'use strict';

const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const STATIC_CLASS_PROPERTIES = ['defaultProps'];
const STATIC_LIFECYCLE_METHODS = ['getDerivedStateFromProps'];
const LIFECYCLE_METHODS = [
  'getDerivedStateFromProps',
  'componentWillMount',
  'componentDidMount',
  'componentWillReceiveProps',
  'shouldComponentUpdate',
  'componentWillUpdate',
  'getSnapshotBeforeUpdate',
  'componentDidUpdate',
  'componentWillUnmount',
  'render'
];

module.exports = {
  meta: {
    docs: {
      description: 'Prevent common typos',
      category: 'Stylistic Issues',
      recommended: false,
      url: docsUrl('no-typos')
    },

    messages: {
      typoPropTypeChain: 'Typo in prop type chain qualifier: {{name}}',
      typoPropType: 'Typo in declared prop type: {{name}}',
      typoStaticClassProp: 'Typo in static class property declaration',
      typoPropDeclaration: 'Typo in property declaration',
      typoLifecycleMethod: 'Typo in component lifecycle method declaration: {{actual}} should be {{expected}}',
      staticLifecycleMethod: 'Lifecycle method should be static: {{method}}',
      noPropTypesBinding: '`\'prop-types\'` imported without a local `PropTypes` binding.'
    },

    schema: []
  },

  create: Components.detect((context, components, utils) => {
    function reportErrorIfPropertyCasingTypo(propertyValue, propertyKey, isClassProperty) {
      const propertyName = propertyKey.name;
      STATIC_CLASS_PROPERTIES.forEach((CLASS_PROP) => {
        if (propertyName && CLASS_PROP.toLowerCase() === propertyName.toLowerCase() && CLASS_PROP !== propertyName) {
          context.report({
            node: propertyKey,
            messageId: isClassProperty
              ? 'typoStaticClassProp'
              : 'typoPropDeclaration'
          });
        }
      });
    }

    function reportErrorIfLifecycleMethodCasingTypo(node) {
      let nodeKeyName = node.key.name;
      if (node.key.type === 'Literal') {
        nodeKeyName = node.key.value;
      }
      if (node.computed && typeof nodeKeyName !== 'string') {
        return;
      }

      STATIC_LIFECYCLE_METHODS.forEach((method) => {
        if (!node.static && nodeKeyName.toLowerCase() === method.toLowerCase()) {
          context.report({
            node,
            messageId: 'staticLifecycleMethod',
            data: {
              method: nodeKeyName
            }
          });
        }
      });

      LIFECYCLE_METHODS.forEach((method) => {
        if (method.toLowerCase() === nodeKeyName.toLowerCase() && method !== nodeKeyName) {
          context.report({
            node,
            messageId: 'typoLifecycleMethod',
            data: {actual: nodeKeyName, expected: method}
          });
        }
      });
    }

    return {

      ClassProperty(node) {
        if (!node.static || !utils.isES6Component(node.parent.parent)) {
          return;
        }

        reportErrorIfPropertyCasingTypo(node.value, node.key, true);
      },

      MemberExpression(node) {
        const propertyName = node.property.name;

        if (
          !propertyName
          || STATIC_CLASS_PROPERTIES.map((prop) => prop.toLocaleLowerCase()).indexOf(propertyName.toLowerCase()) === -1
        ) {
          return;
        }

        const relatedComponent = utils.getRelatedComponent(node);

        if (
          relatedComponent
          && (utils.isES6Component(relatedComponent.node) || utils.isReturningJSX(relatedComponent.node))
          && (node.parent && node.parent.type === 'AssignmentExpression' && node.parent.right)
        ) {
          reportErrorIfPropertyCasingTypo(node.parent.right, node.property, true);
        }
      },

      MethodDefinition(node) {
        if (!utils.isES6Component(node.parent.parent)) {
          return;
        }

        reportErrorIfLifecycleMethodCasingTypo(node);
      },

      ObjectExpression(node) {
        const component = utils.isES5Component(node) && components.get(node);

        if (!component) {
          return;
        }

        node.properties.forEach((property) => {
          if (property.type !== 'SpreadElement') {
            reportErrorIfPropertyCasingTypo(property.value, property.key, false);
            reportErrorIfLifecycleMethodCasingTypo(property);
          }
        });
      }
    };
  })
};
