MCPcopy
hub / github.com/formatjs/formatjs / checkBundle

Function checkBundle

tools/check_package_json.ts:26–68  ·  view source on GitHub ↗
(
  bundlePath: string,
  declared: Set<string>,
  errors: string[]
)

Source from the content-addressed store, hash-verified

24}
25
26function checkBundle(
27 bundlePath: string,
28 declared: Set<string>,
29 errors: string[]
30): void {
31 const stat = statSync(bundlePath, {throwIfNoEntry: false})
32 if (!stat) return
33 if (stat.isFile()) {
34 if (!bundlePath.endsWith('.js')) return
35 const content = readFileSync(bundlePath, 'utf8')
36 const result = parseSync(bundlePath, content, {sourceType: 'module'})
37 const seen = new Set<string>()
38 const specs = [
39 ...result.module.staticImports.map(i => i.moduleRequest.value),
40 ...result.module.staticExports.flatMap(e =>
41 e.entries
42 .map(entry => entry.moduleRequest?.value)
43 .filter((v): v is string => Boolean(v))
44 ),
45 ...result.module.dynamicImports.flatMap(i => {
46 const raw = content.slice(i.moduleRequest.start, i.moduleRequest.end)
47 const m = /^(['"])([^'"]+)\1$/.exec(raw)
48 return m ? [m[2]] : []
49 }),
50 ]
51 for (const spec of specs) {
52 const bare = bareSpecifier(spec)
53 if (!bare || seen.has(bare)) continue
54 seen.add(bare)
55 if (!declared.has(bare)) {
56 errors.push(
57 `'${bare}' imported by ${bundlePath} but not declared in package.json (dependencies/peerDependencies)`
58 )
59 }
60 }
61 return
62 }
63 if (stat.isDirectory()) {
64 for (const entry of readdirSync(bundlePath)) {
65 checkBundle(join(bundlePath, entry), declared, errors)
66 }
67 }
68}
69
70function main(args: Args): void {
71 if (!args.pkg) {

Callers 1

mainFunction · 0.85

Calls 1

bareSpecifierFunction · 0.85

Tested by

no test coverage detected