@vwala/tooling-config 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.vwala/docs/README.md +4 -0
  2. package/.vwala/docs/react-19.md +119 -0
  3. package/.vwala/docs/react-compiler.md +128 -0
  4. package/README.md +22 -2
  5. package/package.json +16 -4
  6. package/src/_scripts/check-conventions.mjs +8 -0
  7. package/src/biome/_scripts/check-naming.mjs +23 -2
  8. package/src/biome/biome.config.json +1 -1
  9. package/src/biome/plugins/no-manual-react-memoization.grit +5 -0
  10. package/src/biome/plugins/no-react-use-context.grit +5 -0
  11. package/src/react-compiler/build/artifact-inventory.mjs +39 -0
  12. package/src/react-compiler/build/build-command-process.mjs +58 -0
  13. package/src/react-compiler/build/compile-react-library.mjs +39 -0
  14. package/src/react-compiler/build/emitted-artifacts.mjs +34 -0
  15. package/src/react-compiler/build/is-react-source.mjs +43 -0
  16. package/src/react-compiler/build/mark-library-compiler-ownership.mjs +47 -0
  17. package/src/react-compiler/build/overlay-react-library.mjs +53 -0
  18. package/src/react-compiler/build/read-build-project.mjs +33 -0
  19. package/src/react-compiler/build/transform-react-source.mjs +72 -0
  20. package/src/react-compiler/build/watch-react-library.mjs +111 -0
  21. package/src/react-compiler/policy/advisory-diagnostic.mjs +30 -0
  22. package/src/react-compiler/policy/biome-exit-code.mjs +30 -0
  23. package/src/react-compiler/policy/biome-report.mjs +46 -0
  24. package/src/react-compiler/policy/check-react-19-apis.mjs +30 -0
  25. package/src/react-compiler/policy/check-react-19-repository.mjs +33 -0
  26. package/src/react-compiler/policy/check-react-memoization.mjs +30 -0
  27. package/src/react-compiler/policy/check-repository.mjs +33 -0
  28. package/src/react-compiler/policy/compiler-scope.mjs +73 -0
  29. package/src/react-compiler/policy/find-manual-memoization.mjs +50 -0
  30. package/src/react-compiler/policy/find-react-19-apis.mjs +33 -0
  31. package/src/react-compiler/policy/memoization-exception.mjs +34 -0
  32. package/src/react-compiler/policy/react-19-scope.mjs +74 -0
  33. package/src/react-compiler/policy/react-call-name.mjs +42 -0
  34. package/src/react-compiler/policy/repository-files.mjs +48 -0
  35. package/src/react-compiler/policy/run-biome.mjs +55 -0
  36. package/src/react-compiler/policy/scoped-source.mjs +35 -0
@@ -0,0 +1,50 @@
1
+ import { parse } from "@babel/parser";
2
+ import traverseModule from "@babel/traverse";
3
+ import { hasMemoizationException } from "./memoization-exception.mjs";
4
+ import { reactCallName } from "./react-call-name.mjs";
5
+
6
+ const traverse = traverseModule.default ?? traverseModule;
7
+ const memoizationNames = new Set(["useMemo", "useCallback", "memo"]);
8
+
9
+ /** Resolve React calls through lexical imports, rather than spelling alone. */
10
+ export function findManualMemoization({ source, filename }) {
11
+ const ast = parse(source, {
12
+ sourceType: "unambiguous",
13
+ sourceFilename: filename,
14
+ plugins: [
15
+ "typescript",
16
+ ...(/\.[cm]?ts$/.test(filename) ? [] : ["jsx"]),
17
+ "decorators-legacy",
18
+ ],
19
+ });
20
+ const findings = [];
21
+ traverse(ast, {
22
+ "CallExpression|OptionalCallExpression"(call) {
23
+ const callee = call.get("callee");
24
+ const name = reactCallName(callee);
25
+ if (!memoizationNames.has(name)) {
26
+ return;
27
+ }
28
+
29
+ findings.push({
30
+ name,
31
+ line: call.node.loc.start.line,
32
+ column: call.node.loc.start.column + 1,
33
+ canonical: callee.isIdentifier() && callee.node.name === name,
34
+ statement: call.getStatementParent()?.node,
35
+ start: call.node.start,
36
+ });
37
+ },
38
+ });
39
+ return findings
40
+ .filter(
41
+ (finding) =>
42
+ !hasMemoizationException({
43
+ finding,
44
+ findings,
45
+ source,
46
+ comments: ast.comments,
47
+ }),
48
+ )
49
+ .map(({ name, line, column }) => ({ name, line, column }));
50
+ }
@@ -0,0 +1,33 @@
1
+ import { parse } from "@babel/parser";
2
+ import traverseModule from "@babel/traverse";
3
+ import { reactCallName } from "./react-call-name.mjs";
4
+
5
+ const traverse = traverseModule.default ?? traverseModule;
6
+ const legacyApiNames = new Set(["useContext", "forwardRef"]);
7
+
8
+ /** Find calls to React APIs superseded by React 19 syntax. */
9
+ export function findReact19Apis({ source, filename }) {
10
+ const ast = parse(source, {
11
+ sourceType: "unambiguous",
12
+ sourceFilename: filename,
13
+ plugins: [
14
+ "typescript",
15
+ ...(/\.[cm]?ts$/.test(filename) ? [] : ["jsx"]),
16
+ "decorators-legacy",
17
+ ],
18
+ });
19
+ const findings = [];
20
+ traverse(ast, {
21
+ "CallExpression|OptionalCallExpression"(call) {
22
+ const name = reactCallName(call.get("callee"));
23
+ if (legacyApiNames.has(name)) {
24
+ findings.push({
25
+ name,
26
+ line: call.node.loc.start.line,
27
+ column: call.node.loc.start.column + 1,
28
+ });
29
+ }
30
+ },
31
+ });
32
+ return findings;
33
+ }
@@ -0,0 +1,34 @@
1
+ /** Exceptions are deliberately statement-local and use canonical named calls. */
2
+ export function hasMemoizationException({
3
+ finding,
4
+ findings,
5
+ source,
6
+ comments,
7
+ }) {
8
+ if (!finding.canonical || !finding.statement) {
9
+ return false;
10
+ }
11
+ if (
12
+ findings.filter(
13
+ (other) =>
14
+ other.start >= finding.statement.start &&
15
+ other.start < finding.statement.end,
16
+ ).length !== 1
17
+ ) {
18
+ return false;
19
+ }
20
+ const statementLine = finding.statement.loc.start.line;
21
+ if (
22
+ !comments.some(
23
+ (comment) =>
24
+ comment.type === "CommentLine" &&
25
+ comment.loc.end.line === statementLine - 1,
26
+ )
27
+ ) {
28
+ return false;
29
+ }
30
+ const previousLine = source.split(/\r?\n/)[statementLine - 2] ?? "";
31
+ return /^\s*\/\/ biome-ignore lint\/plugin\/no-manual-react-memoization:\s*\S.*$/.test(
32
+ previousLine,
33
+ );
34
+ }
@@ -0,0 +1,74 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { parse } from "jsonc-parser";
4
+
5
+ export const react19Plugin = "no-react-use-context.grit";
6
+
7
+ /** Return package roots that explicitly adopt the React 19 API policy. */
8
+ export function readReact19Scope({ root }) {
9
+ const filename = ["biome.json", "biome.jsonc"]
10
+ .map((name) => path.join(root, name))
11
+ .find(existsSync);
12
+ if (!filename) {
13
+ return [];
14
+ }
15
+ const errors = [];
16
+ const config = parse(readFileSync(filename, "utf8"), errors, {
17
+ allowTrailingComma: true,
18
+ });
19
+ if (errors.length || !config || typeof config !== "object") {
20
+ throw new Error("Invalid root Biome JSONC configuration");
21
+ }
22
+ const plugins = (config.plugins ?? []).filter(
23
+ (plugin) =>
24
+ path.basename(
25
+ typeof plugin === "string" ? plugin : (plugin.path ?? ""),
26
+ ) === react19Plugin,
27
+ );
28
+ if (!plugins.length) {
29
+ return [];
30
+ }
31
+ const patterns = plugins.flatMap((plugin) => {
32
+ if (
33
+ typeof plugin === "string" ||
34
+ !Array.isArray(plugin.includes) ||
35
+ !plugin.includes.length
36
+ ) {
37
+ throw new Error("React 19 scope requires explicit package-root includes");
38
+ }
39
+ return plugin.includes;
40
+ });
41
+ for (const pattern of patterns.filter((value) => !value.startsWith("!"))) {
42
+ if (
43
+ typeof pattern !== "string" ||
44
+ !/^\*\*\/(?:[a-zA-Z0-9_-]+\/)+[a-zA-Z0-9_-]+\/\*\*$/.test(pattern)
45
+ ) {
46
+ throw new Error(
47
+ `React 19 scope must use literal package-root patterns: ${pattern}`,
48
+ );
49
+ }
50
+ }
51
+ const diagnosticPatterns = (config.overrides ?? []).flatMap((override) => {
52
+ const rule = override.linter?.rules?.suspicious?.noReactForwardRef;
53
+ const level = typeof rule === "string" ? rule : rule?.level;
54
+ if (level && level !== "off" && level !== "warn") {
55
+ throw new Error(
56
+ "React 19 noReactForwardRef scope must use warn severity",
57
+ );
58
+ }
59
+ return level === "warn" ? (override.includes ?? []) : [];
60
+ });
61
+ const normalize = (values) => JSON.stringify([...new Set(values)].sort());
62
+ if (normalize(patterns) !== normalize(diagnosticPatterns)) {
63
+ throw new Error(
64
+ "React 19 forwardRef diagnostic scope must exactly match useContext plugin scope",
65
+ );
66
+ }
67
+ return [
68
+ ...new Set(
69
+ patterns
70
+ .filter((pattern) => !pattern.startsWith("!"))
71
+ .map((pattern) => pattern.slice(3, -3)),
72
+ ),
73
+ ];
74
+ }
@@ -0,0 +1,42 @@
1
+ function reactImportBinding(identifier) {
2
+ if (!identifier.isIdentifier()) {
3
+ return undefined;
4
+ }
5
+ const binding = identifier.scope.getBinding(identifier.node.name)?.path;
6
+ return binding?.parentPath.isImportDeclaration() &&
7
+ binding.parent.source.value === "react"
8
+ ? binding
9
+ : undefined;
10
+ }
11
+
12
+ /** Resolve a call's API name only when its callee is bound to a React import. */
13
+ export function reactCallName(callee) {
14
+ if (callee.isIdentifier()) {
15
+ const binding = reactImportBinding(callee);
16
+ return binding?.isImportSpecifier()
17
+ ? (binding.node.imported.name ?? binding.node.imported.value)
18
+ : undefined;
19
+ }
20
+ if (!callee.isMemberExpression() && !callee.isOptionalMemberExpression()) {
21
+ return undefined;
22
+ }
23
+ const binding = reactImportBinding(callee.get("object"));
24
+ const importsDefault =
25
+ binding?.isImportSpecifier() &&
26
+ (binding.node.imported.name ?? binding.node.imported.value) === "default";
27
+ if (
28
+ !binding?.isImportDefaultSpecifier() &&
29
+ !binding?.isImportNamespaceSpecifier() &&
30
+ !importsDefault
31
+ ) {
32
+ return undefined;
33
+ }
34
+ if (!callee.node.computed) {
35
+ return callee.node.property.name;
36
+ }
37
+ const property = callee.get("property");
38
+ if (property.isTemplateLiteral() && property.node.expressions.length === 0) {
39
+ return property.node.quasis[0].value.cooked;
40
+ }
41
+ return property.node.value;
42
+ }
@@ -0,0 +1,48 @@
1
+ import { execFileSync } from "node:child_process";
2
+
3
+ /** Snapshot files and every changed destination without parsing human Git output. */
4
+ export function repositoryFiles({ root, baseRef }) {
5
+ const git = (args) =>
6
+ execFileSync("git", args, {
7
+ cwd: root,
8
+ encoding: "utf8",
9
+ maxBuffer: 32 * 1024 * 1024,
10
+ stdio: ["ignore", "pipe", "pipe"],
11
+ });
12
+ const list = (args) => git(args).split("\0").filter(Boolean);
13
+ let base;
14
+ try {
15
+ base = baseRef
16
+ ? git([
17
+ "rev-parse",
18
+ "--verify",
19
+ "--end-of-options",
20
+ `${baseRef}^{commit}`,
21
+ ]).trim()
22
+ : git(["merge-base", "origin/main", "HEAD"]).trim();
23
+ } catch (error) {
24
+ throw new Error(
25
+ `Repository comparison history unavailable for ${baseRef ?? "origin/main"}; fetch the comparison commit and retry`,
26
+ { cause: error },
27
+ );
28
+ }
29
+ const untracked = list(["ls-files", "--others", "--exclude-standard", "-z"]);
30
+ const diff = [
31
+ "diff",
32
+ "--name-only",
33
+ "--no-renames",
34
+ "--diff-filter=ACMRT",
35
+ "-z",
36
+ ];
37
+ return {
38
+ files: [
39
+ ...new Set([...list(["ls-files", "--cached", "-z"]), ...untracked]),
40
+ ].sort(),
41
+ touched: new Set([
42
+ ...list([...diff, base, "HEAD", "--"]),
43
+ ...list([...diff, "--cached", "--"]),
44
+ ...list([...diff, "--"]),
45
+ ...untracked,
46
+ ]),
47
+ };
48
+ }
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+ import { spawnSync } from "node:child_process";
3
+ import { createRequire } from "node:module";
4
+ import path from "node:path";
5
+ import { biomeExitCode } from "./biome-exit-code.mjs";
6
+ import { readBiomeReport } from "./biome-report.mjs";
7
+ import { readCompilerScope } from "./compiler-scope.mjs";
8
+ import { readReact19Scope } from "./react-19-scope.mjs";
9
+
10
+ try {
11
+ const root = process.cwd();
12
+ const scopes = readCompilerScope({ root });
13
+ const react19Scopes = readReact19Scope({ root });
14
+ const args = process.argv.slice(2);
15
+ if (
16
+ args.some((arg) => /^--(?:reporter|max-diagnostics)(?:=|\s*$)/.test(arg))
17
+ ) {
18
+ throw new Error(
19
+ "The wrapper owns --reporter and --max-diagnostics to require complete JSON diagnostics",
20
+ );
21
+ }
22
+ const warningFlags = new Set(["--error-on-warnings", "--warnings-as-errors"]);
23
+ const warningsAsErrors = args.some((arg) => warningFlags.has(arg));
24
+ const require = createRequire(path.join(root, "package.json"));
25
+ const result = spawnSync(
26
+ process.execPath,
27
+ [
28
+ require.resolve("@biomejs/biome/bin/biome"),
29
+ ...args.filter((arg) => !warningFlags.has(arg)),
30
+ "--reporter=json",
31
+ "--max-diagnostics=none",
32
+ ],
33
+ { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 },
34
+ );
35
+ process.stderr.write(result.stderr ?? "");
36
+ if (result.error) {
37
+ throw result.error;
38
+ }
39
+ if (result.signal) {
40
+ throw new Error(`Biome terminated by ${result.signal}`);
41
+ }
42
+ const report = readBiomeReport(result.stdout);
43
+ process.stdout.write(result.stdout);
44
+ process.exitCode = biomeExitCode({
45
+ report,
46
+ status: result.status,
47
+ warningsAsErrors,
48
+ root,
49
+ scopes,
50
+ react19Scopes,
51
+ });
52
+ } catch (error) {
53
+ console.error(`Biome compiler wrapper: ${error.message}`);
54
+ process.exitCode = 1;
55
+ }
@@ -0,0 +1,35 @@
1
+ const excludedDirectories = [
2
+ "node_modules",
3
+ ".git",
4
+ "dist",
5
+ "build",
6
+ ".next",
7
+ ".expo",
8
+ "coverage",
9
+ "generated",
10
+ "__generated__",
11
+ "__tests__",
12
+ "test-support",
13
+ "fixtures",
14
+ "__fixtures__",
15
+ "vendor",
16
+ "vendored",
17
+ ];
18
+ export const memoizationExclusions = [
19
+ ...excludedDirectories.map((directory) => `!**/${directory}/**`),
20
+ "!**/*.test.*",
21
+ "!**/*.spec.*",
22
+ "!**/*.d.ts",
23
+ "!**/*.d.mts",
24
+ "!**/*.d.cts",
25
+ ];
26
+
27
+ /** Production React-capable source within explicitly adopted package roots. */
28
+ export function isScopedSource({ filename, scopes }) {
29
+ return (
30
+ scopes.some((scope) => filename.startsWith(`${scope}/`)) &&
31
+ /\.[cm]?[jt]sx?$/.test(filename) &&
32
+ !/\.(?:test|spec)\.[^/]+$|\.d\.[cm]?ts$/.test(filename) &&
33
+ !filename.split("/").some((part) => excludedDirectories.includes(part))
34
+ );
35
+ }