UNPKG

@neo-one/smart-contract-compiler

Version:

NEO•ONE TypeScript smart contract compiler.

77 lines (75 loc) 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; const tslib_1 = require("tslib"); const ts_utils_1 = require("@neo-one/ts-utils"); const lodash_1 = tslib_1.__importDefault(require("lodash")); const typescript_1 = tslib_1.__importStar(require("typescript")); const util_1 = require("util"); const analysis_1 = require("./analysis"); const builtins_1 = require("./compile/builtins"); const CompilerDiagnostic_1 = require("./CompilerDiagnostic"); const DiagnosticCode_1 = require("./DiagnosticCode"); const DiagnosticMessage_1 = require("./DiagnosticMessage"); const getErrorKey = (diagnostic) => `${diagnostic.file}:${diagnostic.start}:${diagnostic.length}:${diagnostic.code}`; const getFullKey = (diagnostic) => `${diagnostic.file}:${diagnostic.start}:${diagnostic.length}:${diagnostic.category}:${diagnostic.code}:${diagnostic.messageText}`; class Context { constructor(sourceFiles, program, typeChecker, languageService, host, mutableDiagnostics = [...typescript_1.default.getPreEmitDiagnostics(program)]) { this.sourceFiles = sourceFiles; this.program = program; this.typeChecker = typeChecker; this.languageService = languageService; this.host = host; this.mutableDiagnostics = mutableDiagnostics; this.analysis = new analysis_1.AnalysisService(this); this.builtins = builtins_1.createBuiltins(this); } get diagnostics() { const errorDiagnostics = new Set(); for (const diagnostic of this.mutableDiagnostics) { if (diagnostic.category === typescript_1.DiagnosticCategory.Error) { errorDiagnostics.add(getErrorKey(diagnostic)); } } const diagnostics = this.mutableDiagnostics.filter((diagnostic) => diagnostic.category === typescript_1.DiagnosticCategory.Error || !errorDiagnostics.has(getErrorKey(diagnostic))); return lodash_1.default.uniqBy(diagnostics, getFullKey); } update(sourceFiles, program, typeChecker, languageService) { return new Context(sourceFiles, program, typeChecker, languageService, this.host, [...this.mutableDiagnostics]); } reportError(node, code, message, ...args) { if (!this.isDeclarationFile(node)) { this.mutableDiagnostics.push(new CompilerDiagnostic_1.CompilerDiagnostic(node, this.getDiagnosticMessage(message, ...args), code, typescript_1.default.DiagnosticCategory.Error)); } } reportWarning(node, code, message, ...args) { if (!this.isDeclarationFile(node)) { this.mutableDiagnostics.push(new CompilerDiagnostic_1.CompilerDiagnostic(node, this.getDiagnosticMessage(message, ...args), code, typescript_1.default.DiagnosticCategory.Warning)); } } reportUnsupported(node) { this.reportError(node, DiagnosticCode_1.DiagnosticCode.UnsupportedSyntax, DiagnosticMessage_1.DiagnosticMessage.UnsupportedSyntax); } reportUnsupportedEfficiency(node) { this.reportError(node, DiagnosticCode_1.DiagnosticCode.UnsupportedSyntax, DiagnosticMessage_1.DiagnosticMessage.EfficiencyUnsupportedSyntax); } reportTypeError(node) { this.reportError(node, DiagnosticCode_1.DiagnosticCode.UnknownType, DiagnosticMessage_1.DiagnosticMessage.CouldNotInferType); } reportTypeWarning(node) { this.reportWarning(node, DiagnosticCode_1.DiagnosticCode.UnknownType, DiagnosticMessage_1.DiagnosticMessage.CouldNotInferTypeDeopt); } getDiagnosticMessage(message, ...args) { const match = message.match(/%[dfijoOs]/g); const expectedLength = (match === null ? [] : match).length; if (expectedLength !== args.length) { throw new Error(`The provided arguments length (${args.length}) does not match the required arguments length (${expectedLength})`); } return util_1.format(message, ...args); } isDeclarationFile(node) { return ts_utils_1.tsUtils.file.isDeclarationFile(ts_utils_1.tsUtils.node.getSourceFile(node)); } } exports.Context = Context; //# sourceMappingURL=Context.js.map