-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathscope.test.ts
More file actions
177 lines (161 loc) · 5.51 KB
/
scope.test.ts
File metadata and controls
177 lines (161 loc) · 5.51 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import path from 'node:path'
import type { Identifier, MemberExpression, Node } from 'estree'
import { parseAstAsync } from 'vite'
import { describe, expect, it } from 'vitest'
import { type Scope, type ScopeTree, buildScopeTree } from './scope'
// TODO:
// - use single markdown as snaphsot? (cf. review-scope-fixtures.ts)
describe('fixtures', () => {
const fixtures = import.meta.glob('./fixtures/scope/**/*.js', {
query: 'raw',
})
for (const [file, mod] of Object.entries(fixtures)) {
it(path.basename(file), async () => {
const input = ((await mod()) as any).default as string
const ast = await parseAstAsync(input)
const scopeTree = buildScopeTree(ast)
const showReferenceNode = file.includes('/scope/reference-node/')
const serialized = serializeScopeTree(scopeTree, { showReferenceNode })
await expect(
JSON.stringify(serialized, null, 2) + '\n',
).toMatchFileSnapshot(file + '.snap.json')
})
}
})
// ── Serializer ────────────────────────────────────────────────────────────────
type SerializedScope = {
type: string
declarations: string[]
references: SerializedReference[]
children: SerializedScope[]
}
type SerializedReference = {
name: string
declaredAt: string | null
referenceNode?: string
}
function serializeScopeTree(
scopeTree: ScopeTree,
options?: {
showReferenceNode?: boolean
},
): SerializedScope {
const {
nodeScope,
referenceToDeclaredScope,
referenceToNode,
scopeToReferences,
moduleScope,
} = scopeTree
// Build scope → label and scope → direct children.
// Labels are disambiguated per parent (e.g. BlockStatement[2] for the second sibling).
const scopeLabelMap = new Map<Scope, string>()
const scopeChildrenMap = new DefaultMap<Scope, Scope[]>(() => [])
const siblingCount = new DefaultMap<Scope, DefaultMap<string, number>>(
() => new DefaultMap(() => 0),
)
for (const [node, scope] of nodeScope.entries()) {
const base = toScopeNodeLabel(node)
scopeLabelMap.set(scope, base)
scopeChildrenMap.set(scope, [])
if (!scope.parent) {
continue
}
const parent = scope.parent
const counts = siblingCount.get(parent)
const n = counts.get(base) + 1
counts.set(base, n)
scopeLabelMap.set(scope, n === 1 ? base : `${base}[${n}]`)
scopeChildrenMap.get(parent).push(scope)
}
// Direct references for a scope = all propagated refs minus those in child scopes.
function getDirectReferences(scope: Scope) {
const allRefs = scopeToReferences.get(scope) ?? []
const childRefSet = new Set(
scopeChildrenMap
.get(scope)
.flatMap((c) => scopeToReferences.get(c) ?? []),
)
return allRefs.filter((id) => !childRefSet.has(id))
}
function serializeDeclaredPath(id: Identifier): string | null {
const declScope = referenceToDeclaredScope.get(id)
if (!declScope) {
return null
}
const scopes = [declScope, ...declScope.getAncestorScopes()].reverse()
return scopes.map((s) => scopeLabelMap.get(s)!).join(' > ')
}
function serializeReferenceNode(node: Identifier | MemberExpression): string {
if (node.type === 'Identifier') {
return node.name
}
// <unknown>/computed/optional shouldn't show up
// since they aren't collected as reference node yet.
const object =
node.object.type === 'Identifier' ||
node.object.type === 'MemberExpression'
? serializeReferenceNode(node.object)
: '<unknown>'
const property =
node.property.type === 'Identifier' ? node.property.name : '<unknown>'
const suffix = node.computed
? `${node.optional ? '?.' : ''}[${property}]`
: `${node.optional ? '?.' : '.'}${property}`
return object + suffix
}
function serializeScope(scope: Scope): SerializedScope {
return {
type: scopeLabelMap.get(scope)!,
declarations: [...scope.declarations].sort(),
references: getDirectReferences(scope).map((id) => {
const reference: SerializedReference = {
name: id.name,
declaredAt: serializeDeclaredPath(id),
}
if (options?.showReferenceNode) {
reference.referenceNode = serializeReferenceNode(
referenceToNode.get(id)!,
)
}
return reference
}),
children: scopeChildrenMap
.get(scope)!
.map((child) => serializeScope(child)),
}
}
return serializeScope(moduleScope)
}
function toScopeNodeLabel(node: Node): string {
switch (node.type) {
case 'FunctionDeclaration':
// ESTree typing says `id` is present here, but the parser can still produce
// `export default function () {}` as a FunctionDeclaration with `id: null`.
return node.type + (node.id ? `:${node.id.name}` : '')
case 'FunctionExpression':
return node.type + (node.id ? `:${node.id.name}` : '')
case 'ClassDeclaration':
return node.type + (node.id ? `:${node.id.name}` : '')
case 'ClassExpression':
return node.type + (node.id ? `:${node.id.name}` : '')
case 'ArrowFunctionExpression':
return 'ArrowFunction'
default:
return node.type
}
}
class DefaultMap<K, V> extends Map<K, V> {
constructor(private readonly init: (key: K) => V) {
super()
}
override get(key: K): V {
let value = super.get(key)
if (value !== undefined) {
return value
}
value = this.init(key)
this.set(key, value)
return value
}
}