2 * @fileoverview Rule to flag use of a leading/trailing decimal point in a numeric literal
3 * @author James Allardice
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "disallow leading or trailing decimal points in numeric literals",
24 category: "Best Practices",
26 url: "https://eslint.org/docs/rules/no-floating-decimal"
32 leading: "A leading decimal point can be confused with a dot.",
33 trailing: "A trailing decimal point can be confused with a dot."
38 const sourceCode = context.getSourceCode();
43 if (typeof node.value === "number") {
44 if (node.raw.startsWith(".")) {
49 const tokenBefore = sourceCode.getTokenBefore(node);
50 const needsSpaceBefore = tokenBefore &&
51 tokenBefore.range[1] === node.range[0] &&
52 !astUtils.canTokensBeAdjacent(tokenBefore, `0${node.raw}`);
54 return fixer.insertTextBefore(node, needsSpaceBefore ? " 0" : "0");
58 if (node.raw.indexOf(".") === node.raw.length - 1) {
61 messageId: "trailing",
62 fix: fixer => fixer.insertTextAfter(node, "0")