properties.js 2.3 KB

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