properties.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const { isNodeType } = require('solidity-ast/utils');
  2. const { slug } = require('./helpers');
  3. module.exports.anchor = function anchor ({ item, contract }) {
  4. let res = '';
  5. if (contract) {
  6. res += contract.name + '-';
  7. }
  8. res += item.name;
  9. if ('parameters' in item) {
  10. const signature = item.parameters.parameters.map(v => v.typeName.typeDescriptions.typeString).join(',');
  11. res += slug('(' + signature + ')');
  12. }
  13. if (isNodeType('VariableDeclaration', item)) {
  14. res += '-' + slug(item.typeName.typeDescriptions.typeString);
  15. }
  16. return res;
  17. };
  18. module.exports.inheritance = function ({ item, build }) {
  19. if (!isNodeType('ContractDefinition', item)) {
  20. throw new Error('used inherited-items on non-contract');
  21. }
  22. return item.linearizedBaseContracts
  23. .map(id => build.deref('ContractDefinition', id))
  24. .filter((c, i) => c.name !== 'Context' || i === 0);
  25. };
  26. module.exports['has-functions'] = function ({ item }) {
  27. return item.inheritance.some(c => c.functions.length > 0);
  28. };
  29. module.exports['has-events'] = function ({ item }) {
  30. return item.inheritance.some(c => c.events.length > 0);
  31. };
  32. module.exports['inherited-functions'] = function ({ item }) {
  33. const { inheritance } = item;
  34. const baseFunctions = new Set(
  35. inheritance.flatMap(c => c.functions.flatMap(f => f.baseFunctions ?? [])),
  36. );
  37. return inheritance.map((contract, i) => ({
  38. contract,
  39. functions: contract.functions.filter(f =>
  40. !baseFunctions.has(f.id) && (f.name !== 'constructor' || i === 0),
  41. ),
  42. }));
  43. };