@@ -1289,16 +1289,8 @@ export default function (input, callbacks) {
1289
1289
1290
1290
describe ( 'custom shorthands' , ( ) => {
1291
1291
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
-
1298
1292
assert . equal (
1299
- generate ( [ '$.components.schemas[*]..@@schema()' ] , {
1300
- customShorthands : shorthands ,
1301
- } ) ,
1293
+ generate ( [ '$.components.schemas[*]..@@schema(0)' ] ) ,
1302
1294
`import {Scope} from "nimma/runtime";
1303
1295
const zones = {
1304
1296
keys: ["components"],
@@ -1310,24 +1302,19 @@ const zones = {
1310
1302
}]
1311
1303
};
1312
1304
const tree = {
1313
- "$.components.schemas[*]..@@schema()": function (scope) {
1305
+ "$.components.schemas[*]..@@schema(0 )": function (scope, shorthands ) {
1314
1306
if (scope.path.length < 4) return;
1315
1307
if (scope.path[0] !== "components") return;
1316
1308
if (scope.path[1] !== "schemas") return;
1317
1309
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);
1324
1311
}
1325
1312
};
1326
- export default function (input, callbacks) {
1313
+ export default function (input, callbacks, shorthands ) {
1327
1314
const scope = new Scope(input, callbacks);
1328
1315
try {
1329
1316
scope.traverse(() => {
1330
- tree["$.components.schemas[*]..@@schema()"](scope);
1317
+ tree["$.components.schemas[*]..@@schema(0 )"](scope, shorthands );
1331
1318
}, zones);
1332
1319
} finally {
1333
1320
scope.destroy();
@@ -1337,13 +1324,46 @@ export default function (input, callbacks) {
1337
1324
) ;
1338
1325
} ) ;
1339
1326
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
+ ` ,
1347
1367
) ;
1348
1368
} ) ;
1349
1369
} ) ;
0 commit comments