@puzzmo/cli 1.0.62 → 1.0.64
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,3 +1,9 @@
|
|
|
1
1
|
import type { PuzzmoFile } from "./api.js";
|
|
2
2
|
/** Non-fatal checks on a schema-valid puzzmo.json. Returns human-readable warning messages. */
|
|
3
3
|
export declare const lintPuzzmoFile: (puzzmoFile: PuzzmoFile) => string[];
|
|
4
|
+
/**
|
|
5
|
+
* `userAggregateStats` expressions see deeds nested under `deeds.` while every other section sees them
|
|
6
|
+
* as bare identifiers. Getting it the wrong way round evaluates to `undefined` and silently stores
|
|
7
|
+
* nothing (or skips the entry entirely, for a filterExp), so flag both directions.
|
|
8
|
+
*/
|
|
9
|
+
export declare const lintDeedScopeUsage: (integrations: Record<string, unknown>) => string[];
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/** Non-fatal checks on a schema-valid puzzmo.json. Returns human-readable warning messages. */
|
|
2
2
|
export const lintPuzzmoFile = (puzzmoFile) => {
|
|
3
3
|
const warnings = [];
|
|
4
|
-
const
|
|
4
|
+
const integrations = puzzmoFile.integrations ?? {};
|
|
5
|
+
const leaderboards = integrations.leaderboards;
|
|
5
6
|
if (Array.isArray(leaderboards)) {
|
|
6
7
|
for (const leaderboard of leaderboards) {
|
|
7
8
|
if (!leaderboard || typeof leaderboard !== "object")
|
|
@@ -13,6 +14,90 @@ export const lintPuzzmoFile = (puzzmoFile) => {
|
|
|
13
14
|
warnings.push(`Leaderboard "${label}" has no sortValue — you probably want one, it controls the ordering when shown alongside other leaderboards and stats.`);
|
|
14
15
|
}
|
|
15
16
|
}
|
|
17
|
+
warnings.push(...lintDeedScopeUsage(integrations));
|
|
16
18
|
return warnings;
|
|
17
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* `userAggregateStats` expressions see deeds nested under `deeds.` while every other section sees them
|
|
22
|
+
* as bare identifiers. Getting it the wrong way round evaluates to `undefined` and silently stores
|
|
23
|
+
* nothing (or skips the entry entirely, for a filterExp), so flag both directions.
|
|
24
|
+
*/
|
|
25
|
+
export const lintDeedScopeUsage = (integrations) => {
|
|
26
|
+
const warnings = [];
|
|
27
|
+
const deedNames = camelizedDeedNamesIn(integrations);
|
|
28
|
+
for (const [index, entry] of expressionEntries(integrations.userAggregateStats)) {
|
|
29
|
+
for (const [field, expression] of entry) {
|
|
30
|
+
for (const identifier of bareIdentifiersIn(expression)) {
|
|
31
|
+
if (!deedNames.has(identifier))
|
|
32
|
+
continue;
|
|
33
|
+
warnings.push(`userAggregateStats[${index}].${field}: "${identifier}" is a deed, so it needs the deeds prefix here — use "deeds.${identifier}". Bare deed names only work in the other sections.`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (const section of flatScopeSections) {
|
|
38
|
+
for (const [index, entry] of expressionEntries(integrations[section])) {
|
|
39
|
+
for (const [field, expression] of entry) {
|
|
40
|
+
for (const identifier of deedsPrefixedIdentifiersIn(expression)) {
|
|
41
|
+
warnings.push(`${section}[${index}].${field}: deeds are bare identifiers in this section — use "${identifier}" rather than "deeds.${identifier}".`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return warnings;
|
|
47
|
+
};
|
|
48
|
+
// Every section whose expressions are evaluated against the flat deed scope (getScopeFromDeedsForGame).
|
|
49
|
+
const flatScopeSections = ["leaderboards", "puzzleAggregateStats", "persistedDeeds", "completionSidebar"];
|
|
50
|
+
// `points` and `time` are sent by the platform for every game, so they are deeds even when unreferenced.
|
|
51
|
+
const builtInDeedIDs = ["points", "time"];
|
|
52
|
+
/** Deed IDs referenced anywhere in the file, camelized to how they appear in an expression scope. */
|
|
53
|
+
const camelizedDeedNamesIn = (integrations) => {
|
|
54
|
+
const names = new Set(builtInDeedIDs.map(camelize));
|
|
55
|
+
collectDeedIDs(integrations, names);
|
|
56
|
+
return names;
|
|
57
|
+
};
|
|
58
|
+
const deedIDFields = ["deedID", "persistedDeedID"];
|
|
59
|
+
const collectDeedIDs = (node, into) => {
|
|
60
|
+
if (Array.isArray(node)) {
|
|
61
|
+
for (const item of node)
|
|
62
|
+
collectDeedIDs(item, into);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (!node || typeof node !== "object")
|
|
66
|
+
return;
|
|
67
|
+
for (const [key, value] of Object.entries(node)) {
|
|
68
|
+
if (deedIDFields.includes(key) && typeof value === "string" && value !== "")
|
|
69
|
+
into.add(camelize(value));
|
|
70
|
+
else
|
|
71
|
+
collectDeedIDs(value, into);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/** Yields `[index, fields]` for each entry in a section that carries expressions. */
|
|
75
|
+
const expressionEntries = (section) => {
|
|
76
|
+
if (!Array.isArray(section))
|
|
77
|
+
return [];
|
|
78
|
+
return section.map((entry, index) => {
|
|
79
|
+
if (!entry || typeof entry !== "object")
|
|
80
|
+
return [index, []];
|
|
81
|
+
const { valueExp, filterExp } = entry;
|
|
82
|
+
const fields = [];
|
|
83
|
+
if (typeof valueExp === "string")
|
|
84
|
+
fields.push(["valueExp", valueExp]);
|
|
85
|
+
if (typeof filterExp === "string")
|
|
86
|
+
fields.push(["filterExp", filterExp]);
|
|
87
|
+
return [index, fields];
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
/** Identifiers used standalone, i.e. not reached through a property access like `deeds.foo`. */
|
|
91
|
+
const bareIdentifiersIn = (expression) => matchIdentifiers(withoutStringLiterals(expression), /(^|[^.\w$])([A-Za-z_$][\w$]*)/g);
|
|
92
|
+
/** Identifiers reached through a `deeds.` property access. */
|
|
93
|
+
const deedsPrefixedIdentifiersIn = (expression) => matchIdentifiers(withoutStringLiterals(expression), /(^|[^.\w$])deeds\.([A-Za-z_$][\w$]*)/g);
|
|
94
|
+
const matchIdentifiers = (expression, pattern) => {
|
|
95
|
+
const found = new Set();
|
|
96
|
+
for (const match of expression.matchAll(pattern))
|
|
97
|
+
found.add(match[2]);
|
|
98
|
+
return [...found];
|
|
99
|
+
};
|
|
100
|
+
// Deed names never contain quotes, so dropping literals wholesale avoids matching inside them.
|
|
101
|
+
const withoutStringLiterals = (expression) => expression.replace(/'[^']*'|"[^"]*"/g, " ");
|
|
102
|
+
const camelize = (s) => s.replace(/-./g, (x) => x[1].toUpperCase());
|
|
18
103
|
//# sourceMappingURL=lintPuzzmoFile.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lintPuzzmoFile.js","sourceRoot":"","sources":["../../src/util/lintPuzzmoFile.ts"],"names":[],"mappings":"AAEA,+FAA+F;AAC/F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,UAAsB,EAAY,EAAE,CAAC;IAClE,MAAM,QAAQ,GAAa,EAAE,CAAA;
|
|
1
|
+
{"version":3,"file":"lintPuzzmoFile.js","sourceRoot":"","sources":["../../src/util/lintPuzzmoFile.ts"],"names":[],"mappings":"AAEA,+FAA+F;AAC/F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,UAAsB,EAAY,EAAE,CAAC;IAClE,MAAM,QAAQ,GAAa,EAAE,CAAA;IAC7B,MAAM,YAAY,GAAG,UAAU,CAAC,YAAY,IAAI,EAAE,CAAA;IAElD,MAAM,YAAY,GAAG,YAAY,CAAC,YAAY,CAAA;IAC9C,IAAI,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC;QAChC,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;YACvC,IAAI,CAAC,WAAW,IAAI,OAAO,WAAW,KAAK,QAAQ;gBAAE,SAAQ;YAC7D,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,WAA+E,CAAA;YAC5H,IAAI,SAAS,KAAK,SAAS;gBAAE,SAAQ;YACrC,MAAM,KAAK,GAAG,WAAW,IAAI,QAAQ,IAAI,SAAS,CAAA;YAClD,QAAQ,CAAC,IAAI,CACX,gBAAgB,KAAK,2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C,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IACrE,OAAO,CAAC,GAAG,KAAK,CAAC,CAAA;AAAA,CAClB,CAAA;AAED,+FAA+F;AAC/F,MAAM,qBAAqB,GAAG,CAAC,UAAkB,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAA;AAEjG,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -131,7 +131,7 @@
|
|
|
131
131
|
"properties": {
|
|
132
132
|
"userAggregateStatStableID": {
|
|
133
133
|
"type": "string",
|
|
134
|
-
"description": "The stableID from userAggregateStats to display.
|
|
134
|
+
"description": "The stableID from userAggregateStats to display. Matched verbatim, so it must be identical to the stat's stableID."
|
|
135
135
|
},
|
|
136
136
|
"formatString": {
|
|
137
137
|
"type": "string",
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
|
|
3
|
+
import { lintDeedScopeUsage } from "./lintPuzzmoFile.js"
|
|
4
|
+
|
|
5
|
+
describe(lintDeedScopeUsage.name, () => {
|
|
6
|
+
it("flags a bare deed name in a userAggregateStats expression", () => {
|
|
7
|
+
const warnings = lintDeedScopeUsage({
|
|
8
|
+
completionTable: [{ persistedDeedID: "best-pickups", title: "Largest pickup", formatString: "%@" }],
|
|
9
|
+
userAggregateStats: [
|
|
10
|
+
{ stableID: "largest-pickup", valueExp: "prior == null || bestPickups > prior ? bestPickups : prior" },
|
|
11
|
+
{ stableID: "total-misses", deedID: "misses" },
|
|
12
|
+
{ stableID: "no-miss-completions", filterExp: "misses === 0", valueExp: "(prior || 0) + 1" },
|
|
13
|
+
],
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
expect(warnings).toEqual([
|
|
17
|
+
'userAggregateStats[0].valueExp: "bestPickups" is a deed, so it needs the deeds prefix here — use "deeds.bestPickups". Bare deed names only work in the other sections.',
|
|
18
|
+
'userAggregateStats[2].filterExp: "misses" is a deed, so it needs the deeds prefix here — use "deeds.misses". Bare deed names only work in the other sections.',
|
|
19
|
+
])
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("accepts userAggregateStats expressions that use the deeds prefix", () => {
|
|
23
|
+
const warnings = lintDeedScopeUsage({
|
|
24
|
+
completionTable: [{ persistedDeedID: "best-pickups", title: "Largest pickup", formatString: "%@" }],
|
|
25
|
+
userAggregateStats: [
|
|
26
|
+
{ stableID: "largest-pickup", valueExp: "prior == null || deeds.bestPickups > prior ? deeds.bestPickups : prior" },
|
|
27
|
+
{ stableID: "no-miss-completions", filterExp: "deeds.misses === 0", valueExp: "(prior || 0) + 1" },
|
|
28
|
+
{ stableID: "completions", valueExp: "(prior || 0) + 1" },
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
expect(warnings).toEqual([])
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("flags a deeds prefix in the sections which get a flat scope", () => {
|
|
36
|
+
const warnings = lintDeedScopeUsage({
|
|
37
|
+
puzzleAggregateStats: [{ stableID: "perfects", filterExp: "deeds.excessMoves === 0", valueExp: "prior + 1" }],
|
|
38
|
+
leaderboards: [{ stableID: "g:star", displayName: "Star by", valueExp: "deeds.wordsToStarred" }],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
expect(warnings).toEqual([
|
|
42
|
+
'leaderboards[0].valueExp: deeds are bare identifiers in this section — use "wordsToStarred" rather than "deeds.wordsToStarred".',
|
|
43
|
+
'puzzleAggregateStats[0].filterExp: deeds are bare identifiers in this section — use "excessMoves" rather than "deeds.excessMoves".',
|
|
44
|
+
])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it("knows about the platform-supplied points and time deeds", () => {
|
|
48
|
+
const warnings = lintDeedScopeUsage({
|
|
49
|
+
userAggregateStats: [{ stableID: "total-points", valueExp: "(prior || 0) + points" }],
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
expect(warnings).toEqual([
|
|
53
|
+
'userAggregateStats[0].valueExp: "points" is a deed, so it needs the deeds prefix here — use "deeds.points". Bare deed names only work in the other sections.',
|
|
54
|
+
])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it("leaves scope roots and unknown identifiers alone", () => {
|
|
58
|
+
const warnings = lintDeedScopeUsage({
|
|
59
|
+
userAggregateStats: [{ stableID: "best", valueExp: "prior > uas.site[1] ? prior : uas.site[1]" }],
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
expect(warnings).toEqual([])
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it("does not read deed names out of string literals", () => {
|
|
66
|
+
const warnings = lintDeedScopeUsage({
|
|
67
|
+
userAggregateStats: [{ stableID: "tag", deedID: "misses", valueExp: "'misses' + prior" }],
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
expect(warnings).toEqual([])
|
|
71
|
+
})
|
|
72
|
+
})
|
|
@@ -3,8 +3,9 @@ import type { PuzzmoFile } from "./api.js"
|
|
|
3
3
|
/** Non-fatal checks on a schema-valid puzzmo.json. Returns human-readable warning messages. */
|
|
4
4
|
export const lintPuzzmoFile = (puzzmoFile: PuzzmoFile): string[] => {
|
|
5
5
|
const warnings: string[] = []
|
|
6
|
+
const integrations = puzzmoFile.integrations ?? {}
|
|
6
7
|
|
|
7
|
-
const leaderboards =
|
|
8
|
+
const leaderboards = integrations.leaderboards
|
|
8
9
|
if (Array.isArray(leaderboards)) {
|
|
9
10
|
for (const leaderboard of leaderboards) {
|
|
10
11
|
if (!leaderboard || typeof leaderboard !== "object") continue
|
|
@@ -17,5 +18,105 @@ export const lintPuzzmoFile = (puzzmoFile: PuzzmoFile): string[] => {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
warnings.push(...lintDeedScopeUsage(integrations))
|
|
22
|
+
|
|
23
|
+
return warnings
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `userAggregateStats` expressions see deeds nested under `deeds.` while every other section sees them
|
|
28
|
+
* as bare identifiers. Getting it the wrong way round evaluates to `undefined` and silently stores
|
|
29
|
+
* nothing (or skips the entry entirely, for a filterExp), so flag both directions.
|
|
30
|
+
*/
|
|
31
|
+
export const lintDeedScopeUsage = (integrations: Record<string, unknown>): string[] => {
|
|
32
|
+
const warnings: string[] = []
|
|
33
|
+
const deedNames = camelizedDeedNamesIn(integrations)
|
|
34
|
+
|
|
35
|
+
for (const [index, entry] of expressionEntries(integrations.userAggregateStats)) {
|
|
36
|
+
for (const [field, expression] of entry) {
|
|
37
|
+
for (const identifier of bareIdentifiersIn(expression)) {
|
|
38
|
+
if (!deedNames.has(identifier)) continue
|
|
39
|
+
warnings.push(
|
|
40
|
+
`userAggregateStats[${index}].${field}: "${identifier}" is a deed, so it needs the deeds prefix here — use "deeds.${identifier}". Bare deed names only work in the other sections.`,
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const section of flatScopeSections) {
|
|
47
|
+
for (const [index, entry] of expressionEntries(integrations[section])) {
|
|
48
|
+
for (const [field, expression] of entry) {
|
|
49
|
+
for (const identifier of deedsPrefixedIdentifiersIn(expression)) {
|
|
50
|
+
warnings.push(
|
|
51
|
+
`${section}[${index}].${field}: deeds are bare identifiers in this section — use "${identifier}" rather than "deeds.${identifier}".`,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
20
58
|
return warnings
|
|
21
59
|
}
|
|
60
|
+
|
|
61
|
+
// Every section whose expressions are evaluated against the flat deed scope (getScopeFromDeedsForGame).
|
|
62
|
+
const flatScopeSections = ["leaderboards", "puzzleAggregateStats", "persistedDeeds", "completionSidebar"]
|
|
63
|
+
|
|
64
|
+
// `points` and `time` are sent by the platform for every game, so they are deeds even when unreferenced.
|
|
65
|
+
const builtInDeedIDs = ["points", "time"]
|
|
66
|
+
|
|
67
|
+
/** Deed IDs referenced anywhere in the file, camelized to how they appear in an expression scope. */
|
|
68
|
+
const camelizedDeedNamesIn = (integrations: Record<string, unknown>): Set<string> => {
|
|
69
|
+
const names = new Set(builtInDeedIDs.map(camelize))
|
|
70
|
+
collectDeedIDs(integrations, names)
|
|
71
|
+
return names
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const deedIDFields = ["deedID", "persistedDeedID"]
|
|
75
|
+
|
|
76
|
+
const collectDeedIDs = (node: unknown, into: Set<string>) => {
|
|
77
|
+
if (Array.isArray(node)) {
|
|
78
|
+
for (const item of node) collectDeedIDs(item, into)
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
if (!node || typeof node !== "object") return
|
|
82
|
+
|
|
83
|
+
for (const [key, value] of Object.entries(node)) {
|
|
84
|
+
if (deedIDFields.includes(key) && typeof value === "string" && value !== "") into.add(camelize(value))
|
|
85
|
+
else collectDeedIDs(value, into)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type ExpressionField = ["valueExp" | "filterExp", string]
|
|
90
|
+
|
|
91
|
+
/** Yields `[index, fields]` for each entry in a section that carries expressions. */
|
|
92
|
+
const expressionEntries = (section: unknown): Array<[number, ExpressionField[]]> => {
|
|
93
|
+
if (!Array.isArray(section)) return []
|
|
94
|
+
|
|
95
|
+
return section.map((entry, index): [number, ExpressionField[]] => {
|
|
96
|
+
if (!entry || typeof entry !== "object") return [index, []]
|
|
97
|
+
const { valueExp, filterExp } = entry as { valueExp?: unknown; filterExp?: unknown }
|
|
98
|
+
const fields: ExpressionField[] = []
|
|
99
|
+
if (typeof valueExp === "string") fields.push(["valueExp", valueExp])
|
|
100
|
+
if (typeof filterExp === "string") fields.push(["filterExp", filterExp])
|
|
101
|
+
return [index, fields]
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Identifiers used standalone, i.e. not reached through a property access like `deeds.foo`. */
|
|
106
|
+
const bareIdentifiersIn = (expression: string): string[] =>
|
|
107
|
+
matchIdentifiers(withoutStringLiterals(expression), /(^|[^.\w$])([A-Za-z_$][\w$]*)/g)
|
|
108
|
+
|
|
109
|
+
/** Identifiers reached through a `deeds.` property access. */
|
|
110
|
+
const deedsPrefixedIdentifiersIn = (expression: string): string[] =>
|
|
111
|
+
matchIdentifiers(withoutStringLiterals(expression), /(^|[^.\w$])deeds\.([A-Za-z_$][\w$]*)/g)
|
|
112
|
+
|
|
113
|
+
const matchIdentifiers = (expression: string, pattern: RegExp): string[] => {
|
|
114
|
+
const found = new Set<string>()
|
|
115
|
+
for (const match of expression.matchAll(pattern)) found.add(match[2])
|
|
116
|
+
return [...found]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Deed names never contain quotes, so dropping literals wholesale avoids matching inside them.
|
|
120
|
+
const withoutStringLiterals = (expression: string) => expression.replace(/'[^']*'|"[^"]*"/g, " ")
|
|
121
|
+
|
|
122
|
+
const camelize = (s: string) => s.replace(/-./g, (x) => x[1].toUpperCase())
|