.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-floating-decimal.js
1 /**
2  * @fileoverview Rule to flag use of a leading/trailing decimal point in a numeric literal
3  * @author James Allardice
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 module.exports = {
19     meta: {
20         type: "suggestion",
21
22         docs: {
23             description: "disallow leading or trailing decimal points in numeric literals",
24             category: "Best Practices",
25             recommended: false,
26             url: "https://eslint.org/docs/rules/no-floating-decimal"
27         },
28
29         schema: [],
30         fixable: "code",
31         messages: {
32             leading: "A leading decimal point can be confused with a dot.",
33             trailing: "A trailing decimal point can be confused with a dot."
34         }
35     },
36
37     create(context) {
38         const sourceCode = context.getSourceCode();
39
40         return {
41             Literal(node) {
42
43                 if (typeof node.value === "number") {
44                     if (node.raw.startsWith(".")) {
45                         context.report({
46                             node,
47                             messageId: "leading",
48                             fix(fixer) {
49                                 const tokenBefore = sourceCode.getTokenBefore(node);
50                                 const needsSpaceBefore = tokenBefore &&
51                                     tokenBefore.range[1] === node.range[0] &&
52                                     !astUtils.canTokensBeAdjacent(tokenBefore, `0${node.raw}`);
53
54                                 return fixer.insertTextBefore(node, needsSpaceBefore ? " 0" : "0");
55                             }
56                         });
57                     }
58                     if (node.raw.indexOf(".") === node.raw.length - 1) {
59                         context.report({
60                             node,
61                             messageId: "trailing",
62                             fix: fixer => fixer.insertTextAfter(node, "0")
63                         });
64                     }
65                 }
66             }
67         };
68
69     }
70 };