项目原始demo,不改动
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Pode ver ficheiros e cloná-lo, mas não pode fazer envios ou lançar questões ou pedidos de integração.
 
 
 
 

117 linhas
3.9 KiB

  1. /**
  2. * @fileoverview Rule to enforce the position of line comments
  3. * @author Alberto Rodríguez
  4. */
  5. "use strict";
  6. const astUtils = require("../ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "enforce position of line comments",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/line-comment-position"
  17. },
  18. schema: [
  19. {
  20. oneOf: [
  21. {
  22. enum: ["above", "beside"]
  23. },
  24. {
  25. type: "object",
  26. properties: {
  27. position: {
  28. enum: ["above", "beside"]
  29. },
  30. ignorePattern: {
  31. type: "string"
  32. },
  33. applyDefaultPatterns: {
  34. type: "boolean"
  35. },
  36. applyDefaultIgnorePatterns: {
  37. type: "boolean"
  38. }
  39. },
  40. additionalProperties: false
  41. }
  42. ]
  43. }
  44. ]
  45. },
  46. create(context) {
  47. const options = context.options[0];
  48. let above,
  49. ignorePattern,
  50. applyDefaultIgnorePatterns = true;
  51. if (!options || typeof options === "string") {
  52. above = !options || options === "above";
  53. } else {
  54. above = options.position === "above";
  55. ignorePattern = options.ignorePattern;
  56. if (options.hasOwnProperty("applyDefaultIgnorePatterns")) {
  57. applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns !== false;
  58. } else {
  59. applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false;
  60. }
  61. }
  62. const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
  63. const fallThroughRegExp = /^\s*falls?\s?through/;
  64. const customIgnoreRegExp = new RegExp(ignorePattern);
  65. const sourceCode = context.getSourceCode();
  66. //--------------------------------------------------------------------------
  67. // Public
  68. //--------------------------------------------------------------------------
  69. return {
  70. Program() {
  71. const comments = sourceCode.getAllComments();
  72. comments.filter(token => token.type === "Line").forEach(node => {
  73. if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) {
  74. return;
  75. }
  76. if (ignorePattern && customIgnoreRegExp.test(node.value)) {
  77. return;
  78. }
  79. const previous = sourceCode.getTokenBefore(node, { includeComments: true });
  80. const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line;
  81. if (above) {
  82. if (isOnSameLine) {
  83. context.report({
  84. node,
  85. message: "Expected comment to be above code."
  86. });
  87. }
  88. } else {
  89. if (!isOnSameLine) {
  90. context.report({
  91. node,
  92. message: "Expected comment to be beside code."
  93. });
  94. }
  95. }
  96. });
  97. }
  98. };
  99. }
  100. };