Reputation: 4428
I get the error in the title from this code:
try {
let n = undefined;
let nx = n?.x;
} catch (err) {
missingFeatures.push("Syntax n?.x not recognized");
}
As you can see the code is designed with the hope to catch support for this syntax. But it does not work. Is there any way to catch this syntax error?
A bit interesting is that this happens in the new chromium based Edge on Android 10.
Upvotes: 0
Views: 877
Reputation: 370779
You can construct a new Function
instead, and see if the construction of the function throws. There's no need to define the other variables first, since all you care about is whether the syntax is proper, and syntax checking doesn't care whether the variables are defined, since the function never gets run:
try {
const fn = new Function('n?.x');
} catch (err) {
missingFeatures.push("Syntax n?.x not recognized");
}
That said, testing for whether a particular syntax is supported is pretty odd. Unless you're writing something dev-oriented for the user (like a live JavaScript editor), it would almost always make more sense to simply transpile your code down to ES5 or ES6 for production and serve that to everyone.
Upvotes: 1