FunctionNode.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import CodeNode from './CodeNode.js';
  2. import FunctionCallNode from './FunctionCallNode.js';
  3. class FunctionNode extends CodeNode {
  4. constructor( code = '' ) {
  5. super( code );
  6. this.inputs = [];
  7. this.nodeFunction = null;
  8. this.useKeywords = true;
  9. }
  10. getNodeType( builder ) {
  11. return this.getNodeFunction( builder ).type;
  12. }
  13. getInputs( builder ) {
  14. return this.getNodeFunction( builder ).inputs;
  15. }
  16. getNodeFunction( builder ) {
  17. if ( this.nodeFunction === null ) {
  18. this.nodeFunction = builder.parser.parseFunction( this.code );
  19. }
  20. return this.nodeFunction;
  21. }
  22. call( parameters = {} ) {
  23. return new FunctionCallNode( this, parameters );
  24. }
  25. generate( builder, output ) {
  26. super.generate( builder );
  27. const nodeFunction = this.getNodeFunction( builder );
  28. const name = nodeFunction.name;
  29. const type = nodeFunction.type;
  30. const nodeCode = builder.getCodeFromNode( this, type );
  31. if ( name !== '' ) {
  32. // use a custom property name
  33. nodeCode.name = name;
  34. }
  35. const propertyName = builder.getPropertyName( nodeCode );
  36. nodeCode.code = this.getNodeFunction( builder ).getCode( propertyName );
  37. if ( output === 'property' ) {
  38. return propertyName;
  39. } else {
  40. return builder.format( `${ propertyName }()`, type, output );
  41. }
  42. }
  43. }
  44. export default FunctionNode;