-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrefactor.ts
More file actions
53 lines (49 loc) · 1.8 KB
/
refactor.ts
File metadata and controls
53 lines (49 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { getCssModuleFileName, isComponentFileName } from '@css-modules-kit/core';
import type ts from 'typescript';
export const createCssModuleFileRefactor = {
name: 'Create CSS Module file',
description: 'Create CSS Module file',
actions: [{ name: 'Create CSS Module file', description: 'Create CSS Module file for current file' }],
} as const satisfies ts.ApplicableRefactorInfo;
export function getApplicableRefactors(
languageService: ts.LanguageService,
project: ts.server.Project,
): ts.LanguageService['getApplicableRefactors'] {
return (...args) => {
const [fileName] = args;
const prior = languageService.getApplicableRefactors(...args);
if (isComponentFileName(fileName)) {
// If the CSS Module file does not exist, provide a refactor to create it.
if (!project.fileExists(getCssModuleFileName(fileName))) {
prior.push(createCssModuleFileRefactor);
}
}
return prior;
};
}
export function getEditsForRefactor(languageService: ts.LanguageService): ts.LanguageService['getEditsForRefactor'] {
// eslint-disable-next-line max-params
return (fileName, formatOptions, positionOrRange, refactorName, actionName, preferences) => {
const prior = languageService.getEditsForRefactor(
fileName,
formatOptions,
positionOrRange,
refactorName,
actionName,
preferences,
) ?? { edits: [] };
if (isComponentFileName(fileName)) {
if (refactorName === createCssModuleFileRefactor.name) {
prior.edits.push(createNewCssModuleFileChange(getCssModuleFileName(fileName)));
}
}
return prior;
};
}
function createNewCssModuleFileChange(cssFilename: string): ts.FileTextChanges {
return {
fileName: cssFilename,
textChanges: [{ span: { start: 0, length: 0 }, newText: '' }],
isNewFile: true,
};
}