templateNoAutofocusRule.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 lib_1 = require("tslint/lib");
  17. var ngWalker_1 = require("./angular/ngWalker");
  18. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  19. var Rule = (function (_super) {
  20. __extends(Rule, _super);
  21. function Rule() {
  22. return _super !== null && _super.apply(this, arguments) || this;
  23. }
  24. Rule.prototype.apply = function (sourceFile) {
  25. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  26. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  27. return this.applyWithWalker(walker);
  28. };
  29. Rule.metadata = {
  30. description: 'Ensure that autofocus property is not used',
  31. options: null,
  32. optionsDescription: 'Not configurable.',
  33. rationale: 'autofocus attribute reduces usability and accessibility for users.',
  34. ruleName: 'template-no-autofocus',
  35. type: 'functionality',
  36. typescriptOnly: true
  37. };
  38. Rule.FAILURE_STRING = 'autofocus attribute should not be used, as it reduces usability and accessibility for users.';
  39. return Rule;
  40. }(lib_1.Rules.AbstractRule));
  41. exports.Rule = Rule;
  42. var TemplateVisitorCtrl = (function (_super) {
  43. __extends(TemplateVisitorCtrl, _super);
  44. function TemplateVisitorCtrl() {
  45. return _super !== null && _super.apply(this, arguments) || this;
  46. }
  47. TemplateVisitorCtrl.prototype.visitAttr = function (ast, context) {
  48. this.validateAttribute(ast);
  49. _super.prototype.visitAttr.call(this, ast, context);
  50. };
  51. TemplateVisitorCtrl.prototype.visitElementProperty = function (ast) {
  52. this.validateAttribute(ast);
  53. _super.prototype.visitElementProperty.call(this, ast, context);
  54. };
  55. TemplateVisitorCtrl.prototype.validateAttribute = function (ast) {
  56. if (ast.name === 'autofocus') {
  57. var _a = ast.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  58. this.addFailureFromStartToEnd(startOffset, endOffset, Rule.FAILURE_STRING);
  59. }
  60. };
  61. return TemplateVisitorCtrl;
  62. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));