FunctionNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import CodeNode from './CodeNode.js';
  2. import FunctionCallNode from './FunctionCallNode.js';
  3. class FunctionNode extends CodeNode {
  4. constructor( code = '', includes = [] ) {
  5. super( code, includes );
  6. this.keywords = {};
  7. }
  8. getNodeType( builder ) {
  9. return this.getNodeFunction( builder ).type;
  10. }
  11. getInputs( builder ) {
  12. return this.getNodeFunction( builder ).inputs;
  13. }
  14. getNodeFunction( builder ) {
  15. const nodeData = builder.getDataFromNode( this );
  16. let nodeFunction = nodeData.nodeFunction;
  17. if ( nodeFunction === undefined ) {
  18. nodeFunction = builder.parser.parseFunction( this.code );
  19. nodeData.nodeFunction = nodeFunction;
  20. }
  21. return nodeFunction;
  22. }
  23. call( parameters = {} ) {
  24. return new FunctionCallNode( this, parameters );
  25. }
  26. generate( builder, output ) {
  27. super.generate( builder );
  28. const nodeFunction = this.getNodeFunction( builder );
  29. const name = nodeFunction.name;
  30. const type = nodeFunction.type;
  31. const nodeCode = builder.getCodeFromNode( this, type );
  32. if ( name !== '' ) {
  33. // use a custom property name
  34. nodeCode.name = name;
  35. }
  36. const propertyName = builder.getPropertyName( nodeCode );
  37. let code = this.getNodeFunction( builder ).getCode( propertyName );
  38. const keywords = this.keywords;
  39. const keywordsProperties = Object.keys( keywords );
  40. if ( keywordsProperties.length > 0 ) {
  41. for ( const property of keywordsProperties ) {
  42. const propertyRegExp = new RegExp( `\\b${property}\\b`, 'g' );
  43. const nodeProperty = keywords[ property ].build( builder, 'property' );
  44. code = code.replace( propertyRegExp, nodeProperty );
  45. }
  46. }
  47. nodeCode.code = code;
  48. if ( output === 'property' ) {
  49. return propertyName;
  50. } else {
  51. return builder.format( `${ propertyName }()`, type, output );
  52. }
  53. }
  54. }
  55. export default FunctionNode;