Skip to content

Commit

Permalink
Validator pattern change (#148)
Browse files Browse the repository at this point in the history
feat: implement ValidatorManager
  • Loading branch information
haifeng-li-at-salesforce authored Nov 2, 2024
1 parent 27d73b7 commit 5f8996f
Show file tree
Hide file tree
Showing 15 changed files with 527 additions and 325 deletions.
11 changes: 9 additions & 2 deletions lsp/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
CodeActionKind
} from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { validateDocument } from './validateDocument';
import { OrgUtils } from './utils/orgUtils';
import { WorkspaceUtils } from './utils/workspaceUtils';
import { getSettings } from './diagnostic/DiagnosticSettings';
import { ValidatorManager } from './validatorManager';
import { debounce } from './utils/commonUtils';

// Create a connection for the server, using Node's IPC as a transport.
Expand All @@ -41,6 +41,9 @@ let diagnosticsSettingSection = '';

// initialize default settings
let settings = getSettings({});

const validatorManager = ValidatorManager.createInstance();

const documentCache: Map<string, TextDocument> = new Map();

connection.onInitialize((params: InitializeParams) => {
Expand Down Expand Up @@ -172,7 +175,11 @@ connection.languages.diagnostics.on(async (params) => {
if (document !== undefined) {
return {
kind: DocumentDiagnosticReportKind.Full,
items: await validateDocument(settings, document, extensionTitle)
items: await validatorManager.validateDocument(
settings,
document,
extensionTitle
)
} satisfies DocumentDiagnosticReport;
} else {
// We don't know the document. We can either try to read it from disk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
*/

import { TextDocument } from 'vscode-languageserver-textdocument';
import { validateGraphql } from '../validateGraphql';
import { GraphQLValidator } from '../../validator/gqlValidator';
import { OversizedRecord } from '../../diagnostic/gql/over-sized-record';

import * as assert from 'assert';
import { suite, test, beforeEach, afterEach } from 'mocha';
import * as sinon from 'sinon';
import { OrgUtils } from '../utils/orgUtils';
import Book__c from '../../testFixture/objectInfos/Book__c.json';
import { ObjectInfoRepresentation } from '../types';
import { OrgUtils } from '../../utils/orgUtils';
import Book__c from '../../../testFixture/objectInfos/Book__c.json';
import { ObjectInfoRepresentation } from '../../types';

suite('Diagnostics Test Suite - Server - Validate GraphQL', () => {
suite('Diagnostics Test Suite - Server - GraphQL Validator', () => {
let sandbox: sinon.SinonSandbox;
beforeEach(function () {
sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -54,7 +56,17 @@ suite('Diagnostics Test Suite - Server - Validate GraphQL', () => {
};
`
);
const diagnostics = await validateGraphql({}, textDocument);

const graphqlValidator = new GraphQLValidator();
graphqlValidator.addProducer(new OversizedRecord());
const sections =
graphqlValidator.gatherDiagnosticSections(textDocument);
assert.equal(sections.length, 1);
const diagnostics = await graphqlValidator.validateData(
{},
sections[0].document,
sections[0].data
);

assert.equal(diagnostics.length, 2);
});
Expand All @@ -74,8 +86,10 @@ suite('Diagnostics Test Suite - Server - Validate GraphQL', () => {
};
`
);
const diagnostics = await validateGraphql({}, textDocument);

assert.equal(diagnostics.length, 0);
const graphqlValidator = new GraphQLValidator();
graphqlValidator.addProducer(new OversizedRecord());
const sections =
graphqlValidator.gatherDiagnosticSections(textDocument);
assert.equal(sections.length, 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
*/

import { TextDocument } from 'vscode-languageserver-textdocument';
import { validateHtml } from '../validateHtml';
import { suite, test } from 'mocha';
import { HTMLValidator } from '../../validator/htmlValidator';
import * as assert from 'assert';
import { MobileOfflineFriendly } from '../../diagnostic/html/mobileOfflineFriendly';

suite('Diagnostics Test Suite - Server - HTML Validator', () => {
const htmlValidator: HTMLValidator = new HTMLValidator();
htmlValidator.addProducer(new MobileOfflineFriendly());

suite('Diagnostics Test Suite - Server - Validate html', () => {
test('Correct number of non-friendly mobile offline base components is determined', async () => {
const textDocument = TextDocument.create(
'file://test.html',
Expand All @@ -30,7 +34,15 @@ suite('Diagnostics Test Suite - Server - Validate html', () => {
`
);

const diagnostics = await validateHtml({}, textDocument);
const htmlSections =
htmlValidator.gatherDiagnosticSections(textDocument);
assert.equal(htmlSections.length, 1);

const diagnostics = await htmlValidator.validateData(
{},
htmlSections[0].document,
htmlSections[0].data
);
assert.equal(diagnostics.length, 2);
});

Expand All @@ -57,7 +69,15 @@ suite('Diagnostics Test Suite - Server - Validate html', () => {
`
);

const diagnostics = await validateHtml({}, textDocument);
const htmlSections =
htmlValidator.gatherDiagnosticSections(textDocument);
assert.equal(htmlSections.length, 1);

const diagnostics = await htmlValidator.validateData(
{},
htmlSections[0].document,
htmlSections[0].data
);
assert.equal(diagnostics.length, 3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as assert from 'assert';
import { suite, test } from 'mocha';
import { validateJs } from '../validateJs';

import { JSValidator } from '../../validator/jsValidator';
import {
AdaptersLocalChangeNotAware,
LOCAL_CHANGE_NOT_AWARE_MESSAGE,
RULE_ID
} from '../diagnostic/js/adapters-local-change-not-aware';
} from '../../diagnostic/js/adapters-local-change-not-aware';

suite('Diagnostics Test Suite - Server - JS Validator', () => {
const jsValidator = new JSValidator();
jsValidator.addProducer(new AdaptersLocalChangeNotAware());

suite('Diagnostics Test Suite - Server - Validate JS', () => {
const textDocument = TextDocument.create(
'file://test.js',
'javascript',
Expand All @@ -41,24 +46,38 @@ suite('Diagnostics Test Suite - Server - Validate JS', () => {
);

test('Validate local change not aware adapters', async () => {
const diagnostics = await validateJs({}, textDocument);
const jsSections = jsValidator.gatherDiagnosticSections(textDocument);
assert.equal(jsSections.length, 1);
const diagnostics = await jsValidator.validateData(
{},
jsSections[0].document,
jsSections[0].data
);
assert.equal(diagnostics.length, 1);
assert.equal(diagnostics[0].message, LOCAL_CHANGE_NOT_AWARE_MESSAGE);
});

test('No diagnostics return if individually suppressed', async () => {
const diagnostics = await validateJs(
const jsSections = jsValidator.gatherDiagnosticSections(textDocument);

const diagnostics = await jsValidator.validateData(
{ suppressByRuleId: new Set([RULE_ID]) },
textDocument
jsSections[0].document,
jsSections[0].data
);

assert.equal(diagnostics.length, 0);
});

test('No diagnostics return if all suppressed', async () => {
const diagnostics = await validateJs(
const jsSections = jsValidator.gatherDiagnosticSections(textDocument);

const diagnostics = await jsValidator.validateData(
{ suppressAll: true },
textDocument
jsSections[0].document,
jsSections[0].data
);

assert.equal(diagnostics.length, 0);
});

Expand All @@ -71,8 +90,8 @@ suite('Diagnostics Test Suite - Server - Validate JS', () => {
var var i = 100;
`
);
const diagnostics = await validateJs({}, textDocument);
const jsSections = jsValidator.gatherDiagnosticSections(textDocument);

assert.equal(diagnostics.length, 0);
assert.equal(jsSections.length, 0);
});
});
51 changes: 51 additions & 0 deletions lsp/server/src/test/validatorManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import { TextDocument } from 'vscode-languageserver-textdocument';
import * as assert from 'assert';
import { suite, test } from 'mocha';

import { ValidatorManager } from '../validatorManager';
import { LOCAL_CHANGE_NOT_AWARE_MESSAGE } from '../diagnostic/js/adapters-local-change-not-aware';

suite('Diagnostics Test Suite - Server - ValidatorManager', () => {
const validatorManager = ValidatorManager.createInstance();
const textDocument = TextDocument.create(
'file://test.js',
'javascript',
1,
`
import { LightningElement, wire } from "lwc";
import { getRelatedListRecords } from "lightning/uiRelatedListApi";
export default class RelatedListRecords extends LightningElement {
recordId = "0015g00000XYZABC";
relatedRecords;
@wire(getRelatedListRecords, {
parentRecordId: "$recordId",
relatedListId: "Opportunities",
fields: ["Opportunity.Name"],
})
relatedListHandler({ error, data }) {
}
}
`
);

test('Validate JS file with local change not aware adapter', async () => {
const diagnostics = await validatorManager.validateDocument(
{},
textDocument,
'testExtension'
);
assert.equal(diagnostics.length, 1);
assert.equal(diagnostics[0].message, LOCAL_CHANGE_NOT_AWARE_MESSAGE);
});
});
2 changes: 1 addition & 1 deletion lsp/server/src/utils/babelUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import * as parser from '@babel/parser';
import { Node } from '@babel/types';
import type { Node } from '@babel/types';

/**
* parse the input javascript source code and return the corresponding babel node.
Expand Down
54 changes: 0 additions & 54 deletions lsp/server/src/validateDocument.ts

This file was deleted.

Loading

0 comments on commit 5f8996f

Please sign in to comment.