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

Add function parameter support in interfaces #924

Merged
merged 1 commit into from
Oct 2, 2023
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
19 changes: 18 additions & 1 deletion docs/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,21 @@ end interface
```BrightScript

```
</details>
</details>

</details>

## Methods
Interfaces can describe complex methods as well
```brighterscript
interface Dog
sub barkAt(nemesis as Cat)
end interface
```

<details>
<summary>View the transpiled BrightScript code</summary>

```BrightScript
```
</details>
14 changes: 13 additions & 1 deletion src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,19 @@ export class Parser {
const name = this.identifier(...AllowedProperties);
const leftParen = this.consumeToken(TokenKind.LeftParen);

const params = [];
let params = [] as FunctionParameterExpression[];
if (!this.check(TokenKind.RightParen)) {
do {
if (params.length >= CallExpression.MaximumArguments) {
this.diagnostics.push({
...DiagnosticMessages.tooManyCallableParameters(params.length, CallExpression.MaximumArguments),
range: this.peek().range
});
}

params.push(this.functionParameter());
} while (this.match(TokenKind.Comma));
}
const rightParen = this.consumeToken(TokenKind.RightParen);
let asToken = null as Token;
let returnTypeToken = null as Token;
Expand Down
9 changes: 9 additions & 0 deletions src/parser/tests/statement/InterfaceStatement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,13 @@ describe('InterfaceStatement', () => {
'this comment was throwing exception during transpile
`);
});

it('allows parameters in interface method signatures', () => {
testGetTypedef(`
interface Person
sub someFunc(name as string, age as integer) as string
someField as string
end interface
`, undefined, undefined, undefined, true);
});
});