Skip to content

Commit b79b107

Browse files
committed
add basic code action test
1 parent dacb28d commit b79b107

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

typescript/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export type LanguageServiceMethodWithConfig<T extends keyof ConditionalPick<ts.L
1111
...args: Parameters<ts.LanguageService[T]>
1212
) => ReturnType<ts.LanguageService[T]>
1313

14-
export type PluginCreateArg = Pick<ts.server.PluginCreateInfo, 'languageService' | 'languageServiceHost' | 'config'>
14+
export type PluginCreateArg = Pick<ts.server.PluginCreateInfo, 'languageService' | 'languageServiceHost' | 'config' | 'serverHost'>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { fourslashLikeTester } from './testing'
2+
3+
test('Split Declaration and Initialization', () => {
4+
const { codeAction } = fourslashLikeTester(
5+
/* ts */ `
6+
/*t*/const/*t*/ a = 1
7+
`,
8+
undefined,
9+
{ dedent: true },
10+
)
11+
12+
codeAction(0, {
13+
refactorName: 'Split Declaration and Initialization',
14+
newContent: /* ts */ `
15+
let a: number
16+
a = 1
17+
`,
18+
})
19+
})

typescript/test/completions.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,18 +738,18 @@ test('Object Literal Completions', () => {
738738
[
739739
"a",
740740
"b",
741-
"b: \\"$1\\",$0",
741+
"b: \\"$1\\",",
742742
]
743743
`)
744744
// I guess vitest hangs forever here
745745
expect(pos3.map(x => x.insertText)).toMatchInlineSnapshot(`
746746
[
747747
"bar",
748-
"bar: \${1|true,false|},$0",
748+
"bar: \${1|true,false|},",
749749
"bar2",
750750
"bar2: false,",
751751
"foo",
752-
"foo: \${1|true,false|},$0",
752+
"foo: \${1|true,false|},",
753753
]
754754
`)
755755
expect(pos4.filter(x => x.insertText?.includes(': '))).toEqual([])

typescript/test/testing.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _ from 'lodash'
22
import { getCompletionsAtPosition as getCompletionsAtPositionRaw } from '../src/completionsAtPosition'
33
import { Configuration } from '../src/types'
4+
import codeActionsDecorateProxy from '../src/codeActions/decorateProxy'
5+
import { dedentString } from '../src/utils'
46
import { defaultConfigFunc, entrypoint, sharedLanguageService, settingsOverride, currentTestingContext } from './shared'
57

68
interface CompletionPartMatcher {
@@ -25,6 +27,10 @@ interface CodeActionMatcher {
2527

2628
const { languageService, languageServiceHost, updateProject, getCurrentFile } = sharedLanguageService
2729

30+
const fakeProxy = {} as Pick<typeof languageService, 'getApplicableRefactors' | 'getEditsForRefactor'>
31+
32+
codeActionsDecorateProxy(fakeProxy as typeof languageService, languageService, languageServiceHost, defaultConfigFunc)
33+
2834
export const getCompletionsAtPosition = (pos: number, { fileName = entrypoint, shouldHave }: { fileName?: string; shouldHave?: boolean } = {}) => {
2935
if (pos === undefined) throw new Error('getCompletionsAtPosition: pos is undefined')
3036
const result = getCompletionsAtPositionRaw(
@@ -70,7 +76,8 @@ export const overrideSettings = (newOverrides: Partial<Configuration>) => {
7076
})
7177
}
7278

73-
export const fourslashLikeTester = (contents: string, fileName = entrypoint) => {
79+
export const fourslashLikeTester = (contents: string, fileName = entrypoint, { dedent = false }: { dedent? } = {}) => {
80+
if (dedent) contents = dedentString(contents)
7481
const [positive, _negative, numberedPositions] = fileContentsSpecialPositions(contents, fileName)
7582

7683
const ranges = positive.reduce<number[][]>(
@@ -141,17 +148,25 @@ export const fourslashLikeTester = (contents: string, fileName = entrypoint) =>
141148
if (!ranges[mark]) throw new Error(`No range with index ${mark} found, highest index is ${ranges.length - 1}`)
142149
const start = ranges[mark]![0]!
143150
const end = ranges[mark]![0]!
144-
const appliableRefactors = languageService.getApplicableRefactors(fileName, { pos: start, end }, {}, 'invoked')
145-
const appliableNames = appliableRefactors.map(appliableRefactor => appliableRefactor.actions.map(action => action.description))
151+
const appliableRefactors = fakeProxy.getApplicableRefactors(fileName, { pos: start, end }, {}, 'invoked')
152+
const appliableNames = appliableRefactors.flatMap(appliableRefactor => appliableRefactor.actions.map(action => action.description))
146153
const { refactorName, newContent } = matcher
147154
if (newContent) {
148155
expect(appliableNames, `at marker ${mark}`).toContain(refactorName)
149156
const actionsGroup = appliableRefactors.find(appliableRefactor =>
150157
appliableRefactor.actions.find(action => action.description === refactorName),
151158
)!
152159
const action = actionsGroup.actions.find(action => action.description === refactorName)!
153-
const { edits } = languageService.getEditsForRefactor(fileName, {}, { pos: start, end }, actionsGroup.name, action.name, {})!
154-
const a = tsFull.textChanges.applyChanges(getCurrentFile(), edits[0]!.textChanges)
160+
const { edits } = fakeProxy.getEditsForRefactor(
161+
fileName,
162+
ts.getDefaultFormatCodeSettings(),
163+
{ pos: start, end },
164+
actionsGroup.name,
165+
action.name,
166+
{},
167+
)!
168+
const newContentsActual = tsFull.textChanges.applyChanges(getCurrentFile(), edits[0]!.textChanges)
169+
expect(dedentString(newContent), `at marker ${mark}`).toEqual(newContentsActual)
155170
} else {
156171
expect(appliableNames, `at marker ${mark}`).not.toContain(refactorName)
157172
}

0 commit comments

Comments
 (0)