@spyglassmc/json 0.2.0 → 0.3.0

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 { localize } from '@spyglassmc/locales';
2
2
  import { JsonBooleanNode } from '../../node/index.js';
3
3
  export function boolean(node, ctx) {
4
- ctx.ops.set(node, 'expectation', [{ type: 'json:boolean', typedoc: 'Boolean' }]);
4
+ node.expectation = [{ type: 'json:boolean', typedoc: 'Boolean' }];
5
5
  if (!JsonBooleanNode.is(node)) {
6
6
  ctx.err.report(localize('expected', localize('boolean')), node);
7
7
  }
@@ -3,9 +3,9 @@ import { JsonArrayNode, JsonStringNode } from '../../node/index.js';
3
3
  import { expectation } from './util.js';
4
4
  export function listOf(checker) {
5
5
  return (node, ctx) => {
6
- ctx.ops.set(node, 'expectation', [{ type: 'json:array', typedoc: 'Array' }]);
6
+ node.expectation = [{ type: 'json:array', typedoc: 'Array' }];
7
7
  if (!ctx.depth || ctx.depth <= 0) {
8
- ctx.ops.set(node.expectation[0], 'items', expectation(checker, ctx));
8
+ node.expectation[0].items = expectation(checker, ctx);
9
9
  }
10
10
  if (!JsonArrayNode.is(node)) {
11
11
  ctx.err.report(localize('expected', localize('array')), node);
@@ -3,7 +3,7 @@ import { JsonNumberNode } from '../../node/index.js';
3
3
  const number = (type) => (min, max) => {
4
4
  return (node, ctx) => {
5
5
  const typedoc = 'Number' + (min === undefined && max === undefined ? '' : `(${min ?? '-∞'}, ${max ?? '+∞'})`);
6
- ctx.ops.set(node, 'expectation', [{ type: 'json:number', typedoc }]);
6
+ node.expectation = [{ type: 'json:number', typedoc }];
7
7
  if (!JsonNumberNode.is(node) || (type === 'integer' && !Number.isInteger(node.value))) {
8
8
  ctx.err.report(localize('expected', localize(type)), node);
9
9
  }
@@ -7,22 +7,22 @@ function isComplex(checker) {
7
7
  }
8
8
  export function object(keys, values, options = {}) {
9
9
  return (node, ctx) => {
10
- ctx.ops.set(node, 'expectation', [{ type: 'json:object', typedoc: 'Object' }]);
10
+ node.expectation = [{ type: 'json:object', typedoc: 'Object' }];
11
11
  if (!ctx.depth || ctx.depth <= 0) {
12
12
  if (Array.isArray(keys) && values) {
13
13
  const fields = keys.map(key => [key, values(key, ctx)]).filter(([_, v]) => v !== undefined);
14
- ctx.ops.set(node.expectation[0], 'fields', fields.map(([key, prop]) => {
14
+ node.expectation[0].fields = fields.map(([key, prop]) => {
15
15
  return {
16
16
  key,
17
17
  value: expectation(isComplex(prop) ? prop.checker : prop, ctx),
18
18
  ...isComplex(prop) && (prop.opt || prop.deprecated) ? { opt: true } : {},
19
19
  ...isComplex(prop) && prop.deprecated ? { deprecated: true } : {},
20
20
  };
21
- }));
21
+ });
22
22
  }
23
23
  else if (typeof keys === 'function' && values) {
24
- ctx.ops.set(node.expectation[0], 'keys', expectation(keys, ctx)
25
- ?.filter(JsonStringExpectation.is));
24
+ node.expectation[0].keys = expectation(keys, ctx)
25
+ ?.filter(JsonStringExpectation.is);
26
26
  }
27
27
  }
28
28
  if (!JsonObjectNode.is(node)) {
@@ -1,4 +1,4 @@
1
- import type { AllCategory, AstNode, Checker, Parser, ResourceLocationCategory, TaggableResourceLocationCategory } from '@spyglassmc/core';
1
+ import type { AllCategory, AstNode, Parser, ResourceLocationCategory, SyncChecker, TaggableResourceLocationCategory } from '@spyglassmc/core';
2
2
  import { Lazy } from '@spyglassmc/core';
3
3
  import type { JsonExpectation } from '../../node/index.js';
4
4
  import { JsonStringNode } from '../../node/index.js';
@@ -6,7 +6,7 @@ import type { JsonChecker } from '../JsonChecker.js';
6
6
  export declare function resource(id: TaggableResourceLocationCategory, allowTag?: boolean): JsonChecker;
7
7
  export declare function resource(id: ResourceLocationCategory | string[], allowTag?: false): JsonChecker;
8
8
  export declare function literal(value: AllCategory | readonly string[]): JsonChecker;
9
- export declare function string<T extends AstNode>(name: string | readonly string[] | undefined, parser: Lazy<Parser<T>>, checker?: Lazy<Checker<T>>, expectation?: Partial<JsonExpectation>): JsonChecker;
10
- export declare function string(name?: string | readonly string[], parser?: undefined, checker?: Lazy<Checker<JsonStringNode>>, expectation?: Partial<JsonExpectation>): JsonChecker;
9
+ export declare function string<T extends AstNode>(name: string | readonly string[] | undefined, parser: Lazy<Parser<T>>, checker?: Lazy<SyncChecker<T>>, expectation?: Partial<JsonExpectation>): JsonChecker;
10
+ export declare function string(name?: string | readonly string[], parser?: undefined, checker?: Lazy<SyncChecker<JsonStringNode>>, expectation?: Partial<JsonExpectation>): JsonChecker;
11
11
  export declare const simpleString: JsonChecker;
12
12
  //# sourceMappingURL=string.d.ts.map
@@ -14,7 +14,7 @@ export function literal(value) {
14
14
  }
15
15
  export function string(name, parser, checker, expectation) {
16
16
  return (node, ctx) => {
17
- ctx.ops.set(node, 'expectation', [{ type: 'json:string', typedoc: typedoc(name), ...expectation }]);
17
+ node.expectation = [{ type: 'json:string', typedoc: typedoc(name), ...expectation }];
18
18
  if (!JsonStringNode.is(node)) {
19
19
  ctx.err.report(localize('expected', localize('string')), node);
20
20
  }
@@ -1,4 +1,4 @@
1
- import { ErrorReporter, Operations, Range } from '@spyglassmc/core';
1
+ import { ErrorReporter, Range, StateProxy } from '@spyglassmc/core';
2
2
  import { arrayToMessage, localize } from '@spyglassmc/locales';
3
3
  export function ref(checker) {
4
4
  return (node, ctx) => {
@@ -15,12 +15,11 @@ export function attempt(checker, node, ctx) {
15
15
  const tempCtx = {
16
16
  ...ctx,
17
17
  err: new ErrorReporter(),
18
- ops: new Operations(),
19
18
  symbols: ctx.symbols.clone(),
20
19
  };
21
20
  checker(node, tempCtx);
22
21
  const tempExpectation = node.expectation;
23
- tempCtx.ops.undo();
22
+ StateProxy.undoChanges(node);
24
23
  const totalErrorSpan = tempCtx.err.errors
25
24
  .map(e => e.range.end - e.range.start)
26
25
  .reduce((a, b) => a + b, 0);
@@ -30,7 +29,7 @@ export function attempt(checker, node, ctx) {
30
29
  expectation: tempExpectation,
31
30
  updateNodeAndCtx: () => {
32
31
  ctx.err.absorb(tempCtx.err);
33
- tempCtx.ops.redo();
32
+ StateProxy.redoChanges(node);
34
33
  tempCtx.symbols.applyDelayedEdits();
35
34
  },
36
35
  };
@@ -53,11 +52,11 @@ export function any(checkers = []) {
53
52
  else {
54
53
  sameTypeAttempts[0].updateNodeAndCtx();
55
54
  }
56
- ctx.ops.set(node, 'expectation', allExpectations);
55
+ node.expectation = allExpectations;
57
56
  };
58
57
  }
59
58
  export function expectation(checker, ctx) {
60
- const node = { type: 'json:null', range: Range.create(0) };
59
+ const node = StateProxy.create({ type: 'json:null', range: Range.create(0) });
61
60
  const tempCtx = {
62
61
  ...ctx,
63
62
  err: new ErrorReporter(),
package/lib/index.d.ts CHANGED
@@ -5,5 +5,5 @@ export * as completer from './completer/index.js';
5
5
  export * as formatter from './formatter/index.js';
6
6
  export * from './node/index.js';
7
7
  export * as parser from './parser/index.js';
8
- export declare const initialize: core.ProjectInitializer;
8
+ export declare const initialize: core.SyncProjectInitializer;
9
9
  //# sourceMappingURL=index.d.ts.map
@@ -1,4 +1,4 @@
1
- import type { ItemNode, PairNode } from '@spyglassmc/core';
1
+ import type { DeepReadonly, ItemNode, PairNode } from '@spyglassmc/core';
2
2
  import * as core from '@spyglassmc/core';
3
3
  export declare type JsonNode = JsonObjectNode | JsonArrayNode | JsonStringNode | JsonNumberNode | JsonBooleanNode | JsonNullNode;
4
4
  export declare type JsonRelatedNode = JsonNode | JsonPairNode | JsonItemNode;
@@ -11,9 +11,9 @@ interface JsonBaseAstNode {
11
11
  }
12
12
  export declare type JsonExpectation = JsonObjectExpectation | JsonArrayExpectation | JsonStringExpectation | JsonNumberExpectation | JsonBooleanExpectation;
13
13
  export declare namespace JsonExpectation {
14
- function isArray(e: JsonExpectation): e is JsonArrayExpectation;
15
- function isObject(e: JsonExpectation): e is JsonObjectExpectation;
16
- function isString(e: JsonExpectation): e is JsonStringExpectation;
14
+ function isArray(e: DeepReadonly<JsonExpectation>): e is DeepReadonly<JsonArrayExpectation>;
15
+ function isObject(e: DeepReadonly<JsonExpectation>): e is DeepReadonly<JsonObjectExpectation>;
16
+ function isString(e: DeepReadonly<JsonExpectation>): e is DeepReadonly<JsonStringExpectation>;
17
17
  }
18
18
  interface JsonBaseExpectation {
19
19
  typedoc: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spyglassmc/json",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/SpyglassMC/Spyglass/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@spyglassmc/core": "0.2.0",
29
- "@spyglassmc/locales": "0.2.0"
28
+ "@spyglassmc/core": "0.3.0",
29
+ "@spyglassmc/locales": "0.3.0"
30
30
  }
31
31
  }