Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js how to notice a missing function?

In some Node.js, I had

function examp() {
    ..
    blah = bestAvailableDelauneySlot()
    ..
}

However the function bestAvailableDelauneySlot did not exist at all.

(In my case I had foolishly forgotten to type the module, so, it should have been blah = triangles.bestAvailableDelauneySlot() ..)

Now, the code

  blah = bestAvailableDelauneySlot()

doesn't create any error AT ALL, until, for some reason examp is called at runtime. (Which only happens in obscure situations, once a week).

VSCode does not at all tell me there is no definition for bestAvailableDelauneySlot. "using strict" does not seem to help.

How the heck to safeguard against an undefined function name??

A simple typo

blah = triangles.bestAvailableDelauneySlozz()

and crash. Solution?

Perhaps ideally something that integrates w/ VSCode?

(BTW I generally use VSCode on a Mac ... perhaps that's the problem :O )

like image 439
Fattie Avatar asked Dec 11 '20 18:12

Fattie


People also ask

How do you call a function in node js?

The call() method returns the result of calling the functionName() . By default, the this inside the function is set to the global object i.e., window in the web browsers and global in Node. js. Note that in the strict mode, the this inside the function is set to undefined instead of the global object.

What is FN in NodeJS?

Fn is a lightweight Docker-based serverless functions platform you can run on your laptop, server, or cloud.

What is on () in node js?

In Node. js, you usually bind functions (using 'on' or other functions) to listen to events.

What is Sigint in node js?

The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process. This is typically initiated by pressing Ctrl+C . The SIGTERM signal is sent to a process to request its termination. Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the process.


2 Answers

The tool ESLint is the de facto standard to check for problems in JavaScript code.

In your specific case, the rule no-undef would report the undeclared function.

There's also an extension for Visual Studio Code with more than 12 million downloads:

ESLint for Visual Studio Code applied to the example

like image 78
xehpuk Avatar answered Oct 09 '22 23:10

xehpuk


I want to provide two methods. I hope it could help you. More detail in Type checking JavaScrip.
Note: this is vscode.

First

You could simply add // @ts-check in the head of file to validate. Before you add // @ts-check, it doesn't tell you an error (bestAvailableDelauneySlot doesn't exist).

enter image description here

Then you add // @ts-check, it will tell you it doesn't exist.

enter image description here

Then you declare the function, the error is gone.

enter image description here

If you make some typo, it will show error message.

enter image description here

And it also work in module.exports and require. No // @ts-check.

enter image description here

Add // @ts-check.

enter image description here

Second

If you don't want to add //@ts-check by every file, you could create jsconfig.json in project root to enable whole files checking with adding checkJs in jsconfig.json. Its effect is same as adding //@ts-check in all files.

{
    "compilerOptions": {
        "checkJs": true
    },
    "exclude": ["node_modules", "**/node_modules/*"]
}
like image 24
Jack Yu Avatar answered Oct 09 '22 23:10

Jack Yu