ast-types icon indicating copy to clipboard operation
ast-types copied to clipboard

expose parameter metadata for builder functions

Open jamestalmage opened this issue 8 years ago • 2 comments

Closes #84

var types = require('ast-types');
var assert = require('assert');
var b = types.builders;
var n = types.namedTypes;

assert.strictEqual(b.whileStatement.paramCount, 2);

assert.deepEqual(b.whileStatement[0], {
  name: 'test',
  body: n.Expression,
  required: true
});

assert.deepEqual(b.whileStatement[1], {
  name: 'body',
  body: n.Statement,
  required: true // false for parameters that have a default specified 
});

  

jamestalmage avatar Aug 11 '15 02:08 jamestalmage

Note that the discussion in #84 suggested a length property to determine the number of parameters, but that is already defined (and not configurable) for Functions.

It would be possible to generate build functions with the correct arity, so that the length property would accurately reflect the number of parameters.

Note: the length property will be configurable in es6, but is not in es5.

jamestalmage avatar Aug 11 '15 03:08 jamestalmage

:+1: to the idea, although I think a better way to do this would be to add a parameters array, which removes the need for a separate paramCount property as parameters.length can be used instead.

bnjmnt4n avatar Feb 14 '16 14:02 bnjmnt4n