reactStableReferences.helpers.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { Node, Project } from "ts-morph";
  2. interface Finding {
  3. file: string;
  4. line: number;
  5. message: string;
  6. }
  7. export function createProject() {
  8. const project = new Project({
  9. compilerOptions: {
  10. allowJs: true,
  11. jsx: 4,
  12. },
  13. });
  14. project.addSourceFilesAtPaths(["packages/ui/src/**/*.tsx"]);
  15. return project;
  16. }
  17. export function formatFinding(finding: Finding) {
  18. return `${finding.file}:${finding.line} ${finding.message}`;
  19. }
  20. function sourceLocation(sourceFile: ReturnType<Project["getSourceFiles"]>[number], node: Node) {
  21. const { line } = sourceFile.getLineAndColumnAtPos(node.getStart());
  22. return {
  23. file: sourceFile.getFilePath().replace(`${process.cwd()}/`, ""),
  24. line,
  25. };
  26. }
  27. function getJsxTagRoot(tagName: string) {
  28. return tagName.split(".")[0] ?? tagName;
  29. }
  30. function getUnstableExpressionKind(expression: Node | undefined) {
  31. if (!expression) {
  32. return null;
  33. }
  34. if (Node.isArrowFunction(expression) || Node.isFunctionExpression(expression)) {
  35. return "function";
  36. }
  37. if (Node.isArrayLiteralExpression(expression)) {
  38. return "array";
  39. }
  40. if (Node.isObjectLiteralExpression(expression)) {
  41. return "object";
  42. }
  43. if (
  44. Node.isJsxElement(expression) ||
  45. Node.isJsxFragment(expression) ||
  46. Node.isJsxSelfClosingElement(expression)
  47. ) {
  48. return "jsx";
  49. }
  50. return null;
  51. }
  52. function collectMemoComponentNames(project: Project) {
  53. const memoComponentNames = new Set<string>();
  54. for (const sourceFile of project.getSourceFiles()) {
  55. sourceFile.forEachDescendant((node) => {
  56. if (!Node.isVariableDeclaration(node)) {
  57. return;
  58. }
  59. const initializer = node.getInitializer();
  60. if (
  61. !initializer ||
  62. !Node.isCallExpression(initializer) ||
  63. !["memo", "React.memo"].includes(initializer.getExpression().getText())
  64. ) {
  65. return;
  66. }
  67. const exportedName = node.getNameNode().getText();
  68. memoComponentNames.add(exportedName);
  69. const memoTarget = initializer.getArguments()[0];
  70. if (memoTarget && Node.isIdentifier(memoTarget)) {
  71. memoComponentNames.add(memoTarget.getText());
  72. }
  73. });
  74. }
  75. return memoComponentNames;
  76. }
  77. export function collectInlinePropsForMemoComponents(project: Project) {
  78. const memoComponentNames = collectMemoComponentNames(project);
  79. const findings: Finding[] = [];
  80. for (const sourceFile of project.getSourceFiles()) {
  81. sourceFile.forEachDescendant((node) => {
  82. if (!Node.isJsxOpeningElement(node) && !Node.isJsxSelfClosingElement(node)) {
  83. return;
  84. }
  85. const tagName = node.getTagNameNode().getText();
  86. if (!memoComponentNames.has(getJsxTagRoot(tagName))) {
  87. return;
  88. }
  89. for (const attribute of node.getAttributes()) {
  90. if (!Node.isJsxAttribute(attribute)) {
  91. continue;
  92. }
  93. const initializer = attribute.getInitializer();
  94. if (!initializer || !Node.isJsxExpression(initializer)) {
  95. continue;
  96. }
  97. const unstableKind = getUnstableExpressionKind(initializer.getExpression());
  98. if (!unstableKind) {
  99. continue;
  100. }
  101. const { file, line } = sourceLocation(sourceFile, attribute);
  102. findings.push({
  103. file,
  104. line,
  105. message: `<${tagName}> prop "${attribute.getNameNode().getText()}" uses inline ${unstableKind}`,
  106. });
  107. }
  108. });
  109. }
  110. return findings;
  111. }
  112. export function collectMemoComponentsWithInlineDefaults(project: Project) {
  113. const memoComponentNames = collectMemoComponentNames(project);
  114. const findings: Finding[] = [];
  115. const inspectFunction = (name: string, fn: Node) => {
  116. if (!memoComponentNames.has(name)) {
  117. return;
  118. }
  119. if (
  120. !Node.isFunctionDeclaration(fn) &&
  121. !Node.isFunctionExpression(fn) &&
  122. !Node.isArrowFunction(fn)
  123. ) {
  124. return;
  125. }
  126. const firstParameter = fn.getParameters()[0];
  127. const nameNode = firstParameter?.getNameNode();
  128. if (!nameNode || !Node.isObjectBindingPattern(nameNode)) {
  129. return;
  130. }
  131. for (const element of nameNode.getElements()) {
  132. const unstableKind = getUnstableExpressionKind(element.getInitializer());
  133. if (!unstableKind) {
  134. continue;
  135. }
  136. const sourceFile = fn.getSourceFile();
  137. const { file, line } = sourceLocation(sourceFile, element);
  138. findings.push({
  139. file,
  140. line,
  141. message: `${name} default prop "${element.getNameNode().getText()}" creates inline ${unstableKind}`,
  142. });
  143. }
  144. };
  145. for (const sourceFile of project.getSourceFiles()) {
  146. sourceFile.forEachDescendant((node) => {
  147. if (Node.isFunctionDeclaration(node)) {
  148. const name = node.getNameNode()?.getText();
  149. if (name) {
  150. inspectFunction(name, node);
  151. }
  152. return;
  153. }
  154. if (!Node.isVariableDeclaration(node)) {
  155. return;
  156. }
  157. const initializer = node.getInitializer();
  158. if (!initializer) {
  159. return;
  160. }
  161. const name = node.getNameNode().getText();
  162. if (Node.isArrowFunction(initializer) || Node.isFunctionExpression(initializer)) {
  163. inspectFunction(name, initializer);
  164. return;
  165. }
  166. if (
  167. Node.isCallExpression(initializer) &&
  168. ["memo", "React.memo"].includes(initializer.getExpression().getText())
  169. ) {
  170. const memoTarget = initializer.getArguments()[0];
  171. if (
  172. memoTarget &&
  173. (Node.isArrowFunction(memoTarget) || Node.isFunctionExpression(memoTarget))
  174. ) {
  175. inspectFunction(name, memoTarget);
  176. }
  177. }
  178. });
  179. }
  180. return findings;
  181. }
  182. export function collectInlineProviderValues(project: Project) {
  183. const findings: Finding[] = [];
  184. for (const sourceFile of project.getSourceFiles()) {
  185. sourceFile.forEachDescendant((node) => {
  186. if (!Node.isJsxOpeningElement(node) && !Node.isJsxSelfClosingElement(node)) {
  187. return;
  188. }
  189. const tagName = node.getTagNameNode().getText();
  190. if (!tagName.endsWith(".Provider")) {
  191. return;
  192. }
  193. for (const attribute of node.getAttributes()) {
  194. if (!Node.isJsxAttribute(attribute) || attribute.getNameNode().getText() !== "value") {
  195. continue;
  196. }
  197. const initializer = attribute.getInitializer();
  198. const expression =
  199. initializer && Node.isJsxExpression(initializer)
  200. ? initializer.getExpression()
  201. : undefined;
  202. if (!expression || !Node.isObjectLiteralExpression(expression)) {
  203. continue;
  204. }
  205. const { file, line } = sourceLocation(sourceFile, attribute);
  206. findings.push({
  207. file,
  208. line,
  209. message: `<${tagName}> value uses inline object`,
  210. });
  211. }
  212. });
  213. }
  214. return findings;
  215. }