ignore-unreachable-warnings.js 2.0 KB

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