Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

getter-return.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @fileoverview Enforces that a return statement is present in property getters.
  3. * @author Aladdin-ADD(hh_2013@foxmail.com)
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u;
  14. /**
  15. * Checks a given code path segment is reachable.
  16. * @param {CodePathSegment} segment A segment to check.
  17. * @returns {boolean} `true` if the segment is reachable.
  18. */
  19. function isReachable(segment) {
  20. return segment.reachable;
  21. }
  22. //------------------------------------------------------------------------------
  23. // Rule Definition
  24. //------------------------------------------------------------------------------
  25. module.exports = {
  26. meta: {
  27. type: "problem",
  28. docs: {
  29. description: "enforce `return` statements in getters",
  30. category: "Possible Errors",
  31. recommended: true,
  32. url: "https://eslint.org/docs/rules/getter-return"
  33. },
  34. fixable: null,
  35. schema: [
  36. {
  37. type: "object",
  38. properties: {
  39. allowImplicit: {
  40. type: "boolean",
  41. default: false
  42. }
  43. },
  44. additionalProperties: false
  45. }
  46. ],
  47. messages: {
  48. expected: "Expected to return a value in {{name}}.",
  49. expectedAlways: "Expected {{name}} to always return a value."
  50. }
  51. },
  52. create(context) {
  53. const options = context.options[0] || { allowImplicit: false };
  54. const sourceCode = context.getSourceCode();
  55. let funcInfo = {
  56. upper: null,
  57. codePath: null,
  58. hasReturn: false,
  59. shouldCheck: false,
  60. node: null
  61. };
  62. /**
  63. * Checks whether or not the last code path segment is reachable.
  64. * Then reports this function if the segment is reachable.
  65. *
  66. * If the last code path segment is reachable, there are paths which are not
  67. * returned or thrown.
  68. * @param {ASTNode} node A node to check.
  69. * @returns {void}
  70. */
  71. function checkLastSegment(node) {
  72. if (funcInfo.shouldCheck &&
  73. funcInfo.codePath.currentSegments.some(isReachable)
  74. ) {
  75. context.report({
  76. node,
  77. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  78. messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
  79. data: {
  80. name: astUtils.getFunctionNameWithKind(funcInfo.node)
  81. }
  82. });
  83. }
  84. }
  85. /**
  86. * Checks whether a node means a getter function.
  87. * @param {ASTNode} node a node to check.
  88. * @returns {boolean} if node means a getter, return true; else return false.
  89. */
  90. function isGetter(node) {
  91. const parent = node.parent;
  92. if (TARGET_NODE_TYPE.test(node.type) && node.body.type === "BlockStatement") {
  93. if (parent.kind === "get") {
  94. return true;
  95. }
  96. if (parent.type === "Property" && astUtils.getStaticPropertyName(parent) === "get" && parent.parent.type === "ObjectExpression") {
  97. // Object.defineProperty()
  98. if (parent.parent.parent.type === "CallExpression" &&
  99. astUtils.getStaticPropertyName(parent.parent.parent.callee) === "defineProperty") {
  100. return true;
  101. }
  102. // Object.defineProperties()
  103. if (parent.parent.parent.type === "Property" &&
  104. parent.parent.parent.parent.type === "ObjectExpression" &&
  105. parent.parent.parent.parent.parent.type === "CallExpression" &&
  106. astUtils.getStaticPropertyName(parent.parent.parent.parent.parent.callee) === "defineProperties") {
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. return {
  114. // Stacks this function's information.
  115. onCodePathStart(codePath, node) {
  116. funcInfo = {
  117. upper: funcInfo,
  118. codePath,
  119. hasReturn: false,
  120. shouldCheck: isGetter(node),
  121. node
  122. };
  123. },
  124. // Pops this function's information.
  125. onCodePathEnd() {
  126. funcInfo = funcInfo.upper;
  127. },
  128. // Checks the return statement is valid.
  129. ReturnStatement(node) {
  130. if (funcInfo.shouldCheck) {
  131. funcInfo.hasReturn = true;
  132. // if allowImplicit: false, should also check node.argument
  133. if (!options.allowImplicit && !node.argument) {
  134. context.report({
  135. node,
  136. messageId: "expected",
  137. data: {
  138. name: astUtils.getFunctionNameWithKind(funcInfo.node)
  139. }
  140. });
  141. }
  142. }
  143. },
  144. // Reports a given function if the last path is reachable.
  145. "FunctionExpression:exit": checkLastSegment,
  146. "ArrowFunctionExpression:exit": checkLastSegment
  147. };
  148. }
  149. };