@xylabs/ts-scripts-yarn3 6.4.1 → 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,23 +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 cleanedImports = imports.filter((imp) => !imp.startsWith(".") && !imp.startsWith("#") && !imp.startsWith("node:")).map(getBasePackageName);
718
+ const importsStartsWithExcludes = [".", "#", "node:"];
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);
707
721
  for (const imp of cleanedImports) {
708
722
  importPaths[imp] = importPaths[imp] || [];
709
723
  importPaths[imp].push(filePath);
710
724
  }
711
- return cleanedImports;
725
+ for (const imp of cleanedTypeImports) {
726
+ typeImportPaths[imp] = typeImportPaths[imp] || [];
727
+ typeImportPaths[imp].push(filePath);
728
+ }
729
+ return [cleanedImports, cleanedTypeImports];
712
730
  }
713
731
  function findFilesByGlob(cwd, pattern) {
714
732
  return globSync(pattern, { cwd, absolute: true });
@@ -728,10 +746,17 @@ function findFiles(path4) {
728
746
  }
729
747
  function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
730
748
  const prodImportPaths = {};
731
- 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]);
732
753
  const devImportPaths = {};
733
- 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]);
734
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:"));
735
760
  const externalDevImports = devImports.filter((imp) => !imp.startsWith(".") && !imp.startsWith("#") && !imp.startsWith("node:"));
736
761
  return {
737
762
  prodImports,
@@ -739,7 +764,10 @@ function getExternalImportsFromFiles({ prodSourceFiles, devSourceFiles }) {
739
764
  prodImportPaths,
740
765
  devImportPaths,
741
766
  externalProdImports,
742
- externalDevImports
767
+ externalDevImports,
768
+ prodTypeImports,
769
+ devTypeImports,
770
+ externalProdTypeImports
743
771
  };
744
772
  }
745
773
  function check({
@@ -751,6 +779,7 @@ function check({
751
779
  const { prodSourceFiles, devSourceFiles } = findFiles(location);
752
780
  const {
753
781
  prodImportPaths,
782
+ externalProdTypeImports,
754
783
  devImportPaths,
755
784
  externalProdImports,
756
785
  externalDevImports
@@ -763,6 +792,15 @@ function check({
763
792
  let unlistedDependencies = 0;
764
793
  let unlistedDevDependencies = 0;
765
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
+ }
766
804
  for (const imp of externalProdImports) {
767
805
  if (!dependencies.includes(imp) && !peerDependencies.includes(imp)) {
768
806
  unlistedDependencies++;
@@ -772,6 +810,13 @@ function check({
772
810
  }
773
811
  }
774
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
+ }
775
820
  if (!externalProdImports.includes(dep)) {
776
821
  unusedDependencies++;
777
822
  console.log(`[${chalk12.blue(name)}] Unused dependency in package.json: ${chalk12.red(dep)}`);
@@ -800,7 +845,7 @@ function check({
800
845
  }
801
846
  }
802
847
  }
803
- const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies;
848
+ const totalErrors = unlistedDependencies + unlistedDevDependencies + unusedDependencies + typesInDependencies;
804
849
  return totalErrors;
805
850
  }
806
851
  var deplint = ({ pkg }) => {