@xylabs/ts-scripts-yarn3 6.4.2 → 6.4.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.
package/dist/bin/xy.mjs CHANGED
@@ -683,7 +683,7 @@ function getBasePackageName(importName) {
683
683
  }
684
684
  return importName.split("/")[0];
685
685
  }
686
- function getImportsFromFile(filePath, importPaths) {
686
+ function getImportsFromFile(filePath, importPaths, typeImportPaths) {
687
687
  const sourceCode = fs2.readFileSync(filePath, "utf8");
688
688
  const sourceFile = ts.createSourceFile(
689
689
  path3.basename(filePath),
@@ -692,24 +692,41 @@ function getImportsFromFile(filePath, importPaths) {
692
692
  true
693
693
  );
694
694
  const imports = [];
695
+ const typeImports = [];
695
696
  function visit(node) {
696
697
  if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
697
698
  const moduleSpecifier = node.moduleSpecifier?.getFullText();
699
+ const isTypeImport = ts.isImportDeclaration(node) ? node.importClause?.isTypeOnly ?? false : false;
698
700
  if (moduleSpecifier) {
699
701
  const trimmed = moduleSpecifier.split("'").at(1) ?? moduleSpecifier;
702
+ if (isTypeImport) {
703
+ typeImports.push(trimmed);
704
+ } else {
705
+ imports.push(trimmed);
706
+ }
707
+ }
708
+ } else if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword) {
709
+ const [arg] = node.arguments;
710
+ if (ts.isStringLiteral(arg)) {
711
+ const trimmed = arg.text;
700
712
  imports.push(trimmed);
701
713
  }
702
714
  }
703
715
  ts.forEachChild(node, visit);
704
716
  }
705
717
  visit(sourceFile);
706
- const importsStartsWithExcludes = [".", "#", "node:", "@types/"];
718
+ const importsStartsWithExcludes = [".", "#", "node:"];
707
719
  const cleanedImports = imports.filter((imp) => !importsStartsWithExcludes.some((exc) => imp.startsWith(exc))).map(getBasePackageName);
720
+ const cleanedTypeImports = typeImports.filter((imp) => !importsStartsWithExcludes.some((exc) => imp.startsWith(exc))).map(getBasePackageName);
708
721
  for (const imp of cleanedImports) {
709
722
  importPaths[imp] = importPaths[imp] || [];
710
723
  importPaths[imp].push(filePath);
711
724
  }
712
- return cleanedImports;
725
+ for (const imp of cleanedTypeImports) {
726
+ typeImportPaths[imp] = typeImportPaths[imp] || [];
727
+ typeImportPaths[imp].push(filePath);
728
+ }
729
+ return [cleanedImports, cleanedTypeImports];
713
730
  }
714
731
  function findFilesByGlob(cwd, pattern) {
715
732
  return globSync(pattern, { cwd, absolute: true });
@@ -729,10 +746,17 @@ function findFiles(path4) {
729
746
  }
730
747
  function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
731
748
  const prodImportPaths = {};
732
- const prodImports = prodSourceFiles.flatMap((path4) => getImportsFromFile(path4, prodImportPaths));
749
+ const prodTypeImportPaths = {};
750
+ const prodImportPairs = prodSourceFiles.map((path4) => getImportsFromFile(path4, prodImportPaths, prodTypeImportPaths));
751
+ const prodImports = prodImportPairs.flatMap((pair) => pair[0]);
752
+ const prodTypeImports = prodImportPairs.flatMap((pair) => pair[1]);
733
753
  const devImportPaths = {};
734
- const devImports = devSourceFiles.flatMap((path4) => getImportsFromFile(path4, devImportPaths));
754
+ const devTypeImportPaths = {};
755
+ const devImportPairs = devSourceFiles.map((path4) => getImportsFromFile(path4, devImportPaths, devTypeImportPaths));
756
+ const devImports = devImportPairs.flatMap((pair) => pair[0]);
757
+ const devTypeImports = devImportPairs.flatMap((pair) => pair[1]);
735
758
  const externalProdImports = prodImports.filter((imp) => !imp.startsWith(".") && !imp.startsWith("#") && !imp.startsWith("node:"));
759
+ const externalProdTypeImports = prodTypeImports.filter((imp) => !imp.startsWith(".") && !imp.startsWith("#") && !imp.startsWith("node:"));
736
760
  const externalDevImports = devImports.filter((imp) => !imp.startsWith(".") && !imp.startsWith("#") && !imp.startsWith("node:"));
737
761
  return {
738
762
  prodImports,
@@ -740,7 +764,10 @@ function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
740
764
  prodImportPaths,
741
765
  devImportPaths,
742
766
  externalProdImports,
743
- externalDevImports
767
+ externalDevImports,
768
+ prodTypeImports,
769
+ devTypeImports,
770
+ externalProdTypeImports
744
771
  };
745
772
  }
746
773
  function check({
@@ -752,6 +779,7 @@ function check({
752
779
  const { prodSourceFiles, devSourceFiles } = findFiles(location);
753
780
  const {
754
781
  prodImportPaths,
782
+ externalProdTypeImports,
755
783
  devImportPaths,
756
784
  externalProdImports,
757
785
  externalDevImports
@@ -764,6 +792,15 @@ function check({
764
792
  let unlistedDependencies = 0;
765
793
  let unlistedDevDependencies = 0;
766
794
  let unusedDependencies = 0;
795
+ let typesInDependencies = 0;
796
+ for (const imp of externalProdTypeImports) {
797
+ if (!dependencies.includes(imp) && !peerDependencies.includes(imp) && !devDependencies.includes(imp) && !devDependencies.includes(`@types/${imp}`)) {
798
+ unlistedDependencies++;
799
+ console.log(`[${chalk12.blue(name)}] Missing dependency in package.json: ${chalk12.red(imp)}`);
800
+ console.log(` ${prodImportPaths[imp].join("\n")}`);
801
+ console.log("");
802
+ }
803
+ }
767
804
  for (const imp of externalProdImports) {
768
805
  if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {
769
806
  unlistedDependencies++;
@@ -773,6 +810,13 @@ function check({
773
810
  }
774
811
  }
775
812
  for (const dep of dependencies) {
813
+ if (dep.startsWith("@types/")) {
814
+ typesInDependencies++;
815
+ console.log(`[${chalk12.blue(name)}] @types in dependencies in package.json: ${chalk12.red(dep)}`);
816
+ console.log(` ${location}/package.json
817
+ `);
818
+ console.log("");
819
+ }
776
820
  if (!externalProdImports.includes(dep)) {
777
821
  unusedDependencies++;
778
822
  console.log(`[${chalk12.blue(name)}] Unused dependency in package.json: ${chalk12.red(dep)}`);
@@ -801,7 +845,7 @@ function check({
801
845
  }
802
846
  }
803
847
  }
804
- const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies;
848
+ const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies + typesInDependencies;
805
849
  return totalErrors;
806
850
  }
807
851
  var deplint = ({ pkg }) => {