relativeUrlPrefixRule.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 ts = require("typescript");
  18. var ngWalker_1 = require("./angular/ngWalker");
  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 walker = new Walker(sourceFile, this.getOptions());
  26. return this.applyWithWalker(walker);
  27. };
  28. Rule.metadata = {
  29. description: "The ./ prefix is standard syntax for relative URLs; don't depend on Angular's current ability to do without that prefix.",
  30. descriptionDetails: 'See more at https://angular.io/styleguide#style-05-04.',
  31. rationale: 'A component relative URL requires no change when you move the component files, as long as the files stay together.',
  32. ruleName: 'relative-url-prefix',
  33. options: null,
  34. optionsDescription: 'Not configurable.',
  35. type: 'maintainability',
  36. typescriptOnly: true
  37. };
  38. Rule.FAILURE_STRING = 'The ./ prefix is standard syntax for relative URLs. (https://angular.io/styleguide#style-05-04)';
  39. return Rule;
  40. }(lib_1.Rules.AbstractRule));
  41. exports.Rule = Rule;
  42. var Walker = (function (_super) {
  43. __extends(Walker, _super);
  44. function Walker(sourceFile, options) {
  45. return _super.call(this, sourceFile, options) || this;
  46. }
  47. Walker.prototype.visitClassDecorator = function (decorator) {
  48. var _this = this;
  49. if (ts.isCallExpression(decorator.expression) && decorator.expression.arguments) {
  50. decorator.expression.arguments.forEach(function (arg) {
  51. if (ts.isObjectLiteralExpression(arg) && arg.properties) {
  52. arg.properties.forEach(function (prop) {
  53. if (prop && prop.name.text === 'templateUrl') {
  54. var url = prop.initializer.text;
  55. _this.checkTemplateUrl(url, prop.initializer);
  56. }
  57. else if (prop && prop.name.text === 'styleUrls') {
  58. if (prop.initializer.elements.length > 0) {
  59. prop.initializer.elements.forEach(function (e) {
  60. _this.checkStyleUrls(e);
  61. });
  62. }
  63. }
  64. });
  65. }
  66. });
  67. }
  68. _super.prototype.visitClassDecorator.call(this, decorator);
  69. };
  70. Walker.prototype.checkTemplateUrl = function (url, initializer) {
  71. if (url && !/^\.\/[^\.\/|\.\.\/]/.test(url)) {
  72. this.addFailureAtNode(initializer, Rule.FAILURE_STRING);
  73. }
  74. };
  75. Walker.prototype.checkStyleUrls = function (token) {
  76. if (token && token.text) {
  77. if (!/^\.\/[^\.\/|\.\.\/]/.test(token.text)) {
  78. this.addFailureAtNode(token, Rule.FAILURE_STRING);
  79. }
  80. }
  81. };
  82. return Walker;
  83. }(ngWalker_1.NgWalker));