diff --git a/apps/solidity-compiler/src/app/compiler-api.ts b/apps/solidity-compiler/src/app/compiler-api.ts index 174d40f910..7d5188be6b 100644 --- a/apps/solidity-compiler/src/app/compiler-api.ts +++ b/apps/solidity-compiler/src/app/compiler-api.ts @@ -1,4 +1,4 @@ -import { compile } from '@remix-project/remix-solidity' +import { compile, helper } from '@remix-project/remix-solidity' import { CompileTabLogic, parseContracts } from '@remix-ui/solidity-compiler' // eslint-disable-line import type { ConfigurationSettings } from '@remix-project/remix-lib-ts' @@ -261,7 +261,7 @@ export const CompilerApiMixin = (Base) => class extends Base { this.on('fileManager', 'fileClosed', this.data.eventHandlers.onFileClosed) - this.data.eventHandlers.onCompilationFinished = (success, data, source, input, version) => { + this.data.eventHandlers.onCompilationFinished = async (success, data, source, input, version) => { this.compileErrors = data if (success) { // forwarding the event to the appManager infra @@ -291,6 +291,21 @@ export const CompilerApiMixin = (Base) => class extends Base { if (success) this.compiler.visitContracts((contract) => { this.compilationDetails.contractMap[contract.name] = contract }) this.compilationDetails.target = source.target if (this.onCompilationFinished) this.onCompilationFinished(this.compilationDetails) + // set annotations + if (data.errors) { + for (const error of data.errors) { + let pos = helper.getPositionDetails(error.formattedMessage) + if (pos.errFile) { + pos = { + row: pos.errLine, + column: pos.errCol, + text: error.formattedMessage, + type: error.severity + } + await this.call('editor', 'addAnnotation', pos, pos.errFile) + } + } + } } this.compiler.event.register('compilationFinished', this.data.eventHandlers.onCompilationFinished) diff --git a/libs/remix-solidity/src/compiler/compiler-abstract.ts b/libs/remix-solidity/src/compiler/compiler-abstract.ts index 8a7c25f72f..7471816fea 100644 --- a/libs/remix-solidity/src/compiler/compiler-abstract.ts +++ b/libs/remix-solidity/src/compiler/compiler-abstract.ts @@ -1,5 +1,5 @@ 'use strict' -import txHelper from './txHelper' +import helper from './helper' export class CompilerAbstract { languageversion: any @@ -18,11 +18,11 @@ export class CompilerAbstract { } getContract (name) { - return txHelper.getContract(name, this.data.contracts) + return helper.getContract(name, this.data.contracts) } visitContracts (calllback) { - return txHelper.visitContracts(this.data.contracts, calllback) + return helper.visitContracts(this.data.contracts, calllback) } getData () { diff --git a/libs/remix-solidity/src/compiler/compiler.ts b/libs/remix-solidity/src/compiler/compiler.ts index dc2421ff9c..a10fa6fce2 100644 --- a/libs/remix-solidity/src/compiler/compiler.ts +++ b/libs/remix-solidity/src/compiler/compiler.ts @@ -4,7 +4,7 @@ import { update } from 'solc/abi' import * as webworkify from 'webworkify-webpack' import compilerInput from './compiler-input' import EventManager from '../lib/eventManager' -import txHelper from './txHelper' +import txHelper from './helper' import { Source, SourceWithTarget, MessageFromWorker, CompilerState, CompilationResult, visitContractsCallbackParam, visitContractsCallbackInterface, CompilationError, diff --git a/libs/remix-solidity/src/compiler/txHelper.ts b/libs/remix-solidity/src/compiler/helper.ts similarity index 52% rename from libs/remix-solidity/src/compiler/txHelper.ts rename to libs/remix-solidity/src/compiler/helper.ts index c50bf4c7e6..f4eb8e3f21 100644 --- a/libs/remix-solidity/src/compiler/txHelper.ts +++ b/libs/remix-solidity/src/compiler/helper.ts @@ -35,6 +35,27 @@ export default { if (cb(param)) return } } - } + }, + + // ^ e.g: + // browser/gm.sol: Warning: Source file does not specify required compiler version! Consider adding "pragma solidity ^0.6.12 + // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0/contracts/introspection/IERC1820Registry.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.7.4+commit.3f05b770.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version + getPositionDetails: (msg: string) => { + const result = { } as Record + + // To handle some compiler warning without location like SPDX license warning etc + if (!msg.includes(':')) return { errLine: -1, errCol: -1, errFile: '' } + if (msg.includes('-->')) msg = msg.split('-->')[1].trim() + + // extract line / column + let pos = msg.match(/^(.*?):([0-9]*?):([0-9]*?)?/) + result.errLine = pos ? parseInt(pos[2]) - 1 : -1 + result.errCol = pos ? parseInt(pos[3]) : -1 + + // extract file + pos = msg.match(/^(https:.*?|http:.*?|.*?):/) + result.errFile = pos ? pos[1] : msg + return result + } } diff --git a/libs/remix-solidity/src/index.ts b/libs/remix-solidity/src/index.ts index d5878a0f85..ac9db0e167 100644 --- a/libs/remix-solidity/src/index.ts +++ b/libs/remix-solidity/src/index.ts @@ -4,3 +4,4 @@ export { default as CompilerInput, getValidLanguage } from './compiler/compiler- export { CompilerAbstract } from './compiler/compiler-abstract' export * from './compiler/types' export { promisedMiniXhr, pathToURL, baseURLBin, baseURLWasm, canUseWorker, urlFromVersion } from './compiler/compiler-utils' +export { default as helper } from './compiler/helper' diff --git a/libs/remix-ui/renderer/src/lib/renderer.tsx b/libs/remix-ui/renderer/src/lib/renderer.tsx index 9121f432c6..679d26c490 100644 --- a/libs/remix-ui/renderer/src/lib/renderer.tsx +++ b/libs/remix-ui/renderer/src/lib/renderer.tsx @@ -1,4 +1,5 @@ import React, { useEffect, useState } from 'react' //eslint-disable-line +import { helper } from '@remix-project/remix-solidity' import './renderer.css' interface RendererProps { message: any; @@ -29,51 +30,19 @@ export const Renderer = ({ message, opt = {}, plugin }: RendererProps) => { // ^ e.g: // browser/gm.sol: Warning: Source file does not specify required compiler version! Consider adding "pragma solidity ^0.6.12 // https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.2.0/contracts/introspection/IERC1820Registry.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.7.4+commit.3f05b770.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version - const positionDetails = getPositionDetails(text) + const positionDetails = helper.getPositionDetails(text) opt.errLine = positionDetails.errLine opt.errCol = positionDetails.errCol opt.errFile = positionDetails.errFile ? (positionDetails.errFile as string).trim() : '' - if (!opt.noAnnotations && opt.errFile && opt.errFile !== '') { - addAnnotation(opt.errFile, { - row: opt.errLine, - column: opt.errCol, - text: text, - type: opt.type - }) - } - setMessageText(text) setEditorOptions(opt) setClose(false) setClassList(opt.type === 'error' ? 'alert alert-danger' : 'alert alert-warning') }, [message, opt]) - const getPositionDetails = (msg: string) => { - const result = { } as Record - - // To handle some compiler warning without location like SPDX license warning etc - if (!msg.includes(':')) return { errLine: -1, errCol: -1, errFile: '' } - - if (msg.includes('-->')) msg = msg.split('-->')[1].trim() - - // extract line / column - let pos = msg.match(/^(.*?):([0-9]*?):([0-9]*?)?/) - result.errLine = pos ? parseInt(pos[2]) - 1 : -1 - result.errCol = pos ? parseInt(pos[3]) : -1 - - // extract file - pos = msg.match(/^(https:.*?|http:.*?|.*?):/) - result.errFile = pos ? pos[1] : msg - return result - } - - const addAnnotation = async (file, error) => { - if (file === await plugin.call('config', 'getAppParameter', 'currentFile')) { - await plugin.call('editor', 'addAnnotation', error, file) - } - } + const handleErrorClick = (opt) => { if (opt.click) {