UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getLocation = exports.getFilename = exports.getCodeFromError = exports.CompilerError = void 0;
4/*
5 * Copyright (c) 2018, salesforce.com, inc.
6 * All rights reserved.
7 * SPDX-License-Identifier: MIT
8 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
9 */
10const types_1 = require("../shared/types");
11class CompilerError extends Error {
12 constructor(code, message, filename, location) {
13 super(message);
14 this.level = types_1.DiagnosticLevel.Error;
15 this.code = code;
16 this.filename = filename;
17 this.location = location;
18 }
19 static from(diagnostic, origin) {
20 const { code, message } = diagnostic;
21 const filename = getFilename(origin, diagnostic);
22 const location = getLocation(origin, diagnostic);
23 const compilerError = new CompilerError(code, message, filename, location);
24 // The stack here is misleading and doesn't point to the cause of the original error message
25 // TODO [W-5712064]: Enhance diagnostics with useful stack trace and source code
26 compilerError.stack = undefined;
27 return compilerError;
28 }
29 toDiagnostic() {
30 return {
31 code: this.code,
32 message: this.message,
33 level: this.level,
34 filename: this.filename,
35 location: this.location,
36 };
37 }
38}
39exports.CompilerError = CompilerError;
40function getCodeFromError(error) {
41 if (error.lwcCode && typeof error.lwcCode === 'number') {
42 return error.lwcCode;
43 }
44 else if (error.code && typeof error.code === 'number') {
45 return error.code;
46 }
47 return undefined;
48}
49exports.getCodeFromError = getCodeFromError;
50function getFilename(origin, obj) {
51 // Give priority to explicit origin
52 if (origin && origin.filename) {
53 return origin.filename;
54 }
55 else if (obj) {
56 return obj.filename || obj.fileName || obj.file;
57 }
58 return undefined;
59}
60exports.getFilename = getFilename;
61function getLocation(origin, obj) {
62 // Give priority to explicit origin
63 if (origin && origin.location) {
64 return origin.location;
65 }
66 return getLocationFromObject(obj);
67}
68exports.getLocation = getLocation;
69function getLocationFromObject(obj) {
70 if (obj) {
71 if (obj.location) {
72 return obj.location;
73 }
74 else if (obj.loc) {
75 return obj.loc;
76 }
77 else if (Number.isInteger(obj.line) && Number.isInteger(obj.column)) {
78 return { line: obj.line, column: obj.column, start: obj.start, length: obj.length };
79 }
80 }
81 return undefined;
82}
83//# sourceMappingURL=utils.js.map
\No newline at end of file