From 28c22ac670b7978e3f17157ffaa2f96413036ef5 Mon Sep 17 00:00:00 2001 From: Florian Maunier Date: Tue, 28 Aug 2018 17:55:20 +0200 Subject: [PATCH 1/2] Implement the potential use of more engines Only standardx has been added for now, but any standard-engine derivative should be working fine. --- README.md | 6 ++++-- client/src/extension.ts | 25 ++++++++++++------------- package.json | 13 +++++++++---- server/src/server.ts | 31 ++++++++++++++++++++++--------- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 577239d..a15c04d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ VSCode extension to integrate [JavaScript Standard Style](https://github.com/fer We support [JavaScript Semi-Standard Style](https://github.com/Flet/semistandard) too, if you prefer keeping the semicolon. +[JavaScript Standard Style with custom tweaks](https://github.com/standard/standardx) is also supported if you want to fine-tune your ESLint config while keeping the power of Standard. + ## How to use 1. **Install the 'JavaScript Standard Style' extension** @@ -12,7 +14,7 @@ We support [JavaScript Semi-Standard Style](https://github.com/Flet/semistandard You will need to reload VSCode before new extensions can be used. -2. **Install `standard` or `semistandard`** +2. **Install `standard`, `semistandard` or `standardx`** This can be done globally or locally. We recommend that you install them locally (i.e. saved in your project's `devDependencies`), to ensure that other developers have it installed when working on the project. @@ -32,7 +34,7 @@ Option|Description|Default `standard.nodePath`|use this setting if an installed `standard` package can't be detected.|`null` `standard.validate`|an array of language identifiers specify the files to be validated|`["javascript", "javascriptreact"]` `standard.workingDirectories`|an array for working directories to be used.|`[]` -`standard.semistandard`|You can use `semistandard` if you set this option to `true`. **Just make sure you've installed the `semistandard` package, instead of `standard`.**|`false` +`standard.engine`|You can use `semistandard` or `standardx` instead of `standard`. **Just make sure you've installed the `semistandard` or the `standardx` package, instead of `standard`.**|`standard` `standard.usePackageJson`|if set to `true`, JavaScript Standard Style will use project's `package.json` settings, otherwise globally installed `standard` module is used |`false` diff --git a/client/src/extension.ts b/client/src/extension.ts index caeffda..7fdd676 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -17,9 +17,8 @@ import { ServerOptions, Proposed, DocumentFilter, DidCloseTextDocumentNotification, DidOpenTextDocumentNotification, CancellationToken, WorkspaceMiddleware } from 'vscode-languageclient'; -type LinterValues = 'standard' | 'semistandard'; -type LinterNameValues = 'JavaScript Standard Style' | 'JavaScript Semi-Standard Style'; -var linter: LinterValues; +type LinterValues = 'standard' | 'semistandard' | 'standardx'; +type LinterNameValues = 'JavaScript Standard Style' | 'JavaScript Semi-Standard Style' | 'JavaScript Standard Style with custom tweaks'; var linterName: LinterNameValues; namespace Is { @@ -64,7 +63,7 @@ interface TextDocumentSettings { validate: boolean; autoFix: boolean; autoFixOnSave: boolean; - semistandard: boolean; + engine: LinterValues; usePackageJson: boolean; options: any | undefined; run: RunValues; @@ -111,11 +110,12 @@ interface WorkspaceFolderItem extends QuickPickItem { } function getLinterName() { let configuration = Workspace.getConfiguration('standard'); - return configuration && configuration.get('semistandard', false) ? 'JavaScript Semi-Standard Style': 'JavaScript Standard Style'; -} -function getLinter() { - let configuration = Workspace.getConfiguration('standard'); - return configuration && configuration.get('semistandard', false) ? 'semistandard': 'standard'; + let linterNames: { [linter: string]: LinterNameValues; } = { + 'standard': 'JavaScript Standard Style', + 'semistandard': 'JavaScript Semi-Standard Style', + 'standardx': 'JavaScript Standard Style with custom tweaks' + } + return linterNames[configuration.get('engine', 'standard')]; } function pickFolder(folders: VWorkspaceFolder[], placeHolder: string): Thenable { if (folders.length === 1) { @@ -178,7 +178,7 @@ function disable() { }); } -let dummyCommands: [Disposable]; +let dummyCommands: Disposable[]; let defaultLanguages = ['javascript', 'javascriptreact']; function shouldBeValidated(textDocument: TextDocument): boolean { @@ -243,7 +243,6 @@ export function activate(context: ExtensionContext) { } export function realActivate(context: ExtensionContext) { - linter = getLinter(); linterName = getLinterName(); let statusBarItem = Window.createStatusBarItem(StatusBarAlignment.Right, 0); @@ -427,7 +426,7 @@ export function realActivate(context: ExtensionContext) { validate: false, autoFix: false, autoFixOnSave: false, - semistandard: config.get('semistandard', false), + engine: config.get('engine', 'standard'), usePackageJson: config.get('usePackageJson', false), options: config.get('options', {}), run: config.get('run', 'onType'), @@ -546,7 +545,7 @@ export function realActivate(context: ExtensionContext) { let uri: Uri = Uri.parse(params.source.uri); let workspaceFolder = Workspace.getWorkspaceFolder(uri); let config = Workspace.getConfiguration('standard'); - let linter = config.get('semistandard', false) ? 'semistandard' : 'standard'; + let linter = config.get('engine', 'standard'); if (workspaceFolder) { client.info([ '', diff --git a/package.json b/package.json index 88c87b4..d7f72ba 100644 --- a/package.json +++ b/package.json @@ -62,11 +62,16 @@ "default": {}, "description": "The standard options object to provide args normally passed to JavaScript Standard Style when executed from a command line." }, - "standard.semistandard": { + "standard.engine": { "scope": "resource", - "type": "boolean", - "default": false, - "description": "Controls whether JavaScript Semistandard Style should be enabled or disabled." + "type": "string", + "enum": [ + "standard", + "semistandard", + "standardx" + ], + "default": "standard", + "description": "Controls whether VSCode should use an alternate Standard engine, like semistandard or standardx." }, "standard.trace.server": { "scope": "window", diff --git a/server/src/server.ts b/server/src/server.ts index 56ca204..73ec3ce 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -15,6 +15,8 @@ import { ExecuteCommandRequest, DidChangeWatchedFilesNotification, DidChangeConfigurationNotification, Proposed, ProposedFeatures } from 'vscode-languageserver'; +type LinterValues = 'standard' | 'semistandard' | 'standardx'; +type LinterNameValues = 'JavaScript Standard Style' | 'JavaScript Semi-Standard Style' | 'JavaScript Standard Style with custom tweaks'; import Uri from 'vscode-uri'; import path = require('path'); @@ -103,7 +105,7 @@ interface TextDocumentSettings { validate: boolean; autoFix: boolean; autoFixOnSave: boolean; - semistandard: boolean; + engine: LinterValues; usePackageJson: boolean; options: any | undefined; run: RunValues; @@ -164,8 +166,8 @@ interface StandardModule { lintText(text: string, opts?: CLIOptions, cb?: StandardModuleCallback): void; parseOpts(opts: Object): Opts; } -type SourceValues = 'standard' | 'semistandard'; -function makeDiagnostic(problem: StandardProblem, source: SourceValues): Diagnostic { + +function makeDiagnostic(problem: StandardProblem, source: LinterValues): Diagnostic { let message = (problem.ruleId != null) ? `${problem.message} (${problem.ruleId})` : `${problem.message}`; @@ -312,8 +314,13 @@ function resolveSettings(document: TextDocument): Thenable } resultPromise = connection.workspace.getConfiguration({ scopeUri: uri, section: '' }).then((settings: TextDocumentSettings) => { let uri = Uri.parse(document.uri); - let linter = settings.semistandard ? 'semistandard' : 'standard'; - let linterName = settings.semistandard ? 'JavaScript Semi-Standard Style' : 'JavaScript Standard Style'; + let linterNames: { [linter: string]: LinterNameValues; } = { + 'standard': 'JavaScript Standard Style', + 'semistandard': 'JavaScript Semi-Standard Style', + 'standardx': 'JavaScript Standard Style with custom tweaks' + } + let linter = settings.engine; + let linterName = linterNames[settings.engine]; // when settings.usePackageJson is true // we need to do more let { usePackageJson } = settings @@ -334,12 +341,18 @@ function resolveSettings(document: TextDocument): Thenable } else if (pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { linter = 'semistandard'; linterName = 'JavaScript Semi-Standard Style'; + } else if (pkg && pkg.devDependencies && pkg.devDependencies.standardx) { + linter = 'standardx'; + linterName = 'JavaScript Standard Style with custom tweaks'; } - // if standard or semistandard config presented in package.json - if (pkg && pkg.devDependencies && pkg.devDependencies.standard || pkg && pkg.devDependencies && pkg.devDependencies.semistandard) { + // if standard, semistandard or standardx config presented in package.json + if (pkg && pkg.devDependencies && pkg.devDependencies.standard + || pkg && pkg.devDependencies && pkg.devDependencies.semistandard + || pkg && pkg.devDependencies && pkg.devDependencies.standardx) { if (pkg[linter]) { // if [linter] presented in package.json // combine the global one. + settings.engine = linter; settings.options = Object.assign({}, settings.options, pkg[linter]); } else { // default options to those in settings.json @@ -755,7 +768,7 @@ function validate(document: TextDocument, settings: TextDocumentSettings, publis var deglobOpts = { ignore: opts.ignore, cwd: opts.cwd, - configKey: settings.semistandard ? 'semistandard' : 'standard' + configKey: settings.engine } async.waterfall([ function (callback: any) { @@ -799,7 +812,7 @@ function validate(document: TextDocument, settings: TextDocumentSettings, publis if (docReport.messages && Array.isArray(docReport.messages)) { docReport.messages.forEach((problem) => { if (problem) { - let diagnostic = makeDiagnostic(problem, settings.semistandard ? 'semistandard' : 'standard'); + let diagnostic = makeDiagnostic(problem, settings.engine); diagnostics.push(diagnostic); if (settings.autoFix) { recordCodeAction(document, diagnostic, problem); From 023eed5d2e670e7be8be4303176e3577f08d8cd1 Mon Sep 17 00:00:00 2001 From: Florian Maunier Date: Tue, 28 Aug 2018 17:57:33 +0200 Subject: [PATCH 2/2] fix VSCode tasks --- .vscode/tasks.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index b0d9a4d..95c9b52 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ "version": "2.0.0", "tasks": [ { - "taskName": "watch", + "label": "watch", "dependsOn": [ "npm: watch:client", "npm: watch:server" @@ -16,6 +16,7 @@ "problemMatcher": [] }, { + "label": "npm: watch:client", "type": "npm", "script": "watch:client", "isBackground": true, @@ -28,6 +29,7 @@ ] }, { + "label": "npm: watch:server", "type": "npm", "script": "watch:server", "isBackground": true,