templateI18nRule.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
  16. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  17. return cooked;
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var compiler_1 = require("@angular/compiler");
  21. var rules_1 = require("tslint/lib/rules");
  22. var utils_1 = require("tslint/lib/utils");
  23. var ngWalker_1 = require("./angular/ngWalker");
  24. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  25. var isNotNullOrUndefined_1 = require("./util/isNotNullOrUndefined");
  26. var OPTION_CHECK_ID = 'check-id';
  27. var OPTION_CHECK_TEXT = 'check-text';
  28. var generateFailure = function (failureParameters) {
  29. var _a = failureParameters.ast.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  30. failureParameters.context.addFailureFromStartToEnd(startOffset, endOffset, failureParameters.message);
  31. };
  32. var Rule = (function (_super) {
  33. __extends(Rule, _super);
  34. function Rule() {
  35. return _super !== null && _super.apply(this, arguments) || this;
  36. }
  37. Rule.prototype.apply = function (sourceFile) {
  38. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  39. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  40. return this.applyWithWalker(walker);
  41. };
  42. Rule.prototype.isEnabled = function () {
  43. var _a = Rule.metadata.options, enumItems = _a.items.enum, maxLength = _a.maxLength, minLength = _a.minLength;
  44. var argumentsLength = this.ruleArguments.length;
  45. var optionArgument = utils_1.arrayify(this.ruleArguments).filter(isNotNullOrUndefined_1.isNotNullOrUndefined);
  46. var argumentsLengthInRange = argumentsLength >= minLength && argumentsLength <= maxLength;
  47. var isOptionArgumentValid = optionArgument.length > 0 && optionArgument.every(function (argument) { return enumItems.indexOf(argument) !== -1; });
  48. return _super.prototype.isEnabled.call(this) && argumentsLengthInRange && isOptionArgumentValid;
  49. };
  50. Rule.metadata = {
  51. description: 'Ensures following best practices for i18n.',
  52. optionExamples: [[true, OPTION_CHECK_ID], [true, OPTION_CHECK_TEXT], [true, OPTION_CHECK_ID, OPTION_CHECK_TEXT]],
  53. options: {
  54. items: {
  55. enum: [OPTION_CHECK_ID, OPTION_CHECK_TEXT],
  56. type: 'string'
  57. },
  58. maxLength: 2,
  59. minLength: 1,
  60. type: 'array'
  61. },
  62. optionsDescription: utils_1.dedent(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n One (or both) of the following arguments must be provided:\n * `", "` Makes sure i18n attributes have ID specified\n * `", "` Makes sure there are no elements with text content but no i18n attribute\n "], ["\n One (or both) of the following arguments must be provided:\n * \\`", "\\` Makes sure i18n attributes have ID specified\n * \\`", "\\` Makes sure there are no elements with text content but no i18n attribute\n "])), OPTION_CHECK_ID, OPTION_CHECK_TEXT),
  63. rationale: 'Makes the code more maintainable in i18n sense.',
  64. ruleName: 'template-i18n',
  65. type: 'maintainability',
  66. typescriptOnly: true
  67. };
  68. Rule.FAILURE_STRING_ATTR = 'Missing custom message identifier. For more information visit https://angular.io/guide/i18n';
  69. Rule.FAILURE_STRING_TEXT = 'Each element containing text node should have an i18n attribute';
  70. return Rule;
  71. }(rules_1.AbstractRule));
  72. exports.Rule = Rule;
  73. var TemplateVisitorAttrCtrl = (function (_super) {
  74. __extends(TemplateVisitorAttrCtrl, _super);
  75. function TemplateVisitorAttrCtrl() {
  76. return _super !== null && _super.apply(this, arguments) || this;
  77. }
  78. TemplateVisitorAttrCtrl.prototype.getCheckOption = function () {
  79. return OPTION_CHECK_ID;
  80. };
  81. TemplateVisitorAttrCtrl.prototype.visitAttr = function (ast, context) {
  82. this.validateAttr(ast, context);
  83. _super.prototype.visitAttr.call(this, ast, context);
  84. };
  85. TemplateVisitorAttrCtrl.prototype.validateAttr = function (ast, context) {
  86. if (ast.name !== 'i18n')
  87. return;
  88. var parts = ast.value.split('@@');
  89. if (parts.length > 1 && parts[1].length !== 0)
  90. return;
  91. generateFailure({ ast: ast, context: context, message: Rule.FAILURE_STRING_ATTR });
  92. };
  93. return TemplateVisitorAttrCtrl;
  94. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));
  95. var TemplateVisitorTextCtrl = (function (_super) {
  96. __extends(TemplateVisitorTextCtrl, _super);
  97. function TemplateVisitorTextCtrl() {
  98. var _this = _super !== null && _super.apply(this, arguments) || this;
  99. _this.hasI18n = false;
  100. _this.nestedElements = [];
  101. _this.visited = new Set();
  102. return _this;
  103. }
  104. TemplateVisitorTextCtrl.prototype.getCheckOption = function () {
  105. return OPTION_CHECK_TEXT;
  106. };
  107. TemplateVisitorTextCtrl.prototype.visitBoundText = function (text, context) {
  108. this.validateBoundText(text, context);
  109. _super.prototype.visitBoundText.call(this, text, context);
  110. };
  111. TemplateVisitorTextCtrl.prototype.visitElement = function (element, context) {
  112. var originalI18n = this.hasI18n;
  113. this.hasI18n = originalI18n || element.attrs.some(function (e) { return e.name === 'i18n'; });
  114. this.nestedElements.push(element.name);
  115. _super.prototype.visitElement.call(this, element, context);
  116. this.nestedElements.pop();
  117. this.hasI18n = originalI18n;
  118. _super.prototype.visitElement.call(this, element, context);
  119. };
  120. TemplateVisitorTextCtrl.prototype.visitText = function (text, context) {
  121. this.validateText(text, context);
  122. _super.prototype.visitText.call(this, text, context);
  123. };
  124. TemplateVisitorTextCtrl.prototype.validateBoundText = function (text, context) {
  125. if (this.visited.has(text))
  126. return;
  127. this.visited.add(text);
  128. var value = text.value;
  129. if (!(value instanceof compiler_1.ASTWithSource) || !(value.ast instanceof compiler_1.Interpolation) || this.hasI18n) {
  130. return;
  131. }
  132. var isTextEmpty = !value.ast.strings.some(function (s) { return /\w+/.test(s); });
  133. if (isTextEmpty)
  134. return;
  135. generateFailure({ ast: text, context: context, message: Rule.FAILURE_STRING_TEXT });
  136. };
  137. TemplateVisitorTextCtrl.prototype.validateText = function (text, context) {
  138. if (this.visited.has(text))
  139. return;
  140. this.visited.add(text);
  141. var isTextEmpty = text.value.trim().length === 0;
  142. if (isTextEmpty || (this.hasI18n && this.nestedElements.length > 0))
  143. return;
  144. generateFailure({ ast: text, context: context, message: Rule.FAILURE_STRING_TEXT });
  145. };
  146. return TemplateVisitorTextCtrl;
  147. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));
  148. var TemplateVisitorCtrl = (function (_super) {
  149. __extends(TemplateVisitorCtrl, _super);
  150. function TemplateVisitorCtrl() {
  151. var _this = _super !== null && _super.apply(this, arguments) || this;
  152. _this.visitors = [
  153. new TemplateVisitorAttrCtrl(_this.getSourceFile(), _this.getOptions(), _this.context, _this.templateStart),
  154. new TemplateVisitorTextCtrl(_this.getSourceFile(), _this.getOptions(), _this.context, _this.templateStart)
  155. ];
  156. return _this;
  157. }
  158. TemplateVisitorCtrl.prototype.visit = function (node, context) {
  159. _super.prototype.visit.call(this, node, context);
  160. };
  161. TemplateVisitorCtrl.prototype.visitAttr = function (ast, context) {
  162. this.validateAttr(ast);
  163. _super.prototype.visitAttr.call(this, ast, context);
  164. };
  165. TemplateVisitorCtrl.prototype.visitBoundText = function (text, context) {
  166. this.validateBoundText(text);
  167. _super.prototype.visitBoundText.call(this, text, context);
  168. };
  169. TemplateVisitorCtrl.prototype.visitElement = function (element, context) {
  170. this.validateElement(element);
  171. _super.prototype.visitElement.call(this, element, context);
  172. };
  173. TemplateVisitorCtrl.prototype.visitText = function (text, context) {
  174. this.validateText(text);
  175. _super.prototype.visitText.call(this, text, context);
  176. };
  177. TemplateVisitorCtrl.prototype.validateAttr = function (ast) {
  178. var _this = this;
  179. var options = this.getOptions();
  180. this.visitors
  181. .filter(function (v) { return options.indexOf(v.getCheckOption()) !== -1; })
  182. .map(function (v) { return v.visitAttr(ast, _this); })
  183. .filter(isNotNullOrUndefined_1.isNotNullOrUndefined)
  184. .forEach(function (f) {
  185. return _this.addFailureFromStartToEnd(f.getStartPosition().getPosition(), f.getEndPosition().getPosition(), f.getFailure(), f.getFix());
  186. });
  187. };
  188. TemplateVisitorCtrl.prototype.validateBoundText = function (text) {
  189. var _this = this;
  190. var options = this.getOptions();
  191. this.visitors
  192. .filter(function (v) { return options.indexOf(v.getCheckOption()) !== -1; })
  193. .map(function (v) { return v.visitBoundText(text, _this); })
  194. .filter(isNotNullOrUndefined_1.isNotNullOrUndefined)
  195. .forEach(function (f) {
  196. return _this.addFailureFromStartToEnd(f.getStartPosition().getPosition(), f.getEndPosition().getPosition(), f.getFailure(), f.getFix());
  197. });
  198. };
  199. TemplateVisitorCtrl.prototype.validateElement = function (element) {
  200. var _this = this;
  201. var options = this.getOptions();
  202. this.visitors
  203. .filter(function (v) { return options.indexOf(v.getCheckOption()) !== -1; })
  204. .map(function (v) { return v.visitElement(element, _this); })
  205. .filter(isNotNullOrUndefined_1.isNotNullOrUndefined)
  206. .forEach(function (f) {
  207. return _this.addFailureFromStartToEnd(f.getStartPosition().getPosition(), f.getEndPosition().getPosition(), f.getFailure(), f.getFix());
  208. });
  209. };
  210. TemplateVisitorCtrl.prototype.validateText = function (text) {
  211. var _this = this;
  212. var options = this.getOptions();
  213. this.visitors
  214. .filter(function (v) { return options.indexOf(v.getCheckOption()) !== -1; })
  215. .map(function (v) { return v.visitText(text, _this); })
  216. .filter(isNotNullOrUndefined_1.isNotNullOrUndefined)
  217. .forEach(function (f) {
  218. return _this.addFailureFromStartToEnd(f.getStartPosition().getPosition(), f.getEndPosition().getPosition(), f.getFailure(), f.getFix());
  219. });
  220. };
  221. return TemplateVisitorCtrl;
  222. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));
  223. var templateObject_1;