templateAccessibilityValidAriaRule.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 __spreadArrays = (this && this.__spreadArrays) || function () {
  16. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  17. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  18. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  19. r[k] = a[j];
  20. return r;
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. var aria_query_1 = require("aria-query");
  24. var lib_1 = require("tslint/lib");
  25. var ngWalker_1 = require("./angular/ngWalker");
  26. var basicTemplateAstVisitor_1 = require("./angular/templates/basicTemplateAstVisitor");
  27. var getSuggestion_1 = require("./util/getSuggestion");
  28. var ariaAttributes = __spreadArrays(Array.from(aria_query_1.aria.keys()));
  29. var Rule = (function (_super) {
  30. __extends(Rule, _super);
  31. function Rule() {
  32. return _super !== null && _super.apply(this, arguments) || this;
  33. }
  34. Rule.prototype.apply = function (sourceFile) {
  35. var walkerConfig = { templateVisitorCtrl: TemplateVisitorCtrl };
  36. var walker = new ngWalker_1.NgWalker(sourceFile, this.getOptions(), walkerConfig);
  37. return this.applyWithWalker(walker);
  38. };
  39. Rule.metadata = {
  40. description: 'Ensures that the correct ARIA attributes are used',
  41. options: null,
  42. optionsDescription: 'Not configurable.',
  43. rationale: 'Elements should not use invalid aria attributes (AX_ARIA_11)',
  44. ruleName: 'template-accessibility-valid-aria',
  45. type: 'functionality',
  46. typescriptOnly: true
  47. };
  48. return Rule;
  49. }(lib_1.Rules.AbstractRule));
  50. exports.Rule = Rule;
  51. exports.getFailureMessage = function (name) {
  52. var suggestions = getSuggestion_1.getSuggestion(name, ariaAttributes);
  53. var message = name + ": This attribute is an invalid ARIA attribute.";
  54. if (suggestions.length > 0) {
  55. return message + " Did you mean to use " + suggestions + "?";
  56. }
  57. return message;
  58. };
  59. var TemplateVisitorCtrl = (function (_super) {
  60. __extends(TemplateVisitorCtrl, _super);
  61. function TemplateVisitorCtrl() {
  62. return _super !== null && _super.apply(this, arguments) || this;
  63. }
  64. TemplateVisitorCtrl.prototype.visitAttr = function (ast, context) {
  65. this.validateAttribute(ast);
  66. _super.prototype.visitAttr.call(this, ast, context);
  67. };
  68. TemplateVisitorCtrl.prototype.visitElementProperty = function (ast) {
  69. this.validateAttribute(ast);
  70. _super.prototype.visitElementProperty.call(this, ast, context);
  71. };
  72. TemplateVisitorCtrl.prototype.validateAttribute = function (ast) {
  73. if (ast.name.indexOf('aria-') !== 0)
  74. return;
  75. var isValid = ariaAttributes.indexOf(ast.name) > -1;
  76. if (isValid)
  77. return;
  78. var _a = ast.sourceSpan, endOffset = _a.end.offset, startOffset = _a.start.offset;
  79. this.addFailureFromStartToEnd(startOffset, endOffset, exports.getFailureMessage(ast.name));
  80. };
  81. return TemplateVisitorCtrl;
  82. }(basicTemplateAstVisitor_1.BasicTemplateAstVisitor));