Skip to content
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
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
},
"main": "dist/client/out/extension",
"browser": "dist/web/webextension.js",
"activationEvents": ["onLanguage:vba"],
"activationEvents": [
"onLanguage:vba"
],
"contributes": {
"languages": [
{
Expand Down Expand Up @@ -183,6 +185,7 @@
"@vscode/test-electron": "^2.5.2",
"@vscode/vsce": "^3.7.1",
"antlr4ng-cli": "^2.0.0",
"dedent": "^1.7.1",
"esbuild": "^0.25.4",
"eslint": "^9.27.0",
"js-yaml": "^4.1.0",
Expand Down
4 changes: 4 additions & 0 deletions server/src/project/elements/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
IndexExpressionContext,
LExpressionContext,
MemberAccessExpressionContext,
OptionalParamContext,
ParamArrayContext,
PositionalParamContext,
SimpleNameExpressionContext,
UnrestrictedNameContext,
Expand Down Expand Up @@ -37,6 +39,8 @@ export class WithStatementElement extends BaseRuleSyntaxElement<WithStatementCon
export type NameExpressionContext = LExpressionContext
| SimpleNameExpressionContext
| MemberAccessExpressionContext
| OptionalParamContext
| ParamArrayContext
| PositionalParamContext
| IndexExpressionContext
| WithMemberAccessExpressionContext
Expand Down
6 changes: 6 additions & 0 deletions server/src/project/parser/vbaListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ export class VbaListener extends vbaListener {

enterOptionalParam = (ctx: OptionalParamContext) => {
if (this.verbose) Services.logger.debug(`enterOptionalParam: ${ctx.getText()}`, this.parserStateStack.length);
this.pushNameElement(ctx);
const identifierCtx = ctx.paramDcl().untypedNameParamDcl()?.ambiguousIdentifier()
?? ctx.paramDcl().typedNameParamDcl()?.typedName().ambiguousIdentifier();

Expand All @@ -361,6 +362,7 @@ export class VbaListener extends vbaListener {

enterParamArray = (ctx: ParamArrayContext) => {
if (this.verbose) Services.logger.debug(`enterParamArray: ${ctx.getText()}`, this.parserStateStack.length);
this.pushNameElement(ctx);
this.addNameElementContext(ctx.ambiguousIdentifier(), 'ambigiousNameContext');
Comment thread
DecimalTurn marked this conversation as resolved.
};

Expand All @@ -383,6 +385,10 @@ export class VbaListener extends vbaListener {
nameElement.addName(ctx);
}

/**
* Creates and pushes the active name-expression container for the current
* parse context.
*/
private pushNameElement(ctx: NameExpressionContext): void {
if (this.verbose) Services.logger.debug('Pushing name', this.parserStateStack.length);
const element = new NameExpressionElement(ctx, this.document.textDocument);
Expand Down
106 changes: 106 additions & 0 deletions server/src/test/vbaListener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import 'reflect-metadata';
import { describe, it } from 'mocha';
import * as assert from 'assert';
import dedent from 'dedent';
import { container } from 'tsyringe';
import { CancellationTokenSource } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';

import '../extensions/antlrCoreExtensions';
import '../extensions/antlrVbaParserExtensions';
import '../extensions/stringExtensions';

import { ScopeItemCapability, ScopeType } from '../capabilities/capabilities';
import { BaseProjectDocument } from '../project/document';

type LogNotification = {
type: number;
message: string;
level: number;
};

const ERROR_LOG_TYPE = 1;
Comment thread
DecimalTurn marked this conversation as resolved.

function assertNoErrorLogs(logs: LogNotification[], context: string): void {
const errorLogs = logs.filter(log => log.type === ERROR_LOG_TYPE);

assert.strictEqual(
errorLogs.length,
0,
`${context} produced error logs: ${errorLogs.map(x => x.message).join(' | ')}`
);
}

function registerTestServices(logs: LogNotification[]): void {
container.clearInstances();

container.registerInstance('_Connection', {
sendNotification: (_method: string, payload: LogNotification) => logs.push(payload)
} as any);

container.registerInstance('ILanguageServer', {
clientConfiguration: Promise.resolve({
maxDocumentLines: 50000,
maxNumberOfProblems: 100,
doWarnOptionExplicitMissing: true,
environment: { os: 'test', version: 'test' },
logLevel: { outputChannel: 'debug' }
})
} as any);

container.registerInstance('IWorkspace', {
clearDocumentsConfiguration: () => undefined,
formatParseDocument: async () => undefined,
parseDocument: async () => undefined,
openDocument: () => undefined,
closeDocument: () => undefined,
addWorkspaceFolder: async () => undefined
} as any);

const languageScope = new ScopeItemCapability(undefined, ScopeType.VBA);
const appScope = new ScopeItemCapability(undefined, ScopeType.APPLICATION, undefined, languageScope);
const projectScope = new ScopeItemCapability(undefined, ScopeType.PROJECT, undefined, appScope);
container.registerInstance('ProjectScope', projectScope);
}

async function parseText(uri: string, text: string, logs: LogNotification[]): Promise<void> {
registerTestServices(logs);
const textDocument = TextDocument.create(uri, 'vba', 1, text);
const projectDocument = BaseProjectDocument.create(textDocument);
await projectDocument.parse(new CancellationTokenSource().token);
}

describe('VBA Listener Integration', () => {
it('does not log optional parameter identifiers as unresolved names', async () => {
const logs: LogNotification[] = [];
const vbaCode = dedent`
Attribute VB_Name = "ScopeDiagnostics"

Option Explicit

Public Sub TestSub(Optional test_param As Variant = -0.1)
Attribute TestSub.VB_Description = "docstring."
End Sub
`;

await parseText('file:///test/ScopeDiagnostics.bas', vbaCode, logs);

assertNoErrorLogs(logs, 'Optional parameter parse');
});

it('does not log ParamArray identifiers as unresolved names', async () => {
const logs: LogNotification[] = [];
const vbaCode = dedent`
Attribute VB_Name = "ParamArrayTest"

Option Explicit

Public Sub TestVariadicSub(ParamArray args() As Variant)
End Sub
`;

await parseText('file:///test/ParamArrayTest.bas', vbaCode, logs);

assertNoErrorLogs(logs, 'ParamArray parse');
});
});