Ohm-Management - Projektarbeit B-ME
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-invalid-this.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @fileoverview A rule to disallow `this` keywords outside of classes or class-like objects.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "disallow `this` keywords outside of classes or class-like objects",
  18. category: "Best Practices",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/no-invalid-this"
  21. },
  22. schema: []
  23. },
  24. create(context) {
  25. const stack = [],
  26. sourceCode = context.getSourceCode();
  27. /**
  28. * Gets the current checking context.
  29. *
  30. * The return value has a flag that whether or not `this` keyword is valid.
  31. * The flag is initialized when got at the first time.
  32. *
  33. * @returns {{valid: boolean}}
  34. * an object which has a flag that whether or not `this` keyword is valid.
  35. */
  36. stack.getCurrent = function() {
  37. const current = this[this.length - 1];
  38. if (!current.init) {
  39. current.init = true;
  40. current.valid = !astUtils.isDefaultThisBinding(
  41. current.node,
  42. sourceCode
  43. );
  44. }
  45. return current;
  46. };
  47. /**
  48. * Pushs new checking context into the stack.
  49. *
  50. * The checking context is not initialized yet.
  51. * Because most functions don't have `this` keyword.
  52. * When `this` keyword was found, the checking context is initialized.
  53. *
  54. * @param {ASTNode} node - A function node that was entered.
  55. * @returns {void}
  56. */
  57. function enterFunction(node) {
  58. // `this` can be invalid only under strict mode.
  59. stack.push({
  60. init: !context.getScope().isStrict,
  61. node,
  62. valid: true
  63. });
  64. }
  65. /**
  66. * Pops the current checking context from the stack.
  67. * @returns {void}
  68. */
  69. function exitFunction() {
  70. stack.pop();
  71. }
  72. return {
  73. /*
  74. * `this` is invalid only under strict mode.
  75. * Modules is always strict mode.
  76. */
  77. Program(node) {
  78. const scope = context.getScope(),
  79. features = context.parserOptions.ecmaFeatures || {};
  80. stack.push({
  81. init: true,
  82. node,
  83. valid: !(
  84. scope.isStrict ||
  85. node.sourceType === "module" ||
  86. (features.globalReturn && scope.childScopes[0].isStrict)
  87. )
  88. });
  89. },
  90. "Program:exit"() {
  91. stack.pop();
  92. },
  93. FunctionDeclaration: enterFunction,
  94. "FunctionDeclaration:exit": exitFunction,
  95. FunctionExpression: enterFunction,
  96. "FunctionExpression:exit": exitFunction,
  97. // Reports if `this` of the current context is invalid.
  98. ThisExpression(node) {
  99. const current = stack.getCurrent();
  100. if (current && !current.valid) {
  101. context.report({ node, message: "Unexpected 'this'." });
  102. }
  103. }
  104. };
  105. }
  106. };