forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignore-unreachable-warnings.js
45 lines (40 loc) · 1.97 KB
/
ignore-unreachable-warnings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Warnings about unreachable code are emitted with a source location that corresponds to the unreachable code.
// We have some testing contracts that purposely cause unreachable code, but said code is in the library contracts, and
// with hardhat-ignore-warnings we are not able to selectively ignore them without potentially ignoring relevant
// warnings that we don't want to miss.
// Thus, we need to handle these warnings separately. We force Hardhat to compile them in a separate compilation job and
// then ignore the warnings about unreachable code that come from that compilation job.
const { task } = require('hardhat/config');
const {
TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE,
TASK_COMPILE_SOLIDITY_COMPILE,
} = require('hardhat/builtin-tasks/task-names');
const marker = Symbol('unreachable');
const markedCache = new WeakMap();
task(TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE, async (params, _, runSuper) => {
const job = await runSuper(params);
// If the file is in the unreachable directory, we make a copy of the config and mark it, which will cause it to get
// compiled separately (along with the other marked files).
if (params.file.sourceName.startsWith('contracts/mocks/') && /\bunreachable\b/.test(params.file.sourceName)) {
const originalConfig = job.solidityConfig;
let markedConfig = markedCache.get(originalConfig);
if (markedConfig === undefined) {
markedConfig = { ...originalConfig, [marker]: true };
markedCache.set(originalConfig, markedConfig);
}
job.solidityConfig = markedConfig;
}
return job;
});
const W_UNREACHABLE_CODE = '5740';
task(TASK_COMPILE_SOLIDITY_COMPILE, async (params, _, runSuper) => {
const marked = params.compilationJob.solidityConfig[marker];
const result = await runSuper(params);
if (marked) {
result.output = {
...result.output,
errors: result.output.errors?.filter(e => e.severity !== 'warning' || e.errorCode !== W_UNREACHABLE_CODE),
};
}
return result;
});