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.

dot-location.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @fileoverview Validates newlines before and after dots
  3. * @author Greg Cochard
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce consistent newlines before and after dots",
  15. category: "Best Practices",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/dot-location"
  18. },
  19. schema: [
  20. {
  21. enum: ["object", "property"]
  22. }
  23. ],
  24. fixable: "code",
  25. messages: {
  26. expectedDotAfterObject: "Expected dot to be on same line as object.",
  27. expectedDotBeforeProperty: "Expected dot to be on same line as property."
  28. }
  29. },
  30. create(context) {
  31. const config = context.options[0];
  32. // default to onObject if no preference is passed
  33. const onObject = config === "object" || !config;
  34. const sourceCode = context.getSourceCode();
  35. /**
  36. * Reports if the dot between object and property is on the correct loccation.
  37. * @param {ASTNode} obj The object owning the property.
  38. * @param {ASTNode} prop The property of the object.
  39. * @param {ASTNode} node The corresponding node of the token.
  40. * @returns {void}
  41. */
  42. function checkDotLocation(obj, prop, node) {
  43. const dot = sourceCode.getTokenBefore(prop);
  44. const textBeforeDot = sourceCode.getText().slice(obj.range[1], dot.range[0]);
  45. const textAfterDot = sourceCode.getText().slice(dot.range[1], prop.range[0]);
  46. if (dot.type === "Punctuator" && dot.value === ".") {
  47. if (onObject) {
  48. if (!astUtils.isTokenOnSameLine(obj, dot)) {
  49. const neededTextAfterObj = astUtils.isDecimalInteger(obj) ? " " : "";
  50. context.report({
  51. node,
  52. loc: dot.loc.start,
  53. messageId: "expectedDotAfterObject",
  54. fix: fixer => fixer.replaceTextRange([obj.range[1], prop.range[0]], `${neededTextAfterObj}.${textBeforeDot}${textAfterDot}`)
  55. });
  56. }
  57. } else if (!astUtils.isTokenOnSameLine(dot, prop)) {
  58. context.report({
  59. node,
  60. loc: dot.loc.start,
  61. messageId: "expectedDotBeforeProperty",
  62. fix: fixer => fixer.replaceTextRange([obj.range[1], prop.range[0]], `${textBeforeDot}${textAfterDot}.`)
  63. });
  64. }
  65. }
  66. }
  67. /**
  68. * Checks the spacing of the dot within a member expression.
  69. * @param {ASTNode} node The node to check.
  70. * @returns {void}
  71. */
  72. function checkNode(node) {
  73. checkDotLocation(node.object, node.property, node);
  74. }
  75. return {
  76. MemberExpression: checkNode
  77. };
  78. }
  79. };