Skip to content

Commit

Permalink
Add test for configurePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
minestarks committed Oct 27, 2018
1 parent 4273fd7 commit 2e4fe43
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ namespace ts.server {
return response.body!.map(entry => this.decodeSpan(entry, fileName)); // TODO: GH#18217
}

configurePlugin(pluginName: string, configuration: any): void {
this.processRequest<protocol.ConfigurePluginRequest>("configurePlugin", { pluginName, configuration });
}

getIndentationAtPosition(_fileName: string, _position: number, _options: EditorOptions): number {
return notImplemented();
}
Expand Down
18 changes: 16 additions & 2 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3400,6 +3400,10 @@ Actual: ${stringify(fullActual)}`);
}
}
}

public configurePlugin(pluginName: string, configuration: any): void {
(<ts.server.SessionClient>this.languageService).configurePlugin(pluginName, configuration);
}
}

function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: ReadonlyArray<ts.TextChange>): ts.TextRange {
Expand Down Expand Up @@ -3463,19 +3467,20 @@ Actual: ${stringify(fullActual)}`);
function runCode(code: string, state: TestState): void {
// Compile and execute the test
const wrappedCode =
`(function(test, goTo, verify, edit, debug, format, cancellation, classification, verifyOperationIsCancelled) {
`(function(test, goTo, plugins, verify, edit, debug, format, cancellation, classification, verifyOperationIsCancelled) {
${code}
})`;
try {
const test = new FourSlashInterface.Test(state);
const goTo = new FourSlashInterface.GoTo(state);
const plugins = new FourSlashInterface.Plugins(state);
const verify = new FourSlashInterface.Verify(state);
const edit = new FourSlashInterface.Edit(state);
const debug = new FourSlashInterface.Debug(state);
const format = new FourSlashInterface.Format(state);
const cancellation = new FourSlashInterface.Cancellation(state);
const f = eval(wrappedCode);
f(test, goTo, verify, edit, debug, format, cancellation, FourSlashInterface.Classification, verifyOperationIsCancelled);
f(test, goTo, plugins, verify, edit, debug, format, cancellation, FourSlashInterface.Classification, verifyOperationIsCancelled);
}
catch (err) {
throw err;
Expand Down Expand Up @@ -3975,6 +3980,15 @@ namespace FourSlashInterface {
}
}

export class Plugins {
constructor (private state: FourSlash.TestState) {
}

public configurePlugin(pluginName: string, configuration: any): void {
this.state.configurePlugin(pluginName, configuration);
}
}

export class GoTo {
constructor(private state: FourSlash.TestState) {
}
Expand Down
30 changes: 30 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,36 @@ namespace Harness.LanguageService {
error: undefined
};

// Accepts configurations
case "configurable-diagnostic-adder":
let customMessage = "default message";
return {
module: () => ({
create(info: ts.server.PluginCreateInfo) {
customMessage = info.config.message;
const proxy = makeDefaultProxy(info);
proxy.getSemanticDiagnostics = filename => {
const prev = info.languageService.getSemanticDiagnostics(filename);
const sourceFile: ts.SourceFile = info.project.getSourceFile(ts.toPath(filename, /*basePath*/ undefined, ts.createGetCanonicalFileName(info.serverHost.useCaseSensitiveFileNames)))!;
prev.push({
category: ts.DiagnosticCategory.Error,
file: sourceFile,
code: 9999,
length: 3,
messageText: customMessage,
start: 0
});
return prev;
};
return proxy;
},
onConfigurationChanged(config: any) {
customMessage = config.message;
}
}),
error: undefined
};

default:
return {
module: undefined,
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ declare namespace FourSlashInterface {
symbolsInScope(range: Range): any[];
setTypesRegistry(map: { [key: string]: void }): void;
}
class plugins {
configurePlugin(pluginName: string, configuration: any): void;
}
class goTo {
marker(name?: string | Marker): void;
eachMarker(markers: ReadonlyArray<string>, action: (marker: Marker, index: number) => void): void;
Expand Down Expand Up @@ -651,6 +654,7 @@ declare namespace FourSlashInterface {
}
declare function verifyOperationIsCancelled(f: any): void;
declare var test: FourSlashInterface.test_;
declare var plugins: FourSlashInterface.plugins;
declare var goTo: FourSlashInterface.goTo;
declare var verify: FourSlashInterface.verify;
declare var edit: FourSlashInterface.edit;
Expand Down
22 changes: 22 additions & 0 deletions tests/cases/fourslash/server/configurePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path="../fourslash.ts"/>

// @Filename: tsconfig.json
//// {
//// "compilerOptions": {
//// "plugins": [
//// { "name": "configurable-diagnostic-adder" , "message": "configured error" }
//// ]
//// },
//// "files": ["a.ts"]
//// }

// @Filename: a.ts
//// let x = [1, 2];
//// /**/
////

// Test that plugin adds an error message which is able to be configured
goTo.marker();
verify.getSemanticDiagnostics([{ message: "configured error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);
plugins.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
verify.getSemanticDiagnostics([{ message: "new error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);
Empty file.

0 comments on commit 2e4fe43

Please sign in to comment.