@forgewp/compiler 0.4.3 → 0.4.4
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.
- package/README.md +59 -0
- package/bin/add.js +20 -14
- package/bin/cli.js +145 -70
- package/bin/doctor.js +21 -12
- package/bin/export.js +69 -20
- package/bin/fresh.js +5 -0
- package/bin/i18n-extract.js +3 -10
- package/bin/i18n-translate.js +15 -24
- package/bin/make-block.js +13 -0
- package/bin/make-editable.js +166 -0
- package/bin/make-form.js +131 -0
- package/bin/make-loop.js +40 -43
- package/bin/make-mail.js +27 -0
- package/bin/make-post-type.js +57 -29
- package/bin/make-sandbox.js +62 -11
- package/bin/make-section.js +221 -0
- package/bin/make-shell.js +375 -0
- package/bin/repair.js +14 -26
- package/bin/seed.js +119 -0
- package/bin/sync-routes.js +107 -85
- package/lib/adapters/html-adapter.js +5 -0
- package/lib/adapters/stub-adapter.js +4 -0
- package/lib/add-component.js +153 -92
- package/lib/asset-loader.js +313 -3
- package/lib/assets/asset-graph.js +432 -0
- package/lib/assets/asset-pipeline.js +125 -0
- package/lib/assets/diagnostics-report.js +345 -0
- package/lib/assets/external-fetcher.js +173 -0
- package/lib/assets/font-orchestrator.js +348 -0
- package/lib/assets/font-usage-scanner.js +242 -0
- package/lib/assets/image-optimizer.js +229 -0
- package/lib/assets/index.js +51 -0
- package/lib/assets/preload-generator.js +70 -0
- package/lib/assets/public-url.js +153 -0
- package/lib/block-authoring-help.js +11 -0
- package/lib/blocks/ast-parser.js +67 -0
- package/lib/blocks/discover-blocks.js +146 -0
- package/lib/blocks/editor-scope.js +733 -0
- package/lib/blocks/editor-transpiler.js +1229 -0
- package/lib/blocks/index.js +2838 -0
- package/lib/blocks/php-transpiler.js +4928 -0
- package/lib/blocks/scanner.js +1487 -0
- package/lib/blocks/shared-utils.js +290 -0
- package/lib/blocks/source-sanitize.js +1449 -0
- package/lib/blueprints.js +31 -91
- package/lib/build-assets.js +38 -33
- package/lib/collect-page-template-routes.js +102 -0
- package/lib/collect-static-routes.js +175 -0
- package/lib/diagnostics.js +58 -33
- package/lib/export-theme.js +84 -5
- package/lib/extract-template-content.js +90 -0
- package/lib/forms-preview.js +42 -0
- package/lib/functions/cpt-routing.js +69 -0
- package/lib/functions/forms.js +2461 -0
- package/lib/functions/hydration-enqueuer.js +400 -0
- package/lib/functions/index.js +1623 -0
- package/lib/functions/load-menus.js +112 -0
- package/lib/functions/load-translations.js +148 -0
- package/lib/functions/rest-endpoints.js +2923 -0
- package/lib/functions/routing.js +765 -0
- package/lib/functions/seed-mock-data.js +392 -0
- package/lib/functions/seed-products.js +481 -0
- package/lib/functions/settings-page.js +273 -0
- package/lib/functions/theme-setup.js +494 -0
- package/lib/generate-theme.js +1916 -339
- package/lib/hydration/editable-schemas.js +332 -0
- package/lib/hydration/enter-hidden-initial.js +65 -0
- package/lib/hydration/form-schemas.js +227 -0
- package/lib/hydration/index.js +12 -0
- package/lib/hydration/is-interactive.js +951 -0
- package/lib/hydration/island-split.js +1231 -0
- package/lib/hydration/islands-scanner.js +599 -0
- package/lib/hydration/resolve-island-chunk.js +103 -0
- package/lib/hydration/resolve-island-component.js +52 -0
- package/lib/hydration/rollup-inputs.js +81 -0
- package/lib/hydration/root-class-extractor.js +220 -0
- package/lib/hydration/scan-app-providers.js +154 -0
- package/lib/hydration/static-lint.js +382 -0
- package/lib/hydration/transform.js +257 -0
- package/lib/hydration/uses-block-area.js +49 -0
- package/lib/hydration/utils.js +119 -0
- package/lib/i18n/translation-cache.js +141 -0
- package/lib/icon-registry.js +72 -0
- package/lib/index.d.ts +287 -8
- package/lib/index.js +2 -1
- package/lib/load-config.js +338 -242
- package/lib/mail-templates.js +37 -0
- package/lib/markup-processor.js +388 -102
- package/lib/node-runtime.js +7 -0
- package/lib/page-config-plugin.js +64 -20
- package/lib/php-builders.js +383 -44
- package/lib/react-adapter.js +983 -235
- package/lib/registration-metadata.js +11 -0
- package/lib/render-theme.mts +1410 -616
- package/lib/route-layout.js +76 -0
- package/lib/template-discovery.js +58 -0
- package/lib/theme-compatibility.js +32 -0
- package/lib/validate-export.js +2 -2
- package/lib/validate.js +27 -5
- package/lib/virtual-plugin.js +319 -0
- package/lib/walkthrough.md +26 -0
- package/package.json +16 -5
- package/templates/auth/cms/email-logs.json +9 -0
- package/templates/auth/cms/menus.json +11 -0
- package/templates/auth/cms/mock-data.json +123 -0
- package/templates/auth/cms/roles.json +7 -0
- package/templates/auth/cms/sessions.json +1 -0
- package/templates/auth/cms/site-settings.json +28 -0
- package/templates/auth/cms/translations.json +4 -0
- package/templates/auth/cms/users.json +46 -0
- package/templates/auth/components/forgot-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/login-form-wrapper.tsx +118 -21
- package/templates/auth/components/reset-password-form-wrapper.tsx +10 -2
- package/templates/auth/components/signup-form-wrapper.tsx +2 -2
- package/templates/auth/components/verify-email-view-wrapper.tsx +5 -5
- package/templates/auth/pages/DashboardPage.tsx +1 -4
- package/templates/auth/pages/ForgotPasswordPage.tsx +1 -4
- package/templates/auth/pages/LoginPage.tsx +1 -4
- package/templates/auth/pages/ResetPasswordPage.tsx +1 -4
- package/templates/auth/pages/SignUpPage.tsx +1 -4
- package/templates/auth/pages/VerifyEmailPage.tsx +1 -4
- package/templates/auth/ui/button.tsx +57 -0
- package/templates/auth/ui/card.tsx +76 -0
- package/templates/auth/ui/field.tsx +242 -0
- package/templates/auth/ui/input.tsx +22 -0
- package/templates/auth/ui/label.tsx +24 -0
- package/templates/auth/ui/separator.tsx +31 -0
- package/templates/menus.json +12 -10
- package/templates/users.json +8 -4
- package/templates/wordpress.tsx +690 -245
- package/lib/block-compiler.js +0 -552
- package/lib/functions-builder.js +0 -2970
- package/lib/hydration-scanner.js +0 -833
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ts } from "./is-interactive.js";
|
|
4
|
+
import { scanForHydrationIslandsWithProps } from "./islands-scanner.js";
|
|
5
|
+
import { loadMockData } from "../functions/seed-mock-data.js";
|
|
6
|
+
import { loadProductsData } from "../functions/seed-products.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Statically analyzes theme templates to detect missing metadata,
|
|
10
|
+
* dynamic event handlers in static WpQueryLoop scopes, and hardcoded relative URLs.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} themeRoot
|
|
13
|
+
* @returns {Array<{ type: 'metadata' | 'loop' | 'url', file: string, message: string, severity: 'warning' | 'error', lineContent?: string }>}
|
|
14
|
+
*/
|
|
15
|
+
export function runStaticLintChecks(themeRoot) {
|
|
16
|
+
const violations = [];
|
|
17
|
+
const filesToScan = [];
|
|
18
|
+
|
|
19
|
+
// Load CPTs, Taxonomies, and Custom Fields from wp.config.ts and cms/mock-data.json
|
|
20
|
+
const validPostTypes = new Set(["post", "page"]);
|
|
21
|
+
const postTypeTaxonomies = {
|
|
22
|
+
"post": new Set(["category", "post_tag"]),
|
|
23
|
+
"page": new Set()
|
|
24
|
+
};
|
|
25
|
+
const postTypeCustomFields = {
|
|
26
|
+
"post": new Set(),
|
|
27
|
+
"page": new Set()
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// 1. Scan CPTs from wp.config.ts
|
|
31
|
+
const configPath = path.join(themeRoot, "wp.config.ts");
|
|
32
|
+
if (existsSync(configPath)) {
|
|
33
|
+
try {
|
|
34
|
+
const configContent = readFileSync(configPath, "utf8");
|
|
35
|
+
const postTypesMatch = configContent.match(/postTypes\s*:\s*\{([\s\S]*?)\}/);
|
|
36
|
+
if (postTypesMatch) {
|
|
37
|
+
const block = postTypesMatch[1];
|
|
38
|
+
const keyRegex = /\b([a-zA-Z0-9_-]+)\s*:/g;
|
|
39
|
+
let m;
|
|
40
|
+
while ((m = keyRegex.exec(block)) !== null) {
|
|
41
|
+
const pt = m[1];
|
|
42
|
+
if (pt !== 'labels' && pt !== 'singular' && pt !== 'plural' && pt !== 'translatable') {
|
|
43
|
+
validPostTypes.add(pt);
|
|
44
|
+
if (!postTypeTaxonomies[pt]) {
|
|
45
|
+
postTypeTaxonomies[pt] = new Set(["category", "post_tag"]);
|
|
46
|
+
}
|
|
47
|
+
if (!postTypeCustomFields[pt]) {
|
|
48
|
+
postTypeCustomFields[pt] = new Set();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 2. Scan CPTs, taxonomies, and custom fields from cms/mock-data.(ts|json)
|
|
57
|
+
// and cms/products.(ts|json) via the shared dual-resolution loaders.
|
|
58
|
+
const ingestCmsMap = (mockData) => {
|
|
59
|
+
if (!mockData || typeof mockData !== 'object') return;
|
|
60
|
+
for (const pt of Object.keys(mockData)) {
|
|
61
|
+
if (
|
|
62
|
+
pt !== 'posts' &&
|
|
63
|
+
pt !== 'pages' &&
|
|
64
|
+
pt !== 'post' &&
|
|
65
|
+
pt !== 'page' &&
|
|
66
|
+
pt !== 'menus' &&
|
|
67
|
+
pt !== 'attachment' &&
|
|
68
|
+
!pt.startsWith('_')
|
|
69
|
+
) {
|
|
70
|
+
validPostTypes.add(pt);
|
|
71
|
+
if (!postTypeTaxonomies[pt]) {
|
|
72
|
+
postTypeTaxonomies[pt] = new Set(["category", "post_tag"]);
|
|
73
|
+
}
|
|
74
|
+
if (!postTypeCustomFields[pt]) {
|
|
75
|
+
postTypeCustomFields[pt] = new Set();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const items = mockData[pt];
|
|
79
|
+
if (Array.isArray(items)) {
|
|
80
|
+
for (const item of items) {
|
|
81
|
+
if (item._terms && typeof item._terms === 'object') {
|
|
82
|
+
for (const tax of Object.keys(item._terms)) {
|
|
83
|
+
postTypeTaxonomies[pt].add(tax);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (item.customFields && typeof item.customFields === 'object') {
|
|
87
|
+
for (const key of Object.keys(item.customFields)) {
|
|
88
|
+
postTypeCustomFields[pt].add(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} else if (pt === 'post' || pt === 'page') {
|
|
94
|
+
const items = mockData[pt];
|
|
95
|
+
if (Array.isArray(items)) {
|
|
96
|
+
for (const item of items) {
|
|
97
|
+
if (item._terms && typeof item._terms === 'object') {
|
|
98
|
+
for (const tax of Object.keys(item._terms)) {
|
|
99
|
+
postTypeTaxonomies[pt].add(tax);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (item.customFields && typeof item.customFields === 'object') {
|
|
103
|
+
for (const key of Object.keys(item.customFields)) {
|
|
104
|
+
postTypeCustomFields[pt].add(key);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
ingestCmsMap(loadMockData(themeRoot));
|
|
115
|
+
const products = loadProductsData(themeRoot);
|
|
116
|
+
if (Array.isArray(products) && products.length > 0) {
|
|
117
|
+
ingestCmsMap({ product: products });
|
|
118
|
+
} else if (products && typeof products === 'object' && !Array.isArray(products)) {
|
|
119
|
+
ingestCmsMap(products);
|
|
120
|
+
}
|
|
121
|
+
} catch {}
|
|
122
|
+
|
|
123
|
+
const validSlugs = new Set(["page"]);
|
|
124
|
+
const pagesDir = path.join(themeRoot, "src", "app", "pages");
|
|
125
|
+
if (existsSync(pagesDir)) {
|
|
126
|
+
try {
|
|
127
|
+
const pageFiles = readdirSync(pagesDir).filter(f => f.endsWith(".tsx"));
|
|
128
|
+
for (const pf of pageFiles) {
|
|
129
|
+
const pageName = pf.replace(".tsx", "");
|
|
130
|
+
if (pageName.startsWith("Single")) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const slug = pageName.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
134
|
+
validSlugs.add(slug);
|
|
135
|
+
}
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const dirsToScan = [
|
|
140
|
+
path.join(themeRoot, "src", "app"),
|
|
141
|
+
path.join(themeRoot, "src", "components"),
|
|
142
|
+
path.join(themeRoot, "src", "blocks"),
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
function collectFilesRecursive(dir) {
|
|
146
|
+
if (!existsSync(dir)) return;
|
|
147
|
+
try {
|
|
148
|
+
const items = readdirSync(dir, { withFileTypes: true });
|
|
149
|
+
for (const item of items) {
|
|
150
|
+
const fullPath = path.join(dir, item.name);
|
|
151
|
+
if (item.isDirectory()) {
|
|
152
|
+
collectFilesRecursive(fullPath);
|
|
153
|
+
} else if (item.isFile() && (item.name.endsWith(".tsx") || item.name.endsWith(".ts"))) {
|
|
154
|
+
filesToScan.push(fullPath);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
} catch {}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
for (const dir of dirsToScan) {
|
|
161
|
+
collectFilesRecursive(dir);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
for (const filePath of filesToScan) {
|
|
165
|
+
const relFile = path.relative(themeRoot, filePath).replace(/\\/g, "/");
|
|
166
|
+
try {
|
|
167
|
+
const content = readFileSync(filePath, "utf8");
|
|
168
|
+
const lines = content.split("\n");
|
|
169
|
+
|
|
170
|
+
// 1. Metadata Checks (Only for page files)
|
|
171
|
+
const isPage = path.basename(filePath).startsWith("page.tsx") || filePath.includes("pages/");
|
|
172
|
+
if (isPage) {
|
|
173
|
+
const hasWpHead = content.includes("<WpHead") || content.includes("<WpHead\b");
|
|
174
|
+
if (!hasWpHead) {
|
|
175
|
+
violations.push({
|
|
176
|
+
type: "metadata",
|
|
177
|
+
file: relFile,
|
|
178
|
+
message: "Missing SEO <WpHead> component on this major page template.",
|
|
179
|
+
severity: "warning"
|
|
180
|
+
});
|
|
181
|
+
} else {
|
|
182
|
+
const wpHeadRegex = /<WpHead\b([\s\S]*?)\/?>/g;
|
|
183
|
+
let headMatch;
|
|
184
|
+
while ((headMatch = wpHeadRegex.exec(content)) !== null) {
|
|
185
|
+
const headProps = headMatch[1];
|
|
186
|
+
const hasTitle = /title\s*=\s*(?:["']|\{)/.test(headProps);
|
|
187
|
+
const hasDescription = /description\s*=\s*(?:["']|\{)/.test(headProps);
|
|
188
|
+
|
|
189
|
+
if (!hasTitle) {
|
|
190
|
+
violations.push({
|
|
191
|
+
type: "metadata",
|
|
192
|
+
file: relFile,
|
|
193
|
+
message: "SEO <WpHead> is missing the required 'title' attribute.",
|
|
194
|
+
severity: "warning"
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (!hasDescription) {
|
|
198
|
+
violations.push({
|
|
199
|
+
type: "metadata",
|
|
200
|
+
file: relFile,
|
|
201
|
+
message: "SEO <WpHead> is missing the required 'description' attribute.",
|
|
202
|
+
severity: "warning"
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 2. WpQueryLoop without Hydration check
|
|
210
|
+
const hydrateBlocks = [];
|
|
211
|
+
const hydrateRegex = /<Hydrate\b([\s\S]*?)>([\s\S]*?)<\/Hydrate>/g;
|
|
212
|
+
let hydMatch;
|
|
213
|
+
while ((hydMatch = hydrateRegex.exec(content)) !== null) {
|
|
214
|
+
hydrateBlocks.push({
|
|
215
|
+
start: hydMatch.index,
|
|
216
|
+
end: hydMatch.index + hydMatch[0].length
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const loopRegex = /<WpQueryLoop\b([\s\S]*?)>([\s\S]*?)<\/WpQueryLoop>/g;
|
|
221
|
+
let loopMatch;
|
|
222
|
+
while ((loopMatch = loopRegex.exec(content)) !== null) {
|
|
223
|
+
const loopBody = loopMatch[2];
|
|
224
|
+
const loopStart = loopMatch.index;
|
|
225
|
+
|
|
226
|
+
const isHydrated = hydrateBlocks.some(hb => loopStart >= hb.start && loopStart <= hb.end);
|
|
227
|
+
|
|
228
|
+
if (!isHydrated) {
|
|
229
|
+
const hasEventHandlers = /\bon[A-Z][a-zA-Z]*\s*=\s*\{/.test(loopBody);
|
|
230
|
+
if (hasEventHandlers) {
|
|
231
|
+
const eventMatch = loopBody.match(/\b(on[A-Z][a-zA-Z]*)\s*=\s*\{/);
|
|
232
|
+
const eventName = eventMatch ? eventMatch[1] : "event handler";
|
|
233
|
+
violations.push({
|
|
234
|
+
type: "loop",
|
|
235
|
+
file: relFile,
|
|
236
|
+
message: `WpQueryLoop contains active interactive handler (${eventName}) but is not wrapped inside a <Hydrate> boundary. It will render static and non-interactive in production WordPress.`,
|
|
237
|
+
severity: "error"
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// 3. Hardcoded relative internal links check
|
|
244
|
+
for (let i = 0; i < lines.length; i++) {
|
|
245
|
+
const line = lines[i];
|
|
246
|
+
const hrefRegex = /href\s*=\s*["']([^"']*)["']/g;
|
|
247
|
+
let hrefMatch;
|
|
248
|
+
while ((hrefMatch = hrefRegex.exec(line)) !== null) {
|
|
249
|
+
const val = hrefMatch[1];
|
|
250
|
+
const isRelative = val.startsWith("/") &&
|
|
251
|
+
!val.startsWith("//") &&
|
|
252
|
+
!val.startsWith("/wp-") &&
|
|
253
|
+
!val.startsWith("/feed");
|
|
254
|
+
if (isRelative) {
|
|
255
|
+
violations.push({
|
|
256
|
+
type: "url",
|
|
257
|
+
file: relFile,
|
|
258
|
+
message: `Hardcoded relative URL "${val}" found. Use useWpPermalink() to resolve dynamically from WordPress.`,
|
|
259
|
+
severity: "warning",
|
|
260
|
+
lineContent: line.trim() + ` (line ${i + 1})`
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const jsxHrefRegex = /href\s*=\s*\{\s*["']([^"']*)["']\s*\}/g;
|
|
266
|
+
let jsxHrefMatch;
|
|
267
|
+
while ((jsxHrefMatch = jsxHrefRegex.exec(line)) !== null) {
|
|
268
|
+
const val = jsxHrefMatch[1];
|
|
269
|
+
const isRelative = val.startsWith("/") &&
|
|
270
|
+
!val.startsWith("//") &&
|
|
271
|
+
!val.startsWith("/wp-") &&
|
|
272
|
+
!val.startsWith("/feed");
|
|
273
|
+
if (isRelative) {
|
|
274
|
+
violations.push({
|
|
275
|
+
type: "url",
|
|
276
|
+
file: relFile,
|
|
277
|
+
message: `Hardcoded relative URL "${val}" found. Use useWpPermalink() to resolve dynamically from WordPress.`,
|
|
278
|
+
severity: "warning",
|
|
279
|
+
lineContent: line.trim() + ` (line ${i + 1})`
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 4. useWpPageLink template slug typo check
|
|
286
|
+
const pageLinkRegex = /useWpPageLink\s*\(\s*["']([^"']+)["']/g;
|
|
287
|
+
let pageLinkMatch;
|
|
288
|
+
while ((pageLinkMatch = pageLinkRegex.exec(content)) !== null) {
|
|
289
|
+
const slugUsed = pageLinkMatch[1];
|
|
290
|
+
if (!validSlugs.has(slugUsed)) {
|
|
291
|
+
violations.push({
|
|
292
|
+
type: "pagelink",
|
|
293
|
+
file: relFile,
|
|
294
|
+
message: `useWpPageLink references non-existent page template slug "${slugUsed}". Valid slugs: ${Array.from(validSlugs).join(", ")}`,
|
|
295
|
+
severity: "warning"
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 5. useWpQuery arguments audit
|
|
301
|
+
const useWpQueryRegex = /useWpQuery\s*\(\s*(\{[\s\S]*?\})\s*\)/g;
|
|
302
|
+
let queryMatch;
|
|
303
|
+
while ((queryMatch = useWpQueryRegex.exec(content)) !== null) {
|
|
304
|
+
const argsStr = queryMatch[1];
|
|
305
|
+
|
|
306
|
+
const postTypeMatch = argsStr.match(/postType\s*:\s*['"]([^'"]+)['"]/);
|
|
307
|
+
const postType = postTypeMatch ? postTypeMatch[1] : 'post';
|
|
308
|
+
|
|
309
|
+
if (!validPostTypes.has(postType)) {
|
|
310
|
+
violations.push({
|
|
311
|
+
type: "query",
|
|
312
|
+
file: relFile,
|
|
313
|
+
message: `useWpQuery references unregistered postType "${postType}".`,
|
|
314
|
+
severity: "warning"
|
|
315
|
+
});
|
|
316
|
+
} else {
|
|
317
|
+
// Validate Taxonomies inside taxQuery
|
|
318
|
+
const taxRegex = /\btaxonomy\s*:\s*['"]([^'"]+)['"]/g;
|
|
319
|
+
let taxMatch;
|
|
320
|
+
const allowedTaxonomies = postTypeTaxonomies[postType] || new Set();
|
|
321
|
+
while ((taxMatch = taxRegex.exec(argsStr)) !== null) {
|
|
322
|
+
const taxSlug = taxMatch[1];
|
|
323
|
+
if (!allowedTaxonomies.has(taxSlug)) {
|
|
324
|
+
violations.push({
|
|
325
|
+
type: "query",
|
|
326
|
+
file: relFile,
|
|
327
|
+
message: `useWpQuery in ${path.basename(filePath)} queries taxonomy "${taxSlug}", which is not registered for postType "${postType}" in wp.config.ts.`,
|
|
328
|
+
severity: "warning"
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Validate Custom Fields inside metaQuery
|
|
334
|
+
const keyRegex = /\bkey\s*:\s*['"]([^'"]+)['"]/g;
|
|
335
|
+
let keyMatch;
|
|
336
|
+
const allowedFields = postTypeCustomFields[postType] || new Set();
|
|
337
|
+
while ((keyMatch = keyRegex.exec(argsStr)) !== null) {
|
|
338
|
+
const fieldKey = keyMatch[1];
|
|
339
|
+
if (!allowedFields.has(fieldKey)) {
|
|
340
|
+
violations.push({
|
|
341
|
+
type: "query",
|
|
342
|
+
file: relFile,
|
|
343
|
+
message: `useWpQuery in ${path.basename(filePath)} queries custom field "${fieldKey}", which is not defined for postType "${postType}" in wp.config.ts.`,
|
|
344
|
+
severity: "warning"
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
} catch (err) {
|
|
352
|
+
console.warn(`[Static Lint] Failed to scan file ${filePath}:`, err.message);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// 6. Auto-hydration layout-class safety check
|
|
357
|
+
//
|
|
358
|
+
// Smart Discovery auto-wraps interactive components in an unstyled <Hydrate> boundary
|
|
359
|
+
// div. The compiler tries to hoist that component's own root className onto the wrapper
|
|
360
|
+
// so layout-critical classes (w-full, flex, grid, sticky, ...) survive, but it can only
|
|
361
|
+
// do that when the className is statically provable. When it isn't (a dynamic template,
|
|
362
|
+
// a cn()/clsx() call, a variable), the wrapper falls back to a bare `display:block` box —
|
|
363
|
+
// which can silently break width/flex sizing exactly like ForgeWP's own docs warn about
|
|
364
|
+
// for manual <Hydrate> usage. Flag those cases so a dev can wrap explicitly instead.
|
|
365
|
+
try {
|
|
366
|
+
const islands = scanForHydrationIslandsWithProps(themeRoot);
|
|
367
|
+
for (const island of islands) {
|
|
368
|
+
if (island.smartDiscovered && island.rootClassNameUnresolved) {
|
|
369
|
+
violations.push({
|
|
370
|
+
type: "hydration-layout",
|
|
371
|
+
file: island.file,
|
|
372
|
+
message: `Component behind auto-detected hydration island "${island.name}" has a dynamic/non-static className on its root element. ForgeWP could not automatically copy it onto the generated <Hydrate> wrapper, so layout classes (width, flex, grid, etc.) on that root element may be silently lost in the compiled output. Wrap it explicitly instead: <Hydrate className="...">.`,
|
|
373
|
+
severity: "warning",
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
} catch (err) {
|
|
378
|
+
console.warn(`[Static Lint] Failed to run hydration layout-class check:`, err.message);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return violations;
|
|
382
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ts, isComponentInteractive, isExportInteractive } from "./is-interactive.js";
|
|
4
|
+
import { resolveImportPath, doesComponentUseAuthHooks } from "./utils.js";
|
|
5
|
+
import { getComponentRootClassName } from "./root-class-extractor.js";
|
|
6
|
+
import { scanAppProviders, fileImportsProvider } from "./scan-app-providers.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* True when wrapping this JSX usage as a bare <Hydrate> island would drop
|
|
10
|
+
* children the client cannot JSON-serialize (elements, expressions, text).
|
|
11
|
+
* Those usages stay as layout containers; we recurse to find leaf islands.
|
|
12
|
+
*/
|
|
13
|
+
function hasNonSerializableChildren(node) {
|
|
14
|
+
if (!node.children || node.children.length === 0) return false;
|
|
15
|
+
return node.children.some((c) => {
|
|
16
|
+
if (ts.isJsxElement(c) || ts.isJsxSelfClosingElement(c) || ts.isJsxFragment(c)) return true;
|
|
17
|
+
if (ts.isJsxExpression(c) && c.expression) return true;
|
|
18
|
+
if (ts.isJsxText(c) && c.getText().trim().length > 0) return true;
|
|
19
|
+
return false;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const BUILTIN_OR_PRIMITIVE_TAGS = new Set([
|
|
24
|
+
"Hydrate",
|
|
25
|
+
"WpHead",
|
|
26
|
+
"WpAuthGate",
|
|
27
|
+
"WpCapabilityGate",
|
|
28
|
+
"WpAuthProvider",
|
|
29
|
+
"Link",
|
|
30
|
+
"Switch",
|
|
31
|
+
"Route",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Transforms theme page/component source code to automatically inject page protect hooks
|
|
36
|
+
* and wrap interactive components in <Hydrate> / <WpAuthProvider> islands.
|
|
37
|
+
*/
|
|
38
|
+
export function transformThemeFile(code, id, themeRoot) {
|
|
39
|
+
const normalizedPath = id.replace(/\\/g, '/');
|
|
40
|
+
|
|
41
|
+
// 1. Process pageConfig hook injection if protected: true
|
|
42
|
+
let transformedCode = code;
|
|
43
|
+
let hasPageConfigTransform = false;
|
|
44
|
+
|
|
45
|
+
const hasProtectedConfig = /export\s+const\s+pageConfig\s*=\s*(?:definePageConfig\s*\(\s*)?\{[\s\S]*?protected\s*:\s*true/g.test(code);
|
|
46
|
+
|
|
47
|
+
if (normalizedPath.includes('/src/app/pages/') && hasProtectedConfig) {
|
|
48
|
+
// Prepend hook import
|
|
49
|
+
transformedCode = `import { useWpPageProtect as _useWpPageProtect } from '@forgewp/auth';\n` + transformedCode;
|
|
50
|
+
|
|
51
|
+
// Match functional components
|
|
52
|
+
const funcRegex = /export\s+(default\s+)?function\s+([a-zA-Z0-9_$]+)\s*\(([\s\S]*?)\)\s*\{/g;
|
|
53
|
+
if (funcRegex.test(transformedCode)) {
|
|
54
|
+
transformedCode = transformedCode.replace(funcRegex, `export $1function $2($3) {\n _useWpPageProtect(pageConfig);`);
|
|
55
|
+
hasPageConfigTransform = true;
|
|
56
|
+
} else {
|
|
57
|
+
// Match arrow function component exports
|
|
58
|
+
const arrowRegex = /export\s+(const|let|var)\s+([a-zA-Z0-9_$]+)(\s*:\s*[a-zA-Z0-9_$.<>]+)?\s*=\s*\(([\s\S]*?)\)\s*=>\s*\{/g;
|
|
59
|
+
if (arrowRegex.test(transformedCode)) {
|
|
60
|
+
transformedCode = transformedCode.replace(arrowRegex, `export $1 $2$3 = ($4) => {\n _useWpPageProtect(pageConfig);`);
|
|
61
|
+
hasPageConfigTransform = true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const layoutProviders = themeRoot ? scanAppProviders(themeRoot) : [];
|
|
67
|
+
|
|
68
|
+
// 2. Process automatic island detection and provider wrapping
|
|
69
|
+
if (!ts || !normalizedPath.endsWith('.tsx') || normalizedPath.includes('node_modules')) {
|
|
70
|
+
return transformedCode;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// If the file itself is interactive, it acts as an island root, so DO NOT wrap inner components in <Hydrate>
|
|
74
|
+
if (isComponentInteractive(id)) {
|
|
75
|
+
return transformedCode;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const sourceFile = ts.createSourceFile(
|
|
80
|
+
id,
|
|
81
|
+
transformedCode,
|
|
82
|
+
ts.ScriptTarget.Latest,
|
|
83
|
+
true,
|
|
84
|
+
ts.ScriptKind.TSX
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// Map tag name to import path
|
|
88
|
+
const imports = {};
|
|
89
|
+
function collectImports(node) {
|
|
90
|
+
if (node.kind === ts.SyntaxKind.ImportDeclaration) {
|
|
91
|
+
const specifier = node.moduleSpecifier;
|
|
92
|
+
if (specifier.kind === ts.SyntaxKind.StringLiteral) {
|
|
93
|
+
const importPath = specifier.text;
|
|
94
|
+
const clause = node.importClause;
|
|
95
|
+
if (clause) {
|
|
96
|
+
// default import
|
|
97
|
+
if (clause.name) {
|
|
98
|
+
imports[clause.name.text] = importPath;
|
|
99
|
+
}
|
|
100
|
+
// named imports
|
|
101
|
+
if (clause.namedBindings && clause.namedBindings.kind === ts.SyntaxKind.NamedImports) {
|
|
102
|
+
clause.namedBindings.elements.forEach(el => {
|
|
103
|
+
imports[el.name.text] = importPath;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
ts.forEachChild(node, collectImports);
|
|
110
|
+
}
|
|
111
|
+
collectImports(sourceFile);
|
|
112
|
+
|
|
113
|
+
const replacements = [];
|
|
114
|
+
function isWithinExistingReplacement(start, end) {
|
|
115
|
+
return replacements.some(r => start >= r.start && end <= r.end);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function findJsxElements(node) {
|
|
119
|
+
// 1. Full JSX element with children <Foo>...</Foo>
|
|
120
|
+
if (node.kind === ts.SyntaxKind.JsxElement) {
|
|
121
|
+
const opening = node.openingElement;
|
|
122
|
+
const tagNameNode = opening.tagName;
|
|
123
|
+
if (tagNameNode.kind === ts.SyntaxKind.Identifier) {
|
|
124
|
+
const tagName = tagNameNode.text;
|
|
125
|
+
const firstChar = tagName.charAt(0);
|
|
126
|
+
const isComponent = firstChar === firstChar.toUpperCase() && firstChar !== firstChar.toLowerCase();
|
|
127
|
+
const isBuiltin = BUILTIN_OR_PRIMITIVE_TAGS.has(tagName) || /Provider$/.test(tagName);
|
|
128
|
+
|
|
129
|
+
if (isComponent && !isBuiltin && imports[tagName]) {
|
|
130
|
+
const importPath = imports[tagName];
|
|
131
|
+
if (importPath.startsWith('.') || importPath.startsWith('@/')) {
|
|
132
|
+
const resolvedPath = resolveImportPath(path.dirname(id), importPath, themeRoot);
|
|
133
|
+
const interactive = resolvedPath ? isExportInteractive(resolvedPath, tagName) : false;
|
|
134
|
+
if (resolvedPath && interactive) {
|
|
135
|
+
const start = node.getStart(sourceFile);
|
|
136
|
+
const end = node.getEnd();
|
|
137
|
+
if (!isWithinExistingReplacement(start, end)) {
|
|
138
|
+
const rawElementText = transformedCode.substring(start, end);
|
|
139
|
+
const kebabName = tagName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
140
|
+
const needsAuth = doesComponentUseAuthHooks(resolvedPath);
|
|
141
|
+
const rootInfo = getComponentRootClassName(resolvedPath);
|
|
142
|
+
replacements.push({
|
|
143
|
+
start,
|
|
144
|
+
end,
|
|
145
|
+
tagName,
|
|
146
|
+
kebabName,
|
|
147
|
+
needsAuth,
|
|
148
|
+
rawText: rawElementText,
|
|
149
|
+
rootClassName: rootInfo.resolvable ? rootInfo.className : null,
|
|
150
|
+
slotChildren: node.kind === ts.SyntaxKind.JsxElement,
|
|
151
|
+
needsProviders: fileImportsProvider(resolvedPath, layoutProviders),
|
|
152
|
+
});
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 2. Self-closing element <Foo />
|
|
162
|
+
if (node.kind === ts.SyntaxKind.JsxSelfClosingElement) {
|
|
163
|
+
const tagNameNode = node.tagName;
|
|
164
|
+
if (tagNameNode.kind === ts.SyntaxKind.Identifier) {
|
|
165
|
+
const tagName = tagNameNode.text;
|
|
166
|
+
const firstChar = tagName.charAt(0);
|
|
167
|
+
const isComponent = firstChar === firstChar.toUpperCase() && firstChar !== firstChar.toLowerCase();
|
|
168
|
+
const isBuiltin = BUILTIN_OR_PRIMITIVE_TAGS.has(tagName) || /Provider$/.test(tagName);
|
|
169
|
+
|
|
170
|
+
if (isComponent && !isBuiltin && imports[tagName]) {
|
|
171
|
+
const importPath = imports[tagName];
|
|
172
|
+
if (importPath.startsWith('.') || importPath.startsWith('@/')) {
|
|
173
|
+
const resolvedPath = resolveImportPath(path.dirname(id), importPath, themeRoot);
|
|
174
|
+
const interactive = resolvedPath ? isExportInteractive(resolvedPath, tagName) : false;
|
|
175
|
+
if (resolvedPath && interactive) {
|
|
176
|
+
const start = node.getStart(sourceFile);
|
|
177
|
+
const end = node.getEnd();
|
|
178
|
+
if (!isWithinExistingReplacement(start, end)) {
|
|
179
|
+
const rawElementText = transformedCode.substring(start, end);
|
|
180
|
+
const kebabName = tagName.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
181
|
+
const needsAuth = doesComponentUseAuthHooks(resolvedPath);
|
|
182
|
+
const rootInfo = getComponentRootClassName(resolvedPath);
|
|
183
|
+
replacements.push({
|
|
184
|
+
start,
|
|
185
|
+
end,
|
|
186
|
+
tagName,
|
|
187
|
+
kebabName,
|
|
188
|
+
needsAuth,
|
|
189
|
+
rawText: rawElementText,
|
|
190
|
+
rootClassName: rootInfo.resolvable ? rootInfo.className : null,
|
|
191
|
+
slotChildren: node.kind === ts.SyntaxKind.JsxElement,
|
|
192
|
+
needsProviders: fileImportsProvider(resolvedPath, layoutProviders),
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
ts.forEachChild(node, findJsxElements);
|
|
203
|
+
}
|
|
204
|
+
findJsxElements(sourceFile);
|
|
205
|
+
|
|
206
|
+
if (replacements.length === 0) {
|
|
207
|
+
return transformedCode;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Apply replacements in descending order (end to start of file) to avoid shifting positions
|
|
211
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
212
|
+
|
|
213
|
+
let needsHydrateImport = false;
|
|
214
|
+
let needsAuthImport = false;
|
|
215
|
+
|
|
216
|
+
for (const rep of replacements) {
|
|
217
|
+
const classAttr = rep.rootClassName
|
|
218
|
+
? ` className={${JSON.stringify(rep.rootClassName)}}`
|
|
219
|
+
: "";
|
|
220
|
+
const slotAttr = rep.slotChildren ? " slotChildren" : "";
|
|
221
|
+
const providersAttr = rep.needsProviders ? " needsProviders" : "";
|
|
222
|
+
let wrapped = `<Hydrate id="${rep.kebabName}"${classAttr}${slotAttr}${providersAttr}>`;
|
|
223
|
+
needsHydrateImport = true;
|
|
224
|
+
|
|
225
|
+
if (rep.needsAuth) {
|
|
226
|
+
wrapped += `<WpAuthProvider>${rep.rawText}</WpAuthProvider>`;
|
|
227
|
+
needsAuthImport = true;
|
|
228
|
+
} else {
|
|
229
|
+
wrapped += rep.rawText;
|
|
230
|
+
}
|
|
231
|
+
wrapped += `</Hydrate>`;
|
|
232
|
+
|
|
233
|
+
transformedCode = transformedCode.substring(0, rep.start) + wrapped + transformedCode.substring(rep.end);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Prepend imports if necessary
|
|
237
|
+
const hasHydrateImport = transformedCode.includes('import { Hydrate') || transformedCode.includes('import Hydrate');
|
|
238
|
+
const hasAuthImport = transformedCode.includes('import { WpAuthProvider') || transformedCode.includes('import WpAuthProvider');
|
|
239
|
+
|
|
240
|
+
let prepends = "";
|
|
241
|
+
if (needsHydrateImport && !hasHydrateImport) {
|
|
242
|
+
prepends += `import { Hydrate } from "@forgewp/react";\n`;
|
|
243
|
+
}
|
|
244
|
+
if (needsAuthImport && !hasAuthImport) {
|
|
245
|
+
prepends += `import { WpAuthProvider } from "@forgewp/auth";\n`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (prepends) {
|
|
249
|
+
transformedCode = prepends + transformedCode;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return transformedCode;
|
|
253
|
+
} catch (err) {
|
|
254
|
+
console.error(`[JSX Wrap Plugin] Error parsing ${id}:`, err);
|
|
255
|
+
return transformedCode;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
|
|
3
|
+
export function usesBlockArea(source) {
|
|
4
|
+
let ast;
|
|
5
|
+
try {
|
|
6
|
+
ast = parse(source, {
|
|
7
|
+
sourceType: 'unambiguous',
|
|
8
|
+
plugins: ['jsx', 'typescript'],
|
|
9
|
+
});
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const blockAreaNames = new Set();
|
|
15
|
+
for (const statement of ast.program.body) {
|
|
16
|
+
if (statement.type !== 'ImportDeclaration') continue;
|
|
17
|
+
|
|
18
|
+
for (const specifier of statement.specifiers) {
|
|
19
|
+
if (
|
|
20
|
+
specifier.type === 'ImportSpecifier' &&
|
|
21
|
+
specifier.imported.type === 'Identifier' &&
|
|
22
|
+
specifier.imported.name === 'BlockArea'
|
|
23
|
+
) {
|
|
24
|
+
blockAreaNames.add(specifier.local.name);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (blockAreaNames.size === 0) return false;
|
|
29
|
+
|
|
30
|
+
const visited = new WeakSet();
|
|
31
|
+
const visit = (node) => {
|
|
32
|
+
if (!node || typeof node !== 'object' || visited.has(node)) return false;
|
|
33
|
+
visited.add(node);
|
|
34
|
+
|
|
35
|
+
if (
|
|
36
|
+
node.type === 'JSXOpeningElement' &&
|
|
37
|
+
node.name.type === 'JSXIdentifier' &&
|
|
38
|
+
blockAreaNames.has(node.name.name)
|
|
39
|
+
) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return Object.values(node).some((value) =>
|
|
44
|
+
Array.isArray(value) ? value.some(visit) : visit(value),
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return visit(ast.program);
|
|
49
|
+
}
|