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.

no-promise-executor-return.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @fileoverview Rule to disallow returning values from Promise executor functions
  3. * @author Milos Djermanovic
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const { findVariable } = require("eslint-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const functionTypesToCheck = new Set(["ArrowFunctionExpression", "FunctionExpression"]);
  14. /**
  15. * Determines whether the given identifier node is a reference to a global variable.
  16. * @param {ASTNode} node `Identifier` node to check.
  17. * @param {Scope} scope Scope to which the node belongs.
  18. * @returns {boolean} True if the identifier is a reference to a global variable.
  19. */
  20. function isGlobalReference(node, scope) {
  21. const variable = findVariable(scope, node);
  22. return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
  23. }
  24. /**
  25. * Finds function's outer scope.
  26. * @param {Scope} scope Function's own scope.
  27. * @returns {Scope} Function's outer scope.
  28. */
  29. function getOuterScope(scope) {
  30. const upper = scope.upper;
  31. if (upper.type === "function-expression-name") {
  32. return upper.upper;
  33. }
  34. return upper;
  35. }
  36. /**
  37. * Determines whether the given function node is used as a Promise executor.
  38. * @param {ASTNode} node The node to check.
  39. * @param {Scope} scope Function's own scope.
  40. * @returns {boolean} `true` if the node is a Promise executor.
  41. */
  42. function isPromiseExecutor(node, scope) {
  43. const parent = node.parent;
  44. return parent.type === "NewExpression" &&
  45. parent.arguments[0] === node &&
  46. parent.callee.type === "Identifier" &&
  47. parent.callee.name === "Promise" &&
  48. isGlobalReference(parent.callee, getOuterScope(scope));
  49. }
  50. //------------------------------------------------------------------------------
  51. // Rule Definition
  52. //------------------------------------------------------------------------------
  53. module.exports = {
  54. meta: {
  55. type: "problem",
  56. docs: {
  57. description: "disallow returning values from Promise executor functions",
  58. category: "Possible Errors",
  59. recommended: false,
  60. url: "https://eslint.org/docs/rules/no-promise-executor-return"
  61. },
  62. schema: [],
  63. messages: {
  64. returnsValue: "Return values from promise executor functions cannot be read."
  65. }
  66. },
  67. create(context) {
  68. let funcInfo = null;
  69. /**
  70. * Reports the given node.
  71. * @param {ASTNode} node Node to report.
  72. * @returns {void}
  73. */
  74. function report(node) {
  75. context.report({ node, messageId: "returnsValue" });
  76. }
  77. return {
  78. onCodePathStart(_, node) {
  79. funcInfo = {
  80. upper: funcInfo,
  81. shouldCheck: functionTypesToCheck.has(node.type) && isPromiseExecutor(node, context.getScope())
  82. };
  83. if (funcInfo.shouldCheck && node.type === "ArrowFunctionExpression" && node.expression) {
  84. report(node.body);
  85. }
  86. },
  87. onCodePathEnd() {
  88. funcInfo = funcInfo.upper;
  89. },
  90. ReturnStatement(node) {
  91. if (funcInfo.shouldCheck && node.argument) {
  92. report(node);
  93. }
  94. }
  95. };
  96. }
  97. };