-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add missing members to parser exception type declarations #1322
Conversation
Thanks @mbett7 👍 I will have a look at this PR on the weekend |
|
||
NoViableAltException.prototype = Error.prototype | ||
/* istanbul ignore next - V8 workaround to remove constructor from stacktrace when typescript target is ES5 */ | ||
if (Error.captureStackTrace) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen on Safari / Firefox ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think I now understand this workaround removes some redundant line from the stack trace.
Not fixes the whole stack trace, so its only a minor issue on none V8 runtimes.
Thanks @mbett7 👍 This looks great, I have only one concern: |
Not sure how important this is however... |
I only tested in IE11, Chrome & Nodejs... Should have tested it in Firefox as well. Since it looks like the constructors remain in the stacktrace (also the babel downlevel doesn't turn out so well). Not sure about Safari don't have an iOS environment. Test Code class CustomException extends Error {
constructor(message) {
super(message);
this.name = 'CustomException';
Object.setPrototypeOf(this, new.target.prototype);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
}
}
var customError = new CustomException('msg');
console.log('CustomException.name:', customError.name);
console.log('instanceof CustomException:', customError instanceof CustomException);
console.log('instanceof Error:', customError instanceof Error);
console.log('CustomException log: ', customError);
function throwCustomError() {
throw new CustomException('msg');
}
try {
throwCustomError();
} catch (e) {
console.log('Catch thrown CustomException:', e);
} Test Output
Chrome (Typescript ES5 downlevel)
Chrome (Babel ES5 downlevel)
IE11 (Typescript ES5 downlevel)
IE11 (Babel ES5 downlevel)
Firefox (no downlevel)
Firefox (Typescript ES5 downlevel)
Firefox (Babel ES5 downlevel)
|
Fixes #1316
Also converts error constructors to classes to satisfy typing in the api_type_checking.ts test.
Note: should be able to remove the ES5 downlevel workarounds when the TS target is updated to a more modern runtime. Since babel@7 seems to be able to downlevel custom class errors to behave like ES2015 (i.e. with prototype and stacktrace intact).