From ee4fefac40d84f7add20ba1ac33004433d6c7c9b Mon Sep 17 00:00:00 2001 From: Misode Date: Thu, 21 Nov 2024 17:00:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`exclude`=20option=20to=20`id?= =?UTF-8?q?`=20mcdoc=20attribute=20(#1615)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/locales/src/locales/en.json | 1 + .../mcdoc/src/runtime/attribute/builtin.ts | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/locales/src/locales/en.json b/packages/locales/src/locales/en.json index 6fde60c96..317eaef8a 100644 --- a/packages/locales/src/locales/en.json +++ b/packages/locales/src/locales/en.json @@ -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", diff --git a/packages/mcdoc/src/runtime/attribute/builtin.ts b/packages/mcdoc/src/runtime/attribute/builtin.ts index 1e6d3f251..c869cf7b7 100644 --- a/packages/mcdoc/src/runtime/attribute/builtin.ts +++ b/packages/mcdoc/src/runtime/attribute/builtin.ts @@ -9,6 +9,7 @@ interface IdConfig { definition?: boolean prefix?: '!' empty?: 'allowed' + exclude?: string[] } const idValidator = validator.alternatives( @@ -19,6 +20,17 @@ const idValidator = validator.alternatives( definition: validator.optional(validator.boolean), prefix: validator.optional(validator.options('!')), empty: validator.optional(validator.options('allowed')), + exclude: validator.optional(validator.alternatives( + 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 + }, + )), }), () => ({}), ) @@ -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) => {