mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Validate regex patterns, fix object bodies
This commit is contained in:
@@ -397,7 +397,7 @@ function Body({ type, optional, node, makeEdit, ctx }: BodyProps) {
|
||||
return <UnionBody type={type} optional={optional} node={node} makeEdit={makeEdit} ctx={ctx} />
|
||||
}
|
||||
if (type.kind === 'struct') {
|
||||
if (!JsonObjectNode.is(node) || optional || type.fields.length === 0) {
|
||||
if (!JsonObjectNode.is(node) || type.fields.length === 0) {
|
||||
return <></>
|
||||
}
|
||||
return <div class="node-body">
|
||||
@@ -678,8 +678,14 @@ function getDefault(type: McdocType, range: core.Range, ctx: McdocContext): Json
|
||||
const object = JsonObjectNode.mock(range)
|
||||
if (type.kind === 'struct') {
|
||||
for (const field of type.fields) {
|
||||
if (field.kind === 'pair' && !field.optional && typeof field.key === 'string') {
|
||||
const key: JsonStringNode = { type: 'json:string', range, options: json.parser.JsonStringOptions, value: field.key, valueMap: [{ inner: core.Range.create(0), outer: core.Range.create(range.start) }] }
|
||||
if (field.kind === 'pair' && !field.optional && (typeof field.key === 'string' || field.key.kind === 'literal')) {
|
||||
const key: JsonStringNode = {
|
||||
type: 'json:string',
|
||||
range,
|
||||
options: json.parser.JsonStringOptions,
|
||||
value: typeof field.key === 'string' ? field.key : field.key.value.value.toString(),
|
||||
valueMap: [{ inner: core.Range.create(0), outer: core.Range.create(range.start) }],
|
||||
}
|
||||
const value = getDefault(field.type, range, ctx)
|
||||
const pair: core.PairNode<JsonStringNode, JsonNode> = {
|
||||
type: 'pair',
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user