项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

66 line
2.2 KiB

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