@spyglassmc/json 0.1.0 → 0.2.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.
- package/lib/checker/JsonChecker.d.ts +1 -1
- package/lib/checker/JsonChecker.js +1 -2
- package/lib/checker/index.d.ts +2 -2
- package/lib/checker/index.js +2 -14
- package/lib/checker/primitives/boolean.d.ts +2 -2
- package/lib/checker/primitives/boolean.js +5 -9
- package/lib/checker/primitives/index.d.ts +6 -6
- package/lib/checker/primitives/index.js +6 -18
- package/lib/checker/primitives/list.d.ts +2 -2
- package/lib/checker/primitives/list.js +11 -16
- package/lib/checker/primitives/number.d.ts +1 -1
- package/lib/checker/primitives/number.js +11 -14
- package/lib/checker/primitives/object.d.ts +2 -2
- package/lib/checker/primitives/object.js +33 -47
- package/lib/checker/primitives/string.d.ts +3 -3
- package/lib/checker/primitives/string.js +15 -40
- package/lib/checker/primitives/util.d.ts +2 -2
- package/lib/checker/primitives/util.js +13 -21
- package/lib/colorizer/index.d.ts +1 -1
- package/lib/colorizer/index.js +12 -38
- package/lib/completer/index.d.ts +1 -1
- package/lib/completer/index.js +27 -53
- package/lib/formatter/index.d.ts +1 -1
- package/lib/formatter/index.js +5 -28
- package/lib/index.d.ts +6 -6
- package/lib/index.js +11 -37
- package/lib/node/JsonAstNode.js +25 -47
- package/lib/node/index.d.ts +1 -1
- package/lib/node/index.js +1 -13
- package/lib/parser/array.d.ts +1 -1
- package/lib/parser/array.js +4 -27
- package/lib/parser/boolean.d.ts +1 -1
- package/lib/parser/boolean.js +5 -28
- package/lib/parser/entry.d.ts +1 -1
- package/lib/parser/entry.js +15 -38
- package/lib/parser/index.d.ts +5 -5
- package/lib/parser/index.js +5 -17
- package/lib/parser/null.d.ts +1 -1
- package/lib/parser/null.js +4 -27
- package/lib/parser/number.d.ts +1 -1
- package/lib/parser/number.js +2 -25
- package/lib/parser/object.d.ts +1 -1
- package/lib/parser/object.js +5 -28
- package/lib/parser/string.d.ts +1 -1
- package/lib/parser/string.js +4 -27
- package/package.json +4 -3
- package/lib/util.d.ts +0 -1
- package/lib/util.js +0 -2
package/lib/checker/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './JsonChecker';
|
|
2
|
-
export * from './primitives';
|
|
1
|
+
export * from './JsonChecker.js';
|
|
2
|
+
export * from './primitives/index.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/checker/index.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./JsonChecker"), exports);
|
|
14
|
-
__exportStar(require("./primitives"), exports);
|
|
1
|
+
export * from './JsonChecker.js';
|
|
2
|
+
export * from './primitives/index.js';
|
|
15
3
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { JsonNode } from '../../node';
|
|
2
|
-
import type { JsonCheckerContext } from '../JsonChecker';
|
|
1
|
+
import type { JsonNode } from '../../node/index.js';
|
|
2
|
+
import type { JsonCheckerContext } from '../JsonChecker.js';
|
|
3
3
|
export declare function boolean(node: JsonNode, ctx: JsonCheckerContext): void;
|
|
4
4
|
//# sourceMappingURL=boolean.d.ts.map
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const locales_1 = require("@spyglassmc/locales");
|
|
5
|
-
const node_1 = require("../../node");
|
|
6
|
-
function boolean(node, ctx) {
|
|
1
|
+
import { localize } from '@spyglassmc/locales';
|
|
2
|
+
import { JsonBooleanNode } from '../../node/index.js';
|
|
3
|
+
export function boolean(node, ctx) {
|
|
7
4
|
ctx.ops.set(node, 'expectation', [{ type: 'json:boolean', typedoc: 'Boolean' }]);
|
|
8
|
-
if (!
|
|
9
|
-
ctx.err.report(
|
|
5
|
+
if (!JsonBooleanNode.is(node)) {
|
|
6
|
+
ctx.err.report(localize('expected', localize('boolean')), node);
|
|
10
7
|
}
|
|
11
8
|
}
|
|
12
|
-
exports.boolean = boolean;
|
|
13
9
|
//# sourceMappingURL=boolean.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './boolean';
|
|
2
|
-
export * from './list';
|
|
3
|
-
export * from './number';
|
|
4
|
-
export * from './object';
|
|
5
|
-
export * from './string';
|
|
6
|
-
export * from './util';
|
|
1
|
+
export * from './boolean.js';
|
|
2
|
+
export * from './list.js';
|
|
3
|
+
export * from './number.js';
|
|
4
|
+
export * from './object.js';
|
|
5
|
+
export * from './string.js';
|
|
6
|
+
export * from './util.js';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./boolean"), exports);
|
|
14
|
-
__exportStar(require("./list"), exports);
|
|
15
|
-
__exportStar(require("./number"), exports);
|
|
16
|
-
__exportStar(require("./object"), exports);
|
|
17
|
-
__exportStar(require("./string"), exports);
|
|
18
|
-
__exportStar(require("./util"), exports);
|
|
1
|
+
export * from './boolean.js';
|
|
2
|
+
export * from './list.js';
|
|
3
|
+
export * from './number.js';
|
|
4
|
+
export * from './object.js';
|
|
5
|
+
export * from './string.js';
|
|
6
|
+
export * from './util.js';
|
|
19
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonNode } from '../../node';
|
|
2
|
-
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker';
|
|
1
|
+
import type { JsonNode } from '../../node/index.js';
|
|
2
|
+
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker.js';
|
|
3
3
|
export declare function listOf(checker: JsonChecker): JsonChecker;
|
|
4
4
|
declare type UniqueListOptions = {
|
|
5
5
|
items?: (node: JsonNode) => [string | undefined, JsonNode];
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const node_1 = require("../../node");
|
|
6
|
-
const util_1 = require("./util");
|
|
7
|
-
function listOf(checker) {
|
|
1
|
+
import { localize } from '@spyglassmc/locales';
|
|
2
|
+
import { JsonArrayNode, JsonStringNode } from '../../node/index.js';
|
|
3
|
+
import { expectation } from './util.js';
|
|
4
|
+
export function listOf(checker) {
|
|
8
5
|
return (node, ctx) => {
|
|
9
6
|
ctx.ops.set(node, 'expectation', [{ type: 'json:array', typedoc: 'Array' }]);
|
|
10
7
|
if (!ctx.depth || ctx.depth <= 0) {
|
|
11
|
-
ctx.ops.set(node.expectation[0], 'items',
|
|
8
|
+
ctx.ops.set(node.expectation[0], 'items', expectation(checker, ctx));
|
|
12
9
|
}
|
|
13
|
-
if (!
|
|
14
|
-
ctx.err.report(
|
|
10
|
+
if (!JsonArrayNode.is(node)) {
|
|
11
|
+
ctx.err.report(localize('expected', localize('array')), node);
|
|
15
12
|
}
|
|
16
13
|
else {
|
|
17
14
|
node.children.filter(e => e.value)
|
|
@@ -19,13 +16,12 @@ function listOf(checker) {
|
|
|
19
16
|
}
|
|
20
17
|
};
|
|
21
18
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const reporter = options.report ?? ((node, ctx) => ctx.err.report((0, locales_1.localize)('json.checker.item.duplicate'), node, 2 /* Warning */));
|
|
19
|
+
export function uniqueListOf(checker, options = {}) {
|
|
20
|
+
const getItem = (options.items ?? (node => [JsonStringNode.is(node) ? node.value : undefined, node]));
|
|
21
|
+
const reporter = options.report ?? ((node, ctx) => ctx.err.report(localize('json.checker.item.duplicate'), node, 2 /* ErrorSeverity.Warning */));
|
|
26
22
|
return (node, ctx) => {
|
|
27
23
|
listOf(checker)(node, ctx);
|
|
28
|
-
if (
|
|
24
|
+
if (JsonArrayNode.is(node)) {
|
|
29
25
|
const items = new Map();
|
|
30
26
|
const duplicates = new Set();
|
|
31
27
|
node.children.forEach(c => {
|
|
@@ -45,5 +41,4 @@ function uniqueListOf(checker, options = {}) {
|
|
|
45
41
|
}
|
|
46
42
|
};
|
|
47
43
|
}
|
|
48
|
-
exports.uniqueListOf = uniqueListOf;
|
|
49
44
|
//# sourceMappingURL=list.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { JsonChecker } from '../JsonChecker';
|
|
1
|
+
import type { JsonChecker } from '../JsonChecker.js';
|
|
2
2
|
export declare const int: JsonChecker;
|
|
3
3
|
export declare const float: JsonChecker;
|
|
4
4
|
export declare const intRange: (min: number | undefined, max: number | undefined) => JsonChecker;
|
|
@@ -1,28 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.floatRange = exports.intRange = exports.float = exports.int = void 0;
|
|
4
|
-
const locales_1 = require("@spyglassmc/locales");
|
|
5
|
-
const node_1 = require("../../node");
|
|
1
|
+
import { localize } from '@spyglassmc/locales';
|
|
2
|
+
import { JsonNumberNode } from '../../node/index.js';
|
|
6
3
|
const number = (type) => (min, max) => {
|
|
7
4
|
return (node, ctx) => {
|
|
8
5
|
const typedoc = 'Number' + (min === undefined && max === undefined ? '' : `(${min ?? '-∞'}, ${max ?? '+∞'})`);
|
|
9
6
|
ctx.ops.set(node, 'expectation', [{ type: 'json:number', typedoc }]);
|
|
10
|
-
if (!
|
|
11
|
-
ctx.err.report(
|
|
7
|
+
if (!JsonNumberNode.is(node) || (type === 'integer' && !Number.isInteger(node.value))) {
|
|
8
|
+
ctx.err.report(localize('expected', localize(type)), node);
|
|
12
9
|
}
|
|
13
10
|
else if (min !== undefined && max !== undefined && (node.value < min || node.value > max)) {
|
|
14
|
-
ctx.err.report(
|
|
11
|
+
ctx.err.report(localize('expected', localize('number.between', min, max)), node);
|
|
15
12
|
}
|
|
16
13
|
else if (min !== undefined && node.value < min) {
|
|
17
|
-
ctx.err.report(
|
|
14
|
+
ctx.err.report(localize('expected', localize('number.>=', min)), node);
|
|
18
15
|
}
|
|
19
16
|
else if (max !== undefined && node.value > max) {
|
|
20
|
-
ctx.err.report(
|
|
17
|
+
ctx.err.report(localize('expected', localize('number.<=', max)), node);
|
|
21
18
|
}
|
|
22
19
|
};
|
|
23
20
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
export const int = number('integer')(undefined, undefined);
|
|
22
|
+
export const float = number('float')(undefined, undefined);
|
|
23
|
+
export const intRange = number('integer');
|
|
24
|
+
export const floatRange = number('float');
|
|
28
25
|
//# sourceMappingURL=number.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PairNode } from '@spyglassmc/core';
|
|
2
|
-
import type { JsonNode, JsonStringNode } from '../../node';
|
|
3
|
-
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker';
|
|
2
|
+
import type { JsonNode, JsonStringNode } from '../../node/index.js';
|
|
3
|
+
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker.js';
|
|
4
4
|
declare type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
5
5
|
[key: string]: JsonValue;
|
|
6
6
|
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const locales_1 = require("@spyglassmc/locales");
|
|
6
|
-
const node_1 = require("../../node");
|
|
7
|
-
const util_1 = require("./util");
|
|
1
|
+
import { Range, ResourceLocation } from '@spyglassmc/core';
|
|
2
|
+
import { localeQuote, localize } from '@spyglassmc/locales';
|
|
3
|
+
import { JsonObjectNode, JsonStringExpectation } from '../../node/index.js';
|
|
4
|
+
import { any, expectation } from './util.js';
|
|
8
5
|
function isComplex(checker) {
|
|
9
6
|
return checker?.checker !== undefined;
|
|
10
7
|
}
|
|
11
|
-
function object(keys, values, options = {}) {
|
|
8
|
+
export function object(keys, values, options = {}) {
|
|
12
9
|
return (node, ctx) => {
|
|
13
10
|
ctx.ops.set(node, 'expectation', [{ type: 'json:object', typedoc: 'Object' }]);
|
|
14
11
|
if (!ctx.depth || ctx.depth <= 0) {
|
|
@@ -17,19 +14,19 @@ function object(keys, values, options = {}) {
|
|
|
17
14
|
ctx.ops.set(node.expectation[0], 'fields', fields.map(([key, prop]) => {
|
|
18
15
|
return {
|
|
19
16
|
key,
|
|
20
|
-
value:
|
|
17
|
+
value: expectation(isComplex(prop) ? prop.checker : prop, ctx),
|
|
21
18
|
...isComplex(prop) && (prop.opt || prop.deprecated) ? { opt: true } : {},
|
|
22
19
|
...isComplex(prop) && prop.deprecated ? { deprecated: true } : {},
|
|
23
20
|
};
|
|
24
21
|
}));
|
|
25
22
|
}
|
|
26
23
|
else if (typeof keys === 'function' && values) {
|
|
27
|
-
ctx.ops.set(node.expectation[0], 'keys',
|
|
28
|
-
?.filter(
|
|
24
|
+
ctx.ops.set(node.expectation[0], 'keys', expectation(keys, ctx)
|
|
25
|
+
?.filter(JsonStringExpectation.is));
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
|
-
if (!
|
|
32
|
-
ctx.err.report(
|
|
28
|
+
if (!JsonObjectNode.is(node)) {
|
|
29
|
+
ctx.err.report(localize('expected', localize('object')), node);
|
|
33
30
|
}
|
|
34
31
|
else if (Array.isArray(keys) && values) {
|
|
35
32
|
const givenKeys = node.children.map(n => n.key?.value);
|
|
@@ -39,7 +36,7 @@ function object(keys, values, options = {}) {
|
|
|
39
36
|
return;
|
|
40
37
|
}
|
|
41
38
|
if (!givenKeys.includes(k)) {
|
|
42
|
-
ctx.err.report(
|
|
39
|
+
ctx.err.report(localize('json.checker.property.missing', localeQuote(k)), Range.create(node.range.start, node.range.start + 1));
|
|
43
40
|
}
|
|
44
41
|
});
|
|
45
42
|
node.children.filter(p => p.key).forEach(prop => {
|
|
@@ -47,16 +44,16 @@ function object(keys, values, options = {}) {
|
|
|
47
44
|
const value = values(key, ctx);
|
|
48
45
|
if (!value || !keys.includes(key)) {
|
|
49
46
|
if (!options.allowUnknownProperties) {
|
|
50
|
-
ctx.err.report(
|
|
47
|
+
ctx.err.report(localize('json.checker.property.unknown', localeQuote(key)), prop.key, 2 /* ErrorSeverity.Warning */);
|
|
51
48
|
}
|
|
52
49
|
return;
|
|
53
50
|
}
|
|
54
51
|
if (isComplex(value) && value.deprecated) {
|
|
55
|
-
ctx.err.report(
|
|
52
|
+
ctx.err.report(localize('json.checker.property.deprecated', localeQuote(key)), prop.key, 0 /* ErrorSeverity.Hint */, { deprecated: true });
|
|
56
53
|
}
|
|
57
54
|
const context = ctx.context + (isComplex(value) && value.context ? `.${value.context}` : '');
|
|
58
|
-
const doc =
|
|
59
|
-
const propNode = prop.value !== undefined ? prop.value : { type: 'json:null', range:
|
|
55
|
+
const doc = localize(`json.doc.${context}`);
|
|
56
|
+
const propNode = prop.value !== undefined ? prop.value : { type: 'json:null', range: Range.create(0) };
|
|
60
57
|
const checker = isComplex(value) ? value.checker : value;
|
|
61
58
|
try {
|
|
62
59
|
checker(propNode, { ...ctx, context: `${context}.${key}` });
|
|
@@ -84,31 +81,27 @@ function object(keys, values, options = {}) {
|
|
|
84
81
|
}
|
|
85
82
|
};
|
|
86
83
|
}
|
|
87
|
-
|
|
88
|
-
function record(properties, options) {
|
|
84
|
+
export function record(properties, options) {
|
|
89
85
|
return object(Object.keys(properties), (key) => properties[key], options);
|
|
90
86
|
}
|
|
91
|
-
|
|
92
|
-
function opt(checker, defaultValue) {
|
|
87
|
+
export function opt(checker, defaultValue) {
|
|
93
88
|
if (checker === undefined)
|
|
94
89
|
return undefined;
|
|
95
90
|
return isComplex(checker)
|
|
96
91
|
? { ...checker, opt: true, def: defaultValue }
|
|
97
92
|
: { checker, opt: true, def: defaultValue };
|
|
98
93
|
}
|
|
99
|
-
|
|
100
|
-
function deprecate(checker) {
|
|
94
|
+
export function deprecate(checker) {
|
|
101
95
|
if (checker === undefined)
|
|
102
96
|
return undefined;
|
|
103
97
|
return isComplex(checker)
|
|
104
98
|
? { ...checker, deprecated: true }
|
|
105
99
|
: { checker, deprecated: true };
|
|
106
100
|
}
|
|
107
|
-
|
|
108
|
-
function dispatch(arg1, arg2) {
|
|
101
|
+
export function dispatch(arg1, arg2) {
|
|
109
102
|
return (node, ctx) => {
|
|
110
|
-
if (!
|
|
111
|
-
ctx.err.report(
|
|
103
|
+
if (!JsonObjectNode.is(node)) {
|
|
104
|
+
ctx.err.report(localize('expected', localize('object')), node);
|
|
112
105
|
}
|
|
113
106
|
else if (arg2) {
|
|
114
107
|
const dispatcherIndex = node.children.findIndex(p => p.key?.value === arg1);
|
|
@@ -121,12 +114,11 @@ function dispatch(arg1, arg2) {
|
|
|
121
114
|
}
|
|
122
115
|
};
|
|
123
116
|
}
|
|
124
|
-
|
|
125
|
-
function pick(value, cases) {
|
|
117
|
+
export function pick(value, cases) {
|
|
126
118
|
if (value === undefined) {
|
|
127
119
|
return {};
|
|
128
120
|
}
|
|
129
|
-
const properties = cases[
|
|
121
|
+
const properties = cases[ResourceLocation.shorten(value)];
|
|
130
122
|
if (properties === undefined) {
|
|
131
123
|
return {};
|
|
132
124
|
}
|
|
@@ -138,53 +130,47 @@ function pick(value, cases) {
|
|
|
138
130
|
checker: isComplex(p) ? p.checker : p,
|
|
139
131
|
opt: isComplex(p) ? p.opt : undefined,
|
|
140
132
|
deprecated: isComplex(p) ? p.deprecated : undefined,
|
|
141
|
-
context:
|
|
133
|
+
context: ResourceLocation.shorten(value),
|
|
142
134
|
};
|
|
143
135
|
});
|
|
144
136
|
return properties;
|
|
145
137
|
}
|
|
146
|
-
|
|
147
|
-
function when(value, values, properties, notProperties = {}) {
|
|
138
|
+
export function when(value, values, properties, notProperties = {}) {
|
|
148
139
|
if (value === undefined) {
|
|
149
140
|
return {};
|
|
150
141
|
}
|
|
151
|
-
if (!values.includes(
|
|
142
|
+
if (!values.includes(ResourceLocation.shorten(value))) {
|
|
152
143
|
return notProperties;
|
|
153
144
|
}
|
|
154
145
|
return properties;
|
|
155
146
|
}
|
|
156
|
-
|
|
157
|
-
function extract(value, children) {
|
|
147
|
+
export function extract(value, children) {
|
|
158
148
|
const node = children?.find(p => p.key?.value === value);
|
|
159
149
|
return node?.value?.type === 'json:string' ? node.value.value : undefined;
|
|
160
150
|
}
|
|
161
|
-
|
|
162
|
-
function extractNested(wrap, value, children) {
|
|
151
|
+
export function extractNested(wrap, value, children) {
|
|
163
152
|
const wrapper = children?.find(p => p.key?.value === wrap);
|
|
164
153
|
if (wrapper?.value?.type !== 'json:object')
|
|
165
154
|
return undefined;
|
|
166
155
|
const node = wrapper.children?.find(p => p.key?.value === value);
|
|
167
156
|
return node?.type === 'json:string' ? node.value : undefined;
|
|
168
157
|
}
|
|
169
|
-
|
|
170
|
-
function extractStringArray(value, children) {
|
|
158
|
+
export function extractStringArray(value, children) {
|
|
171
159
|
const node = children?.find(p => p.key?.value === value);
|
|
172
160
|
return node?.value?.type === 'json:array' && node.value.children?.every((n) => n.value?.type === 'json:string')
|
|
173
161
|
? node.value.children.map(n => n.value.value)
|
|
174
162
|
: undefined;
|
|
175
163
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const givenKeys = new Set(node_1.JsonObjectNode.is(node)
|
|
164
|
+
export function having(node, ctx, cases) {
|
|
165
|
+
const givenKeys = new Set(JsonObjectNode.is(node)
|
|
179
166
|
? node.children.map(n => n.key?.value) : []);
|
|
180
167
|
const key = Object.keys(cases).find(c => givenKeys.has(c));
|
|
181
168
|
if (key === undefined) {
|
|
182
|
-
ctx.err.report(
|
|
169
|
+
ctx.err.report(localize('json.checker.property.missing', Object.keys(cases)), Range.create(node.range.start, node.range.start + 1));
|
|
183
170
|
return Object.fromEntries(Object.entries(cases)
|
|
184
|
-
.map(([k, v]) => [k, opt(typeof v === 'function' ?
|
|
171
|
+
.map(([k, v]) => [k, opt(typeof v === 'function' ? any() : v[k] ?? any())]));
|
|
185
172
|
}
|
|
186
173
|
const c = cases[key];
|
|
187
174
|
return typeof c === 'function' ? c() : c;
|
|
188
175
|
}
|
|
189
|
-
exports.having = having;
|
|
190
176
|
//# sourceMappingURL=object.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { AllCategory, AstNode, Checker, Parser, ResourceLocationCategory, TaggableResourceLocationCategory } from '@spyglassmc/core';
|
|
2
2
|
import { Lazy } from '@spyglassmc/core';
|
|
3
|
-
import type { JsonExpectation } from '../../node';
|
|
4
|
-
import { JsonStringNode } from '../../node';
|
|
5
|
-
import type { JsonChecker } from '../JsonChecker';
|
|
3
|
+
import type { JsonExpectation } from '../../node/index.js';
|
|
4
|
+
import { JsonStringNode } from '../../node/index.js';
|
|
5
|
+
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;
|
|
@@ -1,63 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.simpleString = exports.string = exports.literal = exports.resource = void 0;
|
|
23
|
-
const core = __importStar(require("@spyglassmc/core"));
|
|
24
|
-
const core_1 = require("@spyglassmc/core");
|
|
25
|
-
const locales_1 = require("@spyglassmc/locales");
|
|
26
|
-
const node_1 = require("../../node");
|
|
27
|
-
function resource(id, allowTag = false) {
|
|
1
|
+
import * as core from '@spyglassmc/core';
|
|
2
|
+
import { Failure, Lazy, ResourceLocation } from '@spyglassmc/core';
|
|
3
|
+
import { localize } from '@spyglassmc/locales';
|
|
4
|
+
import { JsonStringNode } from '../../node/index.js';
|
|
5
|
+
export function resource(id, allowTag = false) {
|
|
28
6
|
return string(id, core.resourceLocation(typeof id === 'string'
|
|
29
7
|
? { category: id, allowTag }
|
|
30
|
-
: { pool: id.map(
|
|
8
|
+
: { pool: id.map(ResourceLocation.lengthen) }), core.checker.resourceLocation);
|
|
31
9
|
}
|
|
32
|
-
|
|
33
|
-
function literal(value) {
|
|
10
|
+
export function literal(value) {
|
|
34
11
|
return typeof value === 'string'
|
|
35
12
|
? string(value, core.symbol(value))
|
|
36
13
|
: string(value, core.literal(...value));
|
|
37
14
|
}
|
|
38
|
-
|
|
39
|
-
function string(name, parser, checker, expectation) {
|
|
15
|
+
export function string(name, parser, checker, expectation) {
|
|
40
16
|
return (node, ctx) => {
|
|
41
17
|
ctx.ops.set(node, 'expectation', [{ type: 'json:string', typedoc: typedoc(name), ...expectation }]);
|
|
42
|
-
if (!
|
|
43
|
-
ctx.err.report(
|
|
18
|
+
if (!JsonStringNode.is(node)) {
|
|
19
|
+
ctx.err.report(localize('expected', localize('string')), node);
|
|
44
20
|
}
|
|
45
21
|
else if (parser) {
|
|
46
|
-
const result = core.parseStringValue(
|
|
47
|
-
if (result !==
|
|
22
|
+
const result = core.parseStringValue(Lazy.resolve(parser), node.value, node.valueMap, ctx);
|
|
23
|
+
if (result !== Failure) {
|
|
48
24
|
node.children = [result];
|
|
49
25
|
result.parent = node;
|
|
50
26
|
if (checker) {
|
|
51
|
-
|
|
27
|
+
Lazy.resolve(checker)(result, ctx);
|
|
52
28
|
}
|
|
53
29
|
}
|
|
54
30
|
}
|
|
55
31
|
else if (checker) {
|
|
56
|
-
|
|
32
|
+
Lazy.resolve(checker)(node, ctx);
|
|
57
33
|
}
|
|
58
34
|
};
|
|
59
35
|
}
|
|
60
|
-
exports.string = string;
|
|
61
36
|
function typedoc(id) {
|
|
62
37
|
if (!id) {
|
|
63
38
|
return 'String';
|
|
@@ -68,5 +43,5 @@ function typedoc(id) {
|
|
|
68
43
|
return id.slice(0, 10).map(e => `"${e}"`).join(' | ')
|
|
69
44
|
+ (id.length > 10 ? ' | ...' : '');
|
|
70
45
|
}
|
|
71
|
-
|
|
46
|
+
export const simpleString = string();
|
|
72
47
|
//# sourceMappingURL=string.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ErrorSeverity } from '@spyglassmc/core';
|
|
2
|
-
import type { JsonExpectation, JsonNode } from '../../node';
|
|
3
|
-
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker';
|
|
2
|
+
import type { JsonExpectation, JsonNode } from '../../node/index.js';
|
|
3
|
+
import type { JsonChecker, JsonCheckerContext } from '../JsonChecker.js';
|
|
4
4
|
export declare function ref(checker: () => JsonChecker): JsonChecker;
|
|
5
5
|
export declare function as(context: string, checker: JsonChecker): JsonChecker;
|
|
6
6
|
export declare type AttemptResult = {
|
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const core_1 = require("@spyglassmc/core");
|
|
5
|
-
const locales_1 = require("@spyglassmc/locales");
|
|
6
|
-
function ref(checker) {
|
|
1
|
+
import { ErrorReporter, Operations, Range } from '@spyglassmc/core';
|
|
2
|
+
import { arrayToMessage, localize } from '@spyglassmc/locales';
|
|
3
|
+
export function ref(checker) {
|
|
7
4
|
return (node, ctx) => {
|
|
8
5
|
return checker()(node, ctx);
|
|
9
6
|
};
|
|
10
7
|
}
|
|
11
|
-
|
|
12
|
-
function as(context, checker) {
|
|
8
|
+
export function as(context, checker) {
|
|
13
9
|
return (node, ctx) => {
|
|
14
10
|
checker(node, { ...ctx, context });
|
|
15
11
|
};
|
|
16
12
|
}
|
|
17
|
-
|
|
18
|
-
function attempt(checker, node, ctx) {
|
|
13
|
+
export function attempt(checker, node, ctx) {
|
|
19
14
|
// TODO: The code below is mostly copied from core with some changes to support `expectation`. Could be refactored... I guess.
|
|
20
15
|
const tempCtx = {
|
|
21
16
|
...ctx,
|
|
22
|
-
err: new
|
|
23
|
-
ops: new
|
|
17
|
+
err: new ErrorReporter(),
|
|
18
|
+
ops: new Operations(),
|
|
24
19
|
symbols: ctx.symbols.clone(),
|
|
25
20
|
};
|
|
26
21
|
checker(node, tempCtx);
|
|
@@ -40,8 +35,7 @@ function attempt(checker, node, ctx) {
|
|
|
40
35
|
},
|
|
41
36
|
};
|
|
42
37
|
}
|
|
43
|
-
|
|
44
|
-
function any(checkers = []) {
|
|
38
|
+
export function any(checkers = []) {
|
|
45
39
|
return (node, ctx) => {
|
|
46
40
|
if (checkers.length === 0) {
|
|
47
41
|
return;
|
|
@@ -53,8 +47,8 @@ function any(checkers = []) {
|
|
|
53
47
|
.filter(a => a.expectation?.map(e => e.type).includes(node.type));
|
|
54
48
|
const allExpectations = attempts.filter(a => a.expectation).flatMap(a => a.expectation);
|
|
55
49
|
if (sameTypeAttempts.length === 0) {
|
|
56
|
-
const allowedTypes = allExpectations.map(e =>
|
|
57
|
-
ctx.err.report(
|
|
50
|
+
const allowedTypes = allExpectations.map(e => localize(e.type.slice(5)));
|
|
51
|
+
ctx.err.report(localize('expected', arrayToMessage(allowedTypes, false)), node);
|
|
58
52
|
}
|
|
59
53
|
else {
|
|
60
54
|
sameTypeAttempts[0].updateNodeAndCtx();
|
|
@@ -62,16 +56,14 @@ function any(checkers = []) {
|
|
|
62
56
|
ctx.ops.set(node, 'expectation', allExpectations);
|
|
63
57
|
};
|
|
64
58
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const node = { type: 'json:null', range: core_1.Range.create(0) };
|
|
59
|
+
export function expectation(checker, ctx) {
|
|
60
|
+
const node = { type: 'json:null', range: Range.create(0) };
|
|
68
61
|
const tempCtx = {
|
|
69
62
|
...ctx,
|
|
70
|
-
err: new
|
|
63
|
+
err: new ErrorReporter(),
|
|
71
64
|
depth: (ctx.depth ?? 0) + 1,
|
|
72
65
|
};
|
|
73
66
|
checker(node, tempCtx);
|
|
74
67
|
return node.expectation;
|
|
75
68
|
}
|
|
76
|
-
exports.expectation = expectation;
|
|
77
69
|
//# sourceMappingURL=util.js.map
|
package/lib/colorizer/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Colorizer, MetaRegistry } from '@spyglassmc/core';
|
|
2
|
-
import type { JsonBooleanNode, JsonNullNode, JsonObjectNode } from '../node';
|
|
2
|
+
import type { JsonBooleanNode, JsonNullNode, JsonObjectNode } from '../node/index.js';
|
|
3
3
|
export declare const boolean: Colorizer<JsonBooleanNode>;
|
|
4
4
|
export declare const null_: Colorizer<JsonNullNode>;
|
|
5
5
|
export declare const object: Colorizer<JsonObjectNode>;
|