@spyglassmc/nbt 0.3.51 → 0.3.53

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;
@@ -151,11 +162,7 @@ function inferType(node) {
151
162
  case 'nbt:float':
152
163
  return { kind: 'literal', value: { kind: 'float', value: node.value } };
153
164
  case 'nbt:long':
154
- return {
155
- kind: 'literal',
156
- // TODO: this should NOT change type from `bigint` to `number`
157
- value: { kind: 'long', value: Number(node.value) },
158
- };
165
+ return { kind: 'literal', value: { kind: 'long', value: node.value } };
159
166
  case 'nbt:int':
160
167
  return { kind: 'literal', value: { kind: 'int', value: node.value } };
161
168
  case 'nbt:short':
@@ -232,16 +239,20 @@ export function path(registry, id) {
232
239
  mcdoc.runtime.checker.typeDefinition([{ originalNode: link, inferredType: inferPath(link) }], typeDef, mcdoc.runtime.checker.McdocCheckerContext.create(ctx, {
233
240
  allowMissingKeys: true,
234
241
  requireCanonical: true,
235
- isEquivalent: (inferred, def) => {
236
- switch (inferred.kind) {
237
- case 'list':
238
- case 'byte_array':
239
- case 'int_array':
240
- case 'long_array':
241
- return ['list', 'tuple', 'byte_array', 'int_array', 'long_array'].includes(def.kind);
242
- default:
243
- 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
+ }
244
254
  }
255
+ return undefined;
245
256
  },
246
257
  getChildren: (link) => {
247
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.51",
3
+ "version": "0.3.53",
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.45",
29
- "@spyglassmc/locales": "0.3.23",
30
- "@spyglassmc/mcdoc": "0.3.49"
28
+ "@spyglassmc/core": "0.4.47",
29
+ "@spyglassmc/locales": "0.3.24",
30
+ "@spyglassmc/mcdoc": "0.3.51"
31
31
  }
32
32
  }