-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathcheck-hosted-tsx.mjs
More file actions
317 lines (298 loc) · 10.9 KB
/
check-hosted-tsx.mjs
File metadata and controls
317 lines (298 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, isAbsolute, join, relative, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import process from 'node:process'
import ts from 'typescript'
const repoRoot = resolve(fileURLToPath(new URL('../../../', import.meta.url)))
const hostedUiGlobalsPath = join(repoRoot, 'plugin/sdk/hosted-ui/globals.d.ts')
const surfaceKinds = ['panel', 'guide', 'docs']
function parseTomlSurfaces(text) {
const surfaces = []
let current = null
let inPluginUi = false
let pendingInline = null
const stripComment = (line) => {
let quote = null
for (let index = 0; index < line.length; index += 1) {
const char = line[index]
if (quote) {
if (char === quote && line[index - 1] !== '\\') quote = null
continue
}
if (char === '"' || char === "'") {
quote = char
continue
}
if (char === '#') return line.slice(0, index)
}
return line
}
const bracketDelta = (line) => {
let quote = null
let delta = 0
for (let index = 0; index < line.length; index += 1) {
const char = line[index]
if (quote) {
if (char === quote && line[index - 1] !== '\\') quote = null
continue
}
if (char === '"' || char === "'") {
quote = char
} else if (char === '[') {
delta += 1
} else if (char === ']') {
delta -= 1
}
}
return delta
}
const splitInlineFields = (body) => {
const fields = []
let quote = null
let bracketDepth = 0
let start = 0
for (let index = 0; index < body.length; index += 1) {
const char = body[index]
if (quote) {
if (char === quote && body[index - 1] !== '\\') quote = null
continue
}
if (char === '"' || char === "'") {
quote = char
} else if (char === '[') {
bracketDepth += 1
} else if (char === ']') {
bracketDepth -= 1
} else if (char === ',' && bracketDepth === 0) {
fields.push(body.slice(start, index).trim())
start = index + 1
}
}
fields.push(body.slice(start).trim())
return fields.filter(Boolean)
}
const parseInlineTable = (body, kind) => {
const surface = { kind }
for (const field of splitInlineFields(body)) {
const match = field.match(/^([A-Za-z0-9_-]+)\s*=\s*"((?:\\.|[^"])*)"$/)
if (match) surface[match[1]] = match[2].replace(/\\"/g, '"')
}
return surface
}
const addInlineSurfaces = (kind, rawValue) => {
const textValue = rawValue.trim()
const tablePattern = /\{([^{}]*)\}/g
let match
while ((match = tablePattern.exec(textValue)) !== null) {
surfaces.push(parseInlineTable(match[1], kind))
}
}
for (const rawLine of text.split(/\r?\n/)) {
const lineWithoutComment = stripComment(rawLine)
if (pendingInline) {
pendingInline.value += `\n${lineWithoutComment}`
pendingInline.depth += bracketDelta(lineWithoutComment)
if (pendingInline.depth <= 0) {
addInlineSurfaces(pendingInline.kind, pendingInline.value)
pendingInline = null
}
continue
}
const line = lineWithoutComment.trim()
if (!line) continue
const tableHeaderMatch = line.match(/^\[([^\]]+)\]$/)
if (tableHeaderMatch) {
inPluginUi = tableHeaderMatch[1] === 'plugin.ui'
current = null
}
const tableMatch = line.match(/^\[\[plugin\.ui\.(panel|guide|docs)\]\]$/)
if (tableMatch) {
inPluginUi = false
current = { kind: tableMatch[1] }
surfaces.push(current)
continue
}
if (inPluginUi) {
const inlineMatch = line.match(/^(panel|guide|docs)\s*=\s*(.+)$/)
if (inlineMatch) {
const kind = inlineMatch[1]
const value = inlineMatch[2]
const depth = bracketDelta(value)
if (depth > 0) {
pendingInline = { kind, value, depth }
} else {
addInlineSurfaces(kind, value)
}
continue
}
}
const keyValueMatch = line.match(/^([A-Za-z0-9_-]+)\s*=\s*"(.*)"$/)
if (current && keyValueMatch) {
current[keyValueMatch[1]] = keyValueMatch[2]
}
}
return surfaces
}
function inferMode(entry) {
if (!entry) return 'auto'
if (entry.endsWith('.tsx') || entry.endsWith('.jsx')) return 'hosted-tsx'
if (entry.endsWith('.md') || entry.endsWith('.mdx')) return 'markdown'
if (entry.endsWith('.html') || entry.endsWith('.htm')) return 'static'
return 'static'
}
function findPluginTomls(targets) {
const result = []
const visit = (abs) => {
if (!existsSync(abs)) return
const stat = statSync(abs)
if (stat.isFile() && abs.endsWith('plugin.toml')) {
result.push(abs)
return
}
if (!stat.isDirectory()) return
const direct = join(abs, 'plugin.toml')
if (existsSync(direct)) {
result.push(direct)
}
for (const entry of readdirSync(abs, { withFileTypes: true })) {
if (entry.isDirectory()) visit(join(abs, entry.name))
}
}
for (const target of targets.length > 0 ? targets : ['plugin/plugins']) {
const abs = isAbsolute(target) ? target : join(repoRoot, target)
visit(abs)
}
return Array.from(new Set(result))
}
function surfaceLabel(surface) {
return `${surface.kind}:${surface.id || surface.entry || 'main'}`
}
function hasDefaultExport(source) {
return /\bexport\s+default\b/.test(source)
}
function createCheckFile(entryPath, tempDir, index, surface, tomlPath) {
const source = readFileSync(entryPath, 'utf8')
const stripped = source
.replace(/^\s*import[\s\S]*?from\s+['"](?:@neko\/plugin-ui|neko:ui)['"]\s*;?\s*/gm, '')
.replace(/^\s*import\s+['"](?:@neko\/plugin-ui|neko:ui)['"]\s*;?\s*/gm, '')
const relativeEntryPath = relative(repoRoot, entryPath)
const checkPath = relativeEntryPath.startsWith('..') || isAbsolute(relativeEntryPath)
? join(tempDir, `surface-${index}.tsx`)
: join(tempDir, relativeEntryPath)
const prefixLines = 6
mkdirSync(dirname(checkPath), { recursive: true })
writeFileSync(
checkPath,
`/// <reference path="${hostedUiGlobalsPath}" />\nimport * as NekoUi from "@neko/plugin-ui";\nimport type { PluginSurfaceProps, HostedAction, JsonSchema, HostedApi } from "@neko/plugin-ui";\nconst { ${[
'Page', 'Card', 'Section', 'Heading', 'Stack', 'Grid', 'Text', 'Button', 'ButtonGroup',
'StatusBadge', 'StatCard', 'KeyValue', 'DataTable', 'Divider', 'Toolbar', 'ToolbarGroup',
'Alert', 'EmptyState', 'ErrorBoundary', 'Modal', 'ConfirmDialog', 'List', 'Progress', 'JsonView', 'Field', 'Input', 'Select',
'Textarea', 'Switch', 'Form', 'ActionButton', 'RefreshButton', 'ActionForm', 'AsyncBlock', 'InlineError', 'CodeBlock',
'Tip', 'Warning', 'Steps', 'Step', 'Tabs', 'useI18n',
'useState', 'useReducer', 'useEffect', 'useLayoutEffect', 'useMemo', 'useCallback', 'useRef', 'useLocalState',
'useDebounce', 'useDebouncedState', 'useForm', 'useAsync', 'useToast', 'useConfirm',
].join(', ')} } = NekoUi;\ndeclare const h: any;\ndeclare const Fragment: any;\n${stripped}\n`,
'utf8',
)
return {
checkPath,
entryPath,
surface,
tomlPath,
prefixLines,
hasDefaultExport: hasDefaultExport(stripped),
}
}
function formatDiagnostic(diagnostic, metaByCheckPath) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
if (diagnostic.file && diagnostic.start !== undefined) {
const meta = metaByCheckPath.get(diagnostic.file.fileName)
const pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
if (meta) {
const sourceLine = Math.max(1, pos.line + 1 - meta.prefixLines)
return `${meta.entryPath}:${sourceLine}:${pos.character + 1} [${surfaceLabel(meta.surface)}] - ${message}`
}
return `${diagnostic.file.fileName}:${pos.line + 1}:${pos.character + 1} - ${message}`
}
return message
}
function main() {
const pluginTomls = findPluginTomls(process.argv.slice(2))
const tempDir = mkdtempSync(join(tmpdir(), 'neko-hosted-tsx-'))
const checkFiles = []
const errors = []
const warnings = []
try {
for (const tomlPath of pluginTomls) {
const pluginDir = dirname(tomlPath)
const surfaces = parseTomlSurfaces(readFileSync(tomlPath, 'utf8'))
for (const surface of surfaces) {
const entry = surface.entry
if (!entry || inferMode(entry) !== 'hosted-tsx') continue
const entryPath = join(pluginDir, entry)
if (!existsSync(entryPath)) {
console.error(`${tomlPath}: hosted-tsx entry not found: ${entry}`)
process.exitCode = 1
continue
}
const checkFile = createCheckFile(entryPath, tempDir, checkFiles.length, surface, tomlPath)
checkFiles.push(checkFile)
if (!checkFile.hasDefaultExport) {
errors.push(`${entryPath}:1:1 [${surfaceLabel(surface)}] - Hosted TSX must export a default function component.`)
}
if (/\balert\s*\(/.test(readFileSync(entryPath, 'utf8'))) {
warnings.push(`${entryPath} [${surfaceLabel(surface)}] - Prefer inline UI errors over alert(); use ActionForm/ActionButton onError or InlineError.`)
}
if (/(^|[^\w.])api\./m.test(readFileSync(entryPath, 'utf8'))) {
errors.push(`${entryPath}:1:1 [${surfaceLabel(surface)}] - Use props.api from PluginSurfaceProps instead of the global api object.`)
}
}
}
if (checkFiles.length === 0) {
console.log('No hosted-tsx surfaces found.')
return
}
const metaByCheckPath = new Map(checkFiles.map((item) => [item.checkPath, item]))
const program = ts.createProgram(checkFiles.map((item) => item.checkPath), {
jsx: ts.JsxEmit.React,
jsxFactory: 'h',
jsxFragmentFactory: 'Fragment',
module: ts.ModuleKind.ESNext,
target: ts.ScriptTarget.ES2020,
moduleResolution: ts.ModuleResolutionKind.Bundler,
baseUrl: repoRoot,
rootDirs: [tempDir, repoRoot],
paths: {
'@neko/plugin-ui': ['plugin/sdk/hosted-ui'],
},
noEmit: true,
strict: false,
skipLibCheck: true,
esModuleInterop: true,
allowSyntheticDefaultImports: true,
})
const diagnostics = ts.getPreEmitDiagnostics(program)
if (warnings.length > 0) {
console.warn('Hosted TSX warnings:')
for (const warning of warnings) {
console.warn(` ${warning}`)
}
}
if (errors.length > 0 || diagnostics.length > 0) {
console.error('Hosted TSX check failed:')
for (const error of errors) {
console.error(` ${error}`)
}
for (const diagnostic of diagnostics) {
console.error(` ${formatDiagnostic(diagnostic, metaByCheckPath)}`)
}
process.exitCode = 1
return
}
console.log(`Hosted TSX check passed (${checkFiles.length} file${checkFiles.length === 1 ? '' : 's'}).`)
} finally {
rmSync(tempDir, { recursive: true, force: true })
}
}
main()