trailingCommaRule.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var ts = require("typescript");
  8. var Lint = require("../lint");
  9. var Rule = (function (_super) {
  10. __extends(Rule, _super);
  11. function Rule() {
  12. _super.apply(this, arguments);
  13. }
  14. Rule.prototype.apply = function (sourceFile) {
  15. return this.applyWithWalker(new TrailingCommaWalker(sourceFile, this.getOptions()));
  16. };
  17. Rule.FAILURE_STRING_NEVER = "trailing comma";
  18. Rule.FAILURE_STRING_ALWAYS = "missing trailing comma";
  19. return Rule;
  20. }(Lint.Rules.AbstractRule));
  21. exports.Rule = Rule;
  22. var TrailingCommaWalker = (function (_super) {
  23. __extends(TrailingCommaWalker, _super);
  24. function TrailingCommaWalker() {
  25. _super.apply(this, arguments);
  26. }
  27. TrailingCommaWalker.prototype.visitArrayLiteralExpression = function (node) {
  28. this.lintNode(node);
  29. _super.prototype.visitArrayLiteralExpression.call(this, node);
  30. };
  31. TrailingCommaWalker.prototype.visitBindingPattern = function (node) {
  32. if (node.kind === ts.SyntaxKind.ArrayBindingPattern || node.kind === ts.SyntaxKind.ObjectBindingPattern) {
  33. this.lintNode(node);
  34. }
  35. _super.prototype.visitBindingPattern.call(this, node);
  36. };
  37. TrailingCommaWalker.prototype.visitNamedImports = function (node) {
  38. this.lintNode(node);
  39. _super.prototype.visitNamedImports.call(this, node);
  40. };
  41. TrailingCommaWalker.prototype.visitObjectLiteralExpression = function (node) {
  42. this.lintNode(node);
  43. _super.prototype.visitObjectLiteralExpression.call(this, node);
  44. };
  45. TrailingCommaWalker.prototype.lintNode = function (node) {
  46. var child = node.getChildAt(1);
  47. if (child != null && child.kind === ts.SyntaxKind.SyntaxList) {
  48. var isMultiline = child.getText().match(/\n|\r/);
  49. var option = this.getOption(isMultiline ? "multiline" : "singleline");
  50. var grandChildren = child.getChildren();
  51. if (grandChildren.length > 0) {
  52. var lastGrandChild = grandChildren[grandChildren.length - 1];
  53. var hasTrailingComma = lastGrandChild.kind === ts.SyntaxKind.CommaToken;
  54. if (hasTrailingComma && option === "never") {
  55. this.addFailure(this.createFailure(lastGrandChild.getStart(), 1, Rule.FAILURE_STRING_NEVER));
  56. }
  57. else if (!hasTrailingComma && option === "always") {
  58. this.addFailure(this.createFailure(lastGrandChild.getEnd(), 1, Rule.FAILURE_STRING_ALWAYS));
  59. }
  60. }
  61. }
  62. };
  63. TrailingCommaWalker.prototype.getOption = function (option) {
  64. var allOptions = this.getOptions();
  65. if (allOptions == null || allOptions.length === 0) {
  66. return null;
  67. }
  68. return allOptions[0][option];
  69. };
  70. return TrailingCommaWalker;
  71. }(Lint.RuleWalker));