ipal-audit, ipal-doctor: the panel's bilingual labels are not hardcoded text
"Hardcoded Polish text in code" (i18n-literals) grepped every string, so adminText() and { en, pl }
labels in schema files were reported, though AGENT.md 6.5a asks for them. polish-literals.mjs reads
the syntax tree with the project's TypeScript (a plain grep without it) and spares those forms in
schema files only; every .tsx file, Polish-only labels, other keys and defaultValue still fail.
Reported by Niezly Dandys (1.15.2).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
36b27ed323
commit
f73465c3bb
@@ -30,6 +30,12 @@ following the hint (PROGRAMISTA §2) — do not ask whether it is needed. The on
|
||||
a client decision recorded in `AGENTS.md` as `ipal-audit-skip: <keys> — <reason>` (keys
|
||||
in AGENT.md §0b). Never add that line yourself just to make the audit pass.
|
||||
|
||||
## Polish text and panel labels (AGENT.md §2, §6.5a)
|
||||
`hardcoded Polish text in code` covers public text. In a schema file (a `.ts` file under `collections/`, `globals/`,
|
||||
`fields/` or `blocks/`, or one importing Payload's schema types) the panel's own labels are fine: `adminText('Title', 'Tytuł')`,
|
||||
`{ en, pl }` as `label`, `singular`, `plural`, `description`, `placeholder` or `group`, a `rowLabel()` fallback, a
|
||||
`const x: AdminText`. Still an error: any `.tsx` file, a Polish-only label, other keys, anything under `defaultValue`.
|
||||
|
||||
## Design tokens (AGENT.md §6.11)
|
||||
The script checks: hex in classes and inline styles (error), `dark:` on colour utilities and `var(--color-…)` in
|
||||
classes (warnings), `styles.css` without `@intecion/ipal-kit/tokens.css` (warning — a site not yet migrated),
|
||||
|
||||
@@ -238,8 +238,10 @@ fi
|
||||
if [ -n "$FRONT" ]; then
|
||||
report ERROR "logic keyed on content text" "§2 the editor can rename it any time — a style/flag is a panel field (e.g. buildLinkField({ appearances: [...] }))" \
|
||||
"$(g "\.(label|title|heading|name|text)\??\.(toLowerCase|toUpperCase|trim)\(\) *[!=]==? *['\"]|\.(label|title|heading)\?? *[!=]==? *['\"][^'\"]+['\"]" $FRONT --include=*.ts --include=*.tsx | grep -v '/(payload)/' | nc '==')"
|
||||
report ERROR "hardcoded Polish text in code" "§2 every visible text comes from the panel — legal links: getSystemLinks(locale) (page titles), labels: localized fields" \
|
||||
"$(g "['\"\`][^'\"\`]*[ąćęłńóśźżĄĆĘŁŃÓŚŹŻ][^'\"\`]*['\"\`]" $FRONT --include=*.ts --include=*.tsx | grep -v '/(payload)/' | nc '[ąćęłńóśźżĄĆĘŁŃÓŚŹŻ]')"
|
||||
# The panel's own { en, pl } / adminText() labels in schema files are not public text (polish-literals.mjs); without TypeScript, a plain grep.
|
||||
POLISH=$(node "$(dirname "$0")/polish-literals.mjs" $FRONT 2>/dev/null) || \
|
||||
POLISH=$(g "['\"\`][^'\"\`]*[ąćęłńóśźżĄĆĘŁŃÓŚŹŻ][^'\"\`]*['\"\`]" $FRONT --include=*.ts --include=*.tsx | grep -v '/(payload)/' | nc '[ąćęłńóśźżĄĆĘŁŃÓŚŹŻ]')
|
||||
report ERROR "hardcoded Polish text in code" "§2 every visible text comes from the panel — legal links: getSystemLinks(locale) (page titles), labels: localized fields; panel labels: adminText() or { en, pl } in a schema file" "$POLISH"
|
||||
fi
|
||||
GLOBALS=$(dirs src/globals)
|
||||
if [ -n "$GLOBALS" ]; then
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env node
|
||||
// Polish text hardcoded in code (AGENT.md §2) — prints "file:line: source line" per finding, exit 0.
|
||||
// node polish-literals.mjs <dir> [<dir> …]
|
||||
// Reads the syntax tree (the project's TypeScript), so comments never count and multi-line forms work.
|
||||
// Not reported: the panel's own labels (AGENT.md §6.5a), only in a schema file — a .ts file under
|
||||
// collections/, globals/, fields/ or blocks/, or one importing Payload's schema types; never a .tsx file:
|
||||
// - adminText('Title', 'Tytuł') from @intecion/ipal-kit
|
||||
// - { en: 'Title', pl: 'Tytuł' } as the value of label, singular, plural, description, placeholder or group
|
||||
// - rowLabel('title', { en: 'Item', pl: 'Pozycja' }) from @intecion/ipal-kit
|
||||
// - const TITLE: AdminText = { en: 'Title', pl: 'Tytuł' }
|
||||
// Anything under defaultValue is public content, so it is reported. So is a Polish-only label.
|
||||
// Exit 3 = TypeScript not found (audit.sh falls back to a plain grep).
|
||||
import { readFileSync, readdirSync, statSync } from 'node:fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import { join } from 'node:path'
|
||||
|
||||
const load = (from) => {
|
||||
try {
|
||||
return createRequire(from)('typescript')
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
const ts = load(join(process.cwd(), 'package.json')) ?? load(import.meta.url)
|
||||
if (!ts) process.exit(3)
|
||||
|
||||
const POLISH = /[ąćęłńóśźżĄĆĘŁŃÓŚŹŻ]/
|
||||
const SCHEMA_KEYS = new Set(['label', 'singular', 'plural', 'description', 'placeholder', 'group'])
|
||||
const SCHEMA_DIR = /(?:^|\/)(?:collections|globals|fields|blocks)\//
|
||||
const SCHEMA_TYPES = /import\s+type\s*\{[^}]*\b(?:Block|CollectionConfig|Field|GlobalConfig|Tab)\b[^}]*\}\s*from\s*['"]payload['"]/
|
||||
const IPAL = /^@intecion\/ipal-kit(?:\/|$)/
|
||||
|
||||
const files = []
|
||||
const walk = (p) => {
|
||||
let st
|
||||
try { st = statSync(p) } catch { return }
|
||||
if (st.isDirectory()) for (const n of readdirSync(p)) walk(join(p, n))
|
||||
else if (/\.tsx?$/.test(p) && !/\.d\.ts$|\/\(payload\)\//.test(p)) files.push(p)
|
||||
}
|
||||
process.argv.slice(2).forEach(walk)
|
||||
|
||||
const keyName = (node) => (ts.isPropertyAssignment(node) && (ts.isIdentifier(node.name) || ts.isStringLiteralLike(node.name)) ? node.name.text : null)
|
||||
|
||||
for (const file of files) {
|
||||
const text = readFileSync(file, 'utf8')
|
||||
if (!POLISH.test(text)) continue
|
||||
const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith('x') ? ts.ScriptKind.TSX : ts.ScriptKind.TS)
|
||||
const schemaFile = !file.endsWith('x') && !/^['"]use client['"]/m.test(text) && (SCHEMA_DIR.test(file) || SCHEMA_TYPES.test(text))
|
||||
|
||||
// Local names of the plugin's helpers (`import { adminText as t } …`); a lookalike of your own does not count.
|
||||
const helpers = new Map()
|
||||
for (const st of source.statements) {
|
||||
if (!ts.isImportDeclaration(st) || !IPAL.test(st.moduleSpecifier.text)) continue
|
||||
const named = st.importClause?.namedBindings
|
||||
if (!named || !ts.isNamedImports(named)) continue
|
||||
for (const el of named.elements) {
|
||||
const name = (el.propertyName ?? el.name).text
|
||||
if (name === 'adminText' || name === 'rowLabel') helpers.set(el.name.text, name)
|
||||
}
|
||||
}
|
||||
|
||||
const isBilingual = (node) =>
|
||||
ts.isObjectLiteralExpression(node) && ['en', 'pl'].every((k) => node.properties.some((p) => keyName(p) === k))
|
||||
|
||||
// Is this literal one of the panel's own labels? Walks up from the literal.
|
||||
const isAdminLabel = (literal) => {
|
||||
let inside = false
|
||||
for (let node = literal; node.parent; node = node.parent) {
|
||||
const parent = node.parent
|
||||
if (keyName(parent) === 'defaultValue') return false
|
||||
if (inside) continue
|
||||
if (ts.isCallExpression(parent) && parent.arguments.includes(node) && ts.isIdentifier(parent.expression)) {
|
||||
const helper = helpers.get(parent.expression.text)
|
||||
if (helper === 'adminText') inside = true
|
||||
// rowLabel(fields, fallback): the fallback only, as { en, pl }
|
||||
if (helper === 'rowLabel' && node === parent.arguments[1] && isBilingual(node)) inside = true
|
||||
}
|
||||
if (ts.isPropertyAssignment(parent) && parent.initializer === node && SCHEMA_KEYS.has(keyName(parent)) && isBilingual(node)) inside = true
|
||||
if (ts.isVariableDeclaration(parent) && parent.initializer === node && parent.type?.getText(source) === 'AdminText' && isBilingual(node)) inside = true
|
||||
}
|
||||
return inside
|
||||
}
|
||||
|
||||
const seen = new Set()
|
||||
const visit = (node) => {
|
||||
if (ts.isImportDeclaration(node)) return
|
||||
const literal = ts.isStringLiteralLike(node) || ts.isTemplateHead(node) || ts.isTemplateMiddle(node) || ts.isTemplateTail(node)
|
||||
if (literal && POLISH.test(node.text) && !(schemaFile && isAdminLabel(node))) {
|
||||
const line = source.getLineAndCharacterOfPosition(node.getStart(source)).line
|
||||
if (!seen.has(line)) {
|
||||
seen.add(line)
|
||||
console.log(`${file}:${line + 1}:${text.split('\n')[line]}`)
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, visit)
|
||||
}
|
||||
visit(source)
|
||||
}
|
||||
Reference in New Issue
Block a user