Validate regex patterns, fix object bodies

This commit is contained in:
Misode
2024-10-24 21:41:46 +02:00
parent 6649b0aabd
commit 3e72588dc9
2 changed files with 27 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ import type { Position, Range } from 'vscode-languageserver-textdocument'
import { TextDocument } from 'vscode-languageserver-textdocument'
import type { ConfigGenerator } from '../Config.js'
import siteConfig from '../Config.js'
import { computeIfAbsent, genPath } from '../Utils.js'
import { computeIfAbsent, genPath, message } from '../Utils.js'
import type { VersionMeta } from './DataFetcher.js'
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js'
import type { VersionId } from './Versions.js'
@@ -332,6 +332,23 @@ function registerAttributes(meta: core.MetaRegistry, release: ReleaseVersion, ve
}
},
})
// Until spyglass implements this attribute itself
mcdoc.runtime.registerAttribute(meta, 'regex_pattern', () => undefined, {
checker: (_, typeDef) => {
if (typeDef.kind !== 'literal' || typeDef.value.kind !== 'string') {
return undefined
}
const pattern = typeDef.value.value
return (node, ctx) => {
try {
RegExp(pattern)
} catch (e) {
ctx.err.report(message(e), node, 2)
}
}
},
})
}
function getLsPosition(offset: number, doc: TextDocument): Position {