From b5ecfb061ef18f62162aa8834b11d62f0247a519 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Mon, 9 Nov 2026 02:38:45 -0600 Subject: [PATCH] fix(site[ocaml]) Verify constructor destinations why: Constructor links need declaration evidence for their names and parent types rather than a manually approved heading. what: - Read constructor anchors and validate their type, class and name. - Reject markers inside quoted attributes and comments. - Record Option.None's identity and remove its manual review row. - Cover constructor declarations and mismatched markup. --- scripts/lib/api-addresses.mjs | 4 +- scripts/lib/api-identities.mjs | 41 +++++++++++++++++---- site/src/data/api-destination-evidence.json | 2 +- site/src/data/api-link-review.json | 5 --- site/test/api-destination-evidence.test.ts | 26 +++++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/scripts/lib/api-addresses.mjs b/scripts/lib/api-addresses.mjs index 0a1d05833..786e3ff60 100644 --- a/scripts/lib/api-addresses.mjs +++ b/scripts/lib/api-addresses.mjs @@ -55,7 +55,9 @@ export function addressOf(url) { if (host === 'ocaml.org' && /^\/manual\/[^/]+\/api\//.test(u.pathname)) { const path = dotted(stem) const member = /^(?:TYPE|VAL)([a-z_][\w']*)$/.exec(fragment) - return { path: member ? [...path, member[1]] : path, exact: true, fragment: member ? 'member' : none, type: stem } + const constructor = /^TYPEELT[a-z_][\w']*\.([A-Z][\w']*)$/.exec(fragment) + const name = member?.[1] ?? constructor?.[1] + return { path: name ? [...path, name] : path, exact: true, fragment: name ? 'member' : none, type: stem } } if (host === 'doc.rust-lang.org' || host === 'docs.rs') { diff --git a/scripts/lib/api-identities.mjs b/scripts/lib/api-identities.mjs index 0776f666b..0ec87f006 100644 --- a/scripts/lib/api-identities.mjs +++ b/scripts/lib/api-identities.mjs @@ -281,12 +281,7 @@ function documenter(url, page, fragment) { return { via: 'documenter', declared, kind: declared === 'type' ? 'type' : bucket(declared === 'constant' ? 'constant' : declared), path: name.split('.'), title: page.title } } -function ocamldoc(_url, page, fragment) { - const heading = page.headings.find((entry) => entry.level === 1)?.text ?? '' - const module = /^Module ([A-Z][\w']*(?:\.[A-Z][\w']*)*)$/.exec(heading) - if (!module) return undefined - const base = { via: 'ocamldoc', declared: 'module', kind: 'module', path: dotted(module[1]), title: page.title } - if (!fragment) return base +function ocamldocMember(page, fragment) { const member = /^(TYPE|VAL)([a-z_][\w']*)$/.exec(fragment) const anchor = page.anchors.find((entry) => entry.id === fragment) if (!member || !anchor) return undefined @@ -298,7 +293,39 @@ function ocamldoc(_url, page, fragment) { ? /^type\s+(?:(?:[+-]?'[\w']+|\([^)]*\))\s+)?([a-z_][\w']*)(?![\w'])/ : /^val\s+([a-z_][\w']*)(?![\w'])/).exec(declaration) if (named?.[1] !== member[2]) return undefined - return { ...base, declared: isType ? 'type' : 'val', kind: isType ? 'type' : 'member', path: [...base.path, member[2]] } + return { declared: isType ? 'type' : 'val', kind: isType ? 'type' : 'member', name: member[2] } +} + +function ocamldoc(_url, page, fragment) { + const heading = page.headings.find((entry) => entry.level === 1)?.text ?? '' + const module = /^Module ([A-Z][\w']*(?:\.[A-Z][\w']*)*)$/.exec(heading) + if (!module) return undefined + const base = { via: 'ocamldoc', declared: 'module', kind: 'module', path: dotted(module[1]), title: page.title } + if (!fragment) return base + const anchor = page.anchors.find((entry) => entry.id === fragment) + if (!anchor) return undefined + const constructor = /^TYPEELT([a-z_][\w']*)\.([A-Z][\w']*)$/.exec(fragment) + if (constructor) { + const end = page.html.indexOf('', anchor.at) + const declaration = end < 0 ? '' : page.html.slice(anchor.at, end) + if (!ocamldocMember(page, `TYPE${constructor[1]}`)) return undefined + // Consume complete tags so quoted attribute values cannot supply markup. + const tags = declaration.matchAll(/|<\/?[A-Za-z][\w:-]*(?=\s|\/?>)(?:"[^"]*"|'[^']*'|[^"'<>])*>/g) + const constructorSpan = [...tags].some(([tag]) => { + if (!/^)/.test(tag)) return false + const text = tag.slice(5, -1).trimEnd() + const attributes = [...text.matchAll(/\s+([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/gy)] + const last = attributes.at(-1) + if ((last ? last.index + last[0].length : 0) !== text.length) return false + const field = attributes.find(([, name]) => name === 'class') + return (field?.[2] ?? field?.[3] ?? field?.[4]) === 'constructor' + }) + if (!constructorSpan || strip(declaration) !== constructor[2]) return undefined + return { ...base, declared: 'constructor', kind: 'member', path: [...base.path, constructor[2]] } + } + const member = ocamldocMember(page, fragment) + if (!member) return undefined + return { ...base, declared: member.declared, kind: member.kind, path: [...base.path, member.name] } } function mdn(url, page) { diff --git a/site/src/data/api-destination-evidence.json b/site/src/data/api-destination-evidence.json index a18b57ff0..70227c38c 100644 --- a/site/src/data/api-destination-evidence.json +++ b/site/src/data/api-destination-evidence.json @@ -729,7 +729,7 @@ {"url":"https://ocaml.org/manual/5.5/api/Iarray.html","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Iarray","identity":{"via":"ocamldoc","declared":"module","kind":"module","path":["Iarray"]},"ok":true,"terminal":[{"key":"ocaml:Iarray","symbol":"Iarray","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:Iarray"],"sources":["builtin","prose"]}, {"url":"https://ocaml.org/manual/5.5/api/Iarray.html#TYPEt","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Iarray","identity":{"via":"ocamldoc","declared":"type","kind":"type","path":["Iarray","t"]},"fragment":"found","ok":true,"terminal":[{"key":"ocaml:Iarray.t","symbol":"t","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:Iarray.t"],"sources":["builtin","type"]}, {"url":"https://ocaml.org/manual/5.5/api/List.html","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : List","identity":{"via":"ocamldoc","declared":"module","kind":"module","path":["List"]},"ok":true,"terminal":[{"key":"ocaml:List","symbol":"List","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:List"],"sources":["builtin","prose"]}, - {"url":"https://ocaml.org/manual/5.5/api/Option.html#TYPEELTt.None","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Option","identity":{"via":"page","declared":"page","kind":"other","path":[]},"heading":"#TYPEELTt.None","fragment":"found","ok":true,"terminal":[{"key":"authored:`None`","symbol":"`None`","at":"review"}],"checkedAt":"2026-11-09","names":["site/src/content/docs/_staged/ocaml/guides/bootstrap/index.md","site/src/data/port-guides/ocaml.json"],"sources":["authored"]}, + {"url":"https://ocaml.org/manual/5.5/api/Option.html#TYPEELTt.None","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Option","identity":{"via":"ocamldoc","declared":"constructor","kind":"member","path":["Option","None"]},"heading":"Option.None","fragment":"found","ok":true,"terminal":[{"key":"authored:`None`","symbol":"`None`","at":"identity"}],"checkedAt":"2026-11-09","names":["site/src/content/docs/_staged/ocaml/guides/bootstrap/index.md","site/src/data/port-guides/ocaml.json"],"sources":["authored"]}, {"url":"https://ocaml.org/manual/5.5/api/Seq.html","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Seq","identity":{"via":"ocamldoc","declared":"module","kind":"module","path":["Seq"]},"ok":true,"terminal":[{"key":"ocaml:Seq","symbol":"Seq","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:Seq"],"sources":["builtin","prose"]}, {"url":"https://ocaml.org/manual/5.5/api/Seq.html#TYPEt","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Seq","identity":{"via":"ocamldoc","declared":"type","kind":"type","path":["Seq","t"]},"fragment":"found","ok":true,"terminal":[{"key":"ocaml:Seq.t","symbol":"t","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:Seq.t"],"sources":["builtin","type"]}, {"url":"https://ocaml.org/manual/5.5/api/Seq.html#VALfilter","host":"ocaml.org","status":200,"via":"html","title":"OCaml library : Seq","identity":{"via":"ocamldoc","declared":"val","kind":"member","path":["Seq","filter"]},"fragment":"found","ok":true,"terminal":[{"key":"ocaml:Seq.filter","symbol":"filter","at":"identity"}],"checkedAt":"2026-11-08","names":["ocaml:Seq.filter"],"sources":["builtin","prose"]}, diff --git a/site/src/data/api-link-review.json b/site/src/data/api-link-review.json index e1343b1c9..3eb48103f 100644 --- a/site/src/data/api-link-review.json +++ b/site/src/data/api-link-review.json @@ -980,11 +980,6 @@ "url": "https://ocaml.org/docs/libraries-dune", "heading": "Libraries With Dune" }, - { - "label": "`None`", - "url": "https://ocaml.org/manual/5.5/api/Option.html#TYPEELTt.None", - "heading": "#TYPEELTt.None" - }, { "label": "OCaml expressions", "url": "https://ocaml.org/manual/5.5/expr.html#s:value-expr", diff --git a/site/test/api-destination-evidence.test.ts b/site/test/api-destination-evidence.test.ts index 0dff92939..7cd770cc7 100644 --- a/site/test/api-destination-evidence.test.ts +++ b/site/test/api-destination-evidence.test.ts @@ -223,6 +223,32 @@ describe('what the verifier reads from a page', () => { } }) + it('checks OCaml variant constructors against their type and declared name', () => { + const html = 'OCaml library : Option

Module Option

' + + '
type \'a t = \'a option = 
' + + '' + + '
None
Some of \'a
' + for (const name of ['None', 'Some']) { + const url = `https://ocaml.org/manual/5.5/api/Option.html#TYPEELTt.${name}` + expect(addressOf(url)).toMatchObject({ path: ['Option', name], exact: true, fragment: 'member' }) + const identity = pageIdentity(url, readPage(html)) + expect(identity).toMatchObject({ via: 'ocamldoc', declared: 'constructor', kind: 'member', path: ['Option', name] }) + expect(judge(sourceIdentity({ port: 'ocaml', name: `Option.${name}`, source: 'builtin' }), identity)).toBeUndefined() + expect(judge(sourceIdentity({ port: 'ocaml', name: `Other.${name}`, source: 'builtin' }), identity)).toMatchObject({ failure: 'owner' }) + for (const forged of [html.replace('type \'a t', 'type \'a other'), + html.replace(`class="constructor">${name}`, 'class="constructor">Invented'), + html.replace(`class="constructor">${name}`, `class="type">${name}`), + html.replace(`class="constructor">${name}`, `data-class="constructor">${name}`), + html.replace(`class="constructor">${name}`, `data-note.text=" class='constructor' ">${name}`), + html.replace(`class="constructor">${name}`, `title=" class='constructor'">${name}`), + html.replace(`class="constructor">${name}`, `title="${name}`), + html.replace(`${name}`, `${name}`), + html.replace(`class="constructor">${name}`, `>${name}`)]) { + expect(pageIdentity(url, readPage(forged))).toMatchObject({ kind: 'other' }) + } + } + }) + it('matches complete OCaml identifiers including apostrophes in value and type declarations', () => { const actual = [] const expected = []