mirror of
https://github.com/stablyai/orca.git
synced 2026-10-01 00:02:10 +00:00
Enable eleven oxlint rules that simplify code without changing behavior, and fix
every existing violation. Each candidate was gated on measured cost rather than
assumption, so rules that regressed runtime performance or type checking were
dropped instead of suppressed.
typescript/no-redundant-type-constituents is the largest addition: 113 sites, no
autofix. Dead constituents are deleted. Where the redundant literal existed to
document intent (`string | 'all'`), it is preserved as `(string & {})`, which
keeps the autocomplete hint the original code was reaching for instead of
flattening it away. The rule also caught a broken import —
remote-shared-control-retirement-probe.ts pulled RuntimeStatus from
src/shared/types, which does not export it, so the type silently degraded to
`any`; no tsconfig covers that file, so tsc never saw it.
oxlint stays at 1.77.0 rather than 1.78.0 because .npmrc sets
minimum-release-age=4320 and 1.78.0 is younger than that window.
Rules evaluated and rejected, with what disqualified each:
- prefer-string-raw: String.raw is a runtime call, not a literal (184x slower)
- prefer-string-replace-all: 26% slower
- text-encoding-identifier-case: ~5% slower, reproducible
- prefer-spread: [...str] is 110% slower than split('') and differs on surrogates
- no-implicit-coercion: `!!x` narrows types and `Boolean(x)` does not (22 tsc errors)
- prefer-arrow-callback: arrows are not constructible, breaking `new` on mocks
- object-shorthand: rewrites source text asserted by a tracked reliability gate
- switch-case-braces: pushes ten files past max-lines, which cannot be suppressed
- no-useless-switch-case: drops `case undefined:` that switch-exhaustiveness-check needs
- arrow-body-style: 115 violations have no fix, and it breaks max-lines
- newline-after-import: false-positives on the leading-semicolon ASI idiom
electron-vite-output-contract asserted on the literal
Object.prototype.hasOwnProperty.call text; retarget it to Object.hasOwn, which
rejects inherited keys identically.
293 lines
8.8 KiB
TypeScript
293 lines
8.8 KiB
TypeScript
import {
|
|
findMobileMarkdownMarkupTagEnd,
|
|
findNextPairedMarkupOpener,
|
|
isPairedMarkupOpener,
|
|
replaceMobileMarkdownPairedMarkupTags,
|
|
stripMobileMarkdownMarkupTags
|
|
} from './mobile-markdown-preview-tag-stripper'
|
|
|
|
// Why: README HTML snippets can document escaped entities; repeated cleanup
|
|
// passes must not turn `<` into a real tag and strip it.
|
|
const escapedHtmlEntityTokens = [
|
|
{ pattern: / /gi, token: '\uE000ORCA_MD_ENTITY_NBSP\uE000', value: ' ' },
|
|
{ pattern: /&lt;/gi, token: '\uE000ORCA_MD_ENTITY_LT\uE000', value: '<' },
|
|
{ pattern: /&gt;/gi, token: '\uE000ORCA_MD_ENTITY_GT\uE000', value: '>' },
|
|
{ pattern: /&quot;/gi, token: '\uE000ORCA_MD_ENTITY_QUOT\uE000', value: '"' },
|
|
{ pattern: /&#39;/gi, token: '\uE000ORCA_MD_ENTITY_APOS\uE000', value: ''' },
|
|
{ pattern: /</gi, token: '\uE000ORCA_MD_ENTITY_RAW_LT\uE000', value: '<' },
|
|
{ pattern: />/gi, token: '\uE000ORCA_MD_ENTITY_RAW_GT\uE000', value: '>' }
|
|
] as const
|
|
|
|
function protectEscapedHtmlEntities(value: string): string {
|
|
return escapedHtmlEntityTokens.reduce(
|
|
(next, entity) => next.replace(entity.pattern, entity.token),
|
|
value
|
|
)
|
|
}
|
|
|
|
function restoreEscapedHtmlEntities(value: string): string {
|
|
return escapedHtmlEntityTokens.reduce(
|
|
(next, entity) => next.replaceAll(entity.token, entity.value),
|
|
value
|
|
)
|
|
}
|
|
|
|
function decodeHtmlEntities(value: string, preserveEscapedEntities = false): string {
|
|
const next = preserveEscapedEntities ? protectEscapedHtmlEntities(value) : value
|
|
|
|
return next
|
|
.replace(/ /gi, ' ')
|
|
.replace(/</gi, '<')
|
|
.replace(/>/gi, '>')
|
|
.replace(/"/gi, '"')
|
|
.replace(/'/gi, "'")
|
|
.replace(/&/gi, '&')
|
|
}
|
|
|
|
function stripTags(value: string): string {
|
|
const { protectedText, codeSpans, placeholderPrefix } = protectMarkdownCode(value)
|
|
const stripped = decodeHtmlEntities(
|
|
stripMobileMarkdownMarkupTags(protectedText.replace(/<!--[\s\S]*?-->/g, '')),
|
|
true
|
|
)
|
|
.replace(/[ \t]+\n/g, '\n')
|
|
.replace(/\n{3,}/g, '\n\n')
|
|
.trim()
|
|
|
|
return restoreMarkdownCode(stripped, codeSpans, placeholderPrefix)
|
|
}
|
|
|
|
function attrValue(tag: string, name: string): string {
|
|
let cursor = 1
|
|
while (cursor < tag.length && !/[\s/>]/.test(tag[cursor] ?? '')) {
|
|
cursor += 1
|
|
}
|
|
while (cursor < tag.length) {
|
|
while (/\s/.test(tag[cursor] ?? '')) {
|
|
cursor += 1
|
|
}
|
|
if (tag[cursor] === '>' || (tag[cursor] === '/' && tag[cursor + 1] === '>')) {
|
|
return ''
|
|
}
|
|
|
|
const attributeStart = cursor
|
|
while (!/[\s=/>]/.test(tag[cursor] ?? '>')) {
|
|
cursor += 1
|
|
}
|
|
if (attributeStart === cursor) {
|
|
cursor += 1
|
|
continue
|
|
}
|
|
const attributeName = tag.slice(attributeStart, cursor)
|
|
while (/\s/.test(tag[cursor] ?? '')) {
|
|
cursor += 1
|
|
}
|
|
if (tag[cursor] !== '=') {
|
|
continue
|
|
}
|
|
|
|
cursor += 1
|
|
while (/\s/.test(tag[cursor] ?? '')) {
|
|
cursor += 1
|
|
}
|
|
const quote = tag[cursor] === '"' || tag[cursor] === "'" ? tag[cursor] : ''
|
|
if (quote) {
|
|
cursor += 1
|
|
}
|
|
const valueStart = cursor
|
|
if (quote) {
|
|
const valueEnd = tag.indexOf(quote, cursor)
|
|
if (valueEnd === -1) {
|
|
return ''
|
|
}
|
|
cursor = valueEnd + 1
|
|
if (attributeName.toLowerCase() === name) {
|
|
return decodeHtmlEntities(tag.slice(valueStart, valueEnd))
|
|
}
|
|
continue
|
|
}
|
|
|
|
while (!/[\s>]/.test(tag[cursor] ?? '>')) {
|
|
cursor += 1
|
|
}
|
|
if (attributeName.toLowerCase() === name) {
|
|
return decodeHtmlEntities(tag.slice(valueStart, cursor))
|
|
}
|
|
}
|
|
return ''
|
|
}
|
|
|
|
const tagAttributesSource = `(?:[^<>"']|"[^"]*"|'[^']*')*`
|
|
const imageTagPattern = new RegExp(`<img\\b${tagAttributesSource}>`, 'gi')
|
|
|
|
function normalizeAnchorTags(value: string): string {
|
|
const lowerValue = value.toLowerCase()
|
|
let output = ''
|
|
let copyCursor = 0
|
|
let searchCursor = 0
|
|
let closingStart = -1
|
|
|
|
while (searchCursor < value.length) {
|
|
const start = findNextPairedMarkupOpener(value, lowerValue, 'a', searchCursor)
|
|
if (start < 0) {
|
|
break
|
|
}
|
|
const end = findMobileMarkdownMarkupTagEnd(value, start + 2)
|
|
if (end < 0) {
|
|
if (end === -2) {
|
|
break
|
|
}
|
|
searchCursor = start + 2
|
|
continue
|
|
}
|
|
if (!isPairedMarkupOpener(value, start + 2, end)) {
|
|
searchCursor = end + 1
|
|
continue
|
|
}
|
|
|
|
const tag = value.slice(start, end + 1)
|
|
const href = attrValue(tag, 'href')
|
|
if (!href) {
|
|
searchCursor = end + 1
|
|
continue
|
|
}
|
|
|
|
if (closingStart < end + 1) {
|
|
closingStart = lowerValue.indexOf('</a>', end + 1)
|
|
}
|
|
if (closingStart < 0) {
|
|
break
|
|
}
|
|
const nestedStart = findNextPairedMarkupOpener(value, lowerValue, 'a', end + 1)
|
|
if (nestedStart >= 0 && nestedStart < closingStart) {
|
|
searchCursor = nestedStart
|
|
continue
|
|
}
|
|
|
|
const text = stripTags(value.slice(end + 1, closingStart))
|
|
output += value.slice(copyCursor, start)
|
|
output += href && text ? `[${text}](${href})` : text
|
|
copyCursor = closingStart + 4
|
|
searchCursor = copyCursor
|
|
}
|
|
|
|
return output + value.slice(copyCursor)
|
|
}
|
|
|
|
function normalizeInlineHtml(value: string): string {
|
|
const imagesNormalized = value
|
|
.replace(/<br\s*\/?>/gi, '\n')
|
|
.replace(imageTagPattern, (tag) => attrValue(tag, 'alt') || 'image')
|
|
|
|
let next = normalizeAnchorTags(imagesNormalized)
|
|
next = replaceMobileMarkdownPairedMarkupTags(next, ['strong', 'b'], (_name, inner) => {
|
|
const text = stripTags(inner)
|
|
return text ? `**${text}**` : ''
|
|
})
|
|
next = replaceMobileMarkdownPairedMarkupTags(next, ['em', 'i'], (_name, inner) => {
|
|
const text = stripTags(inner)
|
|
return text ? `*${text}*` : ''
|
|
})
|
|
next = replaceMobileMarkdownPairedMarkupTags(next, ['code', 'kbd'], (_name, inner) => {
|
|
const text = stripTags(inner)
|
|
return text ? `\`${text}\`` : ''
|
|
})
|
|
return next
|
|
}
|
|
|
|
// Why: Markdown code is literal source, so it must bypass the HTML strip pass.
|
|
const CODE_PLACEHOLDER_PREFIX_BASE = '\uE000ORCA_MD_CODE_'
|
|
const CODE_PLACEHOLDER_SUFFIX = '\uE000'
|
|
|
|
function escapeRegExp(value: string): string {
|
|
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
}
|
|
|
|
function codePlaceholderPrefix(content: string): string {
|
|
let prefix = CODE_PLACEHOLDER_PREFIX_BASE
|
|
while (content.includes(prefix)) {
|
|
prefix = `${prefix}_`
|
|
}
|
|
return prefix
|
|
}
|
|
|
|
function protectMarkdownCode(content: string): {
|
|
protectedText: string
|
|
codeSpans: string[]
|
|
placeholderPrefix: string
|
|
} {
|
|
const placeholderPrefix = codePlaceholderPrefix(content)
|
|
const codeSpans: string[] = []
|
|
const store = (match: string): string => {
|
|
const token = `${placeholderPrefix}${codeSpans.length}${CODE_PLACEHOLDER_SUFFIX}`
|
|
codeSpans.push(match)
|
|
return token
|
|
}
|
|
|
|
const lines = content.split('\n')
|
|
const protectedLines: string[] = []
|
|
let index = 0
|
|
while (index < lines.length) {
|
|
const line = lines[index] ?? ''
|
|
if (/^```[A-Za-z0-9_-]*\s*$/.test(line)) {
|
|
const start = index
|
|
index += 1
|
|
while (index < lines.length && !/^```\s*$/.test(lines[index] ?? '')) {
|
|
index += 1
|
|
}
|
|
if (index < lines.length) {
|
|
index += 1
|
|
}
|
|
protectedLines.push(store(lines.slice(start, index).join('\n')))
|
|
continue
|
|
}
|
|
|
|
protectedLines.push(line.replace(/`[^`\n]+`/g, store))
|
|
index += 1
|
|
}
|
|
|
|
return { protectedText: protectedLines.join('\n'), codeSpans, placeholderPrefix }
|
|
}
|
|
|
|
function restoreMarkdownCode(
|
|
value: string,
|
|
codeSpans: string[],
|
|
placeholderPrefix: string
|
|
): string {
|
|
const placeholderPattern = new RegExp(
|
|
`${escapeRegExp(placeholderPrefix)}(\\d+)${escapeRegExp(CODE_PLACEHOLDER_SUFFIX)}`,
|
|
'g'
|
|
)
|
|
return value.replace(placeholderPattern, (_token, index) => codeSpans[Number(index)] ?? _token)
|
|
}
|
|
|
|
export function normalizeMobileMarkdownPreviewHtml(content: string): string {
|
|
const { protectedText, codeSpans, placeholderPrefix } = protectMarkdownCode(
|
|
content.replace(/\r\n?/g, '\n')
|
|
)
|
|
let next = protectedText
|
|
|
|
// Why: repository Markdown often uses small HTML islands for centered README
|
|
// headers and badges. Preview mode should read like Markdown, while Source
|
|
// mode remains the exact file bytes.
|
|
next = replaceMobileMarkdownPairedMarkupTags(
|
|
next,
|
|
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
|
(name, inner) => {
|
|
const text = stripTags(normalizeInlineHtml(inner))
|
|
return text ? `\n${'#'.repeat(Number(name.slice(1)))} ${text}\n` : '\n'
|
|
}
|
|
)
|
|
next = replaceMobileMarkdownPairedMarkupTags(next, ['p'], (_name, inner) => {
|
|
const text = stripTags(normalizeInlineHtml(inner))
|
|
return text ? `\n${text}\n` : '\n'
|
|
})
|
|
next = replaceMobileMarkdownPairedMarkupTags(next, ['sub'], (_name, inner) =>
|
|
stripTags(normalizeInlineHtml(inner))
|
|
)
|
|
next = normalizeInlineHtml(next)
|
|
next = stripTags(next)
|
|
|
|
return restoreMarkdownCode(restoreEscapedHtmlEntities(next), codeSpans, placeholderPrefix)
|
|
}
|