项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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