main
ts 39 lines 1.13 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 import {
9 ErrorCategory,
10 getRuleForCategory,
11 } from 'babel-plugin-react-compiler/src/CompilerError';
12 import {normalizeIndent, testRule, makeTestCaseError} from './shared-utils';
13 import {allRules} from '../src/rules/ReactCompilerRule';
14
15 testRule(
16 'no impure function calls rule',
17 allRules[getRuleForCategory(ErrorCategory.Purity).name].rule,
18 {
19 valid: [],
20 invalid: [
21 {
22 name: 'Known impure function calls are caught',
23 code: normalizeIndent`
24 function Component() {
25 const date = Date.now();
26 const now = performance.now();
27 const rand = Math.random();
28 return <Foo date={date} now={now} rand={rand} />;
29 }
30 `,
31 errors: [
32 makeTestCaseError('Cannot call impure function during render'),
33 makeTestCaseError('Cannot call impure function during render'),
34 makeTestCaseError('Cannot call impure function during render'),
35 ],
36 },
37 ],
38 },
39 );