Skip to content

Commit

Permalink
✨ Add exclude option to id mcdoc attribute (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored Nov 21, 2024
1 parent 31a54d5 commit ee4fefa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"nbt.node.string": "a string tag",
"nbt.path": "an NBT path",
"nbt.parser.number.out-of-range": "This looks like %0%, but it is actually %1% due to the numeral value being out of [%2%, %3%]",
"not-allowed-here": "Value %0% is not allowed here",
"not-matching-any-child": "Invalid argument type",
"nothing": "nothing",
"number": "a number",
Expand Down
28 changes: 27 additions & 1 deletion packages/mcdoc/src/runtime/attribute/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface IdConfig {
definition?: boolean
prefix?: '!'
empty?: 'allowed'
exclude?: string[]
}

const idValidator = validator.alternatives<IdConfig>(
Expand All @@ -19,6 +20,17 @@ const idValidator = validator.alternatives<IdConfig>(
definition: validator.optional(validator.boolean),
prefix: validator.optional(validator.options('!')),
empty: validator.optional(validator.options('allowed')),
exclude: validator.optional(validator.alternatives<string[]>(
validator.map(validator.string, v => [v]),
(value) => {
if (value?.kind === 'tuple') {
return value.items.flatMap(i =>
i.kind === 'literal' && i.value.kind === 'string' ? [i.value.value] : []
)
}
return core.Failure
},
)),
}),
() => ({}),
)
Expand Down Expand Up @@ -149,7 +161,21 @@ export function registerBuiltinAttributes(meta: core.MetaRegistry) {
if (config.prefix) {
return core.prefixed({ prefix: config.prefix, child: resourceLocation })(src, ctx)
}
return resourceLocation(src, ctx)
const node = resourceLocation(src, ctx)
if (config.exclude) {
const resourceLocation = core.ResourceLocationNode.toString(node, 'full')
for (const e of config.exclude ?? []) {
const excluded = core.ResourceLocation.lengthen(e)
if (resourceLocation === excluded) {
ctx.err.report(
localize('not-allowed-here', localeQuote(excluded)),
node,
core.ErrorSeverity.Warning,
)
}
}
}
return node
}
},
stringMocker: (config, typeDef, ctx) => {
Expand Down

0 comments on commit ee4fefa

Please sign in to comment.