UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

161 lines 6.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DuplicateImportNameError = exports.ConsecutiveSlashesInFilePathError = exports.IllegalCharInFilePathError = exports.InvalidFilePathError = exports.CircularImportError = exports.UndefinedDefaultImportError = exports.UndefinedImportError = exports.UndefinedNamespaceImportError = exports.ModuleNotFoundError = exports.ModuleConnectionError = exports.ModuleInternalError = void 0; const constants_1 = require("../constants"); const runtimeSourceError_1 = require("../errors/runtimeSourceError"); const types_1 = require("../types"); const filePaths_1 = require("./preprocessor/filePaths"); class ModuleInternalError extends runtimeSourceError_1.RuntimeSourceError { constructor(moduleName, error, node) { super(node); this.moduleName = moduleName; this.error = error; } explain() { return `Error(s) occured when executing the module '${this.moduleName}'.`; } elaborate() { return 'You may need to contact with the author for this module to fix this error.'; } } exports.ModuleInternalError = ModuleInternalError; class ImportError { get location() { return this.node?.loc ?? constants_1.UNKNOWN_LOCATION; } constructor(node) { this.node = node; this.severity = types_1.ErrorSeverity.ERROR; } } class ModuleConnectionError extends ImportError { constructor(node) { super(node); } explain() { return ModuleConnectionError.message; } elaborate() { return ModuleConnectionError.elaboration; } } exports.ModuleConnectionError = ModuleConnectionError; ModuleConnectionError.message = `Unable to get modules.`; ModuleConnectionError.elaboration = `You should check your Internet connection, and ensure you have used the correct module path.`; class ModuleNotFoundError extends ImportError { constructor(moduleName, node) { super(node); this.moduleName = moduleName; } explain() { return `Module '${this.moduleName}' not found.`; } elaborate() { return 'You should check your import declarations, and ensure that all are valid modules.'; } } exports.ModuleNotFoundError = ModuleNotFoundError; class UndefinedNamespaceImportError extends ImportError { constructor(moduleName, node) { super(node); this.moduleName = moduleName; } explain() { return `'${this.moduleName}' does not export any symbols!`; } elaborate() { return "Check your imports and make sure what you're trying to import exists!"; } } exports.UndefinedNamespaceImportError = UndefinedNamespaceImportError; class UndefinedImportError extends UndefinedNamespaceImportError { constructor(symbol, moduleName, node) { super(moduleName, node); this.symbol = symbol; } explain() { return `'${this.moduleName}' does not contain a definition for '${this.symbol}'`; } } exports.UndefinedImportError = UndefinedImportError; class UndefinedDefaultImportError extends UndefinedImportError { constructor(moduleName, node) { super('default', moduleName, node); } explain() { return `'${this.moduleName}' does not have a default export!`; } } exports.UndefinedDefaultImportError = UndefinedDefaultImportError; class CircularImportError extends ImportError { constructor(filePathsInCycle) { super(); this.filePathsInCycle = filePathsInCycle; } explain() { // We need to reverse the file paths in the cycle so that the // semantics of "'/a.js' -> '/b.js'" is "'/a.js' imports '/b.js'". const formattedCycle = this.filePathsInCycle .map(filePath => `'${filePath}'`) .reverse() .join(' -> '); return `Circular import detected: ${formattedCycle}.`; } elaborate() { return 'Break the circular import cycle by removing imports from any of the offending files.'; } } exports.CircularImportError = CircularImportError; class InvalidFilePathError extends ImportError { constructor(filePath) { super(); this.filePath = filePath; } } exports.InvalidFilePathError = InvalidFilePathError; class IllegalCharInFilePathError extends InvalidFilePathError { explain() { const validNonAlphanumericChars = Object.keys(filePaths_1.nonAlphanumericCharEncoding) .map(char => `'${char}'`) .join(', '); return `File path '${this.filePath}' must only contain alphanumeric chars and/or ${validNonAlphanumericChars}.`; } elaborate() { return 'Rename the offending file path to only use valid chars.'; } } exports.IllegalCharInFilePathError = IllegalCharInFilePathError; class ConsecutiveSlashesInFilePathError extends InvalidFilePathError { explain() { return `File path '${this.filePath}' cannot contain consecutive slashes '//'.`; } elaborate() { return 'Remove consecutive slashes from the offending file path.'; } } exports.ConsecutiveSlashesInFilePathError = ConsecutiveSlashesInFilePathError; class DuplicateImportNameError extends ImportError { get location() { return this.nodes[0].loc ?? constants_1.UNKNOWN_LOCATION; } constructor(name, nodes) { super(); this.name = name; this.nodes = nodes; this.locString = nodes .map(({ loc }) => { const { source, start } = loc ?? constants_1.UNKNOWN_LOCATION; return `(${source ?? 'Unknown File'}:${start.line}:${start.column})`; }) .join(', '); } explain() { return `Source does not support different imports from Source modules being given the same name. The following are the offending imports: ${this.locString}`; } elaborate() { return `You cannot have different symbols across different files being given the same declared name, for example: \`import { foo as a } from 'one_module';\` and \`import { bar as a } from 'another_module'; You also cannot have different symbols from the same module with the same declared name, for example: \`import { foo as a } from 'one_module';\` and \`import { bar as a } from 'one_module'; `; } } exports.DuplicateImportNameError = DuplicateImportNameError; //# sourceMappingURL=errors.js.map