Comments (2)
Made quick & dirty change to no-invalid-this
that seems to work for me (add isListener
function and call it together with astUtils.isDefaultThisBinding
)
/**
* @fileoverview A rule to disallow `this` keywords outside of classes or class-like objects.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow `this` keywords outside of classes or class-like objects",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/no-invalid-this"
},
schema: [
{
type: "object",
properties: {
capIsConstructor: {
type: "boolean",
default: true
}
},
additionalProperties: false
}
],
messages: {
unexpectedThis: "Unexpected 'this'."
}
},
create(context) {
const options = context.options[0] || {};
const capIsConstructor = options.capIsConstructor !== false;
const stack = [],
sourceCode = context.getSourceCode();
/**
* Gets the current checking context.
*
* The return value has a flag that whether or not `this` keyword is valid.
* The flag is initialized when got at the first time.
* @returns {{valid: boolean}}
* an object which has a flag that whether or not `this` keyword is valid.
*/
stack.getCurrent = function() {
const current = this[this.length - 1];
if (!current.init) {
current.init = true;
current.valid = !astUtils.isDefaultThisBinding(
current.node,
sourceCode,
{ capIsConstructor }
) || isListener(current.node);
}
return current;
};
/**
* Checks if function node is used in NodeJS listener addition.
* @param {ASTNode} node A function node
* @returns {boolean} A function node is used in NodeJS listener addition.
*/
function isListener(node)
{
const listenersArray = ["on", "once", "addListener", "prependListener", "prependOnceListener"];
if (node.type !== "FunctionExpression") return false;
if (!node.parent || node.parent.type !== "CallExpression") return false;
const callee = node.parent.callee;
if (!callee || callee.type !== "MemberExpression") return false;
return (listenersArray.indexOf(callee.property.name) != -1);
}
/**
* Pushs new checking context into the stack.
*
* The checking context is not initialized yet.
* Because most functions don't have `this` keyword.
* When `this` keyword was found, the checking context is initialized.
* @param {ASTNode} node A function node that was entered.
* @returns {void}
*/
function enterFunction(node) {
// `this` can be invalid only under strict mode.
stack.push({
init: !context.getScope().isStrict,
node,
valid: true
});
}
/**
* Pops the current checking context from the stack.
* @returns {void}
*/
function exitFunction() {
stack.pop();
}
return {
/*
* `this` is invalid only under strict mode.
* Modules is always strict mode.
*/
Program(node) {
const scope = context.getScope(),
features = context.parserOptions.ecmaFeatures || {};
stack.push({
init: true,
node,
valid: !(
scope.isStrict ||
node.sourceType === "module" ||
(features.globalReturn && scope.childScopes[0].isStrict)
)
});
},
"Program:exit"() {
stack.pop();
},
FunctionDeclaration: enterFunction,
"FunctionDeclaration:exit": exitFunction,
FunctionExpression: enterFunction,
"FunctionExpression:exit": exitFunction,
// Reports if `this` of the current context is invalid.
ThisExpression(node) {
const current = stack.getCurrent();
if (current && !current.valid) {
context.report({
node,
messageId: "unexpectedThis"
});
}
}
};
}
};
from eslint-plugin-node.
Reworked this rule along with several others to use as custom rule set in https://github.com/Antonius-S/ESLint-custom-rules
from eslint-plugin-node.
Related Issues (20)
- allow regex and wildcards for `allowModules` and other issues HOT 1
- `no-extraneous-import` should not complain about self referencing
- not triggering `no-extraneous-require` for submodules HOT 1
- Add new contributors to library to unblock fixes HOT 3
- node/no-unpublished-import inconsistency HOT 3
- Legacy URL is not deprecated anymore - `node/no-deprecated-api` HOT 3
- Unclear how to configure this for `.mjs` files in workspaces
- Invitation to move to official `eslint-community` org HOT 3
- Rule node/no-missing-import is not compatible with typescript import with .js suffix HOT 2
- `node/no-sync` doesn't work for named imports
- Rule suggestion: prefer node: prefixed imports when possible
- Rule suggestion: no skip/only for node test runner
- dns.promises.setServers should be ignored from prefer-promises/dns HOT 1
- 'import()' expressions are not supported yet inconsistency HOT 5
- semver security warning HOT 3
- Rule suggestion: `node/no-unsupported-features/es-builtins` the `version` option should not take precedence over the `engines` field
- Suggestion: Make a less intrusive and/or smarter recommended config HOT 1
- RegEx pattern '^(?:@[a-zA-Z0-9_\-.]+/)?[a-zA-Z0-9_\-.]+$' does not match absolute paths HOT 4
- "node:test" is not found in v17.0.0 HOT 1
- Incompatible with eslint 9.x HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint-plugin-node.