TemplateLiteralTransformer.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  8. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  9. import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
  10. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  11. import { NodeGuards } from '../../node/NodeGuards';
  12. import { Nodes } from '../../node/Nodes';
  13. import { NodeUtils } from '../../node/NodeUtils';
  14. /**
  15. * Transform ES2015 template literals to ES5
  16. * Thanks to Babel for algorithm
  17. */
  18. @injectable()
  19. export class TemplateLiteralTransformer extends AbstractNodeTransformer {
  20. /**
  21. * @param {IRandomGenerator} randomGenerator
  22. * @param {IOptions} options
  23. */
  24. constructor (
  25. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  26. @inject(ServiceIdentifiers.IOptions) options: IOptions
  27. ) {
  28. super(randomGenerator, options);
  29. }
  30. /**
  31. * @param {NodeGuards} node
  32. * @returns {boolean}
  33. */
  34. private static isLiteralNodeWithStringValue (node: ESTree.Node): boolean {
  35. return node && NodeGuards.isLiteralNode(node) && typeof node.value === 'string';
  36. }
  37. /**
  38. * @param {TransformationStage} transformationStage
  39. * @returns {IVisitor | null}
  40. */
  41. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  42. switch (transformationStage) {
  43. case TransformationStage.Converting:
  44. return {
  45. enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
  46. if (parentNode
  47. && NodeGuards.isExpressionStatementNode(node)
  48. && NodeGuards.isTemplateLiteralNode(node.expression)
  49. ) {
  50. return this.fixEsprimaReturnStatementTemplateLiteralNode(node, node.expression);
  51. }
  52. if (parentNode && NodeGuards.isTemplateLiteralNode(node)) {
  53. return this.transformNode(node, parentNode);
  54. }
  55. }
  56. };
  57. default:
  58. return null;
  59. }
  60. }
  61. /**
  62. * @param {TemplateLiteral} templateLiteralNode
  63. * @param {NodeGuards} parentNode
  64. * @returns {NodeGuards}
  65. */
  66. public transformNode (templateLiteralNode: ESTree.TemplateLiteral, parentNode: ESTree.Node): ESTree.Node {
  67. const templateLiteralExpressions: ESTree.Expression[] = templateLiteralNode.expressions;
  68. let nodes: (ESTree.Literal | ESTree.Expression)[] = [];
  69. templateLiteralNode.quasis.forEach((templateElement: ESTree.TemplateElement) => {
  70. nodes.push(Nodes.getLiteralNode(templateElement.value.cooked));
  71. const expression: ESTree.Expression | undefined = templateLiteralExpressions.shift();
  72. if (!expression) {
  73. return;
  74. }
  75. nodes.push(expression);
  76. });
  77. nodes = nodes.filter((node: ESTree.Literal | ESTree.Expression) => {
  78. return !(NodeGuards.isLiteralNode(node) && node.value === '');
  79. });
  80. // since `+` is left-to-right associative
  81. // ensure the first node is a string if first/second isn't
  82. if (
  83. !TemplateLiteralTransformer.isLiteralNodeWithStringValue(nodes[0]) &&
  84. !TemplateLiteralTransformer.isLiteralNodeWithStringValue(nodes[1])
  85. ) {
  86. nodes.unshift(Nodes.getLiteralNode(''));
  87. }
  88. if (nodes.length > 1) {
  89. let root: ESTree.BinaryExpression = Nodes.getBinaryExpressionNode(
  90. '+',
  91. <ESTree.Literal>nodes.shift(),
  92. <ESTree.Expression>nodes.shift()
  93. );
  94. nodes.forEach((node: ESTree.Literal | ESTree.Expression) => {
  95. root = Nodes.getBinaryExpressionNode('+', root, <ESTree.Literal | ESTree.Expression>node);
  96. });
  97. return root;
  98. }
  99. return nodes[0];
  100. }
  101. /**
  102. * @param {ExpressionStatement} expressionStatementNode
  103. * @param {TemplateLiteral} templateLiteralNode
  104. * @returns {Node | VisitorOption}
  105. */
  106. private fixEsprimaReturnStatementTemplateLiteralNode (
  107. expressionStatementNode: ESTree.ExpressionStatement,
  108. templateLiteralNode: ESTree.TemplateLiteral
  109. ): ESTree.Node | estraverse.VisitorOption {
  110. const previousSiblingStatementNode: TStatement | null = NodeUtils
  111. .getPreviousSiblingStatementNode(expressionStatementNode);
  112. if (
  113. !previousSiblingStatementNode
  114. || !templateLiteralNode.parentNode
  115. || !NodeGuards.isReturnStatementNode(previousSiblingStatementNode)
  116. || previousSiblingStatementNode.argument !== null
  117. ) {
  118. return expressionStatementNode;
  119. }
  120. previousSiblingStatementNode.argument = <ESTree.Expression>this.transformNode(
  121. templateLiteralNode,
  122. templateLiteralNode.parentNode
  123. );
  124. NodeUtils.parentizeNode(templateLiteralNode, previousSiblingStatementNode);
  125. return estraverse.VisitorOption.Remove;
  126. }
  127. }