Skip to content

Commit

Permalink
Merge pull request #410 from jackrl/workspace-lint-task
Browse files Browse the repository at this point in the history
Create a task provider for linting the whole workspace
  • Loading branch information
dbaeumer authored Feb 23, 2018
2 parents 6c4677a + 311fbaa commit 09418a4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
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

0 comments on commit 09418a4

Please sign in to comment.