module() — react Function Reference
Architecture documentation for the module() function in transform-prevent-infinite-loops.js from the react codebase.
Entity Profile
Dependency Diagram
graph TD c5499f95_7fe5_8f90_777d_6a20d309ecc3["module()"] 116af516_4542_33de_d0ea_bedd0d6903ad["transform-prevent-infinite-loops.js"] c5499f95_7fe5_8f90_777d_6a20d309ecc3 -->|defined in| 116af516_4542_33de_d0ea_bedd0d6903ad style c5499f95_7fe5_8f90_777d_6a20d309ecc3 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
scripts/babel/transform-prevent-infinite-loops.js lines 21–74
module.exports = ({types: t, template}) => {
// We set a global so that we can later fail the test
// even if the error ends up being caught by the code.
const buildGuard = template(`
if (%%iterator%%++ > %%maxIterations%%) {
global.infiniteLoopError = new RangeError(
'Potential infinite loop: exceeded ' +
%%maxIterations%% +
' iterations.'
);
throw global.infiniteLoopError;
}
`);
return {
visitor: {
'WhileStatement|ForStatement|DoWhileStatement': (path, file) => {
const filename = file.file.opts.filename;
const maxIterations = t.logicalExpression(
'||',
t.memberExpression(
t.identifier('global'),
t.identifier('__MAX_ITERATIONS__')
),
t.numericLiteral(
filename.indexOf('__tests__') === -1
? MAX_SOURCE_ITERATIONS
: MAX_TEST_ITERATIONS
)
);
// An iterator that is incremented with each iteration
const iterator = path.scope.parent.generateUidIdentifier('loopIt');
const iteratorInit = t.numericLiteral(0);
path.scope.parent.push({
id: iterator,
init: iteratorInit,
});
// If statement and throw error if it matches our criteria
const guard = buildGuard({
iterator,
maxIterations,
});
// No block statement e.g. `while (1) 1;`
if (!path.get('body').isBlockStatement()) {
const statement = path.get('body').node;
path.get('body').replaceWith(t.blockStatement([guard, statement]));
} else {
path.get('body').unshiftContainer('body', guard);
}
},
},
};
};
Domain
Subdomains
Source
Frequently Asked Questions
What does module() do?
module() is a function in the react codebase, defined in scripts/babel/transform-prevent-infinite-loops.js.
Where is module() defined?
module() is defined in scripts/babel/transform-prevent-infinite-loops.js at line 21.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free