johnstonbl01 / eslint-no-inferred-method-name Goto Github PK
View Code? Open in Web Editor NEWA custom rule for ESLint that checks for inferred method names within object literals.
License: MIT License
A custom rule for ESLint that checks for inferred method names within object literals.
License: MIT License
[Edit: clarified and adjusted tone of my wording]
"Every form of function expression assignment in ES6 infers a name that can be used inside the function for recursion."
That statement is false. The function name
inference has nothing to do with the ability to make lexical name recursive calls. The inference is assigning a name
property to the function object, not giving the function object a named binding in the surrounding (or owned) lexical environment (which would be required for recursion).
The code snippet also reinforces this incorrect information:
return foo(f, i); // foo is inferred within the compact object literal
That foo(f,i)
call will be an error, as there will not be a foo
lexical identifier referencing the function. There is no such thing as inferring a lexical name. The foo
lexical name will exist at that line of code, but it'll be pointing to the foo
object (not function) declared on the first line of the snippet. As such, a TypeError
will be thrown because you're trying to call a non-function value.
Concise methods do not have lexical name bindings and thus such a lexical name cannot be used, being that it doesn't exist, for recursion or any other self-referential purpose.
The following line:
myPromise.then(doSomething)
Generates the following error in ESLint:
[Error - 11:23:34 AM] ESLint stack trace: [Error - 11:23:34 AM] TypeError: Cannot read property 'type' of null Occurred while linting /myapp/script.js:2 at /myapp/node_modules/eslint-plugin-no-inferred-method-name/lib/rules/no-inferred-method-name.js:40:51 at Array.forEach (<anonymous>) at Program:exit (/myapp/node_modules/eslint-plugin-no-inferred-method-name/lib/rules/no-inferred-method-name.js:37:29) at /myapp/node_modules/eslint/lib/linter/safe-emitter.js:45:58 at Array.forEach (<anonymous>) at Object.emit (/myapp/node_modules/eslint/lib/linter/safe-emitter.js:45:38) at NodeEventGenerator.applySelector (/myapp/node_modules/eslint/lib/linter/node-event-generator.js:254:26) at NodeEventGenerator.applySelectors (/myapp/node_modules/eslint/lib/linter/node-event-generator.js:283:22) at NodeEventGenerator.leaveNode (/myapp/node_modules/eslint/lib/linter/node-event-generator.js:306:14) at CodePathAnalyzer.leaveNode (/myapp/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:655:23)
The real issue is with compact object literals. Here's the example we need to fix:
const repeater = {
name: 'Repeater',
types: [
{ f: 'function' },
{ n: 'number' }
],
repeat (f, n) {
let i = n - 1;
if (typeof f === 'function') {
f();
} else {
throw new Error('repeat: A Function is required.');
}
if (!n) {
return undefined;
}
return repeat(f, i);
}
};
repeater.repeat(() => {console.log('foo');}, 10);
Results:
foo
ReferenceError: repeat is not defined
The way to fix that is to report an error when the method name is used within the function, because inside the function, the method name is undefined. The error should be reported for this line:
return repeat(f, i);
The built-in no-undef
rule already catches this error, but we need to explicitly tell the user that compact method names are not available inside the method in order to clear up any confusion.
I'm trying to get this plugin working, but I can't. I've created a repo with only the readme's example and configuration, but still no luck.
Minimal reproduction case:
https://github.com/ferhtgoldaraz/issue-eslint-no-inferred-method-name
If I do:
❯ $(npm bin)/eslint index.js
I get no warnings, no errors, no nothing.
Is it possible to install this rule as an eslint plugin so you don't have to copy the file into the rules directory? That would significantly enhance the install experience.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.