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.
Files changed (2) hide show
  1. package/lib/types.js +99 -61
  2. 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
- Object.defineProperty(builders, getBuilderName(self.typeName), {
537
- enumerable: true,
536
+ function addParam(built, param, arg, isArgAvailable) {
537
+ if (hasOwn.call(built, param))
538
+ return;
538
539
 
539
- value: function () {
540
- var args = arguments;
541
- var argc = args.length;
542
- var built = Object.create(nodePrototype);
540
+ var all = self.allFields;
541
+ if (!hasOwn.call(all, param)) {
542
+ throw new Error("" + param);
543
+ }
543
544
 
544
- if (!self.finalized) {
545
- throw new Error(
546
- "attempting to instantiate unfinalized type " +
547
- self.typeName
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
- function add(param, i) {
552
- if (hasOwn.call(built, param))
553
- return;
554
-
555
- var all = self.allFields;
556
- if (!hasOwn.call(all, param)) {
557
- throw new Error("" + param);
558
- }
559
-
560
- var field = all[param];
561
- var type = field.type;
562
- var value;
563
-
564
- if (isNumber.check(i) && i < argc) {
565
- value = args[i];
566
- } else if (field.defaultFn) {
567
- // Expose the partially-built object to the default
568
- // function as its `this` object.
569
- value = field.defaultFn.call(built);
570
- } else {
571
- var message = "no value or default function given for field " +
572
- JSON.stringify(param) + " of " + self.typeName + "(" +
573
- self.buildParams.map(function (name) {
574
- return all[name];
575
- }).join(", ") + ")";
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
- self.buildParams.forEach(function (param, i) {
593
- add(param, i);
594
- });
600
+ Object.keys(self.allFields).forEach(function (param) {
601
+ // Use the default value.
602
+ addParam(built, param, null, false);
603
+ });
595
604
 
596
- Object.keys(self.allFields).forEach(function (param) {
597
- add(param); // Use the default value.
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
- // Make sure that the "type" field was filled automatically.
601
- if (built.type !== self.typeName) {
602
- throw new Error("");
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
- return built;
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.
package/package.json CHANGED
@@ -18,7 +18,7 @@
18
18
  "transformation",
19
19
  "syntax"
20
20
  ],
21
- "version": "0.11.2",
21
+ "version": "0.11.3",
22
22
  "homepage": "http://github.com/benjamn/ast-types",
23
23
  "repository": {
24
24
  "type": "git",