ast-types 0.11.2 → 0.11.3
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/types.js +99 -61
- package/package.json +1 -1
package/lib/types.js
CHANGED
|
@@ -533,77 +533,115 @@ module.exports = function () {
|
|
|
533
533
|
// Override Dp.buildable for this Def instance.
|
|
534
534
|
Object.defineProperty(self, "buildable", {value: true});
|
|
535
535
|
|
|
536
|
-
|
|
537
|
-
|
|
536
|
+
function addParam(built, param, arg, isArgAvailable) {
|
|
537
|
+
if (hasOwn.call(built, param))
|
|
538
|
+
return;
|
|
538
539
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
540
|
+
var all = self.allFields;
|
|
541
|
+
if (!hasOwn.call(all, param)) {
|
|
542
|
+
throw new Error("" + param);
|
|
543
|
+
}
|
|
543
544
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
545
|
+
var field = all[param];
|
|
546
|
+
var type = field.type;
|
|
547
|
+
var value;
|
|
548
|
+
|
|
549
|
+
if (isArgAvailable) {
|
|
550
|
+
value = arg;
|
|
551
|
+
} else if (field.defaultFn) {
|
|
552
|
+
// Expose the partially-built object to the default
|
|
553
|
+
// function as its `this` object.
|
|
554
|
+
value = field.defaultFn.call(built);
|
|
555
|
+
} else {
|
|
556
|
+
var message = "no value or default function given for field " +
|
|
557
|
+
JSON.stringify(param) + " of " + self.typeName + "(" +
|
|
558
|
+
self.buildParams.map(function (name) {
|
|
559
|
+
return all[name];
|
|
560
|
+
}).join(", ") + ")";
|
|
561
|
+
throw new Error(message);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!type.check(value)) {
|
|
565
|
+
throw new Error(
|
|
566
|
+
shallowStringify(value) +
|
|
567
|
+
" does not match field " + field +
|
|
568
|
+
" of type " + self.typeName
|
|
569
|
+
);
|
|
570
|
+
}
|
|
550
571
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
throw new Error(message);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
if (!type.check(value)) {
|
|
580
|
-
throw new Error(
|
|
581
|
-
shallowStringify(value) +
|
|
582
|
-
" does not match field " + field +
|
|
583
|
-
" of type " + self.typeName
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// TODO Could attach getters and setters here to enforce
|
|
588
|
-
// dynamic type safety.
|
|
589
|
-
built[param] = value;
|
|
572
|
+
built[param] = value;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// Calling the builder function will construct an instance of the Def,
|
|
576
|
+
// with positional arguments mapped to the fields original passed to .build.
|
|
577
|
+
// If not enough arguments are provided, the default value for the remaining fields
|
|
578
|
+
// will be used.
|
|
579
|
+
function builder() {
|
|
580
|
+
var args = arguments;
|
|
581
|
+
var argc = args.length;
|
|
582
|
+
|
|
583
|
+
if (!self.finalized) {
|
|
584
|
+
throw new Error(
|
|
585
|
+
"attempting to instantiate unfinalized type " +
|
|
586
|
+
self.typeName
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
var built = Object.create(nodePrototype);
|
|
591
|
+
|
|
592
|
+
self.buildParams.forEach(function (param, i) {
|
|
593
|
+
if (i < argc) {
|
|
594
|
+
addParam(built, param, args[i], true)
|
|
595
|
+
} else {
|
|
596
|
+
addParam(built, param, null, false);
|
|
590
597
|
}
|
|
598
|
+
});
|
|
591
599
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
600
|
+
Object.keys(self.allFields).forEach(function (param) {
|
|
601
|
+
// Use the default value.
|
|
602
|
+
addParam(built, param, null, false);
|
|
603
|
+
});
|
|
595
604
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
605
|
+
// Make sure that the "type" field was filled automatically.
|
|
606
|
+
if (built.type !== self.typeName) {
|
|
607
|
+
throw new Error("");
|
|
608
|
+
}
|
|
599
609
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
610
|
+
return built;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// Calling .from on the builder function will construct an instance of the Def,
|
|
614
|
+
// using field values from the passed object. For fields missing from the passed object,
|
|
615
|
+
// their default value will be used.
|
|
616
|
+
builder.from = function (obj) {
|
|
617
|
+
if (!self.finalized) {
|
|
618
|
+
throw new Error(
|
|
619
|
+
"attempting to instantiate unfinalized type " +
|
|
620
|
+
self.typeName
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
var built = Object.create(nodePrototype);
|
|
625
|
+
|
|
626
|
+
Object.keys(self.allFields).forEach(function (param) {
|
|
627
|
+
if (hasOwn.call(obj, param)) {
|
|
628
|
+
addParam(built, param, obj[param], true);
|
|
629
|
+
} else {
|
|
630
|
+
addParam(built, param, null, false);
|
|
603
631
|
}
|
|
632
|
+
});
|
|
604
633
|
|
|
605
|
-
|
|
634
|
+
// Make sure that the "type" field was filled automatically.
|
|
635
|
+
if (built.type !== self.typeName) {
|
|
636
|
+
throw new Error("");
|
|
606
637
|
}
|
|
638
|
+
|
|
639
|
+
return built;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
Object.defineProperty(builders, getBuilderName(self.typeName), {
|
|
643
|
+
enumerable: true,
|
|
644
|
+
value: builder
|
|
607
645
|
});
|
|
608
646
|
|
|
609
647
|
return self; // For chaining.
|