1
1
import { FunctionDefinition } from 'solidity-ast' ;
2
2
import { findAll } from 'solidity-ast/utils' ;
3
- import { DocItemWithContext } from '../site' ;
3
+ import { DocItemWithContext , DOC_ITEM_CONTEXT } from '../site' ;
4
4
import { arraysEqual } from './arrays-equal' ;
5
5
import { execAll } from './execall' ;
6
6
import { itemType } from './item-type' ;
7
+ import { ItemError } from './ItemError' ;
7
8
import { readItemDocs } from './read-item-docs' ;
8
9
import { getContractsInScope } from './scope' ;
9
10
@@ -25,7 +26,7 @@ export interface NatSpec {
25
26
}
26
27
27
28
export function parseNatspec ( item : DocItemWithContext ) : NatSpec {
28
- if ( ! item . __item_context ) throw new Error ( `Not an item or item is missing context` ) ;
29
+ if ( ! item [ DOC_ITEM_CONTEXT ] ) throw new Error ( `Not an item or item is missing context` ) ;
29
30
30
31
let res : NatSpec = { } ;
31
32
@@ -46,7 +47,7 @@ export function parseNatspec(item: DocItemWithContext): NatSpec {
46
47
let inheritFrom : FunctionDefinition | undefined ;
47
48
48
49
for ( const [ , tag = 'notice' , content ] of tagMatches ) {
49
- if ( content === undefined ) throw new Error ( 'Unexpected error' ) ;
50
+ if ( content === undefined ) throw new ItemError ( 'Unexpected error' , item ) ;
50
51
51
52
if ( tag === 'dev' || tag === 'notice' ) {
52
53
res [ tag ] ??= '' ;
@@ -68,20 +69,20 @@ export function parseNatspec(item: DocItemWithContext): NatSpec {
68
69
69
70
if ( tag === 'return' ) {
70
71
if ( ! ( 'returnParameters' in item ) ) {
71
- throw new Error ( `Item does not contain return parameters` ) ;
72
+ throw new ItemError ( `Item does not contain return parameters` , item ) ;
72
73
}
73
74
res . returns ??= [ ] ;
74
75
const i = res . returns . length ;
75
76
const p = item . returnParameters . parameters [ i ] ;
76
77
if ( p === undefined ) {
77
- throw new Error ( ` Got more @return tags than expected for ' ${ item . name } '` ) ;
78
+ throw new ItemError ( ' Got more @return tags than expected' , item ) ;
78
79
}
79
80
if ( ! p . name ) {
80
81
res . returns . push ( { description : content . trim ( ) } ) ;
81
82
} else {
82
83
const paramMatches = content . match ( / ( \w + ) ( ( [ ^ ] * ) ) ? / ) ;
83
84
if ( ! paramMatches || paramMatches [ 1 ] !== p . name ) {
84
- throw new Error ( `Expected @return tag to start with name '${ p . name } '` ) ;
85
+ throw new ItemError ( `Expected @return tag to start with name '${ p . name } '` , item ) ;
85
86
}
86
87
const [ , name , description ] = paramMatches as [ string , string , string ?] ;
87
88
res . returns . push ( { name, description : description ?. trim ( ) ?? '' } ) ;
@@ -97,20 +98,20 @@ export function parseNatspec(item: DocItemWithContext): NatSpec {
97
98
98
99
if ( tag === 'inheritdoc' ) {
99
100
if ( ! ( item . nodeType === 'FunctionDefinition' || item . nodeType === 'VariableDeclaration' ) ) {
100
- throw new Error ( `Expected function or variable but saw ${ itemType ( item ) } ` ) ;
101
+ throw new ItemError ( `Expected function or variable but saw ${ itemType ( item ) } ` , item ) ;
101
102
}
102
103
const parentContractName = content . trim ( ) ;
103
104
const parentContract = getContractsInScope ( item ) [ parentContractName ] ;
104
105
if ( ! parentContract ) {
105
- throw new Error ( `Parent contract '${ parentContractName } ' not found` ) ;
106
+ throw new ItemError ( `Parent contract '${ parentContractName } ' not found` , item ) ;
106
107
}
107
108
inheritFrom = [ ...findAll ( 'FunctionDefinition' , parentContract ) ] . find ( f => item . baseFunctions ?. includes ( f . id ) ) ;
108
109
}
109
110
}
110
111
111
112
if ( docString . length === 0 ) {
112
113
if ( 'baseFunctions' in item && item . baseFunctions ?. length === 1 ) {
113
- const baseFn = item . __item_context . build . deref ( 'FunctionDefinition' , item . baseFunctions [ 0 ] ! ) ;
114
+ const baseFn = item [ DOC_ITEM_CONTEXT ] . build . deref ( 'FunctionDefinition' , item . baseFunctions [ 0 ] ! ) ;
114
115
const shouldInherit = item . nodeType === 'VariableDeclaration' || arraysEqual ( item . parameters . parameters , baseFn . parameters . parameters , p => p . name ) ;
115
116
if ( shouldInherit ) {
116
117
inheritFrom = baseFn ;
0 commit comments