no-floating-decimal.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @fileoverview Rule to flag use of a leading/trailing decimal point in a numeric literal
  3. * @author James Allardice
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. /** @type {import('../types').Rule.RuleModule} */
  15. module.exports = {
  16. meta: {
  17. deprecated: {
  18. message: "Formatting rules are being moved out of ESLint core.",
  19. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  20. deprecatedSince: "8.53.0",
  21. availableUntil: "11.0.0",
  22. replacedBy: [
  23. {
  24. message:
  25. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  26. url: "https://eslint.style/guide/migration",
  27. plugin: {
  28. name: "@stylistic/eslint-plugin",
  29. url: "https://eslint.style",
  30. },
  31. rule: {
  32. name: "no-floating-decimal",
  33. url: "https://eslint.style/rules/no-floating-decimal",
  34. },
  35. },
  36. ],
  37. },
  38. type: "suggestion",
  39. docs: {
  40. description:
  41. "Disallow leading or trailing decimal points in numeric literals",
  42. recommended: false,
  43. url: "https://eslint.org/docs/latest/rules/no-floating-decimal",
  44. },
  45. schema: [],
  46. fixable: "code",
  47. messages: {
  48. leading: "A leading decimal point can be confused with a dot.",
  49. trailing: "A trailing decimal point can be confused with a dot.",
  50. },
  51. },
  52. create(context) {
  53. const sourceCode = context.sourceCode;
  54. return {
  55. Literal(node) {
  56. if (typeof node.value === "number") {
  57. if (node.raw.startsWith(".")) {
  58. context.report({
  59. node,
  60. messageId: "leading",
  61. fix(fixer) {
  62. const tokenBefore =
  63. sourceCode.getTokenBefore(node);
  64. const needsSpaceBefore =
  65. tokenBefore &&
  66. tokenBefore.range[1] === node.range[0] &&
  67. !astUtils.canTokensBeAdjacent(
  68. tokenBefore,
  69. `0${node.raw}`,
  70. );
  71. return fixer.insertTextBefore(
  72. node,
  73. needsSpaceBefore ? " 0" : "0",
  74. );
  75. },
  76. });
  77. }
  78. if (node.raw.indexOf(".") === node.raw.length - 1) {
  79. context.report({
  80. node,
  81. messageId: "trailing",
  82. fix: fixer => fixer.insertTextAfter(node, "0"),
  83. });
  84. }
  85. }
  86. },
  87. };
  88. },
  89. };