set annotations from the compiler api

pull/2163/head
yann300 3 years ago
parent e30c246a9f
commit 458e4abe09
  1. 19
      apps/solidity-compiler/src/app/compiler-api.ts
  2. 6
      libs/remix-solidity/src/compiler/compiler-abstract.ts
  3. 2
      libs/remix-solidity/src/compiler/compiler.ts
  4. 23
      libs/remix-solidity/src/compiler/helper.ts
  5. 1
      libs/remix-solidity/src/index.ts
  6. 35
      libs/remix-ui/renderer/src/lib/renderer.tsx

@ -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 (let 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)

@ -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 () {

@ -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,

@ -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<string, number | string>
// 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
}
}

@ -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'

@ -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<string, number | string>
// 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) {

Loading…
Cancel
Save