How to configure JSHint to catch TypeErrors as in a browser console?

113 views Asked by At

I am working on a custom editor which uses Monaco editor to accept the user snippets (pre-defined in JS lang) and I am using JSHint to evaluate their validity before submitting it to the backend.

There are a set number of custom functions acceptable for a data type as per my app. Let's take an example of String to provide some more clarity on my situation.

I have initialized JSHint using the following script:

String.prototype.myFunc = param => {};
String.prototype.myAnotherFunc = paramTwo => {};

So the two acceptable functions in my program for all strings are myFunc and myAnotherFunc.

However JSHint doesn't throw any TypeErrors if a user enters/calls any other function like

"testString".invalidFunc();

I would like it to throw errors like browser console would do.

enter image description here

I have checked the 'errors' array inside jsHintData variable as well. Could somebody please help me understand where I am going wrong?

Here's my JSHint config if that helps:

    const script = [
    'String.prototype.myFunc = param => {};',
'String.prototype.myAnotherFunc = paramTwo => {};',
'"testString".invalidFunc();'
  ];

  try {
    JSHINT(script, { maxerr: Infinity, asi: true, latedef: true });
    const jsHintData = JSHINT.data();
   } catch (error) {
    return error;
  }
0

There are 0 answers