]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-invalid-this.js
2 * @fileoverview A rule to disallow `this` keywords in contexts where the value of `this` is `undefined`.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * Determines if the given code path is a code path with lexical `this` binding.
20 * That is, if `this` within the code path refers to `this` of surrounding code path.
21 * @param {CodePath} codePath Code path.
22 * @param {ASTNode} node Node that started the code path.
23 * @returns {boolean} `true` if it is a code path with lexical `this` binding.
25 function isCodePathWithLexicalThis(codePath
, node
) {
26 return codePath
.origin
=== "function" && node
.type
=== "ArrowFunctionExpression";
29 //------------------------------------------------------------------------------
31 //------------------------------------------------------------------------------
33 /** @type {import('../shared/types').Rule} */
39 description
: "Disallow use of `this` in contexts where the value of `this` is `undefined`",
41 url
: "https://eslint.org/docs/rules/no-invalid-this"
53 additionalProperties
: false
58 unexpectedThis
: "Unexpected 'this'."
63 const options
= context
.options
[0] || {};
64 const capIsConstructor
= options
.capIsConstructor
!== false;
66 sourceCode
= context
.getSourceCode();
69 * Gets the current checking context.
71 * The return value has a flag that whether or not `this` keyword is valid.
72 * The flag is initialized when got at the first time.
73 * @returns {{valid: boolean}}
74 * an object which has a flag that whether or not `this` keyword is valid.
76 stack
.getCurrent = function() {
77 const current
= this[this.length
- 1];
81 current
.valid
= !astUtils
.isDefaultThisBinding(
92 onCodePathStart(codePath
, node
) {
93 if (isCodePathWithLexicalThis(codePath
, node
)) {
97 if (codePath
.origin
=== "program") {
98 const scope
= context
.getScope();
99 const features
= context
.parserOptions
.ecmaFeatures
|| {};
101 // `this` at the top level of scripts always refers to the global object
106 node
.sourceType
=== "module" ||
107 (features
.globalReturn
&& scope
.childScopes
[0].isStrict
)
115 * `init: false` means that `valid` isn't determined yet.
116 * Most functions don't use `this`, and the calculation for `valid`
117 * is relatively costly, so we'll calculate it lazily when the first
118 * `this` within the function is traversed. A special case are non-strict
119 * functions, because `this` refers to the global object and therefore is
120 * always valid, so we can set `init: true` right away.
123 init
: !context
.getScope().isStrict
,
129 onCodePathEnd(codePath
, node
) {
130 if (isCodePathWithLexicalThis(codePath
, node
)) {
137 // Reports if `this` of the current context is invalid.
138 ThisExpression(node
) {
139 const current
= stack
.getCurrent();
141 if (current
&& !current
.valid
) {
144 messageId
: "unexpectedThis"