peachsim 0.1.1667 → 0.1.1674
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cli/bridge-flow-runner.ts +4 -1
- package/cli/commands/coverage-swift.ts +291 -0
- package/cli/commands/coverage.ts +168 -24
- package/cli/commands/diagnose.ts +1 -19
- package/cli/commands/inspect/settling.ts +37 -4
- package/cli/commands/react.ts +3 -18
- package/cli/commands/screenshot.ts +1 -1
- package/cli/commands/screenshots-capture.ts +1 -1
- package/cli/drivers/playwright-provisioning.ts +1 -4
- package/cli/drivers/playwright.ts +15 -1
- package/cli/scene-video.ts +1 -1
- package/cli/ui.ts +20 -0
- package/cli/ws-bridge.ts +7 -36
- package/detox/frame-identity.cjs +21 -0
- package/detox/frame-identity.d.cts +6 -0
- package/detox/index.ts +39 -33
- package/detox/jest-environment.cjs +4 -1
- package/detox/jest-setup-after-env.cjs +0 -4
- package/detox/navigation-lifecycle.ts +2 -2
- package/detox/proof-observer.ts +126 -0
- package/dist-lib/agent-daemon-client.cjs +29 -1
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +10 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +1 -1
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +1 -1
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +1 -1
- package/dist-lib/bundle-channel.cjs +557 -158
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +1 -1
- package/dist-lib/cloud-contract.cjs +1 -1
- package/dist-lib/cloud-contract.mjs +1 -1
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +1 -1
- package/dist-lib/cloud.cjs +1 -1
- package/dist-lib/cloud.mjs +1 -1
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +1 -1
- package/dist-lib/detox/index.cjs +5098 -7344
- package/dist-lib/detox/jest-environment.cjs +4 -1
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/expo-swift-bundle.cjs +602 -152
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +1 -1
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +688 -194
- package/dist-lib/host/fetch-proxy-handler.cjs +6 -3
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +247 -11
- package/dist-lib/jump-to-source-babel.cjs +1 -1
- package/dist-lib/jump-to-source-native.cjs +1 -1
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +1 -1
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +1 -1
- package/dist-lib/metro-production-bundle.cjs +10 -1
- package/dist-lib/metro-production-bundle.mjs +12 -1
- package/dist-lib/metro.cjs +155 -5
- package/dist-lib/native-app-identity-project.cjs +43 -5
- package/dist-lib/native-app-identity-project.mjs +43 -5
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +1 -1
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +1 -1
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +1 -1
- package/dist-lib/profiles.cjs +21 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +73 -27
- package/dist-lib/public-origins-lib-static.json +1 -1
- package/dist-lib/public-origins-lib.json +563 -183
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +1 -1
- package/dist-lib/reap-browser.cjs +964 -0
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +38 -5
- package/dist-lib/sdk.mjs +38 -5
- package/dist-lib/skills.cjs +1031 -530
- package/dist-lib/swift-compile.cjs +298 -86
- package/dist-lib/swift-compile.mjs +298 -86
- package/dist-lib/swift-package.cjs +53 -4
- package/dist-lib/swift-package.mjs +52 -4
- package/dist-lib/swift-submit.cjs +2 -2
- package/dist-lib/swift-submit.mjs +2 -2
- package/dist-lib/swift-toolchain.cjs +25 -44
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vite-swift.cjs +603 -114
- package/dist-lib/vite.cjs +65 -3
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +1 -1
- package/dist-types/packages/peach-engine/src/engine/clipboardContract.d.ts +7 -0
- package/dist-types/packages/peach-engine/src/engine/glass-material.d.ts +1 -1
- package/dist-types/packages/peach-engine/src/engine/image-loader.d.ts +2 -0
- package/dist-types/packages/peach-engine/src/engine/inspect-outline.d.ts +2 -1
- package/dist-types/packages/peach-engine/src/engine/liquid-glass.d.ts +1 -0
- package/dist-types/packages/peach-engine/src/engine/paint-boundary.d.ts +4 -1
- package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +7 -0
- package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime-kind.d.ts +24 -0
- package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +4 -2
- package/dist-types/packages/peach-engine/src/rolldown-bundle-inventory.d.ts +1 -1
- package/dist-types/packages/peach-engine/src/settings/devices.d.ts +1 -0
- package/dist-types/packages/peach-globals/src/clipboardBridgeContract.d.ts +18 -0
- package/dist-types/packages/peach-globals/src/compositorBridgeContract.d.ts +10 -1
- package/dist-types/packages/peach-globals/src/index.d.ts +2 -2
- package/dist-types/packages/peach-globals/src/nodeContract.d.ts +1 -0
- package/dist-types/packages/peach-kit/src/capabilities.d.ts +1 -0
- package/package.json +1 -1
- package/runtime-companion.json +1 -1
- package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFString.h +2 -0
- package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CoreFoundation.h +54 -0
- package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h +23 -4
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCDynamicStore.h +439 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCDynamicStoreCopySpecific.h +216 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCDynamicStoreKey.h +267 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCNetwork.h +204 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCNetworkConfiguration.h +1393 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCNetworkConnection.h +568 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCNetworkReachability.h +369 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCPreferences.h +473 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCPreferencesPath.h +179 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCPreferencesSetSpecific.h +95 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SCSchemaDefinitions.h +2395 -0
- package/sdk/ios/System/Library/Frameworks/SystemConfiguration.framework/Headers/SystemConfiguration.h +184 -0
- package/sdk/ios/usr/include/_ctype.h +388 -0
- package/sdk/ios/usr/include/_inttypes.h +296 -0
- package/sdk/ios/usr/include/_locale.h +83 -0
- package/sdk/ios/usr/include/_locale_posix2008.h +65 -0
- package/sdk/ios/usr/include/_time.h +207 -0
- package/sdk/ios/usr/include/ctype.h +70 -0
- package/sdk/ios/usr/include/dispatch/dispatch.h +69 -6
- package/sdk/ios/usr/include/inttypes.h +35 -0
- package/sdk/ios/usr/include/libkern/OSByteOrder.h +315 -0
- package/sdk/ios/usr/include/libkern/machine/OSByteOrder.h +147 -0
- package/sdk/ios/usr/include/locale.h +59 -0
- package/sdk/ios/usr/include/runetype.h +118 -0
- package/sdk/ios/usr/include/setjmp.h +99 -0
- package/sdk/ios/usr/include/sys/_types/_clock_t.h +32 -0
- package/sdk/ios/usr/include/sys/_types/_ct_rune_t.h +33 -0
- package/sdk/ios/usr/include/sys/_types/_rune_t.h +32 -0
- package/sdk/ios/usr/include/sys/_types/_time_t.h +32 -0
- package/sdk/ios/usr/include/sys/_types/_wint_t.h +33 -0
- package/sdk/ios/usr/include/sys/cdefs.h +2 -0
- package/sdk/ios/usr/include/time.h +67 -2
- package/src/bridge-contract.ts +42 -0
- package/src/chromium-admission.ts +196 -0
- package/src/host/bridge-host.ts +6 -43
- package/src/host/rolldown-bundle-forward.ts +4 -1
- package/src/launch-deadline.ts +74 -0
- package/src/nitro-adapter.lock.json +3 -3
- package/src/profiles.ts +21 -0
- package/src/react-native-coverage/capability-reference-resolution.ts +402 -0
- package/src/react-native-coverage/capability-source-references.ts +1296 -0
- package/src/react-native-coverage/native-typescript-parser.ts +74 -0
- package/src/react-native-coverage/project-aliases.ts +207 -0
- package/src/react-native-coverage/verified-reach.ts +47 -0
- package/src/reap-browser.ts +1019 -0
- package/src/skills/builtin/a11y-review.ts +1 -1
- package/src/skills/builtin/perf-profile.ts +1 -1
- package/src/skills/builtin/screenshot-all.ts +1 -1
- package/src/skills/builtin/visual-diff.ts +1 -1
- package/src/swift-compile.ts +109 -57
- package/src/swift-coverage/indexdump.ts +80 -0
- package/src/swift-coverage/indexing.ts +147 -0
- package/src/swift-coverage/package.ts +230 -0
- package/src/swift-coverage/provenance.ts +47 -0
- package/src/swift-coverage/reader.ts +81 -0
- package/src/swift-coverage/source.ts +252 -0
- package/src/swift-coverage/types.ts +58 -0
- package/src/swift-coverage/xcode-assets.ts +175 -0
- package/src/swift-coverage/xcode-bridging.ts +160 -0
- package/src/swift-coverage/xcode-packages.ts +251 -0
- package/src/swift-coverage/xcode-strings.ts +78 -0
- package/src/swift-coverage/xcode.ts +656 -0
- package/src/swift-distribution.lock.json +8 -8
- package/src/swift-host.ts +21 -1
- package/src/swift-objc-compile.ts +7 -3
- package/src/swift-objc-import.ts +191 -27
- package/src/swift-objc-metadata.ts +2 -2
- package/src/swift-objc-standin.ts +9 -2
- package/src/swift-package-manifest.ts +134 -13
- package/src/swift-package.ts +69 -4
- package/src/swift-standins.ts +4 -1
- package/src/swift-submit.ts +2 -1
- package/src/swift-toolchain.ts +36 -61
- package/src/worker-system-chunks.ts +3 -10
- package/src/xcode-project.ts +5 -5
- package/detox/attempt-events.cjs +0 -914
- package/detox/attempt-events.d.cts +0 -113
- package/detox/motion-change.cjs +0 -524
- package/detox/motion-change.d.cts +0 -140
- package/detox/motion-leg-starts.cjs +0 -20
- package/detox/motion-leg-starts.d.cts +0 -8
- package/detox/proof-frame.cjs +0 -190
- package/detox/record-block-execution.cjs +0 -78
|
@@ -0,0 +1,1296 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import * as ts from 'typescript/ast'
|
|
3
|
+
import type { TypeScriptSourceParser } from './native-typescript-parser'
|
|
4
|
+
import type { Symbol as NativeSymbol } from 'typescript/async'
|
|
5
|
+
|
|
6
|
+
export type CapabilityReferenceKind =
|
|
7
|
+
| 'export'
|
|
8
|
+
| 'member'
|
|
9
|
+
| 'prop'
|
|
10
|
+
| 'style'
|
|
11
|
+
| 'spread'
|
|
12
|
+
| 'side-effect'
|
|
13
|
+
|
|
14
|
+
export type StaticValue = string | number | boolean
|
|
15
|
+
|
|
16
|
+
export type CapabilityReferenceCandidate = {
|
|
17
|
+
id: string
|
|
18
|
+
corpus: string
|
|
19
|
+
path: string
|
|
20
|
+
start: number
|
|
21
|
+
end: number
|
|
22
|
+
originStart: number | null
|
|
23
|
+
packageName: string
|
|
24
|
+
module: string
|
|
25
|
+
kind: CapabilityReferenceKind
|
|
26
|
+
exportName: string
|
|
27
|
+
memberName: string | null
|
|
28
|
+
/** the literal a prop or style entry is set to; null when absent or computed */
|
|
29
|
+
value?: StaticValue | null
|
|
30
|
+
exclusion: string | null
|
|
31
|
+
importForm:
|
|
32
|
+
| 'named'
|
|
33
|
+
| 'default'
|
|
34
|
+
| 'namespace'
|
|
35
|
+
| 'require'
|
|
36
|
+
| 'side-effect'
|
|
37
|
+
| 're-export'
|
|
38
|
+
| null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type CapabilityReferenceResolution = CapabilityReferenceCandidate &
|
|
42
|
+
(
|
|
43
|
+
| { state: 'attributed'; capabilityId: string }
|
|
44
|
+
| { state: 'unmatched'; reason: string }
|
|
45
|
+
| { state: 'excluded'; reason: string }
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
type SourceBinding = {
|
|
49
|
+
packageName: string
|
|
50
|
+
module: string
|
|
51
|
+
exportName: string
|
|
52
|
+
typeOnly: boolean
|
|
53
|
+
memberPath?: readonly string[]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// collect package-bound occurrences before looking at the capability inventory.
|
|
57
|
+
// a nested root is a public export whose static member chain is one reference
|
|
58
|
+
// (`One.iOS.List` is member `iOS.List` of `One`); other exports keep one level.
|
|
59
|
+
export async function collectCapabilityReferences(
|
|
60
|
+
parse: TypeScriptSourceParser,
|
|
61
|
+
content: string,
|
|
62
|
+
path: string,
|
|
63
|
+
packageNames: ReadonlySet<string> | null,
|
|
64
|
+
corpus = 'fixture',
|
|
65
|
+
nestedRoots: readonly { exportName: string | null; module: string }[] = [],
|
|
66
|
+
): Promise<CapabilityReferenceCandidate[]> {
|
|
67
|
+
// an escaped module name needs decoding. otherwise a package-bound literal
|
|
68
|
+
// must contain its package name before opening a native compiler program.
|
|
69
|
+
if (
|
|
70
|
+
packageNames &&
|
|
71
|
+
!content.includes('\\') &&
|
|
72
|
+
![...packageNames].some((name) => content.includes(name))
|
|
73
|
+
) {
|
|
74
|
+
return []
|
|
75
|
+
}
|
|
76
|
+
return parse(new Map([[path, content]]), async (sources, projects) => {
|
|
77
|
+
const source = sources.get(path)
|
|
78
|
+
const project = projects.get(path)
|
|
79
|
+
if (!source || !project)
|
|
80
|
+
throw new Error(`native TypeScript parser did not return ${path}`)
|
|
81
|
+
const identifiers: ts.Identifier[] = []
|
|
82
|
+
const collectIdentifiers = (node: ts.Node): void => {
|
|
83
|
+
if (ts.isIdentifier(node)) identifiers.push(node)
|
|
84
|
+
node.forEachChild(collectIdentifiers)
|
|
85
|
+
}
|
|
86
|
+
collectIdentifiers(source)
|
|
87
|
+
const resolvedSymbols = await project.checker.getSymbolAtLocation(identifiers)
|
|
88
|
+
const symbols = new Map<ts.Node, NativeSymbol | undefined>(
|
|
89
|
+
identifiers.map((identifier, index) => [identifier, resolvedSymbols[index]]),
|
|
90
|
+
)
|
|
91
|
+
// export specifiers and object shorthands have their own symbols; safety
|
|
92
|
+
// checks need the lexical value they expose, without inferring its type.
|
|
93
|
+
await Promise.all(
|
|
94
|
+
identifiers.map(async (identifier) => {
|
|
95
|
+
const parent = identifier.parent
|
|
96
|
+
if (ts.isShorthandPropertyAssignment(parent) && parent.name === identifier)
|
|
97
|
+
symbols.set(
|
|
98
|
+
identifier,
|
|
99
|
+
await project.checker.getShorthandAssignmentValueSymbol(parent),
|
|
100
|
+
)
|
|
101
|
+
else if (
|
|
102
|
+
ts.isExportSpecifier(parent) &&
|
|
103
|
+
(parent.propertyName ?? parent.name) === identifier &&
|
|
104
|
+
ts.isExportDeclaration(parent.parent.parent) &&
|
|
105
|
+
!parent.parent.parent.moduleSpecifier
|
|
106
|
+
)
|
|
107
|
+
symbols.set(
|
|
108
|
+
identifier,
|
|
109
|
+
await project.checker.getExportSpecifierLocalTargetSymbol(parent),
|
|
110
|
+
)
|
|
111
|
+
}),
|
|
112
|
+
)
|
|
113
|
+
const valueDeclarations = new Map<number, ts.Node>()
|
|
114
|
+
await Promise.all(
|
|
115
|
+
[
|
|
116
|
+
...new Map(
|
|
117
|
+
resolvedSymbols.flatMap((symbol) => (symbol ? [[symbol.id, symbol]] : [])),
|
|
118
|
+
).values(),
|
|
119
|
+
].map(async (symbol) => {
|
|
120
|
+
const declaration = await symbol.valueDeclaration?.resolve(project)
|
|
121
|
+
if (declaration) valueDeclarations.set(symbol.id, declaration)
|
|
122
|
+
}),
|
|
123
|
+
)
|
|
124
|
+
const bindings = new Map<number, SourceBinding>()
|
|
125
|
+
const bindingNames = new Map<number, ts.Identifier>()
|
|
126
|
+
const symbolUses = new Map<number, ts.Identifier[]>()
|
|
127
|
+
for (const identifier of identifiers) {
|
|
128
|
+
const symbol = symbols.get(identifier)
|
|
129
|
+
if (!symbol) continue
|
|
130
|
+
const uses = symbolUses.get(symbol.id) ?? []
|
|
131
|
+
uses.push(identifier)
|
|
132
|
+
symbolUses.set(symbol.id, uses)
|
|
133
|
+
}
|
|
134
|
+
const reactFactories = new Map<number, 'container' | 'factory'>()
|
|
135
|
+
const reactRefCreators = new Map<number, 'container' | 'creator'>()
|
|
136
|
+
const candidates = new Map<string, CapabilityReferenceCandidate>()
|
|
137
|
+
const packageFor = (module: string) => {
|
|
138
|
+
if (module.startsWith('.') || module.startsWith('/') || module.includes(':'))
|
|
139
|
+
return undefined
|
|
140
|
+
const parts = module.split('/')
|
|
141
|
+
const packageName = module.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]
|
|
142
|
+
return packageName && (!packageNames || packageNames.has(packageName))
|
|
143
|
+
? packageName
|
|
144
|
+
: undefined
|
|
145
|
+
}
|
|
146
|
+
function emit(
|
|
147
|
+
node: ts.Node,
|
|
148
|
+
binding: SourceBinding,
|
|
149
|
+
kind: CapabilityReferenceKind,
|
|
150
|
+
memberName: string | null = null,
|
|
151
|
+
exclusion: string | null = null,
|
|
152
|
+
importForm: CapabilityReferenceCandidate['importForm'] = null,
|
|
153
|
+
originStart: number | null = null,
|
|
154
|
+
value: StaticValue | null = null,
|
|
155
|
+
): void {
|
|
156
|
+
const start = node.getStart(source)
|
|
157
|
+
const end = node.end
|
|
158
|
+
const id = createHash('sha256')
|
|
159
|
+
.update(
|
|
160
|
+
JSON.stringify([
|
|
161
|
+
corpus,
|
|
162
|
+
path,
|
|
163
|
+
start,
|
|
164
|
+
end,
|
|
165
|
+
binding.packageName,
|
|
166
|
+
binding.module,
|
|
167
|
+
kind,
|
|
168
|
+
binding.exportName,
|
|
169
|
+
memberName,
|
|
170
|
+
originStart,
|
|
171
|
+
]),
|
|
172
|
+
)
|
|
173
|
+
.digest('hex')
|
|
174
|
+
const candidate = {
|
|
175
|
+
id,
|
|
176
|
+
corpus,
|
|
177
|
+
path,
|
|
178
|
+
start,
|
|
179
|
+
end,
|
|
180
|
+
originStart,
|
|
181
|
+
packageName: binding.packageName,
|
|
182
|
+
module: binding.module,
|
|
183
|
+
kind,
|
|
184
|
+
exportName: binding.exportName,
|
|
185
|
+
memberName,
|
|
186
|
+
value,
|
|
187
|
+
exclusion: binding.typeOnly
|
|
188
|
+
? 'type-only import'
|
|
189
|
+
: (exclusion ??
|
|
190
|
+
(kind === 'export' &&
|
|
191
|
+
nestedRoots.some(
|
|
192
|
+
(root) =>
|
|
193
|
+
root.module === binding.module && root.exportName === binding.exportName,
|
|
194
|
+
)
|
|
195
|
+
? 'nested root container; member chains counted at use sites'
|
|
196
|
+
: null)),
|
|
197
|
+
importForm,
|
|
198
|
+
}
|
|
199
|
+
if (candidates.has(id))
|
|
200
|
+
throw new Error(`duplicate candidate visitation: ${path}:${start}:${kind}`)
|
|
201
|
+
candidates.set(id, candidate)
|
|
202
|
+
}
|
|
203
|
+
function bind(name: ts.Identifier, binding: SourceBinding): void {
|
|
204
|
+
const symbol = symbols.get(name)
|
|
205
|
+
if (!symbol)
|
|
206
|
+
throw new Error(`unresolved lexical declaration at ${path}:${name.pos}`)
|
|
207
|
+
bindings.set(symbol.id, binding)
|
|
208
|
+
bindingNames.set(symbol.id, name)
|
|
209
|
+
}
|
|
210
|
+
function bindingFor(node: ts.Node, nestedMembers = false): SourceBinding | undefined {
|
|
211
|
+
if (ts.isIdentifier(node)) {
|
|
212
|
+
const symbol = symbols.get(node)
|
|
213
|
+
return symbol ? bindings.get(symbol.id) : undefined
|
|
214
|
+
}
|
|
215
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
|
|
216
|
+
const owner = bindingFor(node.expression, nestedMembers)
|
|
217
|
+
if (!owner || owner.typeOnly) return undefined
|
|
218
|
+
const member = ts.isPropertyAccessExpression(node)
|
|
219
|
+
? node.name.text
|
|
220
|
+
: node.argumentExpression && ts.isStringLiteralLikeNode(node.argumentExpression)
|
|
221
|
+
? node.argumentExpression.text
|
|
222
|
+
: null
|
|
223
|
+
if (
|
|
224
|
+
member &&
|
|
225
|
+
(owner.exportName === '*' ||
|
|
226
|
+
(owner.exportName === 'default' && owner.packageName === 'react-native'))
|
|
227
|
+
) {
|
|
228
|
+
return { ...owner, exportName: member }
|
|
229
|
+
}
|
|
230
|
+
if (
|
|
231
|
+
nestedMembers &&
|
|
232
|
+
member &&
|
|
233
|
+
nestedRoots.some(
|
|
234
|
+
(root) =>
|
|
235
|
+
root.module === owner.module && root.exportName === owner.exportName,
|
|
236
|
+
)
|
|
237
|
+
)
|
|
238
|
+
return { ...owner, memberPath: [...(owner.memberPath ?? []), member] }
|
|
239
|
+
}
|
|
240
|
+
return undefined
|
|
241
|
+
}
|
|
242
|
+
function memberKey(node: ts.Node): string | null {
|
|
243
|
+
if (ts.isPropertyAccessExpression(node)) return node.name.text
|
|
244
|
+
return ts.isElementAccessExpression(node) &&
|
|
245
|
+
node.argumentExpression &&
|
|
246
|
+
ts.isStringLiteralLikeNode(node.argumentExpression)
|
|
247
|
+
? node.argumentExpression.text
|
|
248
|
+
: null
|
|
249
|
+
}
|
|
250
|
+
// a static chain of member names below a nested root binding
|
|
251
|
+
function nestedChain(
|
|
252
|
+
node: ts.Node,
|
|
253
|
+
): { binding: SourceBinding; names: string[] } | undefined {
|
|
254
|
+
if (!ts.isPropertyAccessExpression(node) && !ts.isElementAccessExpression(node))
|
|
255
|
+
return undefined
|
|
256
|
+
const name = memberKey(node)
|
|
257
|
+
if (name === null) return undefined
|
|
258
|
+
const binding = bindingFor(node.expression, true)
|
|
259
|
+
return binding &&
|
|
260
|
+
!binding.typeOnly &&
|
|
261
|
+
nestedRoots.some(
|
|
262
|
+
(root) =>
|
|
263
|
+
root.module === binding.module && root.exportName === binding.exportName,
|
|
264
|
+
)
|
|
265
|
+
? { binding, names: [...(binding.memberPath ?? []), name] }
|
|
266
|
+
: undefined
|
|
267
|
+
}
|
|
268
|
+
function imports(node: ts.Node): void {
|
|
269
|
+
if (
|
|
270
|
+
ts.isImportDeclaration(node) &&
|
|
271
|
+
ts.isStringLiteralLikeNode(node.moduleSpecifier)
|
|
272
|
+
) {
|
|
273
|
+
const module = node.moduleSpecifier.text
|
|
274
|
+
const clause = node.importClause
|
|
275
|
+
if (
|
|
276
|
+
['react', 'react/jsx-runtime', 'react/jsx-dev-runtime'].includes(module) &&
|
|
277
|
+
clause &&
|
|
278
|
+
clause.phaseModifier !== ts.SyntaxKind.TypeKeyword
|
|
279
|
+
) {
|
|
280
|
+
if (clause.name) {
|
|
281
|
+
const symbol = symbols.get(clause.name)
|
|
282
|
+
if (symbol) reactFactories.set(symbol.id, 'container')
|
|
283
|
+
if (symbol && module === 'react') reactRefCreators.set(symbol.id, 'container')
|
|
284
|
+
}
|
|
285
|
+
if (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings)) {
|
|
286
|
+
const symbol = symbols.get(clause.namedBindings.name)
|
|
287
|
+
if (symbol) reactFactories.set(symbol.id, 'container')
|
|
288
|
+
if (symbol && module === 'react') reactRefCreators.set(symbol.id, 'container')
|
|
289
|
+
} else if (clause.namedBindings) {
|
|
290
|
+
for (const specifier of clause.namedBindings.elements) {
|
|
291
|
+
const imported = specifier.propertyName?.text ?? specifier.name.text
|
|
292
|
+
if (
|
|
293
|
+
module === 'react' &&
|
|
294
|
+
!specifier.isTypeOnly &&
|
|
295
|
+
(imported === 'useRef' || imported === 'createRef')
|
|
296
|
+
) {
|
|
297
|
+
const symbol = symbols.get(specifier.name)
|
|
298
|
+
if (symbol) reactRefCreators.set(symbol.id, 'creator')
|
|
299
|
+
}
|
|
300
|
+
if (
|
|
301
|
+
!specifier.isTypeOnly &&
|
|
302
|
+
['createElement', 'jsx', 'jsxs', 'jsxDEV'].includes(imported)
|
|
303
|
+
) {
|
|
304
|
+
const symbol = symbols.get(specifier.name)
|
|
305
|
+
if (symbol) reactFactories.set(symbol.id, 'factory')
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const packageName = packageFor(module)
|
|
311
|
+
if (packageName) {
|
|
312
|
+
const clause = node.importClause
|
|
313
|
+
if (!clause)
|
|
314
|
+
emit(
|
|
315
|
+
node,
|
|
316
|
+
{ packageName, module, exportName: 'side-effect', typeOnly: false },
|
|
317
|
+
'side-effect',
|
|
318
|
+
null,
|
|
319
|
+
null,
|
|
320
|
+
'side-effect',
|
|
321
|
+
)
|
|
322
|
+
else {
|
|
323
|
+
const typeOnly = clause.phaseModifier === ts.SyntaxKind.TypeKeyword
|
|
324
|
+
if (clause.name) {
|
|
325
|
+
const binding = { packageName, module, exportName: 'default', typeOnly }
|
|
326
|
+
bind(clause.name, binding)
|
|
327
|
+
emit(
|
|
328
|
+
clause.name,
|
|
329
|
+
binding,
|
|
330
|
+
'export',
|
|
331
|
+
null,
|
|
332
|
+
packageName === 'react-native'
|
|
333
|
+
? 'namespace container; members counted at use sites'
|
|
334
|
+
: null,
|
|
335
|
+
'default',
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
if (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings)) {
|
|
339
|
+
const binding = { packageName, module, exportName: '*', typeOnly }
|
|
340
|
+
bind(clause.namedBindings.name, binding)
|
|
341
|
+
emit(
|
|
342
|
+
clause.namedBindings,
|
|
343
|
+
binding,
|
|
344
|
+
'export',
|
|
345
|
+
null,
|
|
346
|
+
'namespace container; members counted at use sites',
|
|
347
|
+
'namespace',
|
|
348
|
+
)
|
|
349
|
+
} else if (clause.namedBindings) {
|
|
350
|
+
for (const specifier of clause.namedBindings.elements) {
|
|
351
|
+
const binding = {
|
|
352
|
+
packageName,
|
|
353
|
+
module,
|
|
354
|
+
exportName: specifier.propertyName?.text ?? specifier.name.text,
|
|
355
|
+
typeOnly: typeOnly || specifier.isTypeOnly,
|
|
356
|
+
}
|
|
357
|
+
bind(specifier.name, binding)
|
|
358
|
+
emit(specifier, binding, 'export', null, null, 'named')
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (
|
|
365
|
+
ts.isExportDeclaration(node) &&
|
|
366
|
+
node.moduleSpecifier &&
|
|
367
|
+
ts.isStringLiteralLikeNode(node.moduleSpecifier)
|
|
368
|
+
) {
|
|
369
|
+
const module = node.moduleSpecifier.text
|
|
370
|
+
const packageName = packageFor(module)
|
|
371
|
+
if (
|
|
372
|
+
packageName &&
|
|
373
|
+
(!node.exportClause || ts.isNamespaceExport(node.exportClause))
|
|
374
|
+
) {
|
|
375
|
+
emit(
|
|
376
|
+
node,
|
|
377
|
+
{ packageName, module, exportName: '*', typeOnly: node.isTypeOnly },
|
|
378
|
+
'export',
|
|
379
|
+
null,
|
|
380
|
+
null,
|
|
381
|
+
're-export',
|
|
382
|
+
)
|
|
383
|
+
}
|
|
384
|
+
if (packageName && node.exportClause && ts.isNamedExports(node.exportClause)) {
|
|
385
|
+
for (const specifier of node.exportClause.elements) {
|
|
386
|
+
emit(
|
|
387
|
+
specifier,
|
|
388
|
+
{
|
|
389
|
+
packageName,
|
|
390
|
+
module,
|
|
391
|
+
exportName: specifier.propertyName?.text ?? specifier.name.text,
|
|
392
|
+
typeOnly: node.isTypeOnly || specifier.isTypeOnly,
|
|
393
|
+
},
|
|
394
|
+
'export',
|
|
395
|
+
null,
|
|
396
|
+
null,
|
|
397
|
+
're-export',
|
|
398
|
+
)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (
|
|
403
|
+
ts.isVariableDeclaration(node) &&
|
|
404
|
+
node.initializer &&
|
|
405
|
+
ts.isCallExpression(node.initializer)
|
|
406
|
+
) {
|
|
407
|
+
const call = node.initializer
|
|
408
|
+
if (
|
|
409
|
+
ts.isIdentifier(call.expression) &&
|
|
410
|
+
call.expression.text === 'require' &&
|
|
411
|
+
!symbols.get(call.expression) &&
|
|
412
|
+
call.arguments[0] &&
|
|
413
|
+
ts.isStringLiteralLikeNode(call.arguments[0])
|
|
414
|
+
) {
|
|
415
|
+
const module = call.arguments[0].text
|
|
416
|
+
const packageName = packageFor(module)
|
|
417
|
+
if (packageName) {
|
|
418
|
+
if (node.name && ts.isIdentifier(node.name)) {
|
|
419
|
+
const binding = { packageName, module, exportName: '*', typeOnly: false }
|
|
420
|
+
bind(node.name, binding)
|
|
421
|
+
emit(
|
|
422
|
+
node.name,
|
|
423
|
+
binding,
|
|
424
|
+
'export',
|
|
425
|
+
null,
|
|
426
|
+
'namespace container; members counted at use sites',
|
|
427
|
+
'require',
|
|
428
|
+
)
|
|
429
|
+
} else if (node.name && ts.isObjectBindingPattern(node.name)) {
|
|
430
|
+
for (const element of node.name.elements) {
|
|
431
|
+
if (!element.name) continue
|
|
432
|
+
if (!ts.isIdentifier(element.name)) continue
|
|
433
|
+
const exportName =
|
|
434
|
+
element.propertyName &&
|
|
435
|
+
(ts.isIdentifier(element.propertyName) ||
|
|
436
|
+
ts.isStringLiteralLikeNode(element.propertyName))
|
|
437
|
+
? element.propertyName.text
|
|
438
|
+
: element.name.text
|
|
439
|
+
const binding = {
|
|
440
|
+
packageName,
|
|
441
|
+
module,
|
|
442
|
+
exportName: element.dotDotDotToken ? '*' : exportName,
|
|
443
|
+
typeOnly: false,
|
|
444
|
+
}
|
|
445
|
+
bind(element.name, binding)
|
|
446
|
+
emit(
|
|
447
|
+
element,
|
|
448
|
+
binding,
|
|
449
|
+
'export',
|
|
450
|
+
null,
|
|
451
|
+
element.dotDotDotToken ? 'namespace rest container' : null,
|
|
452
|
+
'require',
|
|
453
|
+
)
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (
|
|
460
|
+
ts.isCallExpression(node) &&
|
|
461
|
+
ts.isExpressionStatement(node.parent) &&
|
|
462
|
+
ts.isIdentifier(node.expression) &&
|
|
463
|
+
node.expression.text === 'require' &&
|
|
464
|
+
!symbols.get(node.expression) &&
|
|
465
|
+
node.arguments[0] &&
|
|
466
|
+
ts.isStringLiteralLikeNode(node.arguments[0])
|
|
467
|
+
) {
|
|
468
|
+
const module = node.arguments[0].text
|
|
469
|
+
const packageName = packageFor(module)
|
|
470
|
+
if (packageName)
|
|
471
|
+
emit(
|
|
472
|
+
node,
|
|
473
|
+
{ packageName, module, exportName: 'side-effect', typeOnly: false },
|
|
474
|
+
'side-effect',
|
|
475
|
+
null,
|
|
476
|
+
null,
|
|
477
|
+
'side-effect',
|
|
478
|
+
)
|
|
479
|
+
}
|
|
480
|
+
node.forEachChild(imports)
|
|
481
|
+
}
|
|
482
|
+
imports(source)
|
|
483
|
+
const namespaceModuleBindings = new Map<string, ts.Identifier[]>()
|
|
484
|
+
for (const [id, binding] of bindings) {
|
|
485
|
+
const name = bindingNames.get(id)
|
|
486
|
+
if (
|
|
487
|
+
name &&
|
|
488
|
+
!binding.typeOnly &&
|
|
489
|
+
nestedRoots.some(
|
|
490
|
+
(root) =>
|
|
491
|
+
root.module === binding.module &&
|
|
492
|
+
(binding.exportName === '*' || root.exportName === binding.exportName),
|
|
493
|
+
)
|
|
494
|
+
) {
|
|
495
|
+
const names = namespaceModuleBindings.get(binding.module) ?? []
|
|
496
|
+
names.push(name)
|
|
497
|
+
namespaceModuleBindings.set(binding.module, names)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// namespace aliases must keep every use visible. following const aliases
|
|
501
|
+
// here also rejects a mutation or escape through a second local name.
|
|
502
|
+
function isConstDeclaration(node: ts.VariableDeclarationList): boolean {
|
|
503
|
+
// await using shares the const bit, so compare the full declaration kind.
|
|
504
|
+
return ((node.flags & ts.NodeFlags.BlockScoped) ^ ts.NodeFlags.Const) === 0
|
|
505
|
+
}
|
|
506
|
+
const safeNamespaceBindings = new Map<number, boolean>()
|
|
507
|
+
function safeNamespaceBinding(name: ts.Identifier): boolean {
|
|
508
|
+
const symbol = symbols.get(name)
|
|
509
|
+
if (!symbol) return false
|
|
510
|
+
const cached = safeNamespaceBindings.get(symbol.id)
|
|
511
|
+
if (cached !== undefined) return cached
|
|
512
|
+
safeNamespaceBindings.set(symbol.id, false)
|
|
513
|
+
const bindingName = bindingNames.get(symbol.id) ?? name
|
|
514
|
+
const declaration = valueDeclarations.get(symbol.id) ?? bindingName.parent
|
|
515
|
+
const local = ts.isVariableDeclaration(declaration)
|
|
516
|
+
if (local) {
|
|
517
|
+
if (
|
|
518
|
+
declaration.name !== bindingName ||
|
|
519
|
+
!ts.isVariableDeclarationList(declaration.parent) ||
|
|
520
|
+
!isConstDeclaration(declaration.parent) ||
|
|
521
|
+
(ts.isVariableStatement(declaration.parent.parent) &&
|
|
522
|
+
declaration.parent.parent.modifiers?.some(
|
|
523
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
|
|
524
|
+
))
|
|
525
|
+
)
|
|
526
|
+
return false
|
|
527
|
+
} else if (
|
|
528
|
+
!(
|
|
529
|
+
(ts.isImportSpecifier(declaration) ||
|
|
530
|
+
ts.isNamespaceImport(declaration) ||
|
|
531
|
+
ts.isImportClause(declaration)) &&
|
|
532
|
+
declaration.name === bindingName
|
|
533
|
+
)
|
|
534
|
+
)
|
|
535
|
+
return false
|
|
536
|
+
for (const use of symbolUses.get(symbol.id) ?? []) {
|
|
537
|
+
if (use === bindingName) continue
|
|
538
|
+
let context = use.parent
|
|
539
|
+
while (ts.isQualifiedName(context)) context = context.parent
|
|
540
|
+
if (ts.isTypeNode(context)) continue
|
|
541
|
+
if (local && use.getStart(source) < declaration.end) return false
|
|
542
|
+
let access: ts.Node = use
|
|
543
|
+
while (
|
|
544
|
+
(ts.isPropertyAccessExpression(access.parent) ||
|
|
545
|
+
ts.isElementAccessExpression(access.parent)) &&
|
|
546
|
+
access.parent.expression === access
|
|
547
|
+
) {
|
|
548
|
+
if (memberKey(access.parent) === null || access.parent.questionDotToken)
|
|
549
|
+
return false
|
|
550
|
+
access = access.parent
|
|
551
|
+
}
|
|
552
|
+
const parent = access.parent
|
|
553
|
+
if (ts.isVariableDeclaration(parent) && parent.initializer === access) {
|
|
554
|
+
if (!ts.isIdentifier(parent.name) || !safeNamespaceBinding(parent.name))
|
|
555
|
+
return false
|
|
556
|
+
continue
|
|
557
|
+
}
|
|
558
|
+
if (
|
|
559
|
+
((ts.isJsxOpeningLikeElement(parent) || ts.isJsxClosingElement(parent)) &&
|
|
560
|
+
parent.tagName === access) ||
|
|
561
|
+
(ts.isCallExpression(parent) &&
|
|
562
|
+
parent.expression === access &&
|
|
563
|
+
!parent.questionDotToken) ||
|
|
564
|
+
(access !== use && ts.isExpressionStatement(parent))
|
|
565
|
+
)
|
|
566
|
+
continue
|
|
567
|
+
return false
|
|
568
|
+
}
|
|
569
|
+
safeNamespaceBindings.set(symbol.id, true)
|
|
570
|
+
return true
|
|
571
|
+
}
|
|
572
|
+
const safeNamespaceModules = new Map(
|
|
573
|
+
[...namespaceModuleBindings].map(([module, names]) => [
|
|
574
|
+
module,
|
|
575
|
+
names.every(safeNamespaceBinding),
|
|
576
|
+
]),
|
|
577
|
+
)
|
|
578
|
+
function aliases(node: ts.Node): void {
|
|
579
|
+
if (
|
|
580
|
+
ts.isVariableDeclaration(node) &&
|
|
581
|
+
node.initializer &&
|
|
582
|
+
ts.isVariableDeclarationList(node.parent) &&
|
|
583
|
+
isConstDeclaration(node.parent)
|
|
584
|
+
) {
|
|
585
|
+
const owner = bindingFor(node.initializer, true)
|
|
586
|
+
let root: ts.Node = node.initializer
|
|
587
|
+
while (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root))
|
|
588
|
+
root = root.expression
|
|
589
|
+
if (
|
|
590
|
+
owner &&
|
|
591
|
+
node.name &&
|
|
592
|
+
ts.isObjectBindingPattern(node.name) &&
|
|
593
|
+
owner.exportName === '*'
|
|
594
|
+
) {
|
|
595
|
+
for (const element of node.name.elements) {
|
|
596
|
+
if (!element.name) continue
|
|
597
|
+
if (!ts.isIdentifier(element.name)) continue
|
|
598
|
+
const exportName =
|
|
599
|
+
element.propertyName &&
|
|
600
|
+
(ts.isIdentifier(element.propertyName) ||
|
|
601
|
+
ts.isStringLiteralLikeNode(element.propertyName))
|
|
602
|
+
? element.propertyName.text
|
|
603
|
+
: element.name.text
|
|
604
|
+
const binding = {
|
|
605
|
+
...owner,
|
|
606
|
+
exportName: element.dotDotDotToken ? '*' : exportName,
|
|
607
|
+
}
|
|
608
|
+
bind(element.name, binding)
|
|
609
|
+
emit(
|
|
610
|
+
element,
|
|
611
|
+
binding,
|
|
612
|
+
'export',
|
|
613
|
+
null,
|
|
614
|
+
element.dotDotDotToken ? 'namespace rest container' : null,
|
|
615
|
+
'namespace',
|
|
616
|
+
)
|
|
617
|
+
}
|
|
618
|
+
} else if (
|
|
619
|
+
owner &&
|
|
620
|
+
node.name &&
|
|
621
|
+
ts.isIdentifier(node.name) &&
|
|
622
|
+
(!nestedRoots.some(
|
|
623
|
+
(root) =>
|
|
624
|
+
root.module === owner.module && root.exportName === owner.exportName,
|
|
625
|
+
) ||
|
|
626
|
+
(safeNamespaceBinding(node.name) &&
|
|
627
|
+
ts.isIdentifier(root) &&
|
|
628
|
+
safeNamespaceBinding(root) &&
|
|
629
|
+
safeNamespaceModules.get(owner.module)))
|
|
630
|
+
) {
|
|
631
|
+
bind(node.name, owner)
|
|
632
|
+
emit(
|
|
633
|
+
node.name,
|
|
634
|
+
owner,
|
|
635
|
+
'export',
|
|
636
|
+
null,
|
|
637
|
+
'local alias container; imported API counted at its declaration',
|
|
638
|
+
null,
|
|
639
|
+
)
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
node.forEachChild(aliases)
|
|
643
|
+
}
|
|
644
|
+
aliases(source)
|
|
645
|
+
// a const React ref whose every use is lexically visible binds its
|
|
646
|
+
// `.current` method calls to the one package component it is passed to as
|
|
647
|
+
// `ref={name}`. any other use (another target, an unbound or wrapper tag, a
|
|
648
|
+
// write, a computed access, an alias, a call argument) leaves it unknown.
|
|
649
|
+
function refMethods(): void {
|
|
650
|
+
const creates = (init: ts.Expression): boolean => {
|
|
651
|
+
const callee = ts.isCallExpression(init) ? init.expression : undefined
|
|
652
|
+
if (!callee) return false
|
|
653
|
+
if (ts.isIdentifier(callee)) {
|
|
654
|
+
const symbol = symbols.get(callee)
|
|
655
|
+
return symbol !== undefined && reactRefCreators.get(symbol.id) === 'creator'
|
|
656
|
+
}
|
|
657
|
+
if (
|
|
658
|
+
ts.isPropertyAccessExpression(callee) &&
|
|
659
|
+
ts.isIdentifier(callee.expression) &&
|
|
660
|
+
(callee.name.text === 'useRef' || callee.name.text === 'createRef')
|
|
661
|
+
) {
|
|
662
|
+
const symbol = symbols.get(callee.expression)
|
|
663
|
+
return symbol !== undefined && reactRefCreators.get(symbol.id) === 'container'
|
|
664
|
+
}
|
|
665
|
+
return false
|
|
666
|
+
}
|
|
667
|
+
type RefUse = {
|
|
668
|
+
binding?: SourceBinding
|
|
669
|
+
invalid: boolean
|
|
670
|
+
sites: ts.Node[]
|
|
671
|
+
}
|
|
672
|
+
const refs = new Map<number, RefUse>()
|
|
673
|
+
for (const identifier of identifiers) {
|
|
674
|
+
const declaration = identifier.parent
|
|
675
|
+
if (
|
|
676
|
+
ts.isVariableDeclaration(declaration) &&
|
|
677
|
+
declaration.name === identifier &&
|
|
678
|
+
declaration.initializer &&
|
|
679
|
+
ts.isVariableDeclarationList(declaration.parent) &&
|
|
680
|
+
isConstDeclaration(declaration.parent) &&
|
|
681
|
+
creates(declaration.initializer)
|
|
682
|
+
) {
|
|
683
|
+
const symbol = symbols.get(identifier)
|
|
684
|
+
if (symbol) refs.set(symbol.id, { invalid: false, sites: [] })
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (refs.size === 0) return
|
|
688
|
+
const effectiveRefs = new Map<ts.JsxAttributes, ts.JsxAttribute | null>()
|
|
689
|
+
for (const identifier of identifiers) {
|
|
690
|
+
const symbol = symbols.get(identifier)
|
|
691
|
+
const use = symbol ? refs.get(symbol.id) : undefined
|
|
692
|
+
if (!use || use.invalid) continue
|
|
693
|
+
const parent = identifier.parent
|
|
694
|
+
if (ts.isVariableDeclaration(parent) && parent.name === identifier) continue
|
|
695
|
+
if (
|
|
696
|
+
ts.isJsxExpression(parent) &&
|
|
697
|
+
parent.expression === identifier &&
|
|
698
|
+
ts.isJsxAttribute(parent.parent) &&
|
|
699
|
+
ts.isIdentifier(parent.parent.name) &&
|
|
700
|
+
parent.parent.name.text === 'ref' &&
|
|
701
|
+
ts.isJsxAttributes(parent.parent.parent) &&
|
|
702
|
+
ts.isJsxOpeningLikeElement(parent.parent.parent.parent)
|
|
703
|
+
) {
|
|
704
|
+
const attributes = parent.parent.parent
|
|
705
|
+
let effective = effectiveRefs.get(attributes)
|
|
706
|
+
if (effective === undefined) {
|
|
707
|
+
effective = null
|
|
708
|
+
for (const attribute of attributes.properties) {
|
|
709
|
+
if (ts.isJsxSpreadAttribute(attribute)) {
|
|
710
|
+
// a later spread can replace ref. only a literal whose own
|
|
711
|
+
// keys are all known and exclude ref is transparent here.
|
|
712
|
+
if (
|
|
713
|
+
!ts.isObjectLiteralExpression(attribute.expression) ||
|
|
714
|
+
!attribute.expression.properties.every((property) => {
|
|
715
|
+
if (ts.isSpreadAssignment(property)) return false
|
|
716
|
+
const name = key(property.name)
|
|
717
|
+
return name !== null && name !== 'ref'
|
|
718
|
+
})
|
|
719
|
+
)
|
|
720
|
+
effective = null
|
|
721
|
+
} else if (ts.isIdentifier(attribute.name) && attribute.name.text === 'ref')
|
|
722
|
+
effective = attribute
|
|
723
|
+
}
|
|
724
|
+
effectiveRefs.set(attributes, effective)
|
|
725
|
+
}
|
|
726
|
+
const binding = bindingFor(parent.parent.parent.parent.tagName)
|
|
727
|
+
if (
|
|
728
|
+
effective !== parent.parent ||
|
|
729
|
+
!binding ||
|
|
730
|
+
binding.typeOnly ||
|
|
731
|
+
(use.binding &&
|
|
732
|
+
(use.binding.module !== binding.module ||
|
|
733
|
+
use.binding.exportName !== binding.exportName))
|
|
734
|
+
)
|
|
735
|
+
use.invalid = true
|
|
736
|
+
else use.binding = binding
|
|
737
|
+
continue
|
|
738
|
+
}
|
|
739
|
+
// the only other accepted use is a static method call on `.current`
|
|
740
|
+
if (
|
|
741
|
+
ts.isPropertyAccessExpression(parent) &&
|
|
742
|
+
parent.expression === identifier &&
|
|
743
|
+
parent.name.text === 'current'
|
|
744
|
+
) {
|
|
745
|
+
const method = parent.parent
|
|
746
|
+
if (
|
|
747
|
+
(ts.isPropertyAccessExpression(method) ||
|
|
748
|
+
ts.isElementAccessExpression(method)) &&
|
|
749
|
+
method.expression === parent &&
|
|
750
|
+
memberKey(method) !== null &&
|
|
751
|
+
ts.isCallExpression(method.parent) &&
|
|
752
|
+
method.parent.expression === method
|
|
753
|
+
) {
|
|
754
|
+
use.sites.push(method)
|
|
755
|
+
continue
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
use.invalid = true
|
|
759
|
+
}
|
|
760
|
+
for (const use of refs.values()) {
|
|
761
|
+
if (use.invalid || !use.binding) continue
|
|
762
|
+
for (const site of use.sites) {
|
|
763
|
+
const member = memberKey(site)
|
|
764
|
+
if (member !== null) emit(site, use.binding, 'member', member)
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
refMethods()
|
|
769
|
+
function key(name: ts.PropertyName): string | null {
|
|
770
|
+
return ts.isIdentifier(name) ||
|
|
771
|
+
ts.isStringLiteralLikeNode(name) ||
|
|
772
|
+
ts.isNumericLiteral(name)
|
|
773
|
+
? name.text
|
|
774
|
+
: null
|
|
775
|
+
}
|
|
776
|
+
function factoryBinding(node: ts.CallExpression): SourceBinding | undefined {
|
|
777
|
+
if (
|
|
778
|
+
!node.arguments[0] ||
|
|
779
|
+
!node.arguments[1] ||
|
|
780
|
+
!ts.isObjectLiteralExpression(node.arguments[1])
|
|
781
|
+
)
|
|
782
|
+
return undefined
|
|
783
|
+
const callee = ts.isPropertyAccessExpression(node.expression)
|
|
784
|
+
? node.expression.expression
|
|
785
|
+
: node.expression
|
|
786
|
+
const symbol = ts.isIdentifier(callee) ? symbols.get(callee) : undefined
|
|
787
|
+
const factoryKind = symbol ? reactFactories.get(symbol.id) : undefined
|
|
788
|
+
if (
|
|
789
|
+
factoryKind !== 'factory' &&
|
|
790
|
+
!(
|
|
791
|
+
factoryKind === 'container' &&
|
|
792
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
793
|
+
['createElement', 'jsx', 'jsxs', 'jsxDEV'].includes(node.expression.name.text)
|
|
794
|
+
)
|
|
795
|
+
)
|
|
796
|
+
return undefined
|
|
797
|
+
const binding = bindingFor(node.arguments[0])
|
|
798
|
+
return binding && !binding.typeOnly ? binding : undefined
|
|
799
|
+
}
|
|
800
|
+
// const does not freeze an object. every alias, containing array and
|
|
801
|
+
// spread copy must keep all its uses inside visible package inputs.
|
|
802
|
+
function transparentStyleSyntax(node: ts.Node) {
|
|
803
|
+
return (
|
|
804
|
+
ts.isParenthesizedExpression(node) ||
|
|
805
|
+
ts.isAsExpression(node) ||
|
|
806
|
+
ts.isTypeAssertion(node) ||
|
|
807
|
+
ts.isNonNullExpression(node) ||
|
|
808
|
+
ts.isSatisfiesExpression(node)
|
|
809
|
+
)
|
|
810
|
+
}
|
|
811
|
+
const safeStyleBindings = new Map<number, boolean>()
|
|
812
|
+
function safeStyleBinding(name: ts.Identifier): boolean {
|
|
813
|
+
const symbol = symbols.get(name)
|
|
814
|
+
if (!symbol) return false
|
|
815
|
+
const cached = safeStyleBindings.get(symbol.id)
|
|
816
|
+
if (cached !== undefined) return cached
|
|
817
|
+
safeStyleBindings.set(symbol.id, false)
|
|
818
|
+
const declaration = valueDeclarations.get(symbol.id)
|
|
819
|
+
if (
|
|
820
|
+
!declaration ||
|
|
821
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
822
|
+
!ts.isIdentifier(declaration.name) ||
|
|
823
|
+
!declaration.initializer ||
|
|
824
|
+
!ts.isVariableDeclarationList(declaration.parent) ||
|
|
825
|
+
!isConstDeclaration(declaration.parent) ||
|
|
826
|
+
(ts.isVariableStatement(declaration.parent.parent) &&
|
|
827
|
+
declaration.parent.parent.modifiers?.some(
|
|
828
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword,
|
|
829
|
+
))
|
|
830
|
+
)
|
|
831
|
+
return false
|
|
832
|
+
for (const use of symbolUses.get(symbol.id) ?? []) {
|
|
833
|
+
if (use === declaration.name) continue
|
|
834
|
+
let context = use.parent
|
|
835
|
+
while (ts.isQualifiedName(context)) context = context.parent
|
|
836
|
+
if (ts.isTypeNode(context)) continue
|
|
837
|
+
if (use.getStart(source) < declaration.end || !safeStyleUse(use)) return false
|
|
838
|
+
}
|
|
839
|
+
safeStyleBindings.set(symbol.id, true)
|
|
840
|
+
return true
|
|
841
|
+
}
|
|
842
|
+
function safeStyleUse(use: ts.Expression): boolean {
|
|
843
|
+
let value = use
|
|
844
|
+
while (true) {
|
|
845
|
+
const parent = value.parent
|
|
846
|
+
if (transparentStyleSyntax(parent) || ts.isArrayLiteralExpression(parent))
|
|
847
|
+
value = parent
|
|
848
|
+
else if (
|
|
849
|
+
ts.isSpreadAssignment(parent) &&
|
|
850
|
+
parent.expression === value &&
|
|
851
|
+
ts.isObjectLiteralExpression(parent.parent)
|
|
852
|
+
)
|
|
853
|
+
value = parent.parent
|
|
854
|
+
else if (
|
|
855
|
+
(ts.isPropertyAccessExpression(parent) ||
|
|
856
|
+
ts.isElementAccessExpression(parent)) &&
|
|
857
|
+
parent.expression === value &&
|
|
858
|
+
!parent.questionDotToken &&
|
|
859
|
+
memberKey(parent) !== null
|
|
860
|
+
)
|
|
861
|
+
value = parent
|
|
862
|
+
else break
|
|
863
|
+
}
|
|
864
|
+
const parent = value.parent
|
|
865
|
+
if (ts.isVariableDeclaration(parent) && parent.initializer === value)
|
|
866
|
+
return ts.isIdentifier(parent.name) && safeStyleBinding(parent.name)
|
|
867
|
+
if (
|
|
868
|
+
ts.isJsxSpreadAttribute(parent) &&
|
|
869
|
+
parent.expression === value &&
|
|
870
|
+
ts.isJsxAttributes(parent.parent) &&
|
|
871
|
+
ts.isJsxOpeningLikeElement(parent.parent.parent)
|
|
872
|
+
) {
|
|
873
|
+
const binding = bindingFor(parent.parent.parent.tagName)
|
|
874
|
+
return binding !== undefined && !binding.typeOnly
|
|
875
|
+
}
|
|
876
|
+
if (
|
|
877
|
+
ts.isObjectLiteralExpression(value) &&
|
|
878
|
+
ts.isCallExpression(parent) &&
|
|
879
|
+
parent.arguments[1] === value
|
|
880
|
+
)
|
|
881
|
+
return factoryBinding(parent) !== undefined
|
|
882
|
+
if (
|
|
883
|
+
ts.isJsxExpression(parent) &&
|
|
884
|
+
parent.expression === value &&
|
|
885
|
+
ts.isJsxAttribute(parent.parent) &&
|
|
886
|
+
ts.isIdentifier(parent.parent.name) &&
|
|
887
|
+
parent.parent.name.text === 'style' &&
|
|
888
|
+
ts.isJsxAttributes(parent.parent.parent) &&
|
|
889
|
+
ts.isJsxOpeningLikeElement(parent.parent.parent.parent)
|
|
890
|
+
) {
|
|
891
|
+
const binding = bindingFor(parent.parent.parent.parent.tagName)
|
|
892
|
+
return binding !== undefined && !binding.typeOnly
|
|
893
|
+
}
|
|
894
|
+
if (
|
|
895
|
+
((ts.isPropertyAssignment(parent) && parent.initializer === value) ||
|
|
896
|
+
(ts.isShorthandPropertyAssignment(parent) && parent.name === value)) &&
|
|
897
|
+
key(parent.name) === 'style' &&
|
|
898
|
+
ts.isObjectLiteralExpression(parent.parent) &&
|
|
899
|
+
ts.isCallExpression(parent.parent.parent) &&
|
|
900
|
+
parent.parent.parent.arguments[1] === parent.parent
|
|
901
|
+
)
|
|
902
|
+
return factoryBinding(parent.parent.parent) !== undefined
|
|
903
|
+
return false
|
|
904
|
+
}
|
|
905
|
+
const styleBindingObjects = new Map<
|
|
906
|
+
number,
|
|
907
|
+
Map<string, ts.ObjectLiteralExpression[]>
|
|
908
|
+
>()
|
|
909
|
+
const styleObjectProperties = new Map<
|
|
910
|
+
ts.ObjectLiteralExpression,
|
|
911
|
+
Map<string, ts.Expression> | null
|
|
912
|
+
>()
|
|
913
|
+
function styleObjects(
|
|
914
|
+
node: ts.Expression,
|
|
915
|
+
site?: ts.Expression,
|
|
916
|
+
members: readonly string[] = [],
|
|
917
|
+
allowArrays = true,
|
|
918
|
+
): { node: ts.ObjectLiteralExpression; site?: ts.Expression }[] {
|
|
919
|
+
if (transparentStyleSyntax(node))
|
|
920
|
+
return styleObjects(node.expression, site, members, allowArrays)
|
|
921
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
|
|
922
|
+
const member = memberKey(node)
|
|
923
|
+
return member !== null && !node.questionDotToken
|
|
924
|
+
? styleObjects(node.expression, site ?? node, [member, ...members], allowArrays)
|
|
925
|
+
: []
|
|
926
|
+
}
|
|
927
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
928
|
+
// accessors, spreads and computed names can hide effects or identity.
|
|
929
|
+
// direct literals retain their existing candidate path.
|
|
930
|
+
if (site || members.length > 0) {
|
|
931
|
+
let properties = styleObjectProperties.get(node)
|
|
932
|
+
if (properties === undefined) {
|
|
933
|
+
properties = new Map<string, ts.Expression>()
|
|
934
|
+
for (const property of node.properties) {
|
|
935
|
+
if (
|
|
936
|
+
!(
|
|
937
|
+
ts.isPropertyAssignment(property) ||
|
|
938
|
+
ts.isShorthandPropertyAssignment(property)
|
|
939
|
+
)
|
|
940
|
+
) {
|
|
941
|
+
properties = null
|
|
942
|
+
break
|
|
943
|
+
}
|
|
944
|
+
const name = key(property.name)
|
|
945
|
+
const expression = propertyExpression(property)
|
|
946
|
+
if (name === null || expression === null) {
|
|
947
|
+
properties = null
|
|
948
|
+
break
|
|
949
|
+
}
|
|
950
|
+
properties.set(name, expression)
|
|
951
|
+
}
|
|
952
|
+
styleObjectProperties.set(node, properties)
|
|
953
|
+
}
|
|
954
|
+
if (!properties) return []
|
|
955
|
+
if (members.length > 0) {
|
|
956
|
+
// __proto__ in an object initializer changes its prototype; it is
|
|
957
|
+
// not an own data property whose value we can follow.
|
|
958
|
+
const value =
|
|
959
|
+
members[0] === '__proto__' ? undefined : properties.get(members[0])
|
|
960
|
+
return value ? styleObjects(value, site, members.slice(1), allowArrays) : []
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
return [{ node, site }]
|
|
964
|
+
}
|
|
965
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
966
|
+
if (!allowArrays || members.length > 0) return []
|
|
967
|
+
const objects = node.elements.flatMap((element) => styleObjects(element, site))
|
|
968
|
+
const seen = new Map<ts.Node | undefined, Set<ts.Node>>()
|
|
969
|
+
return objects.filter((object) => {
|
|
970
|
+
const nodes = seen.get(object.site) ?? new Set<ts.Node>()
|
|
971
|
+
if (nodes.has(object.node)) return false
|
|
972
|
+
nodes.add(object.node)
|
|
973
|
+
seen.set(object.site, nodes)
|
|
974
|
+
return true
|
|
975
|
+
})
|
|
976
|
+
}
|
|
977
|
+
if (ts.isIdentifier(node) && safeStyleBinding(node)) {
|
|
978
|
+
const symbol = symbols.get(node)
|
|
979
|
+
const declaration = symbol ? valueDeclarations.get(symbol.id) : undefined
|
|
980
|
+
if (
|
|
981
|
+
symbol &&
|
|
982
|
+
declaration &&
|
|
983
|
+
ts.isVariableDeclaration(declaration) &&
|
|
984
|
+
declaration.initializer
|
|
985
|
+
) {
|
|
986
|
+
let paths = styleBindingObjects.get(symbol.id)
|
|
987
|
+
if (!paths) {
|
|
988
|
+
paths = new Map()
|
|
989
|
+
styleBindingObjects.set(symbol.id, paths)
|
|
990
|
+
}
|
|
991
|
+
const path = JSON.stringify([allowArrays, members])
|
|
992
|
+
let objects = paths.get(path)
|
|
993
|
+
if (!objects) {
|
|
994
|
+
paths.set(path, [])
|
|
995
|
+
objects = styleObjects(
|
|
996
|
+
declaration.initializer,
|
|
997
|
+
node,
|
|
998
|
+
members,
|
|
999
|
+
allowArrays,
|
|
1000
|
+
).map((value) => value.node)
|
|
1001
|
+
paths.set(path, objects)
|
|
1002
|
+
}
|
|
1003
|
+
return objects.map((value) => ({ node: value, site: site ?? node }))
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
return []
|
|
1007
|
+
}
|
|
1008
|
+
function staticValue(expression: ts.Expression): StaticValue | null {
|
|
1009
|
+
if (
|
|
1010
|
+
ts.isParenthesizedExpression(expression) ||
|
|
1011
|
+
ts.isAsExpression(expression) ||
|
|
1012
|
+
ts.isSatisfiesExpression(expression) ||
|
|
1013
|
+
ts.isNonNullExpression(expression)
|
|
1014
|
+
)
|
|
1015
|
+
return staticValue(expression.expression)
|
|
1016
|
+
if (ts.isStringLiteralLikeNode(expression)) return expression.text
|
|
1017
|
+
if (ts.isNumericLiteral(expression)) return Number(expression.text)
|
|
1018
|
+
if (expression.kind === ts.SyntaxKind.TrueKeyword) return true
|
|
1019
|
+
if (expression.kind === ts.SyntaxKind.FalseKeyword) return false
|
|
1020
|
+
if (
|
|
1021
|
+
ts.isPrefixUnaryExpression(expression) &&
|
|
1022
|
+
expression.operator === ts.SyntaxKind.MinusToken &&
|
|
1023
|
+
ts.isNumericLiteral(expression.operand)
|
|
1024
|
+
)
|
|
1025
|
+
return -Number(expression.operand.text)
|
|
1026
|
+
return null
|
|
1027
|
+
}
|
|
1028
|
+
function propertyExpression(
|
|
1029
|
+
property: ts.ObjectLiteralElementLike,
|
|
1030
|
+
): ts.Expression | null {
|
|
1031
|
+
if (ts.isPropertyAssignment(property)) return property.initializer
|
|
1032
|
+
return ts.isShorthandPropertyAssignment(property) && ts.isIdentifier(property.name)
|
|
1033
|
+
? property.name
|
|
1034
|
+
: null
|
|
1035
|
+
}
|
|
1036
|
+
function propertyValue(property: ts.ObjectLiteralElementLike): StaticValue | null {
|
|
1037
|
+
return ts.isPropertyAssignment(property) ? staticValue(property.initializer) : null
|
|
1038
|
+
}
|
|
1039
|
+
function attributeValue(attribute: ts.JsxAttribute): StaticValue | null {
|
|
1040
|
+
const initializer = attribute.initializer
|
|
1041
|
+
if (!initializer) return true
|
|
1042
|
+
if (ts.isStringLiteralLikeNode(initializer)) return initializer.text
|
|
1043
|
+
return ts.isJsxExpression(initializer) && initializer.expression
|
|
1044
|
+
? staticValue(initializer.expression)
|
|
1045
|
+
: null
|
|
1046
|
+
}
|
|
1047
|
+
function spread(
|
|
1048
|
+
expression: ts.Expression,
|
|
1049
|
+
site: ts.Node,
|
|
1050
|
+
binding: SourceBinding,
|
|
1051
|
+
kind: 'prop' | 'style' = 'prop',
|
|
1052
|
+
): void {
|
|
1053
|
+
const value = ts.isObjectLiteralExpression(expression)
|
|
1054
|
+
? expression
|
|
1055
|
+
: (styleObjects(expression, expression, [], false)[0]?.node ?? null)
|
|
1056
|
+
emit(
|
|
1057
|
+
site,
|
|
1058
|
+
binding,
|
|
1059
|
+
'spread',
|
|
1060
|
+
null,
|
|
1061
|
+
value ? 'static spread container; properties counted separately' : null,
|
|
1062
|
+
)
|
|
1063
|
+
if (value)
|
|
1064
|
+
for (const property of value.properties) {
|
|
1065
|
+
emit(
|
|
1066
|
+
site,
|
|
1067
|
+
binding,
|
|
1068
|
+
ts.isSpreadAssignment(property) ? 'spread' : kind,
|
|
1069
|
+
ts.isSpreadAssignment(property) ? null : key(property.name),
|
|
1070
|
+
null,
|
|
1071
|
+
null,
|
|
1072
|
+
property.getStart(source),
|
|
1073
|
+
ts.isSpreadAssignment(property) ? null : propertyValue(property),
|
|
1074
|
+
)
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
function object(
|
|
1078
|
+
node: ts.ObjectLiteralExpression,
|
|
1079
|
+
binding: SourceBinding,
|
|
1080
|
+
kind: 'prop' | 'style',
|
|
1081
|
+
site?: ts.Expression,
|
|
1082
|
+
): void {
|
|
1083
|
+
for (const property of node.properties) {
|
|
1084
|
+
if (ts.isSpreadAssignment(property))
|
|
1085
|
+
spread(property.expression, property, binding, kind)
|
|
1086
|
+
else {
|
|
1087
|
+
const name = key(property.name)
|
|
1088
|
+
emit(
|
|
1089
|
+
site ?? property,
|
|
1090
|
+
binding,
|
|
1091
|
+
kind,
|
|
1092
|
+
name,
|
|
1093
|
+
null,
|
|
1094
|
+
null,
|
|
1095
|
+
site ? property.getStart(source) : null,
|
|
1096
|
+
propertyValue(property),
|
|
1097
|
+
)
|
|
1098
|
+
const value =
|
|
1099
|
+
kind === 'prop' && name === 'style' ? propertyExpression(property) : null
|
|
1100
|
+
if (value) {
|
|
1101
|
+
for (const style of styleObjects(value))
|
|
1102
|
+
object(
|
|
1103
|
+
style.node,
|
|
1104
|
+
{
|
|
1105
|
+
...binding,
|
|
1106
|
+
exportName:
|
|
1107
|
+
binding.packageName === 'react-native'
|
|
1108
|
+
? 'StyleSheet'
|
|
1109
|
+
: binding.exportName,
|
|
1110
|
+
},
|
|
1111
|
+
'style',
|
|
1112
|
+
style.site,
|
|
1113
|
+
)
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
function visit(node: ts.Node): void {
|
|
1119
|
+
const chain = nestedChain(node)
|
|
1120
|
+
if (chain) {
|
|
1121
|
+
const parent = node.parent
|
|
1122
|
+
// only the longest static chain is a reference; its prefixes are not
|
|
1123
|
+
if (
|
|
1124
|
+
!(
|
|
1125
|
+
(ts.isPropertyAccessExpression(parent) ||
|
|
1126
|
+
ts.isElementAccessExpression(parent)) &&
|
|
1127
|
+
parent.expression === node &&
|
|
1128
|
+
memberKey(parent) !== null
|
|
1129
|
+
)
|
|
1130
|
+
)
|
|
1131
|
+
emit(node, chain.binding, 'member', chain.names.join('.'))
|
|
1132
|
+
} else if (
|
|
1133
|
+
ts.isPropertyAccessExpression(node) ||
|
|
1134
|
+
ts.isElementAccessExpression(node)
|
|
1135
|
+
) {
|
|
1136
|
+
const owner = bindingFor(node.expression)
|
|
1137
|
+
if (owner && !owner.typeOnly) {
|
|
1138
|
+
const member = ts.isPropertyAccessExpression(node)
|
|
1139
|
+
? node.name.text
|
|
1140
|
+
: node.argumentExpression &&
|
|
1141
|
+
ts.isStringLiteralLikeNode(node.argumentExpression)
|
|
1142
|
+
? node.argumentExpression.text
|
|
1143
|
+
: null
|
|
1144
|
+
const namespace =
|
|
1145
|
+
owner.exportName === '*' ||
|
|
1146
|
+
(owner.exportName === 'default' && owner.packageName === 'react-native')
|
|
1147
|
+
emit(
|
|
1148
|
+
node,
|
|
1149
|
+
namespace && member ? { ...owner, exportName: member } : owner,
|
|
1150
|
+
namespace && member ? 'export' : 'member',
|
|
1151
|
+
namespace && member ? null : member,
|
|
1152
|
+
)
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
if (ts.isJsxOpeningLikeElement(node)) {
|
|
1156
|
+
const binding = bindingFor(node.tagName)
|
|
1157
|
+
if (binding && !binding.typeOnly) {
|
|
1158
|
+
for (const attribute of node.attributes.properties) {
|
|
1159
|
+
if (ts.isJsxSpreadAttribute(attribute))
|
|
1160
|
+
spread(attribute.expression, attribute, binding)
|
|
1161
|
+
else {
|
|
1162
|
+
const name = ts.isIdentifier(attribute.name) ? attribute.name.text : null
|
|
1163
|
+
emit(
|
|
1164
|
+
attribute,
|
|
1165
|
+
binding,
|
|
1166
|
+
'prop',
|
|
1167
|
+
name,
|
|
1168
|
+
null,
|
|
1169
|
+
null,
|
|
1170
|
+
null,
|
|
1171
|
+
attributeValue(attribute),
|
|
1172
|
+
)
|
|
1173
|
+
if (
|
|
1174
|
+
name === 'style' &&
|
|
1175
|
+
attribute.initializer &&
|
|
1176
|
+
ts.isJsxExpression(attribute.initializer) &&
|
|
1177
|
+
attribute.initializer.expression
|
|
1178
|
+
) {
|
|
1179
|
+
for (const style of styleObjects(attribute.initializer.expression))
|
|
1180
|
+
object(
|
|
1181
|
+
style.node,
|
|
1182
|
+
{
|
|
1183
|
+
...binding,
|
|
1184
|
+
exportName:
|
|
1185
|
+
binding.packageName === 'react-native'
|
|
1186
|
+
? 'StyleSheet'
|
|
1187
|
+
: binding.exportName,
|
|
1188
|
+
},
|
|
1189
|
+
'style',
|
|
1190
|
+
style.site,
|
|
1191
|
+
)
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
if (
|
|
1198
|
+
ts.isCallExpression(node) &&
|
|
1199
|
+
node.arguments[0] &&
|
|
1200
|
+
node.arguments[1] &&
|
|
1201
|
+
ts.isObjectLiteralExpression(node.arguments[1])
|
|
1202
|
+
) {
|
|
1203
|
+
const binding = factoryBinding(node)
|
|
1204
|
+
if (binding) object(node.arguments[1], binding, 'prop')
|
|
1205
|
+
}
|
|
1206
|
+
if (
|
|
1207
|
+
ts.isCallExpression(node) &&
|
|
1208
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
1209
|
+
node.expression.name.text === 'create' &&
|
|
1210
|
+
bindingFor(node.expression.expression)?.exportName === 'StyleSheet' &&
|
|
1211
|
+
bindingFor(node.expression.expression)?.packageName === 'react-native' &&
|
|
1212
|
+
node.arguments[0] &&
|
|
1213
|
+
ts.isObjectLiteralExpression(node.arguments[0])
|
|
1214
|
+
) {
|
|
1215
|
+
const binding = bindingFor(node.expression.expression)
|
|
1216
|
+
if (binding)
|
|
1217
|
+
for (const rule of node.arguments[0].properties) {
|
|
1218
|
+
if (
|
|
1219
|
+
ts.isPropertyAssignment(rule) &&
|
|
1220
|
+
ts.isObjectLiteralExpression(rule.initializer)
|
|
1221
|
+
)
|
|
1222
|
+
object(rule.initializer, binding, 'style')
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
node.forEachChild(visit)
|
|
1226
|
+
}
|
|
1227
|
+
visit(source)
|
|
1228
|
+
return [...candidates.values()].sort(
|
|
1229
|
+
(a, b) => a.start - b.start || a.id.localeCompare(b.id),
|
|
1230
|
+
)
|
|
1231
|
+
})
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
export const EXCLUDED_SOURCE_DIRECTORIES = new Set([
|
|
1235
|
+
'.git',
|
|
1236
|
+
'.expo',
|
|
1237
|
+
'.next',
|
|
1238
|
+
'.storybook',
|
|
1239
|
+
'carthage',
|
|
1240
|
+
'pods',
|
|
1241
|
+
'android',
|
|
1242
|
+
'ios',
|
|
1243
|
+
'artifacts',
|
|
1244
|
+
'build',
|
|
1245
|
+
'coverage',
|
|
1246
|
+
'dist',
|
|
1247
|
+
'node_modules',
|
|
1248
|
+
'vendor',
|
|
1249
|
+
'__tests__',
|
|
1250
|
+
'__e2e__',
|
|
1251
|
+
'__mocks__',
|
|
1252
|
+
'fixtures',
|
|
1253
|
+
'__fixtures__',
|
|
1254
|
+
'test',
|
|
1255
|
+
'testing',
|
|
1256
|
+
'tests',
|
|
1257
|
+
'e2e',
|
|
1258
|
+
'stories',
|
|
1259
|
+
'storybook',
|
|
1260
|
+
'doc',
|
|
1261
|
+
'docs',
|
|
1262
|
+
'example',
|
|
1263
|
+
'examples',
|
|
1264
|
+
'samples',
|
|
1265
|
+
'website',
|
|
1266
|
+
'scripts',
|
|
1267
|
+
'tools',
|
|
1268
|
+
'__generated__',
|
|
1269
|
+
])
|
|
1270
|
+
|
|
1271
|
+
export function capabilitySourceExclusion(
|
|
1272
|
+
path: string,
|
|
1273
|
+
sourceRoots: readonly string[],
|
|
1274
|
+
mode = '100644',
|
|
1275
|
+
): string | null {
|
|
1276
|
+
if (mode === '120000') return 'symbolic link'
|
|
1277
|
+
const sourceRoot = sourceRoots.find(
|
|
1278
|
+
(root) => root === '.' || path === root || path.startsWith(`${root}/`),
|
|
1279
|
+
)
|
|
1280
|
+
if (!sourceRoot) return 'outside application source roots'
|
|
1281
|
+
const relativePath = sourceRoot === '.' ? path : path.slice(sourceRoot.length + 1)
|
|
1282
|
+
if (
|
|
1283
|
+
relativePath
|
|
1284
|
+
.split('/')
|
|
1285
|
+
.slice(0, -1)
|
|
1286
|
+
.some((part) => EXCLUDED_SOURCE_DIRECTORIES.has(part.toLowerCase()))
|
|
1287
|
+
)
|
|
1288
|
+
return 'dependency, generated output, test, documentation, tool, or bundled sample directory'
|
|
1289
|
+
if (/\.d\.[cm]?ts$/.test(path)) return 'declaration-only source'
|
|
1290
|
+
if (
|
|
1291
|
+
/(?:^|[./_-])(?:test|spec|stories|story|fixture|mock)(?:[./_-]|$)/i.test(relativePath)
|
|
1292
|
+
)
|
|
1293
|
+
return 'test, fixture, mock, or story source'
|
|
1294
|
+
if (/(?:^|\/)(?:jest[.-]|.*\.config\.)/.test(relativePath)) return 'tool configuration'
|
|
1295
|
+
return null
|
|
1296
|
+
}
|