Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-whitespace-before-property.js 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @fileoverview Rule to disallow whitespace before properties
  3. * @author Kai Cataldo
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "disallow whitespace before properties",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-whitespace-before-property"
  21. },
  22. fixable: "whitespace",
  23. schema: []
  24. },
  25. create(context) {
  26. const sourceCode = context.getSourceCode();
  27. //--------------------------------------------------------------------------
  28. // Helpers
  29. //--------------------------------------------------------------------------
  30. /**
  31. * Reports whitespace before property token
  32. * @param {ASTNode} node - the node to report in the event of an error
  33. * @param {Token} leftToken - the left token
  34. * @param {Token} rightToken - the right token
  35. * @returns {void}
  36. * @private
  37. */
  38. function reportError(node, leftToken, rightToken) {
  39. const replacementText = node.computed ? "" : ".";
  40. context.report({
  41. node,
  42. message: "Unexpected whitespace before property {{propName}}.",
  43. data: {
  44. propName: sourceCode.getText(node.property)
  45. },
  46. fix(fixer) {
  47. if (!node.computed && astUtils.isDecimalInteger(node.object)) {
  48. /*
  49. * If the object is a number literal, fixing it to something like 5.toString() would cause a SyntaxError.
  50. * Don't fix this case.
  51. */
  52. return null;
  53. }
  54. return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], replacementText);
  55. }
  56. });
  57. }
  58. //--------------------------------------------------------------------------
  59. // Public
  60. //--------------------------------------------------------------------------
  61. return {
  62. MemberExpression(node) {
  63. let rightToken;
  64. let leftToken;
  65. if (!astUtils.isTokenOnSameLine(node.object, node.property)) {
  66. return;
  67. }
  68. if (node.computed) {
  69. rightToken = sourceCode.getTokenBefore(node.property, astUtils.isOpeningBracketToken);
  70. leftToken = sourceCode.getTokenBefore(rightToken);
  71. } else {
  72. rightToken = sourceCode.getFirstToken(node.property);
  73. leftToken = sourceCode.getTokenBefore(rightToken, 1);
  74. }
  75. if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken)) {
  76. reportError(node, leftToken, rightToken);
  77. }
  78. }
  79. };
  80. }
  81. };