generated from un-ts/lib-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoverride.ts
161 lines (133 loc) · 4.18 KB
/
override.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import fs from 'fs'
import type { TsResolverOptions } from 'eslint-import-resolver-typescript'
import { resolve } from 'eslint-import-resolver-typescript'
import type { ClassDeclaration, Collection, File, Transform } from 'jscodeshift'
const defaultPackageFilter = (pkg: Record<string, string>) => {
pkg.main =
// prefer single bundled file
pkg.fesm2015 ||
pkg.module ||
pkg.types ||
pkg.typings ||
pkg['jsnext:main'] ||
pkg.main
return pkg
}
const transform: Transform = (
fileInfo,
{ j, report },
options?:
| (Omit<TsResolverOptions, 'extensions'> & {
extensions?: string
tsResolverExtensions?: string[]
})
| null,
// eslint-disable-next-line sonarjs/cognitive-complexity
) => {
const { ClassDeclaration, ClassProperty, ClassMethod, ImportDeclaration } = j
const root = j(fileInfo.source) as Collection<File>
const imports = root.find(ImportDeclaration)
const classes = root.find(ClassDeclaration)
const subClasses = classes.filter(path => !!path.node.superClass)
for (const path of subClasses.paths()) {
const superClass = path.node.superClass!
if (
!(path.scope as { isGlobal: boolean }).isGlobal ||
superClass.type !== 'Identifier'
) {
report(`unsupported class node: ${String(superClass)}`)
continue
}
let superCls = superClass.name
let superClassNodes = classes
.filter(path => path.node.id!.name === superCls)
.nodes()
if (superClassNodes.length === 0) {
// it could be imported
const matched = imports.nodes().find(node =>
node.specifiers?.some(specifier => {
const matched = specifier.local?.name === superCls
if (matched && 'imported' in specifier) {
superCls = specifier.imported.name
}
return matched
}),
)
if (matched) {
const resolved = resolve(
matched.source.value as string,
fileInfo.path,
{
...options,
extensions: options?.tsResolverExtensions,
packageFilter: options?.packageFilter || defaultPackageFilter,
project:
typeof options?.project === 'string'
? options.project.split(',')
: options?.project,
},
)
if (!resolved.found) {
report(`unable to resolve super class: ${superCls}`)
continue
}
if (!resolved.path) {
report('Node core modules are not supported')
continue
}
const superRoot = j(
fs.readFileSync(resolved.path, 'utf8'),
) as Collection<File>
superClassNodes = superRoot
.find(ClassDeclaration)
.filter(path => path.node.id!.name === superCls)
.nodes()
}
if (superClassNodes.length === 0) {
report(`super class not found: ${superCls}`)
continue
}
}
if (superClassNodes.length > 1) {
report(`ambiguous super class: ${superCls}`)
continue
}
const superClassBody = superClassNodes[0].body.body
const subCollection = j(path) as Collection<ClassDeclaration>
const subPaths = [
...subCollection.find(ClassProperty).paths(),
...subCollection.find(ClassMethod).paths(),
]
for (const subPath of subPaths) {
const { key } = subPath.node
if (key.type !== 'Identifier' || key.name === 'constructor') {
continue
}
const hasSuperKey = superClassBody.some(
node =>
(node.type === 'ClassProperty' || node.type === 'ClassMethod') &&
node.key.type === 'Identifier' &&
node.key.name === key.name,
)
if (
!hasSuperKey ||
'override' in subPath.node ||
key.name.startsWith('override ')
) {
continue
}
// eslint-disable-next-line unicorn/consistent-destructuring
if ('async' in subPath.node && subPath.node.async) {
report(`async method is not supported: ${key.name}`)
continue
}
Object.assign(key, {
name: 'override ' + key.name,
override: true,
})
}
}
return root.toSource()
}
export const parser = 'ts'
export default transform