Skip to content

Commit

Permalink
adds warning for mismatched class member accessibility (#402)
Browse files Browse the repository at this point in the history
* adds warning for mismatched class member accessibility

* updates overridden accessType message, as requested in pr review
  • Loading branch information
georgejecook authored May 4, 2021
1 parent fc4a44c commit cf19c70
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/DiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,11 @@ export let DiagnosticMessages = {
message: `Missing expression(s) in 'dim' statement`,
code: 1121,
severity: DiagnosticSeverity.Error
}),
mismatchedOverriddenMemberVisibility: (childClassName: string, memberName: string, childAccessModifier: string, ancestorAccessModifier: string, ancestorClassName: string) => ({
message: `Access modifier mismatch: '${memberName}' is ${childAccessModifier} in type '${childClassName}' but is ${ancestorAccessModifier} in base type '${ancestorClassName}'.`,
code: 1122,
severity: DiagnosticSeverity.Error
})
};

Expand Down
53 changes: 53 additions & 0 deletions src/files/BrsFile.Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,59 @@ describe('BrsFile BrighterScript classes', () => {
});
});

it('detects overridden methods with different visibility', () => {
program.addOrReplaceFile({ src: `${rootDir}/source/main.bs`, dest: 'source/main.bs' }, `
class Animal
sub speakInPublic()
end sub
protected sub speakWithFriends()
end sub
private sub speakWithFamily()
end sub
end class
class Duck extends Animal
private override sub speakInPublic()
end sub
public override sub speakWithFriends()
end sub
override sub speakWithFamily()
end sub
end class
`);
program.validate();
expect(program.getDiagnostics()[0]).to.exist.and.to.include({
...DiagnosticMessages.mismatchedOverriddenMemberVisibility('Duck', 'speakInPublic', 'private', 'public', 'Animal')
});
expect(program.getDiagnostics()[1]).to.exist.and.to.include({
...DiagnosticMessages.mismatchedOverriddenMemberVisibility('Duck', 'speakWithFriends', 'public', 'protected', 'Animal')
});
expect(program.getDiagnostics()[2]).to.exist.and.to.include({
...DiagnosticMessages.mismatchedOverriddenMemberVisibility('Duck', 'speakWithFamily', 'public', 'private', 'Animal')
});
});
it('allows overridden methods with matching visibility', () => {
program.addOrReplaceFile({ src: `${rootDir}/source/main.bs`, dest: 'source/main.bs' }, `
class Animal
sub speakInPublic()
end sub
protected sub speakWithFriends()
end sub
private sub speakWithFamily()
end sub
end class
class Duck extends Animal
override sub speakInPublic()
end sub
protected override sub speakWithFriends()
end sub
private override sub speakWithFamily()
end sub
end class
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
});

it('detects extending unknown parent class', () => {
program.addOrReplaceFile('source/main.brs', `
class Duck extends Animal
Expand Down
20 changes: 20 additions & 0 deletions src/validators/ClassValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isCallExpression, isClassFieldStatement, isClassMethodStatement, isCust
import type { BscFile, BsDiagnostic } from '../interfaces';
import { createVisitor, WalkMode } from '../astUtils';
import type { BrsFile } from '../files/BrsFile';
import { TokenKind } from '../lexer';

export class BsClassValidator {
private scope: Scope;
Expand Down Expand Up @@ -223,6 +224,25 @@ export class BsClassValidator {
range: member.range
});
}

//child member has different visiblity
if (
//is a method
isClassMethodStatement(member) &&
(member.accessModifier?.kind ?? TokenKind.Public) !== (ancestorAndMember.member.accessModifier?.kind ?? TokenKind.Public)
) {
this.diagnostics.push({
...DiagnosticMessages.mismatchedOverriddenMemberVisibility(
classStatement.name.text,
ancestorAndMember.member.name?.text,
member.accessModifier?.text || 'public',
ancestorAndMember.member.accessModifier?.text || 'public',
ancestorAndMember.classStatement.getName(ParseMode.BrighterScript)
),
file: classStatement.file,
range: member.range
});
}
}

if (isClassMethodStatement(member)) {
Expand Down

0 comments on commit cf19c70

Please sign in to comment.