项目原始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.
 
 
 
 

195 lines
7.5 KiB

  1. /**
  2. * @fileoverview Require or disallow newlines around directives.
  3. * @author Kai Cataldo
  4. * @deprecated
  5. */
  6. "use strict";
  7. const astUtils = require("../ast-utils");
  8. //------------------------------------------------------------------------------
  9. // Rule Definition
  10. //------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. description: "require or disallow newlines around directives",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. replacedBy: ["padding-line-between-statements"],
  18. url: "https://eslint.org/docs/rules/lines-around-directive"
  19. },
  20. schema: [{
  21. oneOf: [
  22. {
  23. enum: ["always", "never"]
  24. },
  25. {
  26. type: "object",
  27. properties: {
  28. before: {
  29. enum: ["always", "never"]
  30. },
  31. after: {
  32. enum: ["always", "never"]
  33. }
  34. },
  35. additionalProperties: false,
  36. minProperties: 2
  37. }
  38. ]
  39. }],
  40. fixable: "whitespace",
  41. deprecated: true
  42. },
  43. create(context) {
  44. const sourceCode = context.getSourceCode();
  45. const config = context.options[0] || "always";
  46. const expectLineBefore = typeof config === "string" ? config : config.before;
  47. const expectLineAfter = typeof config === "string" ? config : config.after;
  48. //--------------------------------------------------------------------------
  49. // Helpers
  50. //--------------------------------------------------------------------------
  51. /**
  52. * Check if node is preceded by a blank newline.
  53. * @param {ASTNode} node Node to check.
  54. * @returns {boolean} Whether or not the passed in node is preceded by a blank newline.
  55. */
  56. function hasNewlineBefore(node) {
  57. const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
  58. const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0;
  59. return node.loc.start.line - tokenLineBefore >= 2;
  60. }
  61. /**
  62. * Gets the last token of a node that is on the same line as the rest of the node.
  63. * This will usually be the last token of the node, but it will be the second-to-last token if the node has a trailing
  64. * semicolon on a different line.
  65. * @param {ASTNode} node A directive node
  66. * @returns {Token} The last token of the node on the line
  67. */
  68. function getLastTokenOnLine(node) {
  69. const lastToken = sourceCode.getLastToken(node);
  70. const secondToLastToken = sourceCode.getTokenBefore(lastToken);
  71. return astUtils.isSemicolonToken(lastToken) && lastToken.loc.start.line > secondToLastToken.loc.end.line
  72. ? secondToLastToken
  73. : lastToken;
  74. }
  75. /**
  76. * Check if node is followed by a blank newline.
  77. * @param {ASTNode} node Node to check.
  78. * @returns {boolean} Whether or not the passed in node is followed by a blank newline.
  79. */
  80. function hasNewlineAfter(node) {
  81. const lastToken = getLastTokenOnLine(node);
  82. const tokenAfter = sourceCode.getTokenAfter(lastToken, { includeComments: true });
  83. return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
  84. }
  85. /**
  86. * Report errors for newlines around directives.
  87. * @param {ASTNode} node Node to check.
  88. * @param {string} location Whether the error was found before or after the directive.
  89. * @param {boolean} expected Whether or not a newline was expected or unexpected.
  90. * @returns {void}
  91. */
  92. function reportError(node, location, expected) {
  93. context.report({
  94. node,
  95. message: "{{expected}} newline {{location}} \"{{value}}\" directive.",
  96. data: {
  97. expected: expected ? "Expected" : "Unexpected",
  98. value: node.expression.value,
  99. location
  100. },
  101. fix(fixer) {
  102. const lastToken = getLastTokenOnLine(node);
  103. if (expected) {
  104. return location === "before" ? fixer.insertTextBefore(node, "\n") : fixer.insertTextAfter(lastToken, "\n");
  105. }
  106. return fixer.removeRange(location === "before" ? [node.range[0] - 1, node.range[0]] : [lastToken.range[1], lastToken.range[1] + 1]);
  107. }
  108. });
  109. }
  110. /**
  111. * Check lines around directives in node
  112. * @param {ASTNode} node - node to check
  113. * @returns {void}
  114. */
  115. function checkDirectives(node) {
  116. const directives = astUtils.getDirectivePrologue(node);
  117. if (!directives.length) {
  118. return;
  119. }
  120. const firstDirective = directives[0];
  121. const leadingComments = sourceCode.getCommentsBefore(firstDirective);
  122. /*
  123. * Only check before the first directive if it is preceded by a comment or if it is at the top of
  124. * the file and expectLineBefore is set to "never". This is to not force a newline at the top of
  125. * the file if there are no comments as well as for compatibility with padded-blocks.
  126. */
  127. if (leadingComments.length) {
  128. if (expectLineBefore === "always" && !hasNewlineBefore(firstDirective)) {
  129. reportError(firstDirective, "before", true);
  130. }
  131. if (expectLineBefore === "never" && hasNewlineBefore(firstDirective)) {
  132. reportError(firstDirective, "before", false);
  133. }
  134. } else if (
  135. node.type === "Program" &&
  136. expectLineBefore === "never" &&
  137. !leadingComments.length &&
  138. hasNewlineBefore(firstDirective)
  139. ) {
  140. reportError(firstDirective, "before", false);
  141. }
  142. const lastDirective = directives[directives.length - 1];
  143. const statements = node.type === "Program" ? node.body : node.body.body;
  144. /*
  145. * Do not check after the last directive if the body only
  146. * contains a directive prologue and isn't followed by a comment to ensure
  147. * this rule behaves well with padded-blocks.
  148. */
  149. if (lastDirective === statements[statements.length - 1] && !lastDirective.trailingComments) {
  150. return;
  151. }
  152. if (expectLineAfter === "always" && !hasNewlineAfter(lastDirective)) {
  153. reportError(lastDirective, "after", true);
  154. }
  155. if (expectLineAfter === "never" && hasNewlineAfter(lastDirective)) {
  156. reportError(lastDirective, "after", false);
  157. }
  158. }
  159. //--------------------------------------------------------------------------
  160. // Public
  161. //--------------------------------------------------------------------------
  162. return {
  163. Program: checkDirectives,
  164. FunctionDeclaration: checkDirectives,
  165. FunctionExpression: checkDirectives,
  166. ArrowFunctionExpression: checkDirectives
  167. };
  168. }
  169. };