basicTemplateAstVisitor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var ast = require("@angular/compiler");
  17. var expressionTypes_1 = require("../expressionTypes");
  18. var recursiveAngularExpressionVisitor_1 = require("./recursiveAngularExpressionVisitor");
  19. var sourceMappingVisitor_1 = require("../sourceMappingVisitor");
  20. var getExpressionDisplacement = function (binding) {
  21. var attrLen = 0;
  22. var valLen = 0;
  23. var totalLength = 0;
  24. var result = 0;
  25. if (binding instanceof ast.BoundEventAst ||
  26. binding instanceof ast.BoundElementPropertyAst ||
  27. binding instanceof ast.BoundDirectivePropertyAst) {
  28. var subBindingLen = 0;
  29. if (binding instanceof ast.BoundElementPropertyAst) {
  30. switch (binding.type) {
  31. case 4:
  32. subBindingLen = 'animate'.length + 1;
  33. break;
  34. case 1:
  35. subBindingLen = 'attr'.length + 1;
  36. break;
  37. case 2:
  38. subBindingLen = 'class'.length + 1;
  39. break;
  40. case 3:
  41. subBindingLen = 'style'.length + 1;
  42. break;
  43. }
  44. }
  45. if (!(binding instanceof ast.BoundDirectivePropertyAst)) {
  46. attrLen = binding.name.length + 4 + subBindingLen;
  47. }
  48. if (binding instanceof ast.BoundEventAst) {
  49. valLen = binding.handler.span.end;
  50. }
  51. else if (binding instanceof ast.BoundDirectivePropertyAst &&
  52. (binding.templateName === 'ngForOf' || binding.templateName === 'ngIf')) {
  53. result = binding.sourceSpan.start.file.content.lastIndexOf(binding.value.source);
  54. if (binding.templateName === 'ngIf') {
  55. if (binding.value.ast.span.start > 0)
  56. result = binding.sourceSpan.start.file.content.lastIndexOf(binding.value.source) + binding.value.ast.span.start;
  57. }
  58. }
  59. else {
  60. valLen = binding.value.span.end;
  61. }
  62. if (!(binding instanceof ast.BoundDirectivePropertyAst) || (binding.templateName !== 'ngForOf' && binding.templateName !== 'ngIf')) {
  63. totalLength = binding.sourceSpan.end.offset - binding.sourceSpan.start.offset;
  64. var whitespace = totalLength - (attrLen + valLen) - 1;
  65. result = whitespace + attrLen + binding.sourceSpan.start.offset;
  66. }
  67. }
  68. else if (binding instanceof ast.BoundTextAst) {
  69. result = binding.sourceSpan.start.offset;
  70. }
  71. return result;
  72. };
  73. var BasicTemplateAstVisitor = (function (_super) {
  74. __extends(BasicTemplateAstVisitor, _super);
  75. function BasicTemplateAstVisitor(sourceFile, _originalOptions, context, templateStart, expressionVisitorCtrl) {
  76. if (expressionVisitorCtrl === void 0) { expressionVisitorCtrl = recursiveAngularExpressionVisitor_1.RecursiveAngularExpressionVisitor; }
  77. var _this = _super.call(this, sourceFile, _originalOptions, context.template.template, templateStart) || this;
  78. _this._originalOptions = _originalOptions;
  79. _this.context = context;
  80. _this.templateStart = templateStart;
  81. _this.expressionVisitorCtrl = expressionVisitorCtrl;
  82. _this._variables = {};
  83. return _this;
  84. }
  85. BasicTemplateAstVisitor.prototype.visit = function (node, context) {
  86. node.visit(this, context);
  87. };
  88. BasicTemplateAstVisitor.prototype.visitNgContent = function (ast, context) { };
  89. BasicTemplateAstVisitor.prototype.visitEmbeddedTemplate = function (ast, context) {
  90. var _this = this;
  91. ast.directives.forEach(function (d) { return _this.visit(d, context); });
  92. ast.variables.forEach(function (v) { return _this.visit(v, context); });
  93. ast.children.forEach(function (e) { return _this.visit(e, context); });
  94. ast.outputs.forEach(function (o) { return _this.visit(o, context); });
  95. ast.attrs.forEach(function (a) { return _this.visit(a, context); });
  96. ast.references.forEach(function (r) { return _this.visit(r, context); });
  97. };
  98. BasicTemplateAstVisitor.prototype.visitElement = function (element, context) {
  99. var _this = this;
  100. element.references.forEach(function (r) { return _this.visit(r, context); });
  101. element.inputs.forEach(function (i) { return _this.visit(i, context); });
  102. element.outputs.forEach(function (o) { return _this.visit(o, context); });
  103. element.attrs.forEach(function (a) { return _this.visit(a, context); });
  104. element.children.forEach(function (e) { return _this.visit(e, context); });
  105. element.directives.forEach(function (d) { return _this.visit(d, context); });
  106. };
  107. BasicTemplateAstVisitor.prototype.visitReference = function (ast, context) { };
  108. BasicTemplateAstVisitor.prototype.visitVariable = function (ast, context) {
  109. this._variables[ast.name] = true;
  110. };
  111. BasicTemplateAstVisitor.prototype.visitEvent = function (ast, context) {
  112. this._variables['$event'] = true;
  113. this.visitNgTemplateAST(ast.handler, this.templateStart + getExpressionDisplacement(ast));
  114. this._variables['$event'] = false;
  115. };
  116. BasicTemplateAstVisitor.prototype.visitElementProperty = function (prop, context) {
  117. var ast = prop.value.ast;
  118. ast.interpolateExpression = prop.value.source;
  119. this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop), prop);
  120. };
  121. BasicTemplateAstVisitor.prototype.visitAttr = function (ast, context) { };
  122. BasicTemplateAstVisitor.prototype.visitBoundText = function (text, context) {
  123. if (expressionTypes_1.ExpTypes.ASTWithSource(text.value)) {
  124. var ast_1 = text.value.ast;
  125. ast_1.interpolateExpression = text.value.source;
  126. this.visitNgTemplateAST(ast_1, this.templateStart + getExpressionDisplacement(text));
  127. }
  128. };
  129. BasicTemplateAstVisitor.prototype.visitText = function (text, context) { };
  130. BasicTemplateAstVisitor.prototype.visitDirective = function (ast, context) {
  131. var _this = this;
  132. ast.inputs.forEach(function (o) { return _this.visit(o, context); });
  133. ast.hostProperties.forEach(function (p) { return _this.visit(p, context); });
  134. ast.hostEvents.forEach(function (e) { return _this.visit(e, context); });
  135. };
  136. BasicTemplateAstVisitor.prototype.visitDirectiveProperty = function (prop, context) {
  137. if (expressionTypes_1.ExpTypes.ASTWithSource(prop.value)) {
  138. this.visitNgTemplateAST(prop.value, this.templateStart + getExpressionDisplacement(prop), prop);
  139. }
  140. };
  141. BasicTemplateAstVisitor.prototype.visitNgTemplateAST = function (ast, templateStart, prop) {
  142. var _this = this;
  143. var templateVisitor = new this.expressionVisitorCtrl(this.getSourceFile(), this._originalOptions, this.context, templateStart);
  144. templateVisitor.preDefinedVariables = this._variables;
  145. templateVisitor.parentAST = prop;
  146. templateVisitor.visit(ast);
  147. templateVisitor.getFailures().forEach(function (f) { return _this.addFailure(f); });
  148. };
  149. return BasicTemplateAstVisitor;
  150. }(sourceMappingVisitor_1.SourceMappingVisitor));
  151. exports.BasicTemplateAstVisitor = BasicTemplateAstVisitor;