From 53220f25fe2c51c3d4fb97de4287d2c97677c673 Mon Sep 17 00:00:00 2001 From: Pete Gonzalez <4673363+octogonz@users.noreply.github.com> Date: Tue, 19 Jan 2021 21:53:38 -0800 Subject: [PATCH] Fix for incompatibility with the TypeScript transpiler (GitHub issue #3032) --- runtime/JavaScript/src/antlr4/error/Errors.js | 39 ++++++++++++++++++- runtime/JavaScript/src/antlr4/error/index.js | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/runtime/JavaScript/src/antlr4/error/Errors.js b/runtime/JavaScript/src/antlr4/error/Errors.js index 1308e9e5e20..ed0a7c6055d 100644 --- a/runtime/JavaScript/src/antlr4/error/Errors.js +++ b/runtime/JavaScript/src/antlr4/error/Errors.js @@ -13,7 +13,42 @@ const {PredicateTransition} = require('./../atn/Transition') -class RecognitionException extends Error { +/** + * The base class for error classes. This is used instead of the system Error class to avoid + * incompatibilities with transpilers targeting ECMASCript 5. + */ +class AntlrError { + constructor(message) { + const systemError = new Error(message) + this.name = systemError.name; + this.message = systemError.message; + this.stack = systemError.stack; + } + + toString() { + var obj = Object(this); + if (obj !== this) { + throw new TypeError(); + } + + var name = this.name; + name = (name === undefined) ? 'Error' : String(name); + + var msg = this.message; + msg = (msg === undefined) ? '' : String(msg); + + if (name === '') { + return msg; + } + if (msg === '') { + return name; + } + + return name + ': ' + msg; + } +} + +class RecognitionException extends AntlrError { constructor(params) { super(params.message); if (!!Error.captureStackTrace) { @@ -156,7 +191,7 @@ class FailedPredicateException extends RecognitionException { } -class ParseCancellationException extends Error{ +class ParseCancellationException extends AntlrError { constructor() { super() Error.captureStackTrace(this, ParseCancellationException); diff --git a/runtime/JavaScript/src/antlr4/error/index.js b/runtime/JavaScript/src/antlr4/error/index.js index 482b47edbed..61e89c8245a 100644 --- a/runtime/JavaScript/src/antlr4/error/index.js +++ b/runtime/JavaScript/src/antlr4/error/index.js @@ -3,6 +3,7 @@ * can be found in the LICENSE.txt file in the project root. */ +module.exports.AntlrError = require('./Errors').AntlrError; module.exports.RecognitionException = require('./Errors').RecognitionException; module.exports.NoViableAltException = require('./Errors').NoViableAltException; module.exports.LexerNoViableAltException = require('./Errors').LexerNoViableAltException;