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-floating-decimal.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @fileoverview Rule to flag use of a leading/trailing decimal point in a numeric literal
  3. * @author James Allardice
  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: "suggestion",
  16. docs: {
  17. description: "disallow leading or trailing decimal points in numeric literals",
  18. category: "Best Practices",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-floating-decimal"
  21. },
  22. schema: [],
  23. fixable: "code"
  24. },
  25. create(context) {
  26. const sourceCode = context.getSourceCode();
  27. return {
  28. Literal(node) {
  29. if (typeof node.value === "number") {
  30. if (node.raw.startsWith(".")) {
  31. context.report({
  32. node,
  33. message: "A leading decimal point can be confused with a dot.",
  34. fix(fixer) {
  35. const tokenBefore = sourceCode.getTokenBefore(node);
  36. const needsSpaceBefore = tokenBefore &&
  37. tokenBefore.range[1] === node.range[0] &&
  38. !astUtils.canTokensBeAdjacent(tokenBefore, `0${node.raw}`);
  39. return fixer.insertTextBefore(node, needsSpaceBefore ? " 0" : "0");
  40. }
  41. });
  42. }
  43. if (node.raw.indexOf(".") === node.raw.length - 1) {
  44. context.report({
  45. node,
  46. message: "A trailing decimal point can be confused with a dot.",
  47. fix: fixer => fixer.insertTextAfter(node, "0")
  48. });
  49. }
  50. }
  51. }
  52. };
  53. }
  54. };