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 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 | // Requirements
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | const astUtils = require("./utils/ast-utils");
|
---|
14 |
|
---|
15 | //------------------------------------------------------------------------------
|
---|
16 | // Rule Definition
|
---|
17 | //------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | /** @type {import('../shared/types').Rule} */
|
---|
20 | module.exports = {
|
---|
21 | meta: {
|
---|
22 | deprecated: true,
|
---|
23 | replacedBy: [],
|
---|
24 | type: "suggestion",
|
---|
25 |
|
---|
26 | docs: {
|
---|
27 | description: "Disallow leading or trailing decimal points in numeric literals",
|
---|
28 | recommended: false,
|
---|
29 | url: "https://eslint.org/docs/latest/rules/no-floating-decimal"
|
---|
30 | },
|
---|
31 |
|
---|
32 | schema: [],
|
---|
33 | fixable: "code",
|
---|
34 | messages: {
|
---|
35 | leading: "A leading decimal point can be confused with a dot.",
|
---|
36 | trailing: "A trailing decimal point can be confused with a dot."
|
---|
37 | }
|
---|
38 | },
|
---|
39 |
|
---|
40 | create(context) {
|
---|
41 | const sourceCode = context.sourceCode;
|
---|
42 |
|
---|
43 | return {
|
---|
44 | Literal(node) {
|
---|
45 |
|
---|
46 | if (typeof node.value === "number") {
|
---|
47 | if (node.raw.startsWith(".")) {
|
---|
48 | context.report({
|
---|
49 | node,
|
---|
50 | messageId: "leading",
|
---|
51 | fix(fixer) {
|
---|
52 | const tokenBefore = sourceCode.getTokenBefore(node);
|
---|
53 | const needsSpaceBefore = tokenBefore &&
|
---|
54 | tokenBefore.range[1] === node.range[0] &&
|
---|
55 | !astUtils.canTokensBeAdjacent(tokenBefore, `0${node.raw}`);
|
---|
56 |
|
---|
57 | return fixer.insertTextBefore(node, needsSpaceBefore ? " 0" : "0");
|
---|
58 | }
|
---|
59 | });
|
---|
60 | }
|
---|
61 | if (node.raw.indexOf(".") === node.raw.length - 1) {
|
---|
62 | context.report({
|
---|
63 | node,
|
---|
64 | messageId: "trailing",
|
---|
65 | fix: fixer => fixer.insertTextAfter(node, "0")
|
---|
66 | });
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | };
|
---|
71 |
|
---|
72 | }
|
---|
73 | };
|
---|