Skip to content

Commit 9e6173a

Browse files
committed
feat: support more powerful custom shorthands
1 parent 2f6ee9d commit 9e6173a

21 files changed

+510
-309
lines changed

src/__tests__/codegen.test.mjs

+45-25
Original file line numberDiff line numberDiff line change
@@ -1289,16 +1289,8 @@ export default function (input, callbacks) {
12891289

12901290
describe('custom shorthands', () => {
12911291
it('should be supported', () => {
1292-
const shorthands = {
1293-
schema: ['patternProperties', 'properties']
1294-
.map(k => `scope.path[scope.path.length - 2] === '${k}'`)
1295-
.join(' || '),
1296-
};
1297-
12981292
assert.equal(
1299-
generate(['$.components.schemas[*]..@@schema()'], {
1300-
customShorthands: shorthands,
1301-
}),
1293+
generate(['$.components.schemas[*]..@@schema(0)']),
13021294
`import {Scope} from "nimma/runtime";
13031295
const zones = {
13041296
keys: ["components"],
@@ -1310,24 +1302,19 @@ const zones = {
13101302
}]
13111303
};
13121304
const tree = {
1313-
"$.components.schemas[*]..@@schema()": function (scope) {
1305+
"$.components.schemas[*]..@@schema(0)": function (scope, shorthands) {
13141306
if (scope.path.length < 4) return;
13151307
if (scope.path[0] !== "components") return;
13161308
if (scope.path[1] !== "schemas") return;
13171309
if (!shorthands.schema(scope)) return;
1318-
scope.emit("$.components.schemas[*]..@@schema()", 0, false);
1319-
}
1320-
};
1321-
const shorthands = {
1322-
schema: function (scope, state) {
1323-
return scope.path[scope.path.length - 2] === 'patternProperties' || scope.path[scope.path.length - 2] === 'properties';
1310+
scope.emit("$.components.schemas[*]..@@schema(0)", 0, false);
13241311
}
13251312
};
1326-
export default function (input, callbacks) {
1313+
export default function (input, callbacks, shorthands) {
13271314
const scope = new Scope(input, callbacks);
13281315
try {
13291316
scope.traverse(() => {
1330-
tree["$.components.schemas[*]..@@schema()"](scope);
1317+
tree["$.components.schemas[*]..@@schema(0)"](scope, shorthands);
13311318
}, zones);
13321319
} finally {
13331320
scope.destroy();
@@ -1337,13 +1324,46 @@ export default function (input, callbacks) {
13371324
);
13381325
});
13391326

1340-
it('should refuse to use an undefined shorthand', () => {
1341-
assert.throws(
1342-
generate.bind(null, ['$.components.schemas[*]..@@schema()'], {
1343-
customShorthands: {},
1344-
}),
1345-
ReferenceError,
1346-
"Shorthand 'schema' is not defined",
1327+
it('should adjust state', () => {
1328+
assert.deepEqual(
1329+
generate(['$.components.schemas[*]..abc..@@schema(2)..enum']),
1330+
`import {Scope} from "nimma/runtime";
1331+
const zones = {
1332+
keys: ["components"],
1333+
zones: [{
1334+
keys: ["schemas"],
1335+
zones: [{
1336+
zone: null
1337+
}]
1338+
}]
1339+
};
1340+
const tree = {
1341+
"$.components.schemas[*]..abc..@@schema(2)..enum": function (scope, state, shorthands) {
1342+
if (scope.path.length < 4) return;
1343+
if (scope.path[0] !== "components") return;
1344+
if (scope.path[1] !== "schemas") return;
1345+
if (state.initialValue >= 0) {
1346+
if (scope.path[scope.path.length - 1] === "abc") {
1347+
state.value |= 1
1348+
}
1349+
}
1350+
if (!shorthands.schema(scope, state, 1)) return;
1351+
if (state.initialValue < 15 || !(scope.path[scope.path.length - 1] === "enum")) return;
1352+
scope.emit("$.components.schemas[*]..abc..@@schema(2)..enum", 0, false);
1353+
}
1354+
};
1355+
export default function (input, callbacks, shorthands) {
1356+
const scope = new Scope(input, callbacks);
1357+
try {
1358+
const state0 = scope.allocState();
1359+
scope.traverse(() => {
1360+
tree["$.components.schemas[*]..abc..@@schema(2)..enum"](scope, state0, shorthands);
1361+
}, zones);
1362+
} finally {
1363+
scope.destroy();
1364+
}
1365+
}
1366+
`,
13471367
);
13481368
});
13491369
});

0 commit comments

Comments
 (0)