Skip to content

Commit 2e4fe43

Browse files
committed
Add test for configurePlugin
1 parent 4273fd7 commit 2e4fe43

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

src/harness/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ namespace ts.server {
684684
return response.body!.map(entry => this.decodeSpan(entry, fileName)); // TODO: GH#18217
685685
}
686686

687+
configurePlugin(pluginName: string, configuration: any): void {
688+
this.processRequest<protocol.ConfigurePluginRequest>("configurePlugin", { pluginName, configuration });
689+
}
690+
687691
getIndentationAtPosition(_fileName: string, _position: number, _options: EditorOptions): number {
688692
return notImplemented();
689693
}

src/harness/fourslash.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,10 @@ Actual: ${stringify(fullActual)}`);
34003400
}
34013401
}
34023402
}
3403+
3404+
public configurePlugin(pluginName: string, configuration: any): void {
3405+
(<ts.server.SessionClient>this.languageService).configurePlugin(pluginName, configuration);
3406+
}
34033407
}
34043408

34053409
function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: ReadonlyArray<ts.TextChange>): ts.TextRange {
@@ -3463,19 +3467,20 @@ Actual: ${stringify(fullActual)}`);
34633467
function runCode(code: string, state: TestState): void {
34643468
// Compile and execute the test
34653469
const wrappedCode =
3466-
`(function(test, goTo, verify, edit, debug, format, cancellation, classification, verifyOperationIsCancelled) {
3470+
`(function(test, goTo, plugins, verify, edit, debug, format, cancellation, classification, verifyOperationIsCancelled) {
34673471
${code}
34683472
})`;
34693473
try {
34703474
const test = new FourSlashInterface.Test(state);
34713475
const goTo = new FourSlashInterface.GoTo(state);
3476+
const plugins = new FourSlashInterface.Plugins(state);
34723477
const verify = new FourSlashInterface.Verify(state);
34733478
const edit = new FourSlashInterface.Edit(state);
34743479
const debug = new FourSlashInterface.Debug(state);
34753480
const format = new FourSlashInterface.Format(state);
34763481
const cancellation = new FourSlashInterface.Cancellation(state);
34773482
const f = eval(wrappedCode);
3478-
f(test, goTo, verify, edit, debug, format, cancellation, FourSlashInterface.Classification, verifyOperationIsCancelled);
3483+
f(test, goTo, plugins, verify, edit, debug, format, cancellation, FourSlashInterface.Classification, verifyOperationIsCancelled);
34793484
}
34803485
catch (err) {
34813486
throw err;
@@ -3975,6 +3980,15 @@ namespace FourSlashInterface {
39753980
}
39763981
}
39773982

3983+
export class Plugins {
3984+
constructor (private state: FourSlash.TestState) {
3985+
}
3986+
3987+
public configurePlugin(pluginName: string, configuration: any): void {
3988+
this.state.configurePlugin(pluginName, configuration);
3989+
}
3990+
}
3991+
39783992
export class GoTo {
39793993
constructor(private state: FourSlash.TestState) {
39803994
}

src/harness/harnessLanguageService.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,36 @@ namespace Harness.LanguageService {
833833
error: undefined
834834
};
835835

836+
// Accepts configurations
837+
case "configurable-diagnostic-adder":
838+
let customMessage = "default message";
839+
return {
840+
module: () => ({
841+
create(info: ts.server.PluginCreateInfo) {
842+
customMessage = info.config.message;
843+
const proxy = makeDefaultProxy(info);
844+
proxy.getSemanticDiagnostics = filename => {
845+
const prev = info.languageService.getSemanticDiagnostics(filename);
846+
const sourceFile: ts.SourceFile = info.project.getSourceFile(ts.toPath(filename, /*basePath*/ undefined, ts.createGetCanonicalFileName(info.serverHost.useCaseSensitiveFileNames)))!;
847+
prev.push({
848+
category: ts.DiagnosticCategory.Error,
849+
file: sourceFile,
850+
code: 9999,
851+
length: 3,
852+
messageText: customMessage,
853+
start: 0
854+
});
855+
return prev;
856+
};
857+
return proxy;
858+
},
859+
onConfigurationChanged(config: any) {
860+
customMessage = config.message;
861+
}
862+
}),
863+
error: undefined
864+
};
865+
836866
default:
837867
return {
838868
module: undefined,

tests/cases/fourslash/fourslash.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ declare namespace FourSlashInterface {
124124
symbolsInScope(range: Range): any[];
125125
setTypesRegistry(map: { [key: string]: void }): void;
126126
}
127+
class plugins {
128+
configurePlugin(pluginName: string, configuration: any): void;
129+
}
127130
class goTo {
128131
marker(name?: string | Marker): void;
129132
eachMarker(markers: ReadonlyArray<string>, action: (marker: Marker, index: number) => void): void;
@@ -651,6 +654,7 @@ declare namespace FourSlashInterface {
651654
}
652655
declare function verifyOperationIsCancelled(f: any): void;
653656
declare var test: FourSlashInterface.test_;
657+
declare var plugins: FourSlashInterface.plugins;
654658
declare var goTo: FourSlashInterface.goTo;
655659
declare var verify: FourSlashInterface.verify;
656660
declare var edit: FourSlashInterface.edit;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
// @Filename: tsconfig.json
4+
//// {
5+
//// "compilerOptions": {
6+
//// "plugins": [
7+
//// { "name": "configurable-diagnostic-adder" , "message": "configured error" }
8+
//// ]
9+
//// },
10+
//// "files": ["a.ts"]
11+
//// }
12+
13+
// @Filename: a.ts
14+
//// let x = [1, 2];
15+
//// /**/
16+
////
17+
18+
// Test that plugin adds an error message which is able to be configured
19+
goTo.marker();
20+
verify.getSemanticDiagnostics([{ message: "configured error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);
21+
plugins.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
22+
verify.getSemanticDiagnostics([{ message: "new error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);

tests/cases/projects/plugins/plugins.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)