noIrregularWhitespaceRule.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var Lint = require("../index");
  21. var Rule = /** @class */ (function (_super) {
  22. tslib_1.__extends(Rule, _super);
  23. function Rule() {
  24. return _super !== null && _super.apply(this, arguments) || this;
  25. }
  26. Rule.prototype.apply = function (sourceFile) {
  27. return this.applyWithFunction(sourceFile, walk);
  28. };
  29. /* tslint:disable:object-literal-sort-keys */
  30. Rule.metadata = {
  31. ruleName: "no-irregular-whitespace",
  32. description: "Disallow irregular whitespace within a file, including strings and comments.",
  33. hasFix: true,
  34. optionsDescription: "Not configurable.",
  35. options: null,
  36. optionExamples: [true],
  37. type: "style",
  38. typescriptOnly: false,
  39. };
  40. /* tslint:enable:object-literal-sort-keys */
  41. Rule.FAILURE_STRING = "Irregular whitespace not allowed";
  42. return Rule;
  43. }(Lint.Rules.AbstractRule));
  44. exports.Rule = Rule;
  45. /* Inspired by: https://github.com/eslint/eslint/blob/master/lib/rules/no-irregular-whitespace.js */
  46. /* tslint:disable:max-line-length */
  47. exports.IRREGULAR_WHITESPACE_REGEX = /[\u000b\u000c\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\u2028\u2029]+/mg;
  48. /* tslint:enable:max-line-length */
  49. function walk(ctx) {
  50. exports.IRREGULAR_WHITESPACE_REGEX.lastIndex = 0;
  51. var match;
  52. // tslint:disable-next-line no-conditional-assignment
  53. while ((match = exports.IRREGULAR_WHITESPACE_REGEX.exec(ctx.sourceFile.text)) !== null) {
  54. var start = match.index;
  55. var len = match[0].length;
  56. var fix = new Lint.Replacement(start, len, " ".repeat(len));
  57. ctx.addFailureAt(start, len, Rule.FAILURE_STRING, fix);
  58. }
  59. }