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

better parse recover for unknown func params #722

Merged
merged 3 commits into from
Oct 19, 2022
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
2 changes: 1 addition & 1 deletion src/bscPlugin/hover/HoverProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class HoverProcessor {
const fullName = util.getAllDottedGetParts(expression)?.map(x => x.text).join('.');

//find a constant with this name
const constant = scope.getConstFileLink(fullName, containingNamespace);
const constant = scope?.getConstFileLink(fullName, containingNamespace);
if (constant) {
const constantValue = new SourceNode(null, null, null, constant.item.value.transpile(new BrsTranspileState(file))).toString();
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export class BrsFileSemanticTokensProcessor {
private iterateExpressions() {
const scope = this.event.scopes[0];

//if this file has no scopes, there's nothing else we can do about this
if (!scope) {
return;
}

for (let expression of this.event.file.parser.references.expressions) {
//lift the callee from call expressions to handle namespaced function calls
if (isCallExpression(expression)) {
Expand Down
2 changes: 1 addition & 1 deletion src/bscPlugin/validation/ScopeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ScopeValidator {
const symbolTable = info.expression.getSymbolTable();
const firstPart = info.parts[0];
//flag all unknown left-most variables
if (!symbolTable.hasSymbol(firstPart.name?.text)) {
if (!symbolTable?.hasSymbol(firstPart.name?.text)) {
this.addMultiScopeDiagnostic({
file: file as BscFile,
...DiagnosticMessages.cannotFindName(firstPart.name?.text),
Expand Down
6 changes: 3 additions & 3 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1432,15 +1432,15 @@ export class BrsFile {

const scopesForFile = this.program.getScopesForFile(this);
const [scope] = scopesForFile;
scope.linkSymbolTable();

const expression = this.getClosestExpression(position);
if (expression) {
if (scope && expression) {
scope.linkSymbolTable();
let containingNamespace = expression.findAncestor<NamespaceStatement>(isNamespaceStatement)?.getName(ParseMode.BrighterScript);
const fullName = util.getAllDottedGetParts(expression)?.map(x => x.text).join('.');

//find a constant with this name
const constant = scope.getConstFileLink(fullName, containingNamespace);
const constant = scope?.getConstFileLink(fullName, containingNamespace);
if (constant) {
results.push(
util.createLocation(
Expand Down
15 changes: 14 additions & 1 deletion src/parser/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PrintStatement, FunctionStatement, NamespaceStatement, ImportStatement
import { Range } from 'vscode-languageserver';
import { DiagnosticMessages } from '../DiagnosticMessages';
import { isBlock, isCommentStatement, isFunctionStatement, isIfStatement, isIndexedGetExpression } from '../astUtils/reflection';
import { expectZeroDiagnostics } from '../testHelpers.spec';
import { expectDiagnostics, expectZeroDiagnostics } from '../testHelpers.spec';
import { BrsTranspileState } from './BrsTranspileState';
import { SourceNode } from 'source-map';
import { BrsFile } from '../files/BrsFile';
Expand Down Expand Up @@ -366,6 +366,19 @@ describe('parser', () => {
`, ParseMode.BrighterScript).diagnostics[0]?.message).not.to.exist;
});

it('does not scrap the entire function when encountering unknown parameter type', () => {
const parser = parse(`
sub test(param1 as unknownType)
end sub
`);
expectDiagnostics(parser, [{
...DiagnosticMessages.functionParameterTypeIsInvalid('param1', 'unknownType')
}]);
expect(
isFunctionStatement(parser.ast.statements[0])
).to.be.true;
});

describe('namespace', () => {
it('allows namespaces declared inside other namespaces', () => {
const parser = parse(`
Expand Down
3 changes: 1 addition & 2 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ export class Parser {
...DiagnosticMessages.functionParameterTypeIsInvalid(name.text, typeToken.text),
range: typeToken.range
});
throw this.lastDiagnosticAsError();
}
}
return new FunctionParameterExpression(
Expand Down Expand Up @@ -2536,7 +2535,7 @@ export class Parser {
/**
* Tries to get the next token as a type
* Allows for built-in types (double, string, etc.) or namespaced custom types in Brighterscript mode
* Will return a token of whatever is next to be parsed (unless `advanceIfUnknown` is false, in which case undefined will be returned instead
* Will return a token of whatever is next to be parsed
*/
private typeToken(): Token {
let typeToken: Token;
Expand Down