|
| 1 | +import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; |
| 2 | +import tsx from "dedent"; |
| 3 | + |
| 4 | +import { ruleTester } from "../../../../../test"; |
| 5 | +import rule, { RULE_NAME } from "./no-misused-capture-owner-stack"; |
| 6 | + |
| 7 | +ruleTester.run(RULE_NAME, rule, { |
| 8 | + invalid: [ |
| 9 | + { |
| 10 | + code: `import { captureOwnerStack } from 'react';`, |
| 11 | + errors: [ |
| 12 | + { |
| 13 | + type: T.ImportSpecifier, |
| 14 | + messageId: "useNamespaceImport", |
| 15 | + }, |
| 16 | + ], |
| 17 | + }, |
| 18 | + { |
| 19 | + code: `import { captureOwnerStack } from "react";`, |
| 20 | + errors: [ |
| 21 | + { |
| 22 | + type: T.ImportSpecifier, |
| 23 | + messageId: "useNamespaceImport", |
| 24 | + }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + { |
| 28 | + code: tsx` |
| 29 | + // Failing: Using named import directly |
| 30 | + import { captureOwnerStack } from "react"; |
| 31 | +
|
| 32 | + if (process.env.NODE_ENV !== "production") { |
| 33 | + const ownerStack = React.captureOwnerStack(); |
| 34 | + console.log("Owner Stack", ownerStack); |
| 35 | + } |
| 36 | + `, |
| 37 | + errors: [ |
| 38 | + { |
| 39 | + type: T.ImportSpecifier, |
| 40 | + messageId: "useNamespaceImport", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + { |
| 45 | + code: tsx` |
| 46 | + // Failing: Missing environment check |
| 47 | + import * as React from "react"; |
| 48 | +
|
| 49 | + const ownerStack = React.captureOwnerStack(); |
| 50 | +
|
| 51 | + console.log("Owner Stack", ownerStack); |
| 52 | + `, |
| 53 | + errors: [ |
| 54 | + { |
| 55 | + type: T.CallExpression, |
| 56 | + messageId: "missingDevelopmentOnlyCheck", |
| 57 | + }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + code: tsx` |
| 62 | + // Failing: Using named import directly without environment check |
| 63 | + import { captureOwnerStack } from "react"; |
| 64 | +
|
| 65 | + const ownerStack = React.captureOwnerStack(); |
| 66 | +
|
| 67 | + console.log("Owner Stack", ownerStack); |
| 68 | + `, |
| 69 | + errors: [ |
| 70 | + { |
| 71 | + type: T.ImportSpecifier, |
| 72 | + messageId: "useNamespaceImport", |
| 73 | + }, |
| 74 | + { |
| 75 | + type: T.CallExpression, |
| 76 | + messageId: "missingDevelopmentOnlyCheck", |
| 77 | + }, |
| 78 | + ], |
| 79 | + }, |
| 80 | + ], |
| 81 | + valid: [ |
| 82 | + { |
| 83 | + code: `import * as React from 'react';`, |
| 84 | + }, |
| 85 | + { |
| 86 | + code: `import {useState} from 'react';`, |
| 87 | + }, |
| 88 | + { |
| 89 | + code: `import {} from 'react';`, |
| 90 | + }, |
| 91 | + { |
| 92 | + code: `import * as React from "react";`, |
| 93 | + }, |
| 94 | + { |
| 95 | + code: `import {useState} from "react";`, |
| 96 | + }, |
| 97 | + { |
| 98 | + code: `import {} from "react";`, |
| 99 | + }, |
| 100 | + { |
| 101 | + code: tsx` |
| 102 | + // Passing: Correct namespace import with environment check |
| 103 | + import * as React from "react"; |
| 104 | +
|
| 105 | + if (process.env.NODE_ENV !== "production") { |
| 106 | + const ownerStack = React.captureOwnerStack(); |
| 107 | + console.log("Owner Stack", ownerStack); |
| 108 | + } |
| 109 | + `, |
| 110 | + }, |
| 111 | + { |
| 112 | + code: `import * as React from "@pika/react";`, |
| 113 | + settings: { |
| 114 | + "react-x": { |
| 115 | + importSource: "@pika/react", |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + ], |
| 120 | +}); |
0 commit comments