Skip to content

Commit db6b320

Browse files
committed
Parse function visibility instead of inferring it from the function type
1 parent 5745768 commit db6b320

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/ASTBuilder.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ export class ASTBuilder
296296
let name: string | null = null
297297
let parameters: any = []
298298
let returnParameters: AST.VariableDeclaration[] | null = null
299-
let visibility: AST.FunctionDefinition['visibility'] = 'default'
300299

301300
let block: AST.Block | null = null
302301
const ctxBlock = ctx.block()
@@ -318,22 +317,25 @@ export class ASTBuilder
318317

319318
// see what type of function we're dealing with
320319
const ctxReturnParameters = ctx.returnParameters()
320+
321+
let visibility: AST.FunctionDefinition['visibility'] = 'default'
322+
if (ctx.modifierList().ExternalKeyword_list().length > 0) {
323+
visibility = 'external'
324+
} else if (ctx.modifierList().InternalKeyword_list().length > 0) {
325+
visibility = 'internal'
326+
} else if (ctx.modifierList().PublicKeyword_list().length > 0) {
327+
visibility = 'public'
328+
} else if (ctx.modifierList().PrivateKeyword_list().length > 0) {
329+
visibility = 'private'
330+
}
331+
321332
switch (this._toText(ctx.functionDescriptor().getChild(0))) {
322333
case 'constructor':
323334
parameters = ctx
324335
.parameterList()
325336
.parameter_list()
326337
.map((x) => this.visit(x))
327338

328-
// error out on incorrect function visibility
329-
if (ctx.modifierList().InternalKeyword_list().length > 0) {
330-
visibility = 'internal'
331-
} else if (ctx.modifierList().PublicKeyword_list().length > 0) {
332-
visibility = 'public'
333-
} else {
334-
visibility = 'default'
335-
}
336-
337339
isConstructor = true
338340
break
339341
case 'fallback':
@@ -345,11 +347,9 @@ export class ASTBuilder
345347
? this.visitReturnParameters(ctxReturnParameters)
346348
: null
347349

348-
visibility = 'external'
349350
isFallback = true
350351
break
351352
case 'receive':
352-
visibility = 'external'
353353
isReceiveEther = true
354354
break
355355
case 'function': {
@@ -364,17 +364,6 @@ export class ASTBuilder
364364
? this.visitReturnParameters(ctxReturnParameters)
365365
: null
366366

367-
// parse function visibility
368-
if (ctx.modifierList().ExternalKeyword_list().length > 0) {
369-
visibility = 'external'
370-
} else if (ctx.modifierList().InternalKeyword_list().length > 0) {
371-
visibility = 'internal'
372-
} else if (ctx.modifierList().PublicKeyword_list().length > 0) {
373-
visibility = 'public'
374-
} else if (ctx.modifierList().PrivateKeyword_list().length > 0) {
375-
visibility = 'private'
376-
}
377-
378367
isConstructor = name === this._currentContract
379368
isFallback = name === ''
380369
break

test/ast.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,19 @@ describe('AST', () => {
698698
})
699699
})
700700

701+
it('should produce different ASTs when the fallback function has the external keyword and when it does not', () => {
702+
const astWithExternal: any = parseNode('fallback() external {}')
703+
const astWithoutExternal: any = parseNode('fallback() {}')
704+
705+
assert.equal(astWithExternal.type, 'FunctionDefinition')
706+
assert.equal(astWithExternal.isFallback, true)
707+
assert.equal(astWithExternal.visibility, 'external')
708+
709+
assert.equal(astWithoutExternal.type, 'FunctionDefinition')
710+
assert.equal(astWithoutExternal.isFallback, true)
711+
assert.equal(astWithoutExternal.visibility, 'default')
712+
})
713+
701714
it('FunctionDefinition virtual receive', () => {
702715
const ast: any = parseNode('receive () external payable virtual {}')
703716
assert.deepEqual(ast, {

0 commit comments

Comments
 (0)