Skip to content

Commit

Permalink
feat: Improve linting speed & ability to disable default linting (lif…
Browse files Browse the repository at this point in the history
…eart#244)

* improve linting speed

* allow linter control
  • Loading branch information
lifeart authored Apr 14, 2021
1 parent 528a4e2 commit 472c0fe
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
isRootStartingWithFilePath,
getDepIfExists,
getPackageJSON,
cached,
PackageInfo,
} from './utils/layout-helpers';
import Server from './server';
import { Diagnostic, FileChangeType } from 'vscode-languageserver/node';
Expand Down Expand Up @@ -39,6 +41,7 @@ export class Project {
initIssues: Error[] = [];
files: Map<string, { version: number }> = new Map();
podModulePrefix = '';
@cached
get roots() {
const mainRoot = this.root;
const otherRoots = this.addonsMeta.map((meta) => meta.root);
Expand Down Expand Up @@ -105,7 +108,8 @@ export class Project {
addWatcher(cb: Watcher) {
this.watchers.push(cb);
}
get packageJSON() {
@cached
get packageJSON(): PackageInfo {
return getPackageJSON(this.root);
}
get name() {
Expand Down
41 changes: 30 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { Usage, findRelatedFiles } from './utils/usages-api';
import { URI } from 'vscode-uri';
import { MatchResultType } from './utils/path-matcher';
import { FileChangeType } from 'vscode-languageserver/node';
import { debounce } from 'lodash';

export default class Server {
initializers: any[] = [];
Expand Down Expand Up @@ -136,10 +137,16 @@ export default class Server {
this.connection.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders.bind(this));
}

this.executors['els.setConfig'] = async (_, __, [config]: [{ local: { addons: string[]; ignoredProjects: string[] } }]) => {
this.executors['els.setConfig'] = async (_, __, [config]: [{ local: { addons: string[]; ignoredProjects: string[]; useBuiltinLinting: boolean } }]) => {
this.projectRoots.setLocalAddons(config.local.addons);
this.projectRoots.setIgnoredProjects(config.local.ignoredProjects);

if (config.local.useBuiltinLinting === false) {
this.templateLinter.disable();
} else if (config.local.useBuiltinLinting === true) {
this.templateLinter.enable();
}

if (this.lazyInit) {
this.executeInitializers();
}
Expand Down Expand Up @@ -264,11 +271,14 @@ export default class Server {

this.documents.listen(this.connection);

this.onDidChangeContent = this.onDidChangeContent.bind(this);
this._onDidChangeContent = this._onDidChangeContent.bind(this);

// Bind event handlers
this.connection.onInitialize(this.onInitialize.bind(this));
this.connection.onInitialized(this.onInitialized.bind(this));
this.documents.onDidChangeContent(this.onDidChangeContent.bind(this));
this.documents.onDidOpen(this.onDidChangeContent.bind(this));
this.documents.onDidChangeContent(this.onDidChangeContent);
this.documents.onDidOpen(this.onDidChangeContent);
this.connection.onDidChangeWatchedFiles(this.onDidChangeWatchedFiles.bind(this));
this.connection.onDocumentSymbol(this.onDocumentSymbol.bind(this));
this.connection.onDefinition(this.definitionProvider.handler);
Expand Down Expand Up @@ -443,31 +453,40 @@ export default class Server {
return results;
}

lastChangeEvent!: TextDocumentChangeEvent<TextDocument>;

private async onDidChangeContent(change: TextDocumentChangeEvent<TextDocument>) {
this.lastChangeEvent = change;
debounce(this._onDidChangeContent, 250);
}

private async _onDidChangeContent() {
// this.setStatusText('did-change');
const change = this.lastChangeEvent;

const lintResults = await this.templateLinter.lint(change.document);
const results: Diagnostic[] = [];

if (change !== this.lastChangeEvent) {
return;
}

if (Array.isArray(lintResults)) {
lintResults.forEach((result) => {
results.push(result);
});
this.connection.sendDiagnostics({ version: change.document.version, uri: change.document.uri, diagnostics: lintResults });
}

const addonResults = await this.runAddonLinters(change.document);

addonResults.forEach((result) => {
results.push(result);
});
if (change !== this.lastChangeEvent) {
return;
}

const project = this.projectRoots.projectForUri(change.document.uri);

if (project) {
project.trackChange(change.document.uri, FileChangeType.Changed);
}

this.connection.sendDiagnostics({ uri: change.document.uri, diagnostics: results });
this.connection.sendDiagnostics({ version: change.document.version, uri: change.document.uri, diagnostics: addonResults });
}

private onDidChangeWatchedFiles(items: DidChangeWatchedFilesParams) {
Expand Down
13 changes: 13 additions & 0 deletions src/template-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ function setCwd(cwd: string) {

export default class TemplateLinter {
private _linterCache = new Map<Project, any>();
private _isEnabled = true;

constructor(private server: Server) {}

disable() {
this._isEnabled = false;
}

enable() {
this._isEnabled = true;
}

private getProjectForDocument(textDocument: TextDocument) {
const ext = getExtension(textDocument);

Expand Down Expand Up @@ -81,6 +90,10 @@ export default class TemplateLinter {
}
}
async lint(textDocument: TextDocument): Promise<Diagnostic[] | undefined> {
if (this._isEnabled === false) {
return;
}

const cwd = process.cwd();
const project = this.getProjectForDocument(textDocument);

Expand Down
6 changes: 4 additions & 2 deletions src/utils/addon-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PackageInfo,
ADDON_CONFIG_KEY,
hasEmberLanguageServerExtension,
addonVersion,
} from './layout-helpers';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as path from 'path';
Expand Down Expand Up @@ -196,6 +197,7 @@ export function collectProjectProviders(root: string, addons: string[]): Project
addonsMeta.push({
name: info.name,
root: packagePath,
version: addonVersion(info),
});
}

Expand Down Expand Up @@ -224,7 +226,7 @@ export function collectProjectProviders(root: string, addons: string[]): Project
codeActionProviders: CodeActionResolveFunction[];
initFunctions: InitFunction[];
info: string[];
addonsMeta: { name: string; root: string }[];
addonsMeta: AddonMeta[];
} = {
definitionProviders: [],
referencesProviders: [],
Expand Down Expand Up @@ -319,7 +321,7 @@ export function collectProjectProviders(root: string, addons: string[]): Project
return result;
}

export type AddonMeta = { root: string; name: string };
export type AddonMeta = { root: string; name: string; version: null | 1 | 2 };
export type DependencyMeta = { name: string; version: string };
export interface ProjectProviders {
definitionProviders: DefinitionResolveFunction[];
Expand Down
22 changes: 21 additions & 1 deletion src/utils/layout-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ export function isELSAddonRoot(root: string) {
return hasEmberLanguageServerExtension(pack);
}

export function cached(_proto: unknown, prop: string, desc: PropertyDescriptor) {
const values = new WeakMap();

return {
get() {
if (!values.has(this)) {
values.set(this, {});
}

const objects = values.get(this);

if (!(prop in objects)) {
objects[prop] = desc.get?.call(this);
}

return objects[prop];
},
};
}

function getRecursiveInRepoAddonRoots(root: string, roots: string[]) {
const packageData = getPackageJSON(root);
const emberAddonPaths: string[] = (packageData['ember-addon'] && packageData['ember-addon'].paths) || [];
Expand Down Expand Up @@ -384,7 +404,7 @@ export function isEmberAddon(info: PackageInfo) {
return info.keywords && info.keywords.includes('ember-addon');
}

function addonVersion(info: PackageInfo) {
export function addonVersion(info: PackageInfo) {
if (!isEmberAddon(info)) {
return null;
}
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ Object {
Object {
"name": "addon1",
"root": "node_modules/addon1",
"version": null,
},
Object {
"name": "addon2",
"root": "node_modules/addon2",
"version": null,
},
Object {
"name": "addon3",
"root": "node_modules/addon3",
"version": null,
},
Object {
"name": "addon4",
"root": "node_modules/addon4",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -87,6 +91,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand All @@ -110,6 +115,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -146,6 +152,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -179,6 +186,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -212,6 +220,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -317,10 +326,12 @@ Object {
Object {
"name": "biz",
"root": "lib/biz",
"version": 1,
},
Object {
"name": "foo",
"root": "../lib/foo",
"version": 1,
},
],
"registry": Object {
Expand Down Expand Up @@ -743,6 +754,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -776,6 +788,7 @@ Object {
Object {
"name": "provider",
"root": "node_modules/provider",
"version": null,
},
],
"registry": Object {
Expand Down Expand Up @@ -2378,6 +2391,7 @@ Object {
Object {
"name": "biz",
"root": "lib/biz",
"version": 1,
},
],
"registry": Object {
Expand Down Expand Up @@ -2417,6 +2431,7 @@ Object {
Object {
"name": "biz",
"root": "lib/biz",
"version": 1,
},
],
"registry": Object {
Expand Down Expand Up @@ -2804,10 +2819,12 @@ Object {
Object {
"name": "biz",
"root": "lib/biz",
"version": 1,
},
Object {
"name": "foo",
"root": "../lib/foo",
"version": 1,
},
],
"registry": Object {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ES2018",
"module": "commonjs",
"allowSyntheticDefaultImports": true,
Expand Down

0 comments on commit 472c0fe

Please # to comment.