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

Create a task provider for linting the whole workspace #410

Merged
merged 3 commits into from
Feb 23, 2018
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
53 changes: 52 additions & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import * as fs from 'fs';
import {
workspace as Workspace, window as Window, commands as Commands, languages as Languages, Disposable, ExtensionContext, Uri, StatusBarAlignment, TextDocument,
CodeActionContext, Diagnostic, ProviderResult, Command, QuickPickItem, WorkspaceFolder as VWorkspaceFolder
CodeActionContext, Diagnostic, ProviderResult, Command, QuickPickItem, WorkspaceFolder as VWorkspaceFolder, Task, TaskDefinition, ShellExecution
} from 'vscode';
import {
LanguageClient, LanguageClientOptions, RequestType, TransportKind,
Expand Down Expand Up @@ -241,6 +241,52 @@ function createDefaultConfiguration(): void {
});
}

let taskProvider: Disposable | undefined;

function manageEslintTask(): void {
let enableLintTask: boolean = Workspace.getConfiguration('eslint').get('enableLintTask');

if(!taskProvider && enableLintTask) {
interface EslintTaskDefinition extends TaskDefinition {}

function createTask(): Task[] {
try {
let tasks: Task[] = [];

const kind: EslintTaskDefinition = {
type: 'eslint'
};
const taskName = 'lint workspace'
let command = ".\\node_modules\\.bin\\eslint ."
if(process.platform !== 'win32') {
command = "./node_modules/.bin/eslint ."
}
tasks.push(new Task(kind, taskName, 'ESLint', new ShellExecution(command), '$eslint-stylish'));
return tasks;
} catch (e) {
throw new Error(e);
}
}

taskProvider = Workspace.registerTaskProvider('eslint', {
provideTasks: () => {
return createTask();
},
resolveTask(_task: Task): Task | undefined {
return undefined;
}
});
} else if(taskProvider && !enableLintTask) {
const reloadAction = 'Reload';
Window.showInformationMessage("'ESLint: lint workspace' task will be removed after reloading the window.", reloadAction)
.then(selectedAction => {
if(selectedAction === reloadAction) {
Commands.executeCommand('workbench.action.reloadWindow');
}
});
}
}

let dummyCommands: Disposable[];

let defaultLanguages = ['javascript', 'javascriptreact'];
Expand Down Expand Up @@ -279,6 +325,7 @@ export function activate(context: ExtensionContext) {
if (activated) {
return;
}
manageEslintTask();
for (let textDocument of Workspace.textDocuments) {
if (shouldBeValidated(textDocument)) {
openListener.dispose();
Expand Down Expand Up @@ -723,4 +770,8 @@ export function deactivate() {
if (dummyCommands) {
dummyCommands.forEach(command => command.dispose());
}

if (taskProvider) {
taskProvider.dispose();
}
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
"javascriptreact"
],
"description": "An array of language ids which should be validated by ESLint"
},
"eslint.enableLintTask": {
"scope": "resource",
"type": "boolean",
"default": false,
"description": "Controls whether a task for linting the whole workspace will be available."
}
}
},
Expand Down