Skip to content

Commit

Permalink
fix: remove deprecated API
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Extensions that still use these APIs can not be tested anymore with this version of the stubs.
  • Loading branch information
felixfbecker committed May 1, 2020
1 parent b671c17 commit db9cd63
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
38 changes: 4 additions & 34 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@ import { BehaviorSubject, Subject, Subscription } from 'rxjs'
import { mapTo } from 'rxjs/operators'
import * as sinon from 'sinon'
import * as sourcegraph from 'sourcegraph'
import { deprecate } from 'util'
import { assertTypeIsCompatible, notImplemented } from './util'

interface DeprecatedTypeDefinitionProvider {
provideTypeDefinition(
document: sourcegraph.TextDocument,
position: Position
): sourcegraph.ProviderResult<sourcegraph.Definition>
}

interface DeprecatedImplementationProvider {
provideImplementation(
document: sourcegraph.TextDocument,
position: Position
): sourcegraph.ProviderResult<sourcegraph.Definition>
}
import { notImplemented, subTypeOf } from './util'

let decorationTypeCounter = 0

Expand All @@ -30,7 +15,7 @@ export const createStubSourcegraphAPI = () => {
const configSubject = new BehaviorSubject<any>({})
const rootChanges = new Subject<void>()
const openedTextDocuments = new Subject<sourcegraph.TextDocument>()
const stubs /* : typeof import('sourcegraph') */ = {
const stubs = subTypeOf<typeof import('sourcegraph')>()({
// Classes
URI: URL,
Position,
Expand Down Expand Up @@ -75,20 +60,6 @@ export const createStubSourcegraphAPI = () => {
(selector: sourcegraph.DocumentSelector, provider: sourcegraph.CompletionItemProvider) =>
new Subscription()
),
registerTypeDefinitionProvider: sinon.spy(
deprecate(
(selector: sourcegraph.DocumentSelector, provider: DeprecatedTypeDefinitionProvider) =>
new Subscription(),
'sourcegraph.languages.registerTypeDefinitionProvider() is deprecated. Use sourcegraph.languages.registerLocationProvider() instead.'
)
),
registerImplementationProvider: sinon.spy(
deprecate(
(selector: sourcegraph.DocumentSelector, provider: DeprecatedImplementationProvider) =>
new Subscription(),
'sourcegraph.languages.registerImplementationProvider() is deprecated. Use sourcegraph.languages.registerLocationProvider() instead.'
)
),
},
app: {
windows: [] as sourcegraph.Window[],
Expand All @@ -98,7 +69,7 @@ export const createStubSourcegraphAPI = () => {
activeWindowChanges: new BehaviorSubject<sourcegraph.Window | undefined>(undefined),

createDecorationType: () => ({ key: 'decorationType' + decorationTypeCounter++ }),
createPanelView: notImplemented as ((id: string) => sourcegraph.PanelView),
createPanelView: notImplemented as (id: string) => sourcegraph.PanelView,
},
configuration: Object.assign(configSubject.pipe(mapTo(undefined)), {
get: <C extends object = { [key: string]: any }>(): sourcegraph.Configuration<C> => ({
Expand All @@ -123,7 +94,6 @@ export const createStubSourcegraphAPI = () => {
registerCommand: sinon.spy((command: string, callback: (...args: any[]) => any) => new Subscription()),
executeCommand: sinon.spy<(command: string, ...args: any[]) => Promise<any>>(notImplemented),
},
}
assertTypeIsCompatible<typeof sourcegraph>(stubs)
})
return stubs
}
7 changes: 3 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Subscription } from 'rxjs'
import * as sinon from 'sinon'
import { ExtensionContext } from 'sourcegraph'
import { assertTypeIsCompatible } from './util'
import { subTypeOf } from './util'

export const createStubExtensionContext = () => {
const subscriptions = sinon.stub(new Subscription())
subscriptions.add.callThrough()
subscriptions.remove.callThrough()
subscriptions.unsubscribe.callThrough()
const context = {
const context = subTypeOf<ExtensionContext>()({
subscriptions,
}
assertTypeIsCompatible<ExtensionContext>(context)
})
return context
}
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* Compile-time helper to assert a given type.
* Identity-function helper to ensure a value `T` is a subtype of `U`.
*
* @template U The type to check for (explicitly specify this)
* @template T The actual type (inferred, don't specify this)
*/
export const assertTypeIsCompatible = <T>(val: T): void => undefined
export const subTypeOf = <U>() => <T extends U>(value: T): T => value

export function notImplemented(): never {
throw new Error('Stub functionality not implemented')
Expand Down

0 comments on commit db9cd63

Please sign in to comment.