Home / Function/ transform() — react Function Reference

transform() — react Function Reference

Architecture documentation for the transform() function in transform-react-version-pragma.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  d6dad828_e413_234d_ad0f_7e2811351594["transform()"]
  c81d1f91_4fe7_db14_762c_b2e93bcb0944["transform-react-version-pragma.js"]
  d6dad828_e413_234d_ad0f_7e2811351594 -->|defined in| c81d1f91_4fe7_db14_762c_b2e93bcb0944
  style d6dad828_e413_234d_ad0f_7e2811351594 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/babel/transform-react-version-pragma.js lines 8–111

function transform(babel) {
  const {types: t} = babel;

  // Runs tests conditionally based on the version of react (semver range) we are running
  // Input:
  //   @reactVersion >= 17.0
  //   test('some test', () => {/*...*/})
  //
  // Output:
  //    @reactVersion >= 17.0
  //   _test_react_version('>= 17.0', 'some test', () => {/*...*/});
  //
  // See info about semver ranges here:
  // https://www.npmjs.com/package/semver
  function buildGateVersionCondition(comments) {
    if (!comments) {
      return null;
    }

    const resultingCondition = comments.reduce(
      (accumulatedCondition, commentLine) => {
        const commentStr = commentLine.value.trim();

        if (!commentStr.startsWith(GATE_VERSION_STR)) {
          return accumulatedCondition;
        }

        const condition = commentStr.slice(GATE_VERSION_STR.length);
        if (accumulatedCondition === null) {
          return condition;
        }

        return accumulatedCondition.concat(' ', condition);
      },
      null
    );

    if (resultingCondition === null) {
      return null;
    }

    return t.stringLiteral(resultingCondition);
  }

  return {
    name: 'transform-react-version-pragma',
    visitor: {
      ExpressionStatement(path) {
        const statement = path.node;
        const expression = statement.expression;
        if (expression.type === 'CallExpression') {
          const callee = expression.callee;
          switch (callee.type) {
            case 'Identifier': {
              if (
                callee.name === 'test' ||
                callee.name === 'it' ||
                callee.name === 'fit'
              ) {
                const comments = getComments(path);
                const condition = buildGateVersionCondition(comments);
                if (condition !== null) {
                  callee.name =
                    callee.name === 'fit'
                      ? '_test_react_version_focus'
                      : '_test_react_version';
                  expression.arguments = [condition, ...expression.arguments];
                } else if (REACT_VERSION_ENV) {
                  callee.name = '_test_ignore_for_react_version';
                }
              }
              break;
            }
            case 'MemberExpression': {
              if (
                callee.object.type === 'Identifier' &&
                (callee.object.name === 'test' ||
                  callee.object.name === 'it') &&
                callee.property.type === 'Identifier' &&
                callee.property.name === 'only'
              ) {

Domain

Subdomains

Frequently Asked Questions

What does transform() do?
transform() is a function in the react codebase, defined in scripts/babel/transform-react-version-pragma.js.
Where is transform() defined?
transform() is defined in scripts/babel/transform-react-version-pragma.js at line 8.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free