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

Add support for standardx, and potentially more standard-engine based linters #71

Merged
merged 2 commits into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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.

Expand All @@ -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`


Expand Down
25 changes: 12 additions & 13 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -64,7 +63,7 @@ interface TextDocumentSettings {
validate: boolean;
autoFix: boolean;
autoFixOnSave: boolean;
semistandard: boolean;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, but there is no way to alert config changes on extension upgrades?

engine: LinterValues;
usePackageJson: boolean;
options: any | undefined;
run: RunValues;
Expand Down Expand Up @@ -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<LinterValues>('engine', 'standard')];
}
function pickFolder(folders: VWorkspaceFolder[], placeHolder: string): Thenable<VWorkspaceFolder> {
if (folders.length === 1) {
Expand Down Expand Up @@ -178,7 +178,7 @@ function disable() {
});
}

let dummyCommands: [Disposable];
let dummyCommands: Disposable[];

let defaultLanguages = ['javascript', 'javascriptreact'];
function shouldBeValidated(textDocument: TextDocument): boolean {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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([
'',
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 22 additions & 9 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -103,7 +105,7 @@ interface TextDocumentSettings {
validate: boolean;
autoFix: boolean;
autoFixOnSave: boolean;
semistandard: boolean;
engine: LinterValues;
usePackageJson: boolean;
options: any | undefined;
run: RunValues;
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -312,8 +314,13 @@ function resolveSettings(document: TextDocument): Thenable<TextDocumentSettings>
}
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
Expand All @@ -334,12 +341,18 @@ function resolveSettings(document: TextDocument): Thenable<TextDocumentSettings>
} 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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down