-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-css-result.ts
34 lines (31 loc) · 1.45 KB
/
rename-css-result.ts
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
import { ASTPath, ImportSpecifier } from "jscodeshift";
import { DefaultOptions } from "./index";
export function renameCssResult({ root, j }: DefaultOptions) {
// Rename CssResult to CssResultGroup
// e. g.: 'public static styles: CssResult = css``' -> 'public static styles: CssResultGroup = css``';
// Step 1: Rename CSSResult to CSSResultGroup when used as an import specifier
root
.find(j.ImportDeclaration)
.filter(path => (
(path.value.source.type === 'Literal' || path.value.source.type === 'StringLiteral') &&
(path.value.source.value === 'lit-element' || path.value.source.value === 'lit-html'))
).find(j.ImportSpecifier)
.filter((path: ASTPath<ImportSpecifier>) => {
const importSpecifierStr: string = path.value?.imported?.name
return importSpecifierStr === 'CSSResult';
}).replaceWith(nodePath => {
const { node } = nodePath;
const newImportSpecifier = 'CSSResultGroup';
node.imported.name = newImportSpecifier;
return node;
});
// Step 2: Rename CSSResult to CSSResultGroup when used as a typescript type annotation
// @ts-ignore
root.find(j.TSType).filter(type => type?.value?.typeName?.name === 'CSSResult')
.replaceWith(nodePath => {
const { node } = nodePath;
// @ts-ignore
node.typeName.name = 'CSSResultGroup';
return node;
})
}