[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to disallow whitespace before properties
|
---|
| 3 | * @author Kai Cataldo
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const astUtils = require("./utils/ast-utils");
|
---|
| 13 |
|
---|
| 14 | //------------------------------------------------------------------------------
|
---|
| 15 | // Rule Definition
|
---|
| 16 | //------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | /** @type {import('../shared/types').Rule} */
|
---|
| 19 | module.exports = {
|
---|
| 20 | meta: {
|
---|
| 21 | deprecated: true,
|
---|
| 22 | replacedBy: [],
|
---|
| 23 | type: "layout",
|
---|
| 24 |
|
---|
| 25 | docs: {
|
---|
| 26 | description: "Disallow whitespace before properties",
|
---|
| 27 | recommended: false,
|
---|
| 28 | url: "https://eslint.org/docs/latest/rules/no-whitespace-before-property"
|
---|
| 29 | },
|
---|
| 30 |
|
---|
| 31 | fixable: "whitespace",
|
---|
| 32 | schema: [],
|
---|
| 33 |
|
---|
| 34 | messages: {
|
---|
| 35 | unexpectedWhitespace: "Unexpected whitespace before property {{propName}}."
|
---|
| 36 | }
|
---|
| 37 | },
|
---|
| 38 |
|
---|
| 39 | create(context) {
|
---|
| 40 | const sourceCode = context.sourceCode;
|
---|
| 41 |
|
---|
| 42 | //--------------------------------------------------------------------------
|
---|
| 43 | // Helpers
|
---|
| 44 | //--------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Reports whitespace before property token
|
---|
| 48 | * @param {ASTNode} node the node to report in the event of an error
|
---|
| 49 | * @param {Token} leftToken the left token
|
---|
| 50 | * @param {Token} rightToken the right token
|
---|
| 51 | * @returns {void}
|
---|
| 52 | * @private
|
---|
| 53 | */
|
---|
| 54 | function reportError(node, leftToken, rightToken) {
|
---|
| 55 | context.report({
|
---|
| 56 | node,
|
---|
| 57 | messageId: "unexpectedWhitespace",
|
---|
| 58 | data: {
|
---|
| 59 | propName: sourceCode.getText(node.property)
|
---|
| 60 | },
|
---|
| 61 | fix(fixer) {
|
---|
| 62 | let replacementText = "";
|
---|
| 63 |
|
---|
| 64 | if (!node.computed && !node.optional && astUtils.isDecimalInteger(node.object)) {
|
---|
| 65 |
|
---|
| 66 | /*
|
---|
| 67 | * If the object is a number literal, fixing it to something like 5.toString() would cause a SyntaxError.
|
---|
| 68 | * Don't fix this case.
|
---|
| 69 | */
|
---|
| 70 | return null;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // Don't fix if comments exist.
|
---|
| 74 | if (sourceCode.commentsExistBetween(leftToken, rightToken)) {
|
---|
| 75 | return null;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | if (node.optional) {
|
---|
| 79 | replacementText = "?.";
|
---|
| 80 | } else if (!node.computed) {
|
---|
| 81 | replacementText = ".";
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], replacementText);
|
---|
| 85 | }
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | //--------------------------------------------------------------------------
|
---|
| 90 | // Public
|
---|
| 91 | //--------------------------------------------------------------------------
|
---|
| 92 |
|
---|
| 93 | return {
|
---|
| 94 | MemberExpression(node) {
|
---|
| 95 | let rightToken;
|
---|
| 96 | let leftToken;
|
---|
| 97 |
|
---|
| 98 | if (!astUtils.isTokenOnSameLine(node.object, node.property)) {
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (node.computed) {
|
---|
| 103 | rightToken = sourceCode.getTokenBefore(node.property, astUtils.isOpeningBracketToken);
|
---|
| 104 | leftToken = sourceCode.getTokenBefore(rightToken, node.optional ? 1 : 0);
|
---|
| 105 | } else {
|
---|
| 106 | rightToken = sourceCode.getFirstToken(node.property);
|
---|
| 107 | leftToken = sourceCode.getTokenBefore(rightToken, 1);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken)) {
|
---|
| 111 | reportError(node, leftToken, rightToken);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | };
|
---|
| 115 | }
|
---|
| 116 | };
|
---|