Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any node members in brs files #1333

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/bscPlugin/validation/ScopeValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,43 @@ describe('ScopeValidator', () => {
expectZeroDiagnostics(program);
});

it('does not have diagnostic when accessing unknown member of node in Brightscript mode', () => {
program.setFile('source/main.brs', `
' @param {roSGNode} node
function testNodeMember(node)
x = node.whatever
return x
end function
`);
program.validate();
expectZeroDiagnostics(program);
});

it('does not have diagnostic when accessing unknown member of contentnode in Brightscript mode', () => {
program.setFile('source/main.brs', `
' @param {roSgNodeCOntentNode} node
function testNodeMember(node)
x = node.whatever
return x
end function
`);
program.validate();
expectZeroDiagnostics(program);
});

it('does not have diagnostic when accessing unknown member of created node in Brightscript mode', () => {
program.setFile('source/main.brs', `
' @param {string} nodeSubtype
function testNodeMember(nodeSubtype)
x = createObject("roSgNode",nodeSubtype)
x.whatever = true
return x
end function
`);
program.validate();
expectZeroDiagnostics(program);
});

it('allows anything on m in an anonymous function', () => {
program.setFile('source/main.bs', `
function test()
Expand Down
7 changes: 7 additions & 0 deletions src/bscPlugin/validation/ScopeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ export class ScopeValidator {
* In particular, since BrightScript does not support Unions, and there's no way to cast them to something else
* if the result of .getType() is a union, and we're in a .brs (brightScript) file, treat the result as Dynamic
*
* Also, for BrightScript parse-mode, if .getType() returns a node type, do not validate unknown members.
*
* In most cases, this returns the result of node.getType()
*
* @param file the current file being processed
Expand All @@ -1265,6 +1267,11 @@ export class ScopeValidator {
//this is a union
return DynamicType.instance;
}

if (isComponentType(type)) {
// modify type to allow any member access for Node types
type.changeUnknownMemberToDynamic = true;
}
}

// by default return the result of node.getType()
Expand Down
9 changes: 8 additions & 1 deletion src/types/InheritableType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ export abstract class InheritableType extends BscType {
}
}

changeUnknownMemberToDynamic = false;

getMemberType(memberName: string, options: GetTypeOptions) {
let hasRoAssociativeArrayAsAncestor = this.name.toLowerCase() === 'roassociativearray' || this.getAncestorTypeList()?.find(ancestorType => ancestorType.name.toLowerCase() === 'roassociativearray');

if (hasRoAssociativeArrayAsAncestor) {
return super.getMemberType(memberName, options) ?? DynamicType.instance;
}

return super.getMemberType(memberName, { ...options, fullName: memberName, tableProvider: () => this.memberTable });
const resultType = super.getMemberType(memberName, { ...options, fullName: memberName, tableProvider: () => this.memberTable });

if (this.changeUnknownMemberToDynamic && !resultType.isResolvable()) {
return DynamicType.instance;
}
return resultType;
}

public toString() {
Expand Down