Skip to content

⬅️ Back to Table of Contents

📄 validationHelpers

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 4
📊 Variables & Constants 6

📚 Table of Contents

🛠️ File Location:

📂 packages/rule-tester/src/utils/validationHelpers.ts

📦 Imports

Name Source
TSESTree @typescript-eslint/utils
Parser @typescript-eslint/utils/ts-eslint
SourceCode @typescript-eslint/utils/ts-eslint
simpleTraverse @typescript-eslint/typescript-estree

Variables & Constants

Name Type Kind Value Exported
RULE_TESTER_PARAMETERS readonly ["after", "before", "code", ... const [ 'after', 'before', 'code', 'defaultFilenames', 'dependencyConstraints', 'er...
ERROR_OBJECT_PARAMETERS ReadonlySet<string> const new Set([ 'column', 'data', 'endColumn', 'endLine', 'line', 'message', 'messa...
FRIENDLY_ERROR_OBJECT_PARAM... string const [${[ ...ERROR_OBJECT_PARAMETERS, ] .map(key => `'${key}'`) .join(', ')}]
SUGGESTION_OBJECT_PARAMETERS ReadonlySet<string> const new Set([ 'data', 'desc', 'messageId', 'output', ])
FRIENDLY_SUGGESTION_OBJECT_... string const [${[ ...SUGGESTION_OBJECT_PARAMETERS, ] .map(key => `'${key}'`) .join(', ')}]
REQUIRED_SCENARIOS readonly ["valid", "invalid"] const ['valid', 'invalid'] as const

Functions

sanitize(text: string): string

Replace control characters by \u00xx form.

Raw JSDoc
/**
 * Replace control characters by `\u00xx` form.
 */

Calls:

  • text.replaceAll
  • c.codePointAt(0)!.toString(16).padStart

Internal Comments:

// eslint-disable-next-line no-control-regex
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion (x3)

Code
export function sanitize(text: string): string {
  if (typeof text !== 'string') {
    return '';
  }
  return text.replaceAll(
    // eslint-disable-next-line no-control-regex
    /[\u0000-\u0009\u000b-\u001a]/gu,
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    c => `\\u${c.codePointAt(0)!.toString(16).padStart(4, '0')}`,
  );
}

wrapParser(parser: Parser.LooseParserModule): Parser.LooseParserModule

Wraps the given parser in order to intercept and modify return values from the parse and parseForESLint methods, for test purposes. In particular, to modify ast nodes, tokens and comments to throw on access to their start and end properties.

Raw JSDoc
/**
 * Wraps the given parser in order to intercept and modify return values from the `parse` and `parseForESLint` methods, for test purposes.
 * In particular, to modify ast nodes, tokens and comments to throw on access to their `start` and `end` properties.
 */

Calls:

  • Object.defineProperties
  • simpleTraverse (from @typescript-eslint/typescript-estree)
  • defineStartEndAsError
  • ast.tokens?.forEach
  • ast.comments?.forEach
  • parser.parseForESLint
  • defineStartEndAsErrorInTree
  • parser.parse

Internal Comments:

/**
   * Define `start`/`end` properties of all nodes of the given AST as throwing error.
   */
/**
     * Define `start`/`end` properties as throwing error.
     */
// @ts-expect-error -- see above (x4)

Code
export function wrapParser(
  parser: Parser.LooseParserModule,
): Parser.LooseParserModule {
  /**
   * Define `start`/`end` properties of all nodes of the given AST as throwing error.
   */
  function defineStartEndAsErrorInTree(
    ast: TSESTree.Program,
    visitorKeys?: Readonly<SourceCode.VisitorKeys>,
  ): void {
    /**
     * Define `start`/`end` properties as throwing error.
     */
    function defineStartEndAsError(objName: string, node: unknown): void {
      Object.defineProperties(node, {
        end: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[1] instead of ${objName}.end`,
            );
          },
        },
        start: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[0] instead of ${objName}.start`,
            );
          },
        },
      });
    }

    simpleTraverse(ast, {
      enter: node => defineStartEndAsError('node', node),
      visitorKeys,
    });
    ast.tokens?.forEach(token => defineStartEndAsError('token', token));
    ast.comments?.forEach(comment => defineStartEndAsError('token', comment));
  }

  if ('parseForESLint' in parser) {
    return {
      parseForESLint(...args): Parser.ParseResult {
        const parsed = parser.parseForESLint(...args) as Parser.ParseResult;

        defineStartEndAsErrorInTree(parsed.ast, parsed.visitorKeys);
        return parsed;
      },

      // @ts-expect-error -- see above
      [parserSymbol]: parser,
    };
  }

  return {
    parse(...args): TSESTree.Program {
      const ast = parser.parse(...args) as TSESTree.Program;

      defineStartEndAsErrorInTree(ast);
      return ast;
    },

    // @ts-expect-error -- see above
    [parserSymbol]: parser,
  };
}

Internal helpers

Declared inside another function in this file.

defineStartEndAsErrorInTree(ast: TSESTree.Program, visitorKeys: Readonly<SourceCode.VisitorKeys>): void

Define start/end properties of all nodes of the given AST as throwing error.

Raw JSDoc
/**
   * Define `start`/`end` properties of all nodes of the given AST as throwing error.
   */

Calls:

  • Object.defineProperties
  • simpleTraverse (from @typescript-eslint/typescript-estree)
  • defineStartEndAsError
  • ast.tokens?.forEach
  • ast.comments?.forEach

Internal Comments:

/**
     * Define `start`/`end` properties as throwing error.
     */

Code
function defineStartEndAsErrorInTree(
    ast: TSESTree.Program,
    visitorKeys?: Readonly<SourceCode.VisitorKeys>,
  ): void {
    /**
     * Define `start`/`end` properties as throwing error.
     */
    function defineStartEndAsError(objName: string, node: unknown): void {
      Object.defineProperties(node, {
        end: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[1] instead of ${objName}.end`,
            );
          },
        },
        start: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[0] instead of ${objName}.start`,
            );
          },
        },
      });
    }

    simpleTraverse(ast, {
      enter: node => defineStartEndAsError('node', node),
      visitorKeys,
    });
    ast.tokens?.forEach(token => defineStartEndAsError('token', token));
    ast.comments?.forEach(comment => defineStartEndAsError('token', comment));
  }

defineStartEndAsError(objName: string, node: unknown): void

Define start/end properties as throwing error.

Raw JSDoc
/**
     * Define `start`/`end` properties as throwing error.
     */

Calls:

  • Object.defineProperties
Code
function defineStartEndAsError(objName: string, node: unknown): void {
      Object.defineProperties(node, {
        end: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[1] instead of ${objName}.end`,
            );
          },
        },
        start: {
          configurable: true,
          enumerable: false,
          get() {
            throw new Error(
              `Use ${objName}.range[0] instead of ${objName}.start`,
            );
          },
        },
      });
    }

Generated by Syntax Scribe