properties.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { isNodeType, findAll } = 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['has-errors'] = function ({ item }) {
  33. return item.inheritance.some(c => c.errors.length > 0);
  34. };
  35. module.exports.functions = function ({ item }) {
  36. return [
  37. ...[...findAll('FunctionDefinition', item)].filter(f => f.visibility !== 'private'),
  38. ...[...findAll('VariableDeclaration', item)].filter(f => f.visibility === 'public'),
  39. ];
  40. };
  41. module.exports.returns2 = function ({ item }) {
  42. if (isNodeType('VariableDeclaration', item)) {
  43. return [{ type: item.typeDescriptions.typeString }];
  44. } else {
  45. return item.returns;
  46. }
  47. };
  48. module.exports['inherited-functions'] = function ({ item }) {
  49. const { inheritance } = item;
  50. const baseFunctions = new Set(inheritance.flatMap(c => c.functions.flatMap(f => f.baseFunctions ?? [])));
  51. return inheritance.map((contract, i) => ({
  52. contract,
  53. functions: contract.functions.filter(f => !baseFunctions.has(f.id) && (f.name !== 'constructor' || i === 0)),
  54. }));
  55. };