@exodus/stasis 1.0.0-beta.10 → 1.0.0-beta.12

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/src/scan.js CHANGED
@@ -5,10 +5,12 @@ import assert from 'node:assert/strict'
5
5
  import { packageType } from '@exodus/stasis-core/bundle-util'
6
6
  import { classifyExtension, classifyFormat, isTypeDeclaration, relativeEscapes } from '@exodus/stasis-core/util'
7
7
  import { inNodeModules, resolveTypescriptFallback, typescriptSiblings } from './resolve-typescript.js'
8
+ import { branchFolder, parseDefine } from './define.js'
8
9
  import { diskHost } from '@exodus/stasis-core/host'
9
10
 
10
11
  // Static require/import graph walker: parses source, never loads or executes user code.
11
- // Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved.
12
+ // Dynamic specifiers (`require(name)`, `import('./'+x)`) are recorded as unresolved. With
13
+ // `define`, a branch the defined values keep from running is not walked (see define.js).
12
14
 
13
15
  // .jsx/.tsx need no flag: oxc parses them as JSX/TSX purely from the filename, so a reached one
14
16
  // -- e.g. a package whose React Native entry is src/index.tsx -- is parsed and carried like any .ts.
@@ -74,7 +76,9 @@ export function literalSpec(node) {
74
76
  }
75
77
 
76
78
  // Walks require()/import() calls only; ESM static imports/re-exports come from oxc module records.
77
- function findCallSpecifiers(ast, push) {
79
+ // `fold` (define.js's branchFolder, or null) names the children of a branch whose test folds: the
80
+ // side that can't run is never walked.
81
+ function findCallSpecifiers(ast, push, fold = null) {
78
82
  const visit = (node) => {
79
83
  if (!node || typeof node !== 'object') return
80
84
  if (Array.isArray(node)) {
@@ -82,6 +86,11 @@ function findCallSpecifiers(ast, push) {
82
86
  return
83
87
  }
84
88
  if (typeof node.type !== 'string') return
89
+ const live = fold?.(node)
90
+ if (live !== undefined) {
91
+ for (const c of live) visit(c)
92
+ return
93
+ }
85
94
 
86
95
  if (node.type === 'CallExpression') {
87
96
  const { callee, arguments: args } = node
@@ -120,8 +129,9 @@ function findCallSpecifiers(ast, push) {
120
129
  // -- still loads its module, so its edge is real. oxc's module records can't draw that line
121
130
  // (`import type { A }` and `import { type A }` yield identical all-isType entries, and
122
131
  // `export {} from` yields no entry at all), so the statements are read off the AST, where
123
- // importKind/exportKind carry it.
124
- function collectSpecifiers(program) {
132
+ // importKind/exportKind carry it. `defines` (parseDefine's) leave out the calls in branches they
133
+ // keep from running; static import statements are never in one.
134
+ function collectSpecifiers(program, defines = new Map()) {
125
135
  const specs = []
126
136
  for (const node of program.body) {
127
137
  if (node.type === 'ImportDeclaration') {
@@ -132,7 +142,7 @@ function collectSpecifiers(program) {
132
142
  if (node.exportKind !== 'type') specs.push({ kind: 'export-from', spec: node.source.value })
133
143
  }
134
144
  }
135
- findCallSpecifiers(program, (s) => specs.push(s))
145
+ findCallSpecifiers(program, (s) => specs.push(s), branchFolder(defines, program))
136
146
  return specs
137
147
  }
138
148
 
@@ -200,7 +210,9 @@ export class Scan {
200
210
  // are carried as opaque resources (bytes only) rather than rejected as un-carryable -- for graphs
201
211
  // that aren't fully loadable in JS (e.g. Metro consuming .png/.svg assets).
202
212
  // `host`: the filesystem the walk reads and resolves through (@exodus/stasis-core/host).
203
- constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), host = diskHost } = {}) {
213
+ // `define` (each global name -> its value, see define.js): a require()/import() in a branch they
214
+ // keep from running (`if (__DEV__)` under `{ __DEV__: false }`) is no edge.
215
+ constructor({ conditions = [], resolve = null, jsx = false, flow = false, typescript = false, typescriptPaths = null, typescriptResolve = null, resources = new Set(), define, host = diskHost } = {}) {
204
216
  this.extraConditions = [...conditions]
205
217
  this.customResolve = resolve
206
218
  this.typescriptResolve = typescriptResolve
@@ -209,6 +221,7 @@ export class Scan {
209
221
  this.typescript = typescript
210
222
  this.typescriptPaths = typescriptPaths
211
223
  this.resources = resources
224
+ this.defines = parseDefine('scan', define)
212
225
  this.host = host
213
226
  }
214
227
 
@@ -434,7 +447,7 @@ export class Scan {
434
447
  this.parseErrors.push({ url, format, message: parseError, recovered: true })
435
448
  }
436
449
 
437
- const specs = collectSpecifiers(parsed.program)
450
+ const specs = collectSpecifiers(parsed.program, this.defines)
438
451
 
439
452
  // The edge KIND picks the resolution context, not the parent's format: createRequire()d
440
453
  // require() inside ESM resolves under REQUIRE conditions, literal import() inside CJS under
@@ -57,11 +57,13 @@ function ancestorsOf(directory) {
57
57
  return [...out, undefined]
58
58
  }
59
59
 
60
- // The files of the subtree `tree`, or null where one is a symlink out of it.
61
- async function treeFiles(client, { github, tree, where }) {
60
+ // The files of the subtree `tree` at `directory`, or null where one is a symlink out of it. Upstream
61
+ // refuses a tarball whose symlinks climb above its root, unless told how far they may: as far as the
62
+ // repo's root, as many levels as `directory` is deep, less one (its tarball's top directory).
63
+ async function treeFiles(client, { github, tree, directory, where }) {
62
64
  let files
63
65
  try {
64
- files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree }), where)
66
+ files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory.split('/').length - 1 }), where)
65
67
  } catch (error) {
66
68
  if (error instanceof ArchiveError) return null // e.g. a symlink out of the subtree
67
69
  throw error
@@ -129,7 +131,7 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
129
131
  if (!(await installedAlone(packageManager, listing.map((entry) => entry.path), () => Promise.all(above.map(names))))) return null
130
132
  // Its tree id, as the listing above it holds it, held to that directory's own.
131
133
  const tree = (await list(above[0])).find((entry) => entry.path === posix.basename(directory) && entry.type === 'tree')?.sha
132
- const files = tree === undefined ? null : await treeFiles(client, { github, tree, where })
134
+ const files = tree === undefined ? null : await treeFiles(client, { github, tree, directory, where })
133
135
  return files === null || refersAbove(files, lockfileOf(packageManager)) ? null : files
134
136
  }
135
137
 
package/src/why.js CHANGED
@@ -1,4 +1,4 @@
1
- import { hasNodeModulesSegment, moduleFileKey } from '@exodus/stasis-core/util'
1
+ import { hasNodeModulesSegment, moduleFileKey, packageRecords } from '@exodus/stasis-core/util'
2
2
  import { isEvidenceFile } from './audit-corrections.js'
3
3
  import { parseFile } from './parse.js'
4
4
 
@@ -54,12 +54,14 @@ const TERMINAL_PACKAGES = new Set(['@babel/core', 'react-native', 'metro'])
54
54
  // `fileToDir` is complete; `dirFiles` only holds files that count as real code of
55
55
  // the dir (see audit-corrections.js) -- a consumer that recorded nothing of a
56
56
  // package but a corrected file / its package.json manifest is not one of its
57
- // reasons, exactly as in collectReasons.
57
+ // reasons, exactly as in collectReasons. A copy a dependency ships (packageRecords) is a module of its
58
+ // own here, so a chain can end at it (`next -> react-is`): its files are its node's, while its host's
59
+ // evidence keeps them too, the host's reasons being those of all its files, as in collectReasons.
58
60
  function moduleIndex(artifact) {
59
61
  const fileToDir = new Map()
60
62
  const dirInfo = new Map()
61
63
  const dirFiles = new Map()
62
- for (const [dir, { name, version, files }] of artifact.modules) {
64
+ for (const [dir, { name, version, files }] of packageRecords(artifact.modules)) {
63
65
  dirInfo.set(dir, { name: name ?? null, version: version ?? null, dep: hasNodeModulesSegment(dir) })
64
66
  const list = []
65
67
  for (const rel of Object.keys(files)) {