Ignore:
Timestamp:
12/12/24 17:06:06 (5 weeks ago)
Author:
stefan toskovski <stefantoska84@…>
Branches:
main
Parents:
d565449
Message:

Pred finalna verzija

File:
1 edited

Legend:

Unmodified
Added
Removed
  • imaps-frontend/node_modules/eslint-plugin-react/lib/rules/no-unstable-nested-components.js

    rd565449 r0c6b92a  
    66'use strict';
    77
     8const minimatch = require('minimatch');
    89const Components = require('../util/Components');
    910const docsUrl = require('../util/docsUrl');
     11const astUtil = require('../util/ast');
    1012const isCreateElement = require('../util/isCreateElement');
    1113const report = require('../util/report');
     
    2426/**
    2527 * Generate error message with given parent component name
    26  * @param {String} parentName Name of the parent component, if known
    27  * @returns {String} Error message with parent component name
     28 * @param {string} parentName Name of the parent component, if known
     29 * @returns {string} Error message with parent component name
    2830 */
    2931function generateErrorMessageWithParentName(parentName) {
     
    3234
    3335/**
    34  * Check whether given text starts with `render`. Comparison is case-sensitive.
    35  * @param {String} text Text to validate
    36  * @returns {Boolean}
    37  */
    38 function startsWithRender(text) {
    39   return (text || '').startsWith('render');
     36 * Check whether given text matches the pattern passed in.
     37 * @param {string} text Text to validate
     38 * @param {string} pattern Pattern to match against
     39 * @returns {boolean}
     40 */
     41function propMatchesRenderPropPattern(text, pattern) {
     42  return typeof text === 'string' && minimatch(text, pattern);
    4043}
    4144
     
    6366 * @param {ASTNode} node The AST node
    6467 * @param {Context} context eslint context
    65  * @returns {Boolean} True if node is a `createElement` call, false if not
     68 * @returns {boolean} True if node is a `createElement` call, false if not
    6669 */
    6770function isCreateElementMatcher(node, context) {
    6871  return (
    69     node
    70     && node.type === 'CallExpression'
     72    astUtil.isCallExpression(node)
    7173    && isCreateElement(context, node)
    7274  );
     
    7678 * Matcher used to check whether given node is a `ObjectExpression`
    7779 * @param {ASTNode} node The AST node
    78  * @returns {Boolean} True if node is a `ObjectExpression`, false if not
     80 * @returns {boolean} True if node is a `ObjectExpression`, false if not
    7981 */
    8082function isObjectExpressionMatcher(node) {
     
    8587 * Matcher used to check whether given node is a `JSXExpressionContainer`
    8688 * @param {ASTNode} node The AST node
    87  * @returns {Boolean} True if node is a `JSXExpressionContainer`, false if not
     89 * @returns {boolean} True if node is a `JSXExpressionContainer`, false if not
    8890 */
    8991function isJSXExpressionContainerMatcher(node) {
     
    9496 * Matcher used to check whether given node is a `JSXAttribute` of `JSXExpressionContainer`
    9597 * @param {ASTNode} node The AST node
    96  * @returns {Boolean} True if node is a `JSXAttribute` of `JSXExpressionContainer`, false if not
     98 * @returns {boolean} True if node is a `JSXAttribute` of `JSXExpressionContainer`, false if not
    9799 */
    98100function isJSXAttributeOfExpressionContainerMatcher(node) {
     
    108110 * Matcher used to check whether given node is an object `Property`
    109111 * @param {ASTNode} node The AST node
    110  * @returns {Boolean} True if node is a `Property`, false if not
     112 * @returns {boolean} True if node is a `Property`, false if not
    111113 */
    112114function isPropertyOfObjectExpressionMatcher(node) {
     
    116118    && node.parent.type === 'Property'
    117119  );
    118 }
    119 
    120 /**
    121  * Matcher used to check whether given node is a `CallExpression`
    122  * @param {ASTNode} node The AST node
    123  * @returns {Boolean} True if node is a `CallExpression`, false if not
    124  */
    125 function isCallExpressionMatcher(node) {
    126   return node && node.type === 'CallExpression';
    127120}
    128121
     
    133126 * ```
    134127 * @param {ASTNode} node The AST node
    135  * @returns {Boolean} True if node is directly inside `map` call, false if not
     128 * @returns {boolean} True if node is directly inside `map` call, false if not
    136129 */
    137130function isMapCall(node) {
     
    148141 * @param {ASTNode} node The AST node
    149142 * @param {Context} context eslint context
    150  * @returns {Boolean} True if node is a `ReturnStatement` of a React hook, false if not
     143 * @returns {boolean} True if node is a `ReturnStatement` of a React hook, false if not
    151144 */
    152145function isReturnStatementOfHook(node, context) {
     
    159152  }
    160153
    161   const callExpression = getClosestMatchingParent(node, context, isCallExpressionMatcher);
     154  const callExpression = getClosestMatchingParent(node, context, astUtil.isCallExpression);
    162155  return (
    163156    callExpression
     
    175168 * @param {ASTNode} node The AST node
    176169 * @param {Context} context eslint context
    177  * @returns {Boolean} True if component is declared inside a render prop, false if not
    178  */
    179 function isComponentInRenderProp(node, context) {
     170 * @param {string} propNamePattern a pattern to match render props against
     171 * @returns {boolean} True if component is declared inside a render prop, false if not
     172 */
     173function isComponentInRenderProp(node, context, propNamePattern) {
    180174  if (
    181175    node
     
    183177    && node.parent.type === 'Property'
    184178    && node.parent.key
    185     && startsWithRender(node.parent.key.name)
     179    && propMatchesRenderPropPattern(node.parent.key.name, propNamePattern)
    186180  ) {
    187181    return true;
     
    212206
    213207    // Starts with render, e.g. <Component renderFooter={() => <div />} />
    214     if (startsWithRender(propName)) {
     208    if (propMatchesRenderPropPattern(propName, propNamePattern)) {
    215209      return true;
    216210    }
     
    232226 *  ```
    233227 * @param {ASTNode} node The AST node
    234  * @returns {Boolean} True if component is declared inside a render property, false if not
    235  */
    236 function isDirectValueOfRenderProperty(node) {
     228 * @param {string} propNamePattern The pattern to match render props against
     229 * @returns {boolean} True if component is declared inside a render property, false if not
     230 */
     231function isDirectValueOfRenderProperty(node, propNamePattern) {
    237232  return (
    238233    node
     
    241236    && node.parent.key
    242237    && node.parent.key.type === 'Identifier'
    243     && startsWithRender(node.parent.key.name)
     238    && propMatchesRenderPropPattern(node.parent.key.name, propNamePattern)
    244239  );
    245240}
     
    248243 * Resolve the component name of given node
    249244 * @param {ASTNode} node The AST node of the component
    250  * @returns {String} Name of the component, if any
     245 * @returns {string} Name of the component, if any
    251246 */
    252247function resolveComponentName(node) {
     
    287282          type: 'boolean',
    288283        },
     284        propNamePattern: {
     285          type: 'string',
     286        },
    289287      },
    290288      additionalProperties: false,
     
    294292  create: Components.detect((context, components, utils) => {
    295293    const allowAsProps = context.options.some((option) => option && option.allowAsProps);
     294    const propNamePattern = (context.options[0] || {}).propNamePattern || 'render*';
    296295
    297296    /**
     
    304303     * ```
    305304     * @param {ASTNode} node The AST node being checked
    306      * @returns {Boolean} True if node is inside class component's render block, false if not
     305     * @returns {boolean} True if node is inside class component's render block, false if not
    307306     */
    308307    function isInsideRenderMethod(node) {
     
    332331     * ```
    333332     * @param {ASTNode} node The AST node being checked
    334      * @returns {Boolean} True if given node a function component declared inside class component, false if not
     333     * @returns {boolean} True if given node a function component declared inside class component, false if not
    335334     */
    336335    function isFunctionComponentInsideClassComponent(node) {
     
    355354     * ```
    356355     * @param {ASTNode} node The AST node
    357      * @returns {Boolean} True if node is declare inside `createElement` call's props, false if not
     356     * @returns {boolean} True if node is declare inside `createElement` call's props, false if not
    358357     */
    359358    function isComponentInsideCreateElementsProp(node) {
     
    378377     * ```
    379378     * @param {ASTNode} node The AST node being checked
    380      * @returns {Boolean} True if node is a component declared inside prop, false if not
     379     * @returns {boolean} True if node is a component declared inside prop, false if not
    381380     */
    382381    function isComponentInProp(node) {
     
    400399     * ```
    401400     * @param {ASTNode} node The AST node being checked
    402      * @returns {Boolean} True if node is a stateless component returning non-JSX, false if not
     401     * @returns {boolean} True if node is a stateless component returning non-JSX, false if not
    403402     */
    404403    function isStatelessComponentReturningNull(node) {
     
    428427      if (
    429428        // Support allowAsProps option
    430         (isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context)))
     429        (isDeclaredInsideProps && (allowAsProps || isComponentInRenderProp(node, context, propNamePattern)))
    431430
    432431        // Prevent reporting components created inside Array.map calls
     
    438437
    439438        // Do not mark objects containing render methods
    440         || isDirectValueOfRenderProperty(node)
     439        || isDirectValueOfRenderProperty(node, propNamePattern)
    441440
    442441        // Prevent reporting nested class components twice
Note: See TracChangeset for help on using the changeset viewer.