[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Disallow use of multiple spaces.
|
---|
| 3 | * @author Nicholas C. Zakas
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | "use strict";
|
---|
| 8 |
|
---|
| 9 | const astUtils = require("./utils/ast-utils");
|
---|
| 10 |
|
---|
| 11 | //------------------------------------------------------------------------------
|
---|
| 12 | // Rule Definition
|
---|
| 13 | //------------------------------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | /** @type {import('../shared/types').Rule} */
|
---|
| 16 | module.exports = {
|
---|
| 17 | meta: {
|
---|
| 18 | deprecated: true,
|
---|
| 19 | replacedBy: [],
|
---|
| 20 | type: "layout",
|
---|
| 21 |
|
---|
| 22 | docs: {
|
---|
| 23 | description: "Disallow multiple spaces",
|
---|
| 24 | recommended: false,
|
---|
| 25 | url: "https://eslint.org/docs/latest/rules/no-multi-spaces"
|
---|
| 26 | },
|
---|
| 27 |
|
---|
| 28 | fixable: "whitespace",
|
---|
| 29 |
|
---|
| 30 | schema: [
|
---|
| 31 | {
|
---|
| 32 | type: "object",
|
---|
| 33 | properties: {
|
---|
| 34 | exceptions: {
|
---|
| 35 | type: "object",
|
---|
| 36 | patternProperties: {
|
---|
| 37 | "^([A-Z][a-z]*)+$": {
|
---|
| 38 | type: "boolean"
|
---|
| 39 | }
|
---|
| 40 | },
|
---|
| 41 | additionalProperties: false
|
---|
| 42 | },
|
---|
| 43 | ignoreEOLComments: {
|
---|
| 44 | type: "boolean",
|
---|
| 45 | default: false
|
---|
| 46 | }
|
---|
| 47 | },
|
---|
| 48 | additionalProperties: false
|
---|
| 49 | }
|
---|
| 50 | ],
|
---|
| 51 |
|
---|
| 52 | messages: {
|
---|
| 53 | multipleSpaces: "Multiple spaces found before '{{displayValue}}'."
|
---|
| 54 | }
|
---|
| 55 | },
|
---|
| 56 |
|
---|
| 57 | create(context) {
|
---|
| 58 | const sourceCode = context.sourceCode;
|
---|
| 59 | const options = context.options[0] || {};
|
---|
| 60 | const ignoreEOLComments = options.ignoreEOLComments;
|
---|
| 61 | const exceptions = Object.assign({ Property: true }, options.exceptions);
|
---|
| 62 | const hasExceptions = Object.keys(exceptions).some(key => exceptions[key]);
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * Formats value of given comment token for error message by truncating its length.
|
---|
| 66 | * @param {Token} token comment token
|
---|
| 67 | * @returns {string} formatted value
|
---|
| 68 | * @private
|
---|
| 69 | */
|
---|
| 70 | function formatReportedCommentValue(token) {
|
---|
| 71 | const valueLines = token.value.split("\n");
|
---|
| 72 | const value = valueLines[0];
|
---|
| 73 | const formattedValue = `${value.slice(0, 12)}...`;
|
---|
| 74 |
|
---|
| 75 | return valueLines.length === 1 && value.length <= 12 ? value : formattedValue;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | //--------------------------------------------------------------------------
|
---|
| 79 | // Public
|
---|
| 80 | //--------------------------------------------------------------------------
|
---|
| 81 |
|
---|
| 82 | return {
|
---|
| 83 | Program() {
|
---|
| 84 | sourceCode.tokensAndComments.forEach((leftToken, leftIndex, tokensAndComments) => {
|
---|
| 85 | if (leftIndex === tokensAndComments.length - 1) {
|
---|
| 86 | return;
|
---|
| 87 | }
|
---|
| 88 | const rightToken = tokensAndComments[leftIndex + 1];
|
---|
| 89 |
|
---|
| 90 | // Ignore tokens that don't have 2 spaces between them or are on different lines
|
---|
| 91 | if (
|
---|
| 92 | !sourceCode.text.slice(leftToken.range[1], rightToken.range[0]).includes(" ") ||
|
---|
| 93 | leftToken.loc.end.line < rightToken.loc.start.line
|
---|
| 94 | ) {
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | // Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
|
---|
| 99 | if (
|
---|
| 100 | ignoreEOLComments &&
|
---|
| 101 | astUtils.isCommentToken(rightToken) &&
|
---|
| 102 | (
|
---|
| 103 | leftIndex === tokensAndComments.length - 2 ||
|
---|
| 104 | rightToken.loc.end.line < tokensAndComments[leftIndex + 2].loc.start.line
|
---|
| 105 | )
|
---|
| 106 | ) {
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | // Ignore tokens that are in a node in the "exceptions" object
|
---|
| 111 | if (hasExceptions) {
|
---|
| 112 | const parentNode = sourceCode.getNodeByRangeIndex(rightToken.range[0] - 1);
|
---|
| 113 |
|
---|
| 114 | if (parentNode && exceptions[parentNode.type]) {
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | let displayValue;
|
---|
| 120 |
|
---|
| 121 | if (rightToken.type === "Block") {
|
---|
| 122 | displayValue = `/*${formatReportedCommentValue(rightToken)}*/`;
|
---|
| 123 | } else if (rightToken.type === "Line") {
|
---|
| 124 | displayValue = `//${formatReportedCommentValue(rightToken)}`;
|
---|
| 125 | } else {
|
---|
| 126 | displayValue = rightToken.value;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | context.report({
|
---|
| 130 | node: rightToken,
|
---|
| 131 | loc: { start: leftToken.loc.end, end: rightToken.loc.start },
|
---|
| 132 | messageId: "multipleSpaces",
|
---|
| 133 | data: { displayValue },
|
---|
| 134 | fix: fixer => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ")
|
---|
| 135 | });
|
---|
| 136 | });
|
---|
| 137 | }
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | }
|
---|
| 141 | };
|
---|