@spyglassmc/nbt 0.3.52 → 0.3.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import * as core from '@spyglassmc/core';
2
2
  import { localeQuote, localize } from '@spyglassmc/locales';
3
3
  import * as mcdoc from '@spyglassmc/mcdoc';
4
- import { NbtCompoundNode, NbtNode, NbtPathFilterNode, NbtPathIndexNode, NbtPathKeyNode, NbtPrimitiveNode, NbtStringNode, } from '../node/index.js';
4
+ import { NbtByteNode, NbtCollectionNode, NbtCompoundNode, NbtIntegerAlikeNode, NbtListNode, NbtNode, NbtNumberNode, NbtPathFilterNode, NbtPathIndexNode, NbtPathKeyNode, NbtPrimitiveNode, NbtStringNode, } from '../node/index.js';
5
5
  import { getBlocksFromItem, getEntityFromItem } from './mcdocUtil.js';
6
6
  export const typed = (node, ctx) => {
7
7
  typeDefinition(node.targetType)(node.children[0], ctx);
@@ -53,31 +53,42 @@ export function typeDefinition(typeDef, options = {}) {
53
53
  mcdoc.runtime.checker.typeDefinition([{ originalNode: node, inferredType: inferType(node) }], typeDef, mcdoc.runtime.checker.McdocCheckerContext.create(ctx, {
54
54
  allowMissingKeys: options.isPredicate || options.isMerge,
55
55
  requireCanonical: options.isPredicate,
56
- isEquivalent: (inferred, def) => {
57
- if (def.kind === 'boolean') {
58
- // TODO: this should check whether the value is 0 or 1
59
- return inferred.kind === 'byte';
60
- }
61
- if (inferred.kind === 'list') {
62
- return def.kind === 'list' || def.kind === 'tuple';
56
+ tryConvertTo: (node, target) => {
57
+ if (target.kind === 'boolean' && NbtByteNode.is(node)) {
58
+ if (node.value === 0) {
59
+ return { kind: 'literal', value: { kind: 'boolean', value: false } };
60
+ }
61
+ else if (node.value === 1) {
62
+ return { kind: 'literal', value: { kind: 'boolean', value: true } };
63
+ }
63
64
  }
64
- if (options.isPredicate) {
65
- return inferred.kind === def.kind;
65
+ if (target.kind === 'tuple' && NbtListNode.is(node)) {
66
+ return {
67
+ kind: 'tuple',
68
+ items: [],
69
+ };
66
70
  }
67
- switch (inferred.kind) {
68
- case 'struct':
69
- return def.kind === 'struct';
70
- case 'byte':
71
- case 'short':
72
- case 'int':
73
- case 'long':
74
- return ['byte', 'short', 'int', 'long', 'float', 'double'].includes(def.kind);
75
- case 'float':
76
- case 'double':
77
- return ['float', 'double'].includes(def.kind);
78
- default:
79
- return false;
71
+ if (!options.isPredicate) {
72
+ if (NbtNumberNode.is(node)) {
73
+ const literalValue = mcdoc.LiteralNumericValue.makeIfValid(target.kind, node.value, NbtIntegerAlikeNode.is(node), true);
74
+ if (literalValue !== undefined) {
75
+ return { kind: 'literal', value: literalValue };
76
+ }
77
+ }
78
+ if (NbtCollectionNode.is(node)) {
79
+ switch (target.kind) {
80
+ case 'list':
81
+ return { kind: 'list', item: { kind: 'any' } };
82
+ case 'byte_array':
83
+ case 'int_array':
84
+ case 'long_array':
85
+ return { kind: target.kind };
86
+ case 'tuple':
87
+ return { kind: 'tuple', items: [] };
88
+ }
89
+ }
80
90
  }
91
+ return undefined;
81
92
  },
82
93
  getChildren: node => {
83
94
  const { type } = node;
@@ -99,7 +110,7 @@ export function typeDefinition(typeDef, options = {}) {
99
110
  if (options.isPredicate && error.kind === 'invalid_collection_length') {
100
111
  return;
101
112
  }
102
- mcdoc.runtime.checker.getDefaultErrorReporter(ctx, (mcdoc.runtime.checker.getDefaultErrorRange))(error);
113
+ mcdoc.runtime.checker.getDefaultErrorReporter(ctx, mcdoc.runtime.checker.getDefaultErrorRange)(error);
103
114
  },
104
115
  attachTypeInfo: (node, definition, desc = '') => {
105
116
  node.typeDef = definition;
@@ -228,16 +239,20 @@ export function path(registry, id) {
228
239
  mcdoc.runtime.checker.typeDefinition([{ originalNode: link, inferredType: inferPath(link) }], typeDef, mcdoc.runtime.checker.McdocCheckerContext.create(ctx, {
229
240
  allowMissingKeys: true,
230
241
  requireCanonical: true,
231
- isEquivalent: (inferred, def) => {
232
- switch (inferred.kind) {
233
- case 'list':
234
- case 'byte_array':
235
- case 'int_array':
236
- case 'long_array':
237
- return ['list', 'tuple', 'byte_array', 'int_array', 'long_array'].includes(def.kind);
238
- default:
239
- return false;
242
+ tryConvertTo: (node, target) => {
243
+ if (NbtPathIndexNode.is(node.node)) {
244
+ switch (target.kind) {
245
+ case 'list':
246
+ return { kind: 'list', item: { kind: 'any' } };
247
+ case 'byte_array':
248
+ case 'int_array':
249
+ case 'long_array':
250
+ return { kind: target.kind };
251
+ case 'tuple':
252
+ return { kind: 'tuple', items: [] };
253
+ }
240
254
  }
255
+ return undefined;
241
256
  },
242
257
  getChildren: (link) => {
243
258
  while (link.next && link.node.type !== 'leaf' && NbtPathFilterNode.is(link.node)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spyglassmc/nbt",
3
- "version": "0.3.52",
3
+ "version": "0.3.54",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -25,8 +25,8 @@
25
25
  "url": "https://github.com/SpyglassMC/Spyglass/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@spyglassmc/core": "0.4.46",
29
- "@spyglassmc/locales": "0.3.24",
30
- "@spyglassmc/mcdoc": "0.3.50"
28
+ "@spyglassmc/core": "0.4.48",
29
+ "@spyglassmc/locales": "0.3.25",
30
+ "@spyglassmc/mcdoc": "0.3.52"
31
31
  }
32
32
  }