properties.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.fullname = function fullname({ item }) {
  19. let res = '';
  20. res += item.name;
  21. if ('parameters' in item) {
  22. const signature = item.parameters.parameters.map(v => v.typeName.typeDescriptions.typeString).join(',');
  23. res += slug('(' + signature + ')');
  24. }
  25. if (isNodeType('VariableDeclaration', item)) {
  26. res += '-' + slug(item.typeName.typeDescriptions.typeString);
  27. }
  28. if (res.charAt(res.length - 1) === '-') {
  29. return res.slice(0, -1);
  30. }
  31. return res;
  32. };
  33. module.exports.inheritance = function ({ item, build }) {
  34. if (!isNodeType('ContractDefinition', item)) {
  35. throw new Error('inheritance modifier used on non-contract');
  36. }
  37. return item.linearizedBaseContracts
  38. .map(id => build.deref('ContractDefinition', id))
  39. .filter((c, i) => c.name !== 'Context' || i === 0);
  40. };
  41. module.exports['has-functions'] = function ({ item }) {
  42. return item.inheritance.some(c => c.functions.length > 0);
  43. };
  44. module.exports['has-events'] = function ({ item }) {
  45. return item.inheritance.some(c => c.events.length > 0);
  46. };
  47. module.exports['has-errors'] = function ({ item }) {
  48. return item.inheritance.some(c => c.errors.length > 0);
  49. };
  50. module.exports['internal-variables'] = function ({ item }) {
  51. return item.variables.filter(({ visibility }) => visibility === 'internal');
  52. };
  53. module.exports['has-internal-variables'] = function ({ item }) {
  54. return module.exports['internal-variables']({ item }).length > 0;
  55. };
  56. module.exports.functions = function ({ item }) {
  57. return [
  58. ...findAll('FunctionDefinition', item).filter(f => f.visibility !== 'private'),
  59. ...findAll('VariableDeclaration', item).filter(f => f.visibility === 'public'),
  60. ];
  61. };
  62. module.exports.returns2 = function ({ item }) {
  63. if (isNodeType('VariableDeclaration', item)) {
  64. return [{ type: item.typeName.typeDescriptions.typeString }];
  65. } else {
  66. return item.returns;
  67. }
  68. };
  69. module.exports['inherited-functions'] = function ({ item }) {
  70. const { inheritance } = item;
  71. const baseFunctions = new Set(inheritance.flatMap(c => c.functions.flatMap(f => f.baseFunctions ?? [])));
  72. return inheritance.map((contract, i) => ({
  73. contract,
  74. functions: contract.functions.filter(f => !baseFunctions.has(f.id) && (f.name !== 'constructor' || i === 0)),
  75. }));
  76. };