From db914d873917b25c91f29f7faf7db668b97dd44f Mon Sep 17 00:00:00 2001 From: Mine Starks Date: Fri, 26 Oct 2018 17:50:10 -0700 Subject: [PATCH] Add test for configurePlugin --- src/harness/client.ts | 4 +++ src/harness/fourslash.ts | 18 +++++++++-- src/harness/harnessLanguageService.ts | 30 +++++++++++++++++++ tests/cases/fourslash/fourslash.ts | 4 +++ .../cases/fourslash/server/configurePlugin.ts | 22 ++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/server/configurePlugin.ts diff --git a/src/harness/client.ts b/src/harness/client.ts index d873cd9b4fe..a5f80eb784d 100644 --- a/src/harness/client.ts +++ b/src/harness/client.ts @@ -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("configurePlugin", { pluginName, configuration }); + } + getIndentationAtPosition(_fileName: string, _position: number, _options: EditorOptions): number { return notImplemented(); } diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index c06e95339f1..df5ffbefc81 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -3400,6 +3400,10 @@ Actual: ${stringify(fullActual)}`); } } } + + public configurePlugin(pluginName: string, configuration: any): void { + (this.languageService).configurePlugin(pluginName, configuration); + } } function updateTextRangeForTextChanges({ pos, end }: ts.TextRange, textChanges: ReadonlyArray): ts.TextRange { @@ -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; @@ -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) { } diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index e90f29446c3..ee35ab360ca 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -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, diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index e254b2b51db..b365ad4e5fa 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -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, action: (marker: Marker, index: number) => void): void; @@ -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; diff --git a/tests/cases/fourslash/server/configurePlugin.ts b/tests/cases/fourslash/server/configurePlugin.ts new file mode 100644 index 00000000000..90745f93146 --- /dev/null +++ b/tests/cases/fourslash/server/configurePlugin.ts @@ -0,0 +1,22 @@ +/// + +// @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" } }]);