code-inventory 0.3.2 → 0.3.3

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.
Files changed (2) hide show
  1. package/dist/compiler.js +25 -3
  2. package/package.json +1 -1
package/dist/compiler.js CHANGED
@@ -419,12 +419,34 @@ function analyze(project, { root, owners, eligible, analyzed, rules, inventory,
419
419
  location: location(specifier),
420
420
  });
421
421
  }
422
+ function isCommonJsRequire(expression) {
423
+ if (!ts.isIdentifier(expression) || expression.text !== 'require')
424
+ return false;
425
+ const binding = checker.getSymbolAtLocation(expression);
426
+ return (!binding?.declarations?.length ||
427
+ binding.declarations.every((decl) => decl.getSourceFile().isDeclarationFile &&
428
+ ts.isVariableDeclaration(decl) &&
429
+ !!decl.parent.parent.parent &&
430
+ (ts.isSourceFile(decl.parent.parent.parent) ||
431
+ (ts.isModuleBlock(decl.parent.parent.parent) &&
432
+ !!(decl.parent.parent.parent.parent.flags & ts.NodeFlags.GlobalAugmentation)))));
433
+ }
422
434
  function visit(node, callerId, callTargetNode) {
423
435
  const declaration = byNode.get(node);
424
436
  if (declaration)
425
437
  callerId = declaration.id;
426
- if (ts.isImportDeclaration(node))
427
- addModule(node, node.moduleSpecifier, 'import', callerId, node.importClause?.isTypeOnly ?? false);
438
+ if (ts.isImportDeclaration(node)) {
439
+ const clause = node.importClause;
440
+ const bindings = clause?.namedBindings;
441
+ const erased = clause?.isTypeOnly ||
442
+ (!options.verbatimModuleSyntax &&
443
+ !clause?.name &&
444
+ bindings &&
445
+ ts.isNamedImports(bindings) &&
446
+ bindings.elements.length > 0 &&
447
+ bindings.elements.every((item) => item.isTypeOnly));
448
+ addModule(node, node.moduleSpecifier, 'import', callerId, Boolean(erased));
449
+ }
428
450
  if (ts.isExportDeclaration(node) && node.moduleSpecifier)
429
451
  addModule(node, node.moduleSpecifier, 'export', callerId, node.isTypeOnly);
430
452
  if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
@@ -434,7 +456,7 @@ function analyze(project, { root, owners, eligible, analyzed, rules, inventory,
434
456
  const expression = unwrap(node.expression);
435
457
  if (expression.kind === ts.SyntaxKind.ImportKeyword)
436
458
  addModule(node, node.arguments?.[0], 'dynamic-import', callerId);
437
- else if (ts.isIdentifier(expression) && expression.text === 'require' && !resolveExpression(expression)) {
459
+ else if (isCommonJsRequire(expression)) {
438
460
  addModule(node, node.arguments?.[0], 'require', callerId);
439
461
  }
440
462
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-inventory",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "Static code inventory for JavaScript and TypeScript. Find symbol usages and trace paths from entry points.",