* * @param {string} source the source code * @param {object} options the options to pass to the parser * @returns {TransformOutput} an object with a `code` property.
(source, options)
| 44 | * @returns {TransformOutput} an object with a `code` property. |
| 45 | */ |
| 46 | function parseTypeScript(source, options) { |
| 47 | const parse = loadTypeScriptParser(); |
| 48 | try { |
| 49 | return parse(source, options); |
| 50 | } catch (error) { |
| 51 | /** |
| 52 | * Amaro v0.3.0 (from SWC v1.10.7) throws an object with `message` and `code` properties. |
| 53 | * It allows us to distinguish between invalid syntax and unsupported syntax. |
| 54 | */ |
| 55 | switch (error?.code) { |
| 56 | case 'UnsupportedSyntax': { |
| 57 | const unsupportedSyntaxError = new ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX(error.message); |
| 58 | throw decorateErrorWithSnippet(unsupportedSyntaxError, error); /* node-do-not-add-exception-line */ |
| 59 | } |
| 60 | case 'InvalidSyntax': { |
| 61 | const invalidSyntaxError = new ERR_INVALID_TYPESCRIPT_SYNTAX(error.message); |
| 62 | throw decorateErrorWithSnippet(invalidSyntaxError, error); /* node-do-not-add-exception-line */ |
| 63 | } |
| 64 | default: |
| 65 | // SWC may throw strings when something goes wrong. |
| 66 | if (typeof error === 'string') { assert.fail(error); } |
| 67 | assert(error != null && ObjectPrototypeHasOwnProperty(error, 'message')); |
| 68 | assert.fail(error.message); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * |
no test coverage detected
searching dependent graphs…